Author: derek Date: Thu Nov 9 22:20:34 2006 New Revision: 168 Modified: sofaswitch/trunk/sound/audio.cxx sofaswitch/trunk/sound/audio.h Log: Add PQueueChannel to sound, so we cope with receiving smaller packets from the source. Modified: sofaswitch/trunk/sound/audio.cxx ============================================================================== --- sofaswitch/trunk/sound/audio.cxx (original) +++ sofaswitch/trunk/sound/audio.cxx Thu Nov 9 22:20:34 2006 @@ -12,6 +12,8 @@ */ #include <ptlib.h> +#include <ptclib/qchannel.h> + #include "version.h" #include "audio.h" #include "main.h" @@ -68,10 +70,22 @@ TestAudioWrite::TestAudioWrite(PUDPSocket &_socket, PSoundChannel &_sound) : TestAudio(_socket, _sound) { - PTRACE(3, "Writer\tInitiate thread for writing " ); - Resume(); + + audioQueue = new PQueueChannel(); + audioQueue->Open(1000); + audioQueue->SetReadTimeout(40); + audioQueue->SetWriteTimeout(40); + + PTRACE(3, "Writer\tInitiate thread for writing " ); + Resume(); } +TestAudioWrite::~TestAudioWrite() +{ + audioQueue->Close(); + delete(audioQueue); +} + void TestAudioWrite::Main() { if (!OpenAudio(PSoundChannel::Player)) { @@ -79,7 +93,14 @@ return; } PTRACE(3, "TestAudioWrite\tSound device is now open, start running"); - + + PString threadName = PString("QueueToSpeaker"); + PThread *q2s = PThread::Create(PCREATE_NOTIFIER(QueueToSpeakerMain), 30000, + PThread::NoAutoDeleteThread, + PThread::NormalPriority, + threadName); + + PBYTEArray data; data.SetSize(480); @@ -94,8 +115,7 @@ } PINDEX readSize = socket.GetLastReadCount(); data.SetSize(readSize); - sound.SetBuffers(readSize, 2); - sound.Write(data.GetPointer(), data.GetSize()); + audioQueue->Write(data.GetPointer(), data.GetSize()); PTRACE(6, "postWRITE of " << data.GetSize() << " bytes to sound device"); countPackets++; } @@ -105,7 +125,27 @@ if (!keepGoing) { PTRACE(3, "keepgoing is false"); } + + q2s->WaitForTermination(); + delete q2s; } + + +void TestAudioWrite::QueueToSpeakerMain(PThread &, INT) +{ + char silenceBuffer[480]; + char audioBuffer[480]; + memset(silenceBuffer, 0, 480); + while(keepGoing) { + if (audioQueue->Read(audioBuffer, 480)) { + PINDEX count = audioQueue->GetLastReadCount(); + sound.Write(audioBuffer, count); + } else { + sound.Write(silenceBuffer, 480); + } + } +} + ////////////////////////////////////////////////////////////// Modified: sofaswitch/trunk/sound/audio.h ============================================================================== --- sofaswitch/trunk/sound/audio.h (original) +++ sofaswitch/trunk/sound/audio.h Thu Nov 9 22:20:34 2006 @@ -24,6 +24,8 @@ /**The base class for reading and writing audio from/to the sound device. This class will take audio to/from the PUDPSocket, and transfer it to/from the sound device */ +class PQueueChannel; + class TestAudio : public PThread { PCLASSINFO(TestAudio, PThread); @@ -72,6 +74,8 @@ PINDEX countPackets; }; +///////////////////////////////////////////////////////////////////////////// + /**Override of TestAudio, directed towards Reading from the sound card */ class TestAudioRead : public TestAudio { @@ -84,6 +88,7 @@ void Main(); }; +///////////////////////////////////////////////////////////////////////////// /**Override of TestAudio, directed towards writing to the sound card */ class TestAudioWrite : public TestAudio { @@ -92,8 +97,29 @@ /**Constructor */ TestAudioWrite(PUDPSocket &_socket, PSoundChannel &_sound); + /**Destructor. This deletes the PQueueChannel we allocated in the + constructor */ + ~TestAudioWrite(); + /**the main entry point, where this thread starts */ void Main(); + + protected: + + +#ifdef DOC_PLUS_PLUS + /**This thread runs to transfer data from the audio queue to the speaker + of the sound channel. */ + virtual void QueueToSpeakerMain(PThread &, INT); +#else + PDECLARE_NOTIFIER(PThread, TestAudioWrite, QueueToSpeakerMain); +#endif + + + /**The pwlib structure which takes the input sound samples (which may come + in blocks of 160, 240, 320, 480, or other bytes) and glues them + together to give one nice stream of 480 byte blocks. */ + PQueueChannel *audioQueue; };
participants (1)
-
SofaSWITCH SVN