This project has moved and is read-only. For the latest updates, please go here.

Playing from continous data stream

Feb 5, 2010 at 4:32 PM

All,

I'm working on a VoIP softphone type application.  I'm trying to play audio coming from an RTP stream. I can successfully decode the RTP stream and extract the RAW encoded audio. I have been successful buffering the stream and playing it all at once, but I need to be able to play this is real time. 

I've created a class derived from WaveStream to take in RAW G711 data from a memory stream. Below is the method I'm using to play the stream.  Right now every time I receive a new RTP message, I call this method, but I can only call it once before it bombs out.

 

Here's the code that gets called every time a new RTP packet arrives

memStream = new MemoryStream(encodedData); 
audioPlayback.Play(memStream);

Here's the method I use to play back from the memory stream

public  void Play(MemoryStream memoryStream)
        {
            //naudio test
            using (memoryStream)
            {


                //memoryStream.Position = 0;
                RawWavStream waveStream = new RawWavStream(memoryStream);
                WaveStream conversionStream = WaveFormatConversionStream.CreatePcmStream(waveStream);
                using (WaveStream blockAlignedStream = new BlockAlignReductionStream(conversionStream))
                {
                    using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
                    {
                        waveOut.Init(blockAlignedStream);
                        waveOut.Play();
                        while (waveOut.PlaybackState == PlaybackState.Playing)
                        {
                            System.Threading.Thread.Sleep(100);
                        }
                    }
                }
            }

        }

 

 

Mar 25, 2010 at 5:15 PM

I would be tempted to have a single instance of WaveOut, and then feed data to it whenever it becomes available. i.e. your "RawWavStream" ought to be able to be given new MemoryStream objects when they arrive

Mark

 

Apr 10, 2010 at 5:31 AM

Take a look at my bufferedsamplestream class here: http://naudio.codeplex.com/Thread/View.aspx?ThreadId=80369

You basically feed the class byte[] sample buffers and it queues them up until something comes along to start reading from the stream.  If the stream doesn't have any buffered samples it produces silence.

 

- Michael