Пример #1
0
        /// <summary>
        /// Example of how to create a basic RTP session object and hook up the event handlers.
        /// </summary>
        /// <param name="ua">The user agent the RTP session is being created for.</param>
        /// <param name="dst">THe destination specified on an incoming call. Can be used to
        /// set the audio source.</param>
        /// <returns>A new RTP session object.</returns>
        private static RtpAudioSession CreateRtpSession(SIPUserAgent ua, string dst)
        {
            List <SDPMediaFormatsEnum> codecs = new List <SDPMediaFormatsEnum> {
                SDPMediaFormatsEnum.PCMU, SDPMediaFormatsEnum.PCMA, SDPMediaFormatsEnum.G722
            };

            var audioSource = DummyAudioSourcesEnum.SineWave;

            if (string.IsNullOrEmpty(dst) || !Enum.TryParse <DummyAudioSourcesEnum>(dst, out audioSource))
            {
                audioSource = DummyAudioSourcesEnum.Silence;
            }

            var audioOptions = new DummyAudioOptions {
                AudioSource = audioSource
            };

            if (audioSource == DummyAudioSourcesEnum.Music)
            {
                audioOptions.SourceFiles = new Dictionary <SDPMediaFormatsEnum, string>();
                if (codecs.Contains(SDPMediaFormatsEnum.PCMA))
                {
                    audioOptions.SourceFiles.Add(SDPMediaFormatsEnum.PCMA, MUSIC_FILE_PCMA);
                }
                if (codecs.Contains(SDPMediaFormatsEnum.PCMU))
                {
                    audioOptions.SourceFiles.Add(SDPMediaFormatsEnum.PCMU, MUSIC_FILE_PCMU);
                }
                if (codecs.Contains(SDPMediaFormatsEnum.G722))
                {
                    audioOptions.SourceFiles.Add(SDPMediaFormatsEnum.G722, MUSIC_FILE_G722);
                }
            }
            ;

            Log.LogInformation($"RTP audio session source set to {audioSource}.");

            var rtpAudioSession = new RtpAudioSession(audioOptions, codecs);

            // Wire up the event handler for RTP packets received from the remote party.
            rtpAudioSession.OnRtpPacketReceived += (type, rtp) => OnRtpPacketReceived(ua, type, rtp);
            rtpAudioSession.OnTimeout           += (mediaType) =>
            {
                if (ua?.Dialogue != null)
                {
                    Log.LogWarning($"RTP timeout on call with {ua.Dialogue.RemoteTarget}, hanging up.");
                }
                else
                {
                    Log.LogWarning($"RTP timeout on incomplete call, closing RTP session.");
                }

                ua.Hangup();
            };

            return(rtpAudioSession);
        }
Пример #2
0
        /// <summary>
        /// An asynchronous task that attempts to initiate a new call to a listening UAS.
        /// </summary>
        /// <param name="sipTransport">The transport object to use for the send.</param>
        /// <param name="dst">The destination end point to send the request to.</param>
        /// <returns>True if the expected response was received, false otherwise.</returns>
        private static async Task <bool> InitiateCallTaskAsync(SIPTransport sipTransport, SIPURI dst)
        {
            //UdpClient hepClient = new UdpClient(0, AddressFamily.InterNetwork);

            try
            {
                //sipTransport.SIPRequestOutTraceEvent += (localEP, remoteEP, req) =>
                //{
                //    logger.LogDebug($"Request sent: {localEP}->{remoteEP}");
                //    logger.LogDebug(req.ToString());

                //    //var hepBuffer = HepPacket.GetBytes(localEP, remoteEP, DateTimeOffset.Now, 333, "myHep", req.ToString());
                //    //hepClient.SendAsync(hepBuffer, hepBuffer.Length, "192.168.11.49", 9060);
                //};

                //sipTransport.SIPResponseInTraceEvent += (localEP, remoteEP, resp) =>
                //{
                //    logger.LogDebug($"Response received: {localEP}<-{remoteEP}");
                //    logger.LogDebug(resp.ToString());

                //    //var hepBuffer = HepPacket.GetBytes(remoteEP, localEP, DateTimeOffset.Now, 333, "myHep", resp.ToString());
                //    //hepClient.SendAsync(hepBuffer, hepBuffer.Length, "192.168.11.49", 9060);
                //};

                var ua = new SIPUserAgent(sipTransport, null);
                ua.ClientCallTrying   += (uac, resp) => logger.LogInformation($"{uac.CallDescriptor.To} Trying: {resp.StatusCode} {resp.ReasonPhrase}.");
                ua.ClientCallRinging  += (uac, resp) => logger.LogInformation($"{uac.CallDescriptor.To} Ringing: {resp.StatusCode} {resp.ReasonPhrase}.");
                ua.ClientCallFailed   += (uac, err) => logger.LogWarning($"{uac.CallDescriptor.To} Failed: {err}");
                ua.ClientCallAnswered += (uac, resp) => logger.LogInformation($"{uac.CallDescriptor.To} Answered: {resp.StatusCode} {resp.ReasonPhrase}.");

                var audioOptions = new DummyAudioOptions {
                    AudioSource = DummyAudioSourcesEnum.Silence
                };
                var rtpAudioSession = new RtpAudioSession(audioOptions, new List <SDPMediaFormatsEnum> {
                    SDPMediaFormatsEnum.PCMU
                });

                var result = await ua.Call(dst.ToString(), null, null, rtpAudioSession);

                await rtpAudioSession.Start();

                ua.Hangup();

                await Task.Delay(200);

                return(result);
            }
            catch (Exception excp)
            {
                logger.LogError($"Exception InitiateCallTaskAsync. {excp.Message}");
                return(false);
            }
        }
