Re: [GnomeMeeting-devel-list] Advanced support for bluetooth headsets



Julien PUYDT a �it :
Well, oh, well. Let me share a wild idea I have in the back of my mind ever since I worked on turning drivers into plugins in ekiga (but never specifically worked on this).

Of course discussing it arouse my interest in it enough to write some basic code about it...

Here it is. If anybody wants to finish it, please proceed!

Snark
PFacadeSoundChannel::PFacadeSoundChannel ()
{
  realChannel = NULL;
  isOpen = FALSE;
}

PFacadeSoundChannel::~PFacadeSoundChannel()
{
  Close ();
}

PStringArray
PFacadeSoundChannel::GetDeviceNames (PSoundChannel::Directions)
{
  PStringArray devices;

  devices += "---";

  return devices;
}

PString
PFacadeSoundChannel::GetDefaultDevice (PSoundChannel::Directions)
{
  PStringArray names;

  names = PFacadeSoundChannel::GetDeviceNames (dir);

  return names[0];
}

BOOL
PFacadeSoundChannel::SetChannel (PSoundChannel *channel)
{
  /*
   * FIXME: to implement
   *
   * Here we should try to setup the channel to the same state as ourself
   * which may include having the same number of buffers with the same size,
   * the same direction, sample rate, bits per sample, number of channels,
   * being open or not...
   *
   * When this is done, we should take the mutex, take a copy of our old
   * channel, put the new one in place, release the mutex, then dispose of
   * our old channel.
   *
   * If something fails, we return FALSE ; so for example if the user requested
   * ekiga to use a new device, we will be able to show a nice error message.
   * If all goes well, just return TRUE.
   *
   */
}

BOOL
PFacadeSoundChannel::Open (const PString &device,
			   Directions direction,
			   unsigned numChannels,
			   unsigned sampleRate,
			   unsigned bitsPerSample)
{
  BOOL result = FALSE;
  PWaitAndSignal m(realChannelMutex);

  Close ();

  if (realChannel != NULL)
    result = realChannel->Open (device, direction, numChannels,
				sampleRate, bitsPerSample);
  else
    result = TRUE;

  if (result) { /* there was no issue: update ourselves */

    ownDirection = direction;
    ownNumChannels = numChannels;
    ownSampleRate = sampleRate;
    ownBitsPerSample = bitsPerSample;
    isOpen = TRUE;
  }

  return result;
}

BOOL
PFacadeSoundChannel::Close ()
{
  BOOL result = FALSE;
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    result = realChannel->Close ();
  else
    result = TRUE;

  if (result)
    isOpen = FALSE;

  return result;
}

BOOL
PFacadeSoundChannel::Write (const void *buf,
			    PINDEX len)
{
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    return realChannel->Write (buf, len);
  else
    return TRUE; /* FIXME: correct? */
}

BOOL
PFacadeSoundChannel::Read (void *buf,
			   PINDEX len)
{
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    return realChannel->Read (buf, len);
  else
    return TRUE; /* FIXME: should put silence in the buffer */
}

BOOL
PFacadeSoundChannel::SetFormat (unsigned numChannels,
				unsigned sampleRate,
				unsigned bitsPerSample)
{
  BOOL result = FALSE;
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    result = realChannel->SetFormat (numChannels, sampleRate, bitsPerSample);
  else
    result = TRUE;

  if (result) {

    ownNumChannels = numChannels;
    ownSampleRate = sampleRate;
    ownBitsPerSample = bitsPerSample;
  }

  return result;
}

unsigned
PFacadeSoundChannel::GetChannels ()
{
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    return realChannel->GetChannels ();
  else
    return 0; /* FIXME: correct? */
}

unsigned
PFacadeSoundChannel::GetSampleRate ()
{
  return ownSampleRate;
}

unsigned
PFacadeSoundChannel::GetSampleSize ()
{
  return ownBitsPerSample;
}

BOOL
PFacadeSoundChannel::SetBuffers (PINDEX size,
				 PINDEX count)
{
  BOOL result = FALSE;
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    result = realChannel->SetBuffers (size, count);
  else
    result = TRUE;

  if (result) {

    bufferSize = size;
    bufferCount = count;
  }

  return result;
}

BOOL
PFacadeSoundChannel::GetBuffers (PINDEX &size,
				 PINDEX &count)
{
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    return realChannel->GetBuffers (size, count);
  else  {

    size = bufferSize;
    count = bufferCount;
    return TRUE;
  }
}

BOOL
PFacadeSoundChannel::PlaySound (const PSound &sound,
				BOOL wait)
{
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    return realChannel->PlaySound (sound, wait);
  else
    return TRUE;
}

