Author: derek Date: Thu Jan 4 17:33:15 2007 New Revision: 221 Modified: sofaswitch/trunk/sound/comms.cxx sofaswitch/trunk/sound/comms.h sofaswitch/trunk/sound/main.cxx Log: Remove the notion of tar and taw pointers. Remove the need for the read write mutex on the tar/taw objects. Check the sound device opens for reading, before we use it to capture audio. Modified: sofaswitch/trunk/sound/comms.cxx ============================================================================== --- sofaswitch/trunk/sound/comms.cxx (original) +++ sofaswitch/trunk/sound/comms.cxx Thu Jan 4 17:33:15 2007 @@ -38,14 +38,13 @@ //////////////////////////////////////////////////////////////////////////////// -Comms::Comms(LaunchNewId & lid) - : MessageFrame(lid, 63000) +Comms::Comms(LaunchNewId & lid, PSoundChannel & _soundPlayer, PSoundChannel & _soundRecorder) + : MessageFrame(lid, 63000), + tar(* new TestAudioRead(audioSocket, _soundRecorder)), + taw(* new TestAudioWrite(audioSocket, _soundPlayer)) { stopByExit = FALSE; stopByHangup = TRUE; - - tar = NULL; - taw = NULL; } Comms::~Comms() @@ -53,13 +52,6 @@ PTRACE(3, "Comms destructor "); Terminate(); WaitForTermination(); - PWaitAndSignal m(lockReaderWriter); - - if (tar != NULL) - delete tar; - - if (taw != NULL) - delete taw; } void Comms::OnTermination() @@ -73,21 +65,11 @@ void Comms::Terminate() { PTRACE(3, "Terminate comms instance "); - PWaitAndSignal m(lockReaderWriter); + PTRACE(3, "Set tar to terminate"); + tar.Terminate(); - if (tar != NULL) { - PTRACE(3, "Set tar to terminate"); - tar->Terminate(); - } else { - PTRACE(3, "NO tar to terminate"); - } - - if (taw != NULL) { - PTRACE(3, "Set taw to terminate"); - taw->Terminate(); - } else { - PTRACE(3, "NO taw to terminate"); - } + PTRACE(3, "Set taw to terminate"); + taw.Terminate(); } void Comms::WaitForTermination() @@ -96,15 +78,8 @@ while (keepGoing) { { PTRACE(6, "Comms\tWaitForTermination"); - PWaitAndSignal m(lockReaderWriter); - if (tar != NULL) { - keepGoing = !tar->IsTerminated(); - } - if (taw != NULL) { - keepGoing = keepGoing || (!taw->IsTerminated()); - } - if ((taw == NULL) && (tar == NULL)) - keepGoing = !MessageFrame::IsTerminated(); + keepGoing = !tar.IsTerminated(); + keepGoing = keepGoing || (!taw.IsTerminated()); } PThread::Sleep(100); } @@ -116,22 +91,19 @@ { PString newVolume; - PWaitAndSignal m(lockReaderWriter); - TestAudio *ta; + TestAudio * ta; if (dir == PSoundChannel::Player) - ta = taw; + ta = &taw; else - ta = tar; + ta = &tar; - if (ta != NULL) { - if (doRaise) - ta->RaiseVolume(); - else - ta->LowerVolume(); - return ta->GetDeviceDirName() + PString(" ") + ta->GetVolumeString(); - } - return PString("No current sound device"); + if (doRaise) + ta->RaiseVolume(); + else + ta->LowerVolume(); + + return ta->GetDeviceDirName() + PString(" ") + ta->GetVolumeString(); } void Comms::SilentlyEndAll() @@ -171,22 +143,14 @@ { PStringStream countMessage; countMessage << "sound card handler" << endl; - { - PWaitAndSignal m(lockReaderWriter); - if (tar != NULL) - countMessage << PString("Reader = ") << PString(tar->GetCountPackets()) + countMessage << PString("Reader = ") << PString(tar.GetCountPackets()) << PString(" packets"); - else - countMessage << PString(" no read count available"); - countMessage << endl << endl; + countMessage << endl << endl; - if (taw != NULL) - countMessage << PString("writer = ") << PString(taw->GetCountPackets()) - << PString(" packets"); - else - countMessage << PString(" no write count available"); - countMessage << endl << endl; - } + countMessage << PString("writer = ") << PString(taw.GetCountPackets()) + << PString(" packets"); + countMessage << endl << endl; + PString partner = GetMediaPartner(); countMessage << "other node is: " << (partner.IsEmpty() ? " not specified" : partner); countMessage << endl << endl << "............................ "; @@ -231,36 +195,5 @@ } -BOOL Comms::ConstructReader(PSoundChannel & sound) -{ - PWaitAndSignal m(lockReaderWriter); - - tar = new TestAudioRead(audioSocket, sound); - PThread::Sleep(100); - - if (tar->IsTerminated()) { - PTRACE(3, "OOPS TestAudioRead has finished real quick"); - return FALSE; - } - return TRUE; -} - -BOOL Comms::ConstructWriter(PSoundChannel & sound) -{ - PWaitAndSignal m(lockReaderWriter); - - taw = new TestAudioWrite(audioSocket, sound); - PThread::Sleep(100); - - if (taw->IsTerminated()) { - PTRACE(3, "OOPS TestAudioWrite has finished real quick"); - return FALSE; - } - - return TRUE; -} - - - // End of File /////////////////////////////////////////////////////////////// Modified: sofaswitch/trunk/sound/comms.h ============================================================================== --- sofaswitch/trunk/sound/comms.h (original) +++ sofaswitch/trunk/sound/comms.h Thu Jan 4 17:33:15 2007 @@ -49,20 +49,12 @@ public: /**Constuctor */ - Comms(LaunchNewId & lid); + Comms(LaunchNewId & lid, PSoundChannel & _soundPlayer, PSoundChannel & _soundRecorder); /**Terminate, wait for threads to exit, and destroy the tar & taw threads */ ~Comms(); - - /**Open the test audio read thread.. - Return TRUE if successfull, FALSE on failure */ - BOOL ConstructReader(PSoundChannel & sound); - - /**Open the test audio write thread. - Return TRUE if successfull, FALSE on failure */ - BOOL ConstructWriter(PSoundChannel & sound); - + /**Shut down the read and write methods */ virtual void Terminate(); @@ -146,17 +138,14 @@ /**We have a comms error, so just close everything right now. Send no message to the remote node - just end end end. */ virtual void SilentlyEndAll(); - + /**@name manage the audio streams of data to the sound device */ //@{ - /**class that writes audio data to sound device */ - TestAudioWrite *taw; - /**Class that reads audio from the sound device */ - TestAudioRead *tar; - - /**Mutex which protects access to the tar/taw variables */ - PMutex lockReaderWriter; + TestAudioRead & tar; + + /**class that writes audio data to sound device */ + TestAudioWrite & taw; //@} /**The terminating condition was */ Modified: sofaswitch/trunk/sound/main.cxx ============================================================================== --- sofaswitch/trunk/sound/main.cxx (original) +++ sofaswitch/trunk/sound/main.cxx Thu Jan 4 17:33:15 2007 @@ -269,10 +269,7 @@ PString ourPassword = "audio"; PTRACE(3, "Select a default password of audio"); - - LaunchNewId lid(ourId, ourPassword); - Comms c(lid); - + PTRACE(3, "Constructed the comms device "); if (!OpenSoundDevice(soundRecorder, PSoundChannel::Recorder)) { @@ -290,18 +287,15 @@ PTRACE(3, msg); return; } - - if (!c.ConstructReader(soundRecorder)) { - cerr << "Failed to consturct a reader for audio.... OOPs srry " << endl; - PTRACE(3, "Failed to construct a reader for the audio. Exit now "); - return; - } - - if (!c.ConstructWriter(soundPlayer)) { - cerr << "Failed to consturct a writer for audio.... OOPs srry " << endl; - PTRACE(3, "Failed to construct a writer for the audio. Exit now "); - return; - } + + PTRACE(3, "Have opended the sound device just fine, now do a test read"); + BYTE nullBuffer[480]; + soundRecorder.Read(nullBuffer, 480); + soundRecorder.Read(nullBuffer, 480); + PTRACE(3, "finished test read"); + + LaunchNewId lid(ourId, ourPassword); + Comms c(lid, soundPlayer, soundRecorder); c.Resume(); //Get the coms to start working PThread::Sleep(200); //Wait for the comms to work @@ -338,6 +332,7 @@ } if (!sound.IsOpen()) { PTRACE(3, "just opened the device, but it is not open "); + return FALSE; } PINDEX currentVolume = 90; @@ -345,11 +340,13 @@ sound.SetBuffers(480, 2); - if (!sound.IsOpen()) { + if (sound.IsOpen()) { + PTRACE(3, "End of OpenAudio, sound device is opened just fine"); + return TRUE; + } else { PTRACE(3, "End of OpenAudio, but the sound device is closed"); + return FALSE; } - - return TRUE; } ////////////////////////////////////////////////////////////////////////////////