Home | History | Annotate | Download | only in sound
      1 /*
      2  * libjingle
      3  * Copyright 2004--2010, Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *  1. Redistributions of source code must retain the above copyright notice,
      9  *     this list of conditions and the following disclaimer.
     10  *  2. Redistributions in binary form must reproduce the above copyright notice,
     11  *     this list of conditions and the following disclaimer in the documentation
     12  *     and/or other materials provided with the distribution.
     13  *  3. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #ifndef TALK_SOUND_ALSASOUNDSYSTEM_H_
     29 #define TALK_SOUND_ALSASOUNDSYSTEM_H_
     30 
     31 #include "talk/base/constructormagic.h"
     32 #include "talk/sound/alsasymboltable.h"
     33 #include "talk/sound/soundsysteminterface.h"
     34 
     35 namespace cricket {
     36 
     37 class AlsaStream;
     38 class AlsaInputStream;
     39 class AlsaOutputStream;
     40 
     41 // Sound system implementation for ALSA, the predominant sound device API on
     42 // Linux (but typically not used directly by applications anymore).
     43 class AlsaSoundSystem : public SoundSystemInterface {
     44   friend class AlsaStream;
     45   friend class AlsaInputStream;
     46   friend class AlsaOutputStream;
     47  public:
     48   static SoundSystemInterface *Create() {
     49     return new AlsaSoundSystem();
     50   }
     51 
     52   AlsaSoundSystem();
     53 
     54   virtual ~AlsaSoundSystem();
     55 
     56   virtual bool Init();
     57   virtual void Terminate();
     58 
     59   virtual bool EnumeratePlaybackDevices(SoundDeviceLocatorList *devices);
     60   virtual bool EnumerateCaptureDevices(SoundDeviceLocatorList *devices);
     61 
     62   virtual bool GetDefaultPlaybackDevice(SoundDeviceLocator **device);
     63   virtual bool GetDefaultCaptureDevice(SoundDeviceLocator **device);
     64 
     65   virtual SoundOutputStreamInterface *OpenPlaybackDevice(
     66       const SoundDeviceLocator *device,
     67       const OpenParams &params);
     68   virtual SoundInputStreamInterface *OpenCaptureDevice(
     69       const SoundDeviceLocator *device,
     70       const OpenParams &params);
     71 
     72   virtual const char *GetName() const;
     73 
     74  private:
     75   bool IsInitialized() { return initialized_; }
     76 
     77   bool EnumerateDevices(SoundDeviceLocatorList *devices,
     78                         bool capture_not_playback);
     79 
     80   bool GetDefaultDevice(SoundDeviceLocator **device);
     81 
     82   static size_t FrameSize(const OpenParams &params);
     83 
     84   template <typename StreamInterface>
     85   StreamInterface *OpenDevice(
     86       const SoundDeviceLocator *device,
     87       const OpenParams &params,
     88       snd_pcm_stream_t type,
     89       StreamInterface *(AlsaSoundSystem::*start_fn)(
     90           snd_pcm_t *handle,
     91           size_t frame_size,
     92           int wait_timeout_ms,
     93           int flags,
     94           int freq));
     95 
     96   SoundOutputStreamInterface *StartOutputStream(
     97       snd_pcm_t *handle,
     98       size_t frame_size,
     99       int wait_timeout_ms,
    100       int flags,
    101       int freq);
    102 
    103   SoundInputStreamInterface *StartInputStream(
    104       snd_pcm_t *handle,
    105       size_t frame_size,
    106       int wait_timeout_ms,
    107       int flags,
    108       int freq);
    109 
    110   const char *GetError(int err);
    111 
    112   bool initialized_;
    113   AlsaSymbolTable symbol_table_;
    114 
    115   DISALLOW_COPY_AND_ASSIGN(AlsaSoundSystem);
    116 };
    117 
    118 }  // namespace cricket
    119 
    120 #endif  // TALK_SOUND_ALSASOUNDSYSTEM_H_
    121