BOOL
PFacadeSoundChannel::PlayFile (const PFilePath &filename,
			       BOOL wait);
{
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    return realChannel->PlayFile (filename, wait);
  else
    return TRUE;
}

BOOL
PFacadeSoundChannel::HasPlayCompleted ();
{
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    return realChannel->HasPlayCompleted ();
  else
    return TRUE;
}

BOOL
PFacadeSoundChannel::WaitForPlayCompletion ();
{
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    return realChannel->WaitForPlayCompletion ();
  else
    return TRUE;
}

BOOL
PFacadeSoundChannel::RecordSound (PSound &sound);
{
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    return realChannel->RecordSound (sound);
  else
    return TRUE;
}

BOOL
PFacadeSoundChannel::RecordFile (const PFilePath &filename);
{
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    return realChannel->RecordFile (filename);
  else
    return TRUE;
}

BOOL
PFacadeSoundChannel::StartRecording ();
{
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    return realChannel->StartRecording ();
  else
    return TRUE;
}

BOOL
PFacadeSoundChannel::IsRecordBufferFull ();
{
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    return realChannel->IsRecordBufferFull ();
  else
    return FALSE;
}

BOOL
PFacadeSoundChannel::AreAllRecordBuffersFull ();
{
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    return realChannel->AreAllRecordBuffersFull ();
  else
    return FALSE;
}

BOOL
PFacadeSoundChannel::WaitForRecordBufferFull ();
{
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    return realChannel->WaitForRecordBufferFull ();
  else
    return TRUE;
}

BOOL
PFacadeSoundChannel::WaitForAllRecordBuffersFull ();
{
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    return realChannel->WaitForAllRecordBuffersFull ();
  else
    return TRUE;
}

BOOL
PFacadeSoundChannel::Abort ();
{
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    return realChannel->Abort ();
  else
    return TRUE;
}

BOOL
PFacadeSoundChannel::SetVolume (unsigned value);
{
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    return realChannel->SetVolume (value);
  else
    return TRUE;
}

BOOL
PFacadeSoundChannel::GetVolume (unsigned &value);
{
  PWaitAndSignal m(realChannelMutex);

  if (realChannel != NULL)
    return realChannel->GetVolume (value);
  else
    return 100;
}

BOOL
PFacadeSoundChannel::IsOpen ();
{
  return isOpen;
}
class PFacadeSoundChannel:: public PSoundChannel
{
 public:
  PFacadeSoundChannel ();
  ~PFacadeSoundChannel ();

  static PStringArray GetDeviceNames (PSoundChannel::Directions);
  static PString GetDefaultDevice (PSoundChannel::Directions);

  BOOL SetChannel (PSoundChannel *channel);

  BOOL Open (const PString &device,
	     Directions direction,
	     unsigned numChannels,
	     unsigned sampleRate,
	     unsigned bitsPerSample);
  BOOL Close ();

  BOOL Write (const void *buf,
	      PINDEX len);
  BOOL Read (void *buf,
	     PINDEX len);
  BOOL SetFormat (unsigned numChannels,
		  unsigned sampleRate,
		  unsigned bitsPerSample);

  unsigned GetChannels () const;

  unsigned GetSampleRate () const;

  unsigned GetSampleSize () const;

  BOOL SetBuffers (PINDEX size,
		   PINDEX count);
  BOOL GetBuffers (PINDEX &size,
		   PINDEX &count);
  BOOL PlaySound (const PSound &sound,
		  BOOL wait);
  BOOL PlayFile (const PFilePath &filename,
		 BOOL wait);
  BOOL HasPlayCompleted ();
  BOOL WaitForPlayCompletion ();
  BOOL RecordSound (PSound &sound);
  BOOL RecordFile (const PFilePath &filename);
  BOOL StartRecording ();
  BOOL IsRecordBufferFull ();
  BOOL AreAllRecordBuffersFull ();
  BOOL WaitForRecordBufferFull ();
  BOOL WaitForAllRecordBuffersFull ();
  BOOL Abort ();
  BOOL SetVolume (unsigned);
  BOOL GetVolume (unsigned &);
  BOOL IsOpen () const;

 private:

  PMutex realChannelMutex;
  PSoundChannel *realChannel;
  PSoundChannel::Directions ownDirection;
  unsigned ownNumChannels;
  unsigned ownSampleRate;
  unsigned ownBitsPerSample;
  PINDEX bufferSize;
  PINDEX bufferCount;
  BOOL isOpen;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]