Exemplo n.º 1
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="device">Audio output device.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>device</b> is null reference.</exception>
        public WavePlayer(AudioOutDevice device)
        {
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }

            m_pOutputDevice = device;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Cleans up any resource being used.
        /// </summary>
        public void Dispose()
        {
            if(m_IsDisposed){
                return;
            }

            Stop();

            this.Error        = null;
            m_pAudioOutDevice = null;
            m_pRTP_Stream     = null;
            m_pAudioCodecs    = null;
            m_pActiveCodec    = null;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Cleans up any resource being used.
        /// </summary>
        public void Dispose()
        {
            if (m_IsDisposed)
            {
                return;
            }

            Stop();

            this.Error        = null;
            m_pAudioOutDevice = null;
            m_pRTP_Stream     = null;
            m_pAudioCodecs    = null;
            m_pActiveCodec    = null;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="audioOutDevice">Audio-out device used to play out RTP audio.</param>
        /// <param name="stream">RTP receive stream which audio to play.</param>
        /// <param name="codecs">Audio codecs with RTP payload number. For example: 0-PCMU,8-PCMA.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>audioOutDevice</b>,<b>stream</b> or <b>codecs</b> is null reference.</exception>
        public AudioOut_RTP(AudioOutDevice audioOutDevice,RTP_ReceiveStream stream,Dictionary<int,AudioCodec> codecs)
        {
            if(audioOutDevice == null){
                throw new ArgumentNullException("audioOutDevice");
            }
            if(stream == null){
                throw new ArgumentNullException("stream");
            }
            if(codecs == null){
                throw new ArgumentNullException("codecs");
            }

            m_pAudioOutDevice = audioOutDevice;
            m_pRTP_Stream     = stream;
            m_pAudioCodecs    = codecs;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="device">Audio output device.</param>
        /// <param name="format">Audio output format.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>device</b> or <b>format</b> is null reference.</exception>
        public AudioOut(AudioOutDevice device, AudioFormat format)
        {
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }
            if (format == null)
            {
                throw new ArgumentNullException("format");
            }

            m_pDevice      = device;
            m_pAudioFormat = format;

            m_pWaveOut = new WaveOut(device, format.SamplesPerSecond, format.BitsPerSample, format.Channels);
        }
Exemplo n.º 6
0
            /// <summary>
            /// Default constructor.
            /// </summary>
            /// <param name="outputDevice">Output device.</param>
            /// <param name="samplesPerSec">Sample rate, in samples per second (hertz). For PCM common values are
            /// 8.0 kHz, 11.025 kHz, 22.05 kHz, and 44.1 kHz.</param>
            /// <param name="bitsPerSample">Bits per sample. For PCM 8 or 16 are the only valid values.</param>
            /// <param name="channels">Number of channels.</param>
            /// <exception cref="ArgumentNullException">Is raised when <b>outputDevice</b> is null.</exception>
            /// <exception cref="ArgumentException">Is raised when any of the aruments has invalid value.</exception>
            public WaveOut(AudioOutDevice outputDevice, int samplesPerSec, int bitsPerSample, int channels)
            {
                if (outputDevice == null)
                {
                    throw new ArgumentNullException("outputDevice");
                }
                if (samplesPerSec < 8000)
                {
                    throw new ArgumentException("Argument 'samplesPerSec' value must be >= 8000.");
                }
                if (bitsPerSample < 8)
                {
                    throw new ArgumentException("Argument 'bitsPerSample' value must be >= 8.");
                }
                if (channels < 1)
                {
                    throw new ArgumentException("Argument 'channels' value must be >= 1.");
                }

                m_pOutDevice    = outputDevice;
                m_SamplesPerSec = samplesPerSec;
                m_BitsPerSample = bitsPerSample;
                m_Channels      = channels;
                m_BlockSize     = m_Channels * (m_BitsPerSample / 8);
                m_pPlayItems    = new List <PlayItem>();

                // Try to open wav device.
                WAVEFORMATEX format = new WAVEFORMATEX();

                format.wFormatTag      = 0x0001; // PCM - 0x0001
                format.nChannels       = (ushort)m_Channels;
                format.nSamplesPerSec  = (uint)samplesPerSec;
                format.nAvgBytesPerSec = (uint)(m_SamplesPerSec * m_Channels * (m_BitsPerSample / 8));
                format.nBlockAlign     = (ushort)m_BlockSize;
                format.wBitsPerSample  = (ushort)m_BitsPerSample;
                format.cbSize          = 0;
                // We must delegate reference, otherwise GC will collect it.
                m_pWaveOutProc = new waveOutProc(this.OnWaveOutProc);
                int result = WavMethods.waveOutOpen(out m_pWavDevHandle, m_pOutDevice.Index, format, m_pWaveOutProc, 0, WavConstants.CALLBACK_FUNCTION);

                if (result != MMSYSERR.NOERROR)
                {
                    throw new Exception("Failed to open wav device, error: " + result.ToString() + ".");
                }
            }
Exemplo n.º 7
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="audioOutDevice">Audio-out device used to play out RTP audio.</param>
        /// <param name="stream">RTP receive stream which audio to play.</param>
        /// <param name="codecs">Audio codecs with RTP payload number. For example: 0-PCMU,8-PCMA.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>audioOutDevice</b>,<b>stream</b> or <b>codecs</b> is null reference.</exception>
        public AudioOut_RTP(AudioOutDevice audioOutDevice, RTP_ReceiveStream stream, Dictionary <int, AudioCodec> codecs)
        {
            if (audioOutDevice == null)
            {
                throw new ArgumentNullException("audioOutDevice");
            }
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (codecs == null)
            {
                throw new ArgumentNullException("codecs");
            }

            m_pAudioOutDevice = audioOutDevice;
            m_pRTP_Stream     = stream;
            m_pAudioCodecs    = codecs;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="device">Audio aoutput device.</param>
        /// <param name="samplesPerSec">Sample rate, in samples per second (hertz).</param>
        /// <param name="bitsPerSample">Bits per sample. For PCM 8 or 16 are the only valid values.</param>
        /// <param name="channels">Number of channels.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>device</b> is null reference.</exception>
        /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception>
        public AudioOut(AudioOutDevice device,int samplesPerSec,int bitsPerSample,int channels)
        {
            if(device == null){
                throw new ArgumentNullException("device");
            }
            if(samplesPerSec < 1){
                throw new ArgumentException("Argument 'samplesPerSec' value must be >= 1.","samplesPerSec");
            }
            if(bitsPerSample < 8){
                throw new ArgumentException("Argument 'bitsPerSample' value must be >= 8.","bitsPerSample");
            }
            if(channels < 1){
                throw new ArgumentException("Argument 'channels' value must be >= 1.","channels");
            }

            m_pDevice       = device;
            m_SamplesPerSec = samplesPerSec;
            m_BitsPerSample = bitsPerSample;
            m_Channels      = channels;

            m_pWaveOut = new WaveOut(device,samplesPerSec,bitsPerSample,channels);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="device">Audio output device.</param>
        /// <param name="samplesPerSec">Sample rate, in samples per second (hertz).</param>
        /// <param name="bitsPerSample">Bits per sample. For PCM 8 or 16 are the only valid values.</param>
        /// <param name="channels">Number of channels.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>device</b> is null reference.</exception>
        /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception>
        public AudioOut(AudioOutDevice device, int samplesPerSec, int bitsPerSample, int channels)
        {
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }
            if (samplesPerSec < 1)
            {
                throw new ArgumentException("Argument 'samplesPerSec' value must be >= 1.", "samplesPerSec");
            }
            if (bitsPerSample < 8)
            {
                throw new ArgumentException("Argument 'bitsPerSample' value must be >= 8.", "bitsPerSample");
            }
            if (channels < 1)
            {
                throw new ArgumentException("Argument 'channels' value must be >= 1.", "channels");
            }

            m_pDevice      = device;
            m_pAudioFormat = new AudioFormat(samplesPerSec, bitsPerSample, channels);

            m_pWaveOut = new WaveOut(device, samplesPerSec, bitsPerSample, channels);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Initializes SIP stack.
        /// </summary>
        private void InitStack()
        {
            #region Init audio devices

            if (AudioOut.Devices.Length == 0)
            {
                MessageBox.Show("Calling not possible, there are no speakers in computer.", "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;
            }

            if (AudioIn.Devices.Length == 0)
            {
                MessageBox.Show("Calling not possible, there is no microphone in computer.", "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;
            }

            m_pAudioOutDevice = AudioOut.Devices[0];
            m_pAudioInDevice = AudioIn.Devices[0];

            m_pAudioCodecs = new Dictionary<int, AudioCodec>();
            m_pAudioCodecs.Add(0, new PCMU());
            m_pAudioCodecs.Add(8, new PCMA());

            m_pPlayer = new WavePlayer(AudioOut.Devices[0]);

            #endregion

            #region Get NAT handling methods

            m_pUPnP = new UPnP_NAT_Client();

            STUN_Result stunResult = new STUN_Result(STUN_NetType.UdpBlocked, null);
            try
            {
                stunResult = STUN_Client.Query(m_StunServer, 3478, new IPEndPoint(IPAddress.Any, 0));
            }
            catch
            {
            }

            if (stunResult.NetType == STUN_NetType.Symmetric || stunResult.NetType == STUN_NetType.UdpBlocked)
            {
                ToolStripMenuItem item_stun = new ToolStripMenuItem("STUN (" + stunResult.NetType + ")");
                item_stun.Name = "stun";
                item_stun.Enabled = false;
                ((ToolStripDropDownButton)m_pToolbar.Items["nat"]).DropDownItems.Add(item_stun);
            }
            else
            {
                ToolStripMenuItem item_stun = new ToolStripMenuItem("STUN (" + stunResult.NetType + ")");
                item_stun.Name = "stun";
                ((ToolStripDropDownButton)m_pToolbar.Items["nat"]).DropDownItems.Add(item_stun);
            }

            if (m_pUPnP.IsSupported)
            {
                ToolStripMenuItem item_upnp = new ToolStripMenuItem("UPnP");
                item_upnp.Name = "upnp";
                ((ToolStripDropDownButton)m_pToolbar.Items["nat"]).DropDownItems.Add(item_upnp);
            }
            else
            {
                ToolStripMenuItem item_upnp = new ToolStripMenuItem("UPnP Not Supported");
                item_upnp.Name = "upnp";
                item_upnp.Enabled = false;
                ((ToolStripDropDownButton)m_pToolbar.Items["nat"]).DropDownItems.Add(item_upnp);
            }

            //if(!((ToolStripDropDownButton)m_pToolbar.Items["nat"]).DropDownItems["stun"].Enabled && !((ToolStripDropDownButton)m_pToolbar.Items["nat"]).DropDownItems["upnp"].Enabled){
                //MessageBox.Show("Calling may not possible, your firewall or router blocks STUN and doesn't support UPnP.\r\n\r\nSTUN Net Type: " + stunResult.NetType + "\r\n\r\nUPnP Supported: " + m_pUPnP.IsSupported,"Error:",MessageBoxButtons.OK,MessageBoxIcon.Error);
            //}

            ToolStripMenuItem item_no_nat = new ToolStripMenuItem("No NAT handling");
            item_no_nat.Name = "no_nat";
            ((ToolStripDropDownButton)m_pToolbar.Items["nat"]).DropDownItems.Add(item_no_nat);

            // Select first enabled item.
            foreach (ToolStripItem it in ((ToolStripDropDownButton)m_pToolbar.Items["nat"]).DropDownItems)
            {
                if (it.Enabled)
                {
                    ((ToolStripMenuItem)it).Checked = true;
                    m_NatHandlingType = it.Name;

                    break;
                }
            }

            #endregion

            m_pStack = new SIP_Stack();
            m_pStack.UserAgent = "GSDR";
            m_pStack.BindInfo = new IPBindInfo[] { new IPBindInfo("", BindInfoProtocol.UDP, IPAddress.Any, m_SipPort) };
            //m_pStack.Allow
            m_pStack.Error += new EventHandler<ExceptionEventArgs>(m_pStack_Error);
            m_pStack.RequestReceived += new EventHandler<SIP_RequestReceivedEventArgs>(m_pStack_RequestReceived);
            m_pStack.Start();

            if (m_IsDebug)
            {
                wfrm_SIP_Debug debug = new wfrm_SIP_Debug(m_pStack);
                debug.Show();
            }
        }
Exemplo n.º 11
0
        private void m_pToolbar_Audio_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            try
            {
                foreach (ToolStripMenuItem item in ((ToolStripDropDownMenu)sender).Items)
                {
                    if (item.Equals(e.ClickedItem))
                    {
                        item.Checked = true;
                        audio_source = item.Text;
                    }
                    else
                    {
                        item.Checked = false;
                    }
                }

                m_pAudioOutDevice = (AudioOutDevice)e.ClickedItem.Tag;

                // Update active call audio-out device.
                if (m_pCall != null && m_pCall.LocalSDP != null)
                {
                    foreach (SDP_MediaDescription media in m_pCall.LocalSDP.MediaDescriptions)
                    {
                        if (media.Tags.ContainsKey("rtp_audio_out"))
                        {
                            ((AudioOut_RTP)media.Tags["rtp_audio_out"]).AudioOutDevice = m_pAudioOutDevice;
                        }
                    }
                }
            }
            catch (Exception x)
            {
                MessageBox.Show("Error: " + x.Message, "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemplo n.º 12
0
            /// <summary>
            /// Cleans up any resources being used.
            /// </summary>
            public void Dispose()
            {
                if(m_IsDisposed){
                    return;
                }
                m_IsDisposed = true;

                try{
                    // If playing, we need to reset wav device first.
                    WavMethods.waveOutReset(m_pWavDevHandle);

                    // If there are unprepared wav headers, we need to unprepare these.
                    foreach(PlayItem item in m_pPlayItems){
                        WavMethods.waveOutUnprepareHeader(m_pWavDevHandle,item.HeaderHandle.AddrOfPinnedObject(),Marshal.SizeOf(item.Header));
                        item.Dispose();
                    }
                
                    // Close output device.
                    WavMethods.waveOutClose(m_pWavDevHandle);

                    m_pOutDevice    = null;
                    m_pWavDevHandle = IntPtr.Zero;
                    m_pPlayItems    = null;
                    m_pWaveOutProc  = null;
                }
                catch{                
                }
            }
Exemplo n.º 13
0
            /// <summary>
            /// Default constructor.
            /// </summary>
            /// <param name="outputDevice">Output device.</param>
            /// <param name="samplesPerSec">Sample rate, in samples per second (hertz). For PCM common values are 
            /// 8.0 kHz, 11.025 kHz, 22.05 kHz, and 44.1 kHz.</param>
            /// <param name="bitsPerSample">Bits per sample. For PCM 8 or 16 are the only valid values.</param>
            /// <param name="channels">Number of channels.</param>
            /// <exception cref="ArgumentNullException">Is raised when <b>outputDevice</b> is null.</exception>
            /// <exception cref="ArgumentException">Is raised when any of the aruments has invalid value.</exception>
            public WaveOut(AudioOutDevice outputDevice,int samplesPerSec,int bitsPerSample,int channels)
            {
                if(outputDevice == null){
                    throw new ArgumentNullException("outputDevice");
                }
                if(samplesPerSec < 8000){
                    throw new ArgumentException("Argument 'samplesPerSec' value must be >= 8000.");
                }
                if(bitsPerSample < 8){
                    throw new ArgumentException("Argument 'bitsPerSample' value must be >= 8.");
                }
                if(channels < 1){
                    throw new ArgumentException("Argument 'channels' value must be >= 1.");
                }

                m_pOutDevice    = outputDevice;
                m_SamplesPerSec = samplesPerSec;
                m_BitsPerSample = bitsPerSample;
                m_Channels      = channels;
                m_BlockSize     = m_Channels * (m_BitsPerSample / 8);
                m_pPlayItems    = new List<PlayItem>();
            
                // Try to open wav device.            
                WAVEFORMATEX format = new WAVEFORMATEX();
                format.wFormatTag      = 0x0001; // PCM - 0x0001
                format.nChannels       = (ushort)m_Channels;
                format.nSamplesPerSec  = (uint)samplesPerSec;                        
                format.nAvgBytesPerSec = (uint)(m_SamplesPerSec * m_Channels * (m_BitsPerSample / 8));
                format.nBlockAlign     = (ushort)m_BlockSize;
                format.wBitsPerSample  = (ushort)m_BitsPerSample;
                format.cbSize          = 0; 
                // We must delegate reference, otherwise GC will collect it.
                m_pWaveOutProc = new waveOutProc(this.OnWaveOutProc);
                int result = WavMethods.waveOutOpen(out m_pWavDevHandle,m_pOutDevice.Index,format,m_pWaveOutProc,0,WavConstants.CALLBACK_FUNCTION);
                if(result != MMSYSERR.NOERROR){
                    throw new Exception("Failed to open wav device, error: " + result.ToString() + ".");
                }
            }
Exemplo n.º 14
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="device">Audio output device.</param>
        /// <param name="format">Audio output format.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>device</b> or <b>format</b> is null reference.</exception>
        public AudioOut(AudioOutDevice device,AudioFormat format)
        {
            if(device == null){
                throw new ArgumentNullException("device");
            }
            if(format == null){
                throw new ArgumentNullException("format");
            }

            m_pDevice      = device;
            m_pAudioFormat = format;

            m_pWaveOut = new WaveOut(device,format.SamplesPerSecond,format.BitsPerSample,format.Channels);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="device">Audio output device.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>device</b> is null reference.</exception>
        public WavePlayer(AudioOutDevice device)
        {
            if(device == null){
                throw new ArgumentNullException("device");
            }

            m_pOutputDevice = device;
        }