Пример #3
0
        static async Task Main()
        {
            Console.WriteLine("SIPSorcery Convert Audio");

            AddConsoleLogger();

            //WaveFormatConversionStream converter = new WaveFormatConversionStream(_format_s16le48k, )
            _waveFile = new WaveFileWriter("output_s16le48k.mp3", _format_s16le48k);

            var sipTransport = new SIPTransport();
            var userAgent    = new SIPUserAgent(sipTransport, null);

            userAgent.OnCallHungup += (dialog) =>
            {
                Console.WriteLine("Call hungup.");
                _waveFile?.Close();
            };

            //EnableTraceLogs(sipTransport);

            var audioOptions = new DummyAudioOptions {
                AudioSource = DummyAudioSourcesEnum.Silence
            };
            var audioFormats = new List <SDPMediaFormatsEnum> {
                SDPMediaFormatsEnum.PCMU
            };
            var rtpSession = new RtpAudioSession(audioOptions, audioFormats);

            rtpSession.OnRtpPacketReceived += RtpSession_OnRtpPacketReceived;

            // Place the call and wait for the result.
            bool callResult = await userAgent.Call(DESTINATION, null, null, rtpSession);

            Console.WriteLine($"Call result {((callResult) ? "success" : "failure")}.");

            Console.WriteLine("press any key to exit...");
            Console.Read();

            if (userAgent.IsCallActive)
            {
                Console.WriteLine("Hanging up.");
                userAgent.Hangup();
            }

            // Clean up.
            sipTransport.Shutdown();
            SIPSorcery.Net.DNSManager.Stop();
        }
Пример #4
0
        /// <summary>
        /// Example of how to create a basic RTP session object and hook up the event handlers.
        /// </summary>
        /// <param name="ua">The suer agent the RTP session is being created for.</param>
        /// <returns>A new RTP session object.</returns>
        private static RtpAudioSession CreateRtpSession(SIPUserAgent ua, string dst)
        {
            List <SDPMediaFormatsEnum> codecs = new List <SDPMediaFormatsEnum> {
                SDPMediaFormatsEnum.PCMU, SDPMediaFormatsEnum.PCMA, SDPMediaFormatsEnum.G722
            };

            var audioSource = DummyAudioSourcesEnum.SineWave;

            if (!Enum.TryParse <DummyAudioSourcesEnum>(dst, out audioSource))
            {
                audioSource = DummyAudioSourcesEnum.Silence;
            }

            var audioOptions = new DummyAudioOptions {
                AudioSource = audioSource
            };

            if (audioSource == DummyAudioSourcesEnum.Music)
            {
                audioOptions.SourceFiles = new Dictionary <SDPMediaFormatsEnum, string>();
                if (codecs.Contains(SDPMediaFormatsEnum.PCMA))
                {
                    audioOptions.SourceFiles.Add(SDPMediaFormatsEnum.PCMA, MUSIC_FILE_PCMA);
                }
                if (codecs.Contains(SDPMediaFormatsEnum.PCMU))
                {
                    audioOptions.SourceFiles.Add(SDPMediaFormatsEnum.PCMU, MUSIC_FILE_PCMU);
                }
                if (codecs.Contains(SDPMediaFormatsEnum.G722))
                {
                    audioOptions.SourceFiles.Add(SDPMediaFormatsEnum.G722, MUSIC_FILE_G722);
                }
            }
            ;

            Log.LogInformation($"RTP audio session source set to {audioSource}.");

            var rtpAudioSession = new RtpAudioSession(audioOptions, codecs);

            // Wire up the event handler for RTP packets received from the remote party.
            rtpAudioSession.OnRtpPacketReceived += (type, rtp) => OnRtpPacketReceived(ua, type, rtp);

            return(rtpAudioSession);
        }