forked from mono/moon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio-pulse.h
More file actions
189 lines (154 loc) · 4.62 KB
/
audio-pulse.h
File metadata and controls
189 lines (154 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* audio-pulse.h:
*
* Contact:
* Moonlight List (moonlight-list@lists.ximian.com)
*
* Copyright 2008 Novell, Inc. (http://www.novell.com)
*
* See the LICENSE file included with the distribution for details.
*/
#if INCLUDE_PULSEAUDIO
#ifndef __AUDIO_PULSE_H__
#define __AUDIO_PULSE_H__
#include <pulse/pulseaudio.h>
#include "audio.h"
namespace Moonlight {
class PulsePlayer;
class PulseSource: public AudioSource {
PulsePlayer *player;
pa_stream *pulse_stream;
bool triggered;
bool is_ready;
bool initialized;
bool play_pending;
void PACork (bool cork);
void PATrigger ();
void PAFlush ();
void OnUnderflow ();
void OnStateChanged (pa_stream *stream);
void OnWrite (size_t length);
static void OnUnderflow (pa_stream *pulse_stream, void *userdata);
static void OnWrite (pa_stream *pulse_stream, size_t length, void *userdata);
static void OnStateChanged (pa_stream *pulse_stream, void *userdata);
bool InitializePA ();
void ClosePA ();
void WriteAvailable ();
pa_stream_state_t GetPAState (pa_stream *pulse_stream = NULL);
protected:
virtual ~PulseSource ();
virtual void Played ();
virtual void Paused ();
virtual void Stopped ();
virtual void StateChanged (AudioState old_state);
virtual guint64 GetDelayInternal ();
virtual bool InitializeInternal ();
virtual void CloseInternal ();
public:
/* @SkipFactories */
PulseSource (PulsePlayer *player, MediaPlayer *mplayer, AudioStream *stream);
};
class PulseRecorder : public AudioRecorder {
public:
class SourceNode;
private:
struct ReadData : public List::Node {
guint8 *data;
gsize nbytes;
virtual ~ReadData ()
{
g_free (data);
}
};
PulsePlayer *player;
pa_stream *pulse_stream;
bool is_ready;
pthread_mutex_t ready_mutex;
pthread_cond_t ready_cond;
char *name;
char *description;
pa_sample_spec default_spec;
pa_sample_spec recording_spec;
AudioFormat *recording_format;
bool InitializePA ();
void OnStateChanged (pa_stream *stream);
void OnRead (size_t length);
static void OnRead (pa_stream *pulse_stream, size_t length, void *userdata);
static void OnStateChanged (pa_stream *pulse_stream, void *userdata);
pa_stream_state_t GetPAState (pa_stream *pulse_stream = NULL);
static AudioFormat SampleSpecToAudioFormat (const pa_sample_spec *spec);
public:
/* @SkipFactories */
PulseRecorder (PulsePlayer *player, SourceNode *info);
virtual ~PulseRecorder ();
virtual void Dispose ();
virtual void Record ();
virtual void Stop ();
virtual void GetSupportedFormats (AudioFormatCollection *col);
virtual const char *GetFriendlyName () { return description; }
struct SourceNode : public List::Node {
char *name;
char *description;
pa_sample_spec sample_spec;
bool is_monitor;
SourceNode (const pa_source_info *i)
{
this->name = g_strdup (i->name);
this->description = g_strdup (i->description);
this->sample_spec = i->sample_spec;
this->is_monitor = i->monitor_of_sink != PA_INVALID_INDEX;
}
virtual ~SourceNode ()
{
g_free (name);
g_free (description);
}
};
};
class PulsePlayer : public AudioPlayer {
private:
enum ConnectedState {
ConnectionUnknown,
ConnectionFailed,
ConnectionSuccess
};
pa_context *context;
pa_threaded_mainloop *loop;
pa_mainloop_api *api;
pthread_cond_t cond;
pthread_mutex_t mutex;
pthread_cond_t recording_cond;
pthread_mutex_t recording_mutex;
bool fetching_recording_devices;
List recording_devices;
ConnectedState connected; // 0 = don't know, 1 = failed to connect, 2 = connected
static void OnContextStateChanged (pa_context *context, void *userdata);
static void OnSourceInfoCallback (pa_context *context, const pa_source_info *i, int eol, void *userdata);
void OnContextStateChanged ();
void OnSourceInfo (const pa_source_info *i, int eol);
protected:
virtual void AddInternal (AudioSource *node);
virtual void RemoveInternal (AudioSource *node);
virtual void PrepareShutdownInternal ();
virtual void FinishShutdownInternal ();
virtual bool Initialize ();
virtual AudioSource *CreateNode (MediaPlayer *mplayer, AudioStream *stream);
virtual guint32 CreateRecordersInternal (AudioRecorder **recorders, guint32 size);
public:
PulsePlayer ();
virtual ~PulsePlayer ();
pa_context *GetPAContext () { return context; }
pa_threaded_mainloop *GetPALoop () { return loop; }
pa_mainloop_api *GetPAApi () { return api; }
pa_context_state_t GetPAState ();
void LockLoop ();
void UnlockLoop ();
void WaitLoop ();
void WaitForOperation (pa_operation *op);
void SignalLoop ();
static bool IsInstalled ();
};
};
#endif /* __AUDIO_PULSE_H__ */
#endif /* INCLUDE_PULSE */