func RecordCall(sdpofferC *C.char, filenameC *C.char) *C.char {
sdpoffer := C.GoString(sdpofferC)
filename := C.GoString(filenameC)
if fileRecording {
return C.CString("false")
}
mediaEngine := webrtc.MediaEngine{}
if err := mediaEngine.RegisterCodec(webrtc.RTPCodecParameters{
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeOpus, ClockRate: 48000},
PayloadType: 111,
}, webrtc.RTPCodecTypeAudio); err != nil {
panic(err)
}
i := &interceptor.Registry{}
if err := webrtc.RegisterDefaultInterceptors(&mediaEngine, i); err != nil {
panic(err)
}
api := webrtc.NewAPI(webrtc.WithMediaEngine(&mediaEngine), webrtc.WithInterceptorRegistry(i))
config := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{},
ICETransportPolicy: webrtc.ICETransportPolicyRelay,
}
peerConnection, err := api.NewPeerConnection(config)
if err != nil {
panic(err)
}
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
if connectionState == webrtc.ICEConnectionStateFailed || connectionState == webrtc.ICEConnectionStateDisconnected {
StopRecord()
//////////////////////// Я так понимаю что после peerConnection.Close() ошибка
peerConnection.Close()
}
fmt.Printf(">>> OnICEConnectionStateChange: %s <<<\n", connectionState.String())
})
peerConnection.OnTrack(func(track *webrtc.TrackRemote, _ *webrtc.RTPReceiver) {
var err error
oggFile, err = oggwriter.New(fmt.Sprintf("%s%s", filename, ".ogg"), 48000, 2)
if err != nil {
panic(err)
}
defer func() {
fileRecording = false
oggFile.Close()
}()
for {
rtp, _, err := track.ReadRTP()
if err != nil {
log.Fatal(err)
}
if err := oggFile.WriteRTP(rtp); err != nil {
panic(err)
}
}
})
if _, err = peerConnection.AddTransceiverFromKind(webrtc.RTPCodecTypeAudio); err != nil {
panic(err)
}
offer := webrtc.SessionDescription{
Type: webrtc.SDPTypeOffer,
SDP: patchFreeSwitchSDP(sdpoffer),
}
err = peerConnection.SetRemoteDescription(offer)
if err != nil {
panic(err)
}
gatherComplete := webrtc.GatheringCompletePromise(peerConnection)
answer, err := peerConnection.CreateAnswer(nil)
if err != nil {
panic(err)
}
err = peerConnection.SetLocalDescription(answer)
if err != nil {
panic(err)
}
<-gatherComplete
return C.CString(peerConnection.LocalDescription().SDP)
}