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
// For the flag enums, which can't be forward-declared.
34
#include <pulse/def.h>
36
#include "talk/base/constructormagic.h"
37
#include "talk/sound/pulseaudiosymboltable.h"
38
#include "talk/sound/soundsysteminterface.h"
42
struct pa_sample_spec;
43
struct pa_server_info;
45
struct pa_source_info;
47
struct pa_threaded_mainloop;
51
class PulseAudioInputStream;
52
class PulseAudioOutputStream;
53
class PulseAudioStream;
55
// Sound system implementation for PulseAudio, a cross-platform sound server
56
// (but commonly used only on Linux, which is the only platform we support
58
// Init(), Terminate(), and the destructor should never be invoked concurrently,
59
// but all other methods are thread-safe.
60
class PulseAudioSoundSystem : public SoundSystemInterface {
61
friend class PulseAudioInputStream;
62
friend class PulseAudioOutputStream;
63
friend class PulseAudioStream;
65
static SoundSystemInterface *Create() {
66
return new PulseAudioSoundSystem();
69
PulseAudioSoundSystem();
71
virtual ~PulseAudioSoundSystem();
74
virtual void Terminate();
76
virtual bool EnumeratePlaybackDevices(SoundDeviceLocatorList *devices);
77
virtual bool EnumerateCaptureDevices(SoundDeviceLocatorList *devices);
79
virtual bool GetDefaultPlaybackDevice(SoundDeviceLocator **device);
80
virtual bool GetDefaultCaptureDevice(SoundDeviceLocator **device);
82
virtual SoundOutputStreamInterface *OpenPlaybackDevice(
83
const SoundDeviceLocator *device,
84
const OpenParams ¶ms);
85
virtual SoundInputStreamInterface *OpenCaptureDevice(
86
const SoundDeviceLocator *device,
87
const OpenParams ¶ms);
89
virtual const char *GetName() const;
94
static void ConnectToPulseCallbackThunk(pa_context *context, void *userdata);
96
void OnConnectToPulseCallback(pa_context *context, bool *connect_done);
98
bool ConnectToPulse(pa_context *context);
100
pa_context *CreateNewConnection();
102
template <typename InfoStruct>
103
bool EnumerateDevices(SoundDeviceLocatorList *devices,
104
pa_operation *(*enumerate_fn)(
118
static void EnumeratePlaybackDevicesCallbackThunk(pa_context *unused,
119
const pa_sink_info *info,
123
static void EnumerateCaptureDevicesCallbackThunk(pa_context *unused,
124
const pa_source_info *info,
128
void OnEnumeratePlaybackDevicesCallback(
129
SoundDeviceLocatorList *devices,
130
const pa_sink_info *info,
133
void OnEnumerateCaptureDevicesCallback(
134
SoundDeviceLocatorList *devices,
135
const pa_source_info *info,
138
template <const char *(pa_server_info::*field)>
139
static void GetDefaultDeviceCallbackThunk(
141
const pa_server_info *info,
144
template <const char *(pa_server_info::*field)>
145
void OnGetDefaultDeviceCallback(
146
const pa_server_info *info,
147
SoundDeviceLocator **device);
149
template <const char *(pa_server_info::*field)>
150
bool GetDefaultDevice(SoundDeviceLocator **device);
152
static void StreamStateChangedCallbackThunk(pa_stream *stream,
155
void OnStreamStateChangedCallback(pa_stream *stream);
157
template <typename StreamInterface>
158
StreamInterface *OpenDevice(
159
const SoundDeviceLocator *device,
160
const OpenParams ¶ms,
161
const char *stream_name,
162
StreamInterface *(PulseAudioSoundSystem::*connect_fn)(
166
pa_stream_flags_t pa_flags,
168
const pa_sample_spec &spec));
170
SoundOutputStreamInterface *ConnectOutputStream(
174
pa_stream_flags_t pa_flags,
176
const pa_sample_spec &spec);
178
SoundInputStreamInterface *ConnectInputStream(
182
pa_stream_flags_t pa_flags,
184
const pa_sample_spec &spec);
186
bool FinishOperation(pa_operation *op);
193
const char *LastError();
195
pa_threaded_mainloop *mainloop_;
196
pa_context *context_;
197
PulseAudioSymbolTable symbol_table_;
199
DISALLOW_COPY_AND_ASSIGN(PulseAudioSoundSystem);
202
} // namespace cricket
204
#endif // HAVE_LIBPULSE
206
#endif // TALK_SOUND_PULSEAUDIOSOUNDSYSTEM_H_