3 * Copyright 2004--2010, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #ifndef TALK_SOUND_PULSEAUDIOSOUNDSYSTEM_H_
29 #define TALK_SOUND_PULSEAUDIOSOUNDSYSTEM_H_
33 #include "talk/sound/pulseaudiosymboltable.h"
34 #include "talk/sound/soundsysteminterface.h"
35 #include "webrtc/base/constructormagic.h"
39 class PulseAudioInputStream;
40 class PulseAudioOutputStream;
41 class PulseAudioStream;
43 // Sound system implementation for PulseAudio, a cross-platform sound server
44 // (but commonly used only on Linux, which is the only platform we support
46 // Init(), Terminate(), and the destructor should never be invoked concurrently,
47 // but all other methods are thread-safe.
48 class PulseAudioSoundSystem : public SoundSystemInterface {
49 friend class PulseAudioInputStream;
50 friend class PulseAudioOutputStream;
51 friend class PulseAudioStream;
53 static SoundSystemInterface *Create() {
54 return new PulseAudioSoundSystem();
57 PulseAudioSoundSystem();
59 virtual ~PulseAudioSoundSystem();
62 virtual void Terminate();
64 virtual bool EnumeratePlaybackDevices(SoundDeviceLocatorList *devices);
65 virtual bool EnumerateCaptureDevices(SoundDeviceLocatorList *devices);
67 virtual bool GetDefaultPlaybackDevice(SoundDeviceLocator **device);
68 virtual bool GetDefaultCaptureDevice(SoundDeviceLocator **device);
70 virtual SoundOutputStreamInterface *OpenPlaybackDevice(
71 const SoundDeviceLocator *device,
72 const OpenParams ¶ms);
73 virtual SoundInputStreamInterface *OpenCaptureDevice(
74 const SoundDeviceLocator *device,
75 const OpenParams ¶ms);
77 virtual const char *GetName() const;
82 static void ConnectToPulseCallbackThunk(pa_context *context, void *userdata);
84 void OnConnectToPulseCallback(pa_context *context, bool *connect_done);
86 bool ConnectToPulse(pa_context *context);
88 pa_context *CreateNewConnection();
90 template <typename InfoStruct>
91 bool EnumerateDevices(SoundDeviceLocatorList *devices,
92 pa_operation *(*enumerate_fn)(
106 static void EnumeratePlaybackDevicesCallbackThunk(pa_context *unused,
107 const pa_sink_info *info,
111 static void EnumerateCaptureDevicesCallbackThunk(pa_context *unused,
112 const pa_source_info *info,
116 void OnEnumeratePlaybackDevicesCallback(
117 SoundDeviceLocatorList *devices,
118 const pa_sink_info *info,
121 void OnEnumerateCaptureDevicesCallback(
122 SoundDeviceLocatorList *devices,
123 const pa_source_info *info,
126 template <const char *(pa_server_info::*field)>
127 static void GetDefaultDeviceCallbackThunk(
129 const pa_server_info *info,
132 template <const char *(pa_server_info::*field)>
133 void OnGetDefaultDeviceCallback(
134 const pa_server_info *info,
135 SoundDeviceLocator **device);
137 template <const char *(pa_server_info::*field)>
138 bool GetDefaultDevice(SoundDeviceLocator **device);
140 static void StreamStateChangedCallbackThunk(pa_stream *stream,
143 void OnStreamStateChangedCallback(pa_stream *stream);
145 template <typename StreamInterface>
146 StreamInterface *OpenDevice(
147 const SoundDeviceLocator *device,
148 const OpenParams ¶ms,
149 const char *stream_name,
150 StreamInterface *(PulseAudioSoundSystem::*connect_fn)(
154 pa_stream_flags_t pa_flags,
156 const pa_sample_spec &spec));
158 SoundOutputStreamInterface *ConnectOutputStream(
162 pa_stream_flags_t pa_flags,
164 const pa_sample_spec &spec);
166 SoundInputStreamInterface *ConnectInputStream(
170 pa_stream_flags_t pa_flags,
172 const pa_sample_spec &spec);
174 bool FinishOperation(pa_operation *op);
181 const char *LastError();
183 pa_threaded_mainloop *mainloop_;
184 pa_context *context_;
185 PulseAudioSymbolTable symbol_table_;
187 DISALLOW_COPY_AND_ASSIGN(PulseAudioSoundSystem);
190 } // namespace cricket
192 #endif // HAVE_LIBPULSE
194 #endif // TALK_SOUND_PULSEAUDIOSOUNDSYSTEM_H_