Exemple #1
0
        /// <summary>
        /// Sends a sample from a signal generator generated waveform.
        /// </summary>
        private void SendSignalGeneratorSample(object state)
        {
            lock (_audioStreamTimer)
            {
                int inputBufferSize  = RTP_TIMESTAMP_RATE / 1000 * AUDIO_SAMPLE_PERIOD_MILLISECONDS;
                int outputBufferSize = RTP_TIMESTAMP_RATE / 1000 * AUDIO_SAMPLE_PERIOD_MILLISECONDS;

                // Get the signal generator to generate the samples and then convert from
                // signed linear to PCM.
                float[] linear = new float[inputBufferSize];
                _signalGenerator.Read(linear, 0, inputBufferSize);
                short[] pcm = linear.Select(x => (short)(x * 32767f)).ToArray();

                byte[] encodedSample = new byte[outputBufferSize];

                for (int index = 0; index < inputBufferSize; index++)
                {
                    encodedSample[index] = MuLawEncoder.LinearToMuLawSample(pcm[index]);
                }

                _peerConnection.SendAudioFrame((uint)outputBufferSize, _peerConnection.GetSendingFormat(SDPMediaTypesEnum.audio).FormatCodec.GetHashCode(), encodedSample);
            }
        }