2 * FreeRDP: A Remote Desktop Protocol client.
3 * Video Redirection Virtual Channel - PulseAudio Device
5 * Copyright 2010-2011 Vic Lee
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
24 #include <pulse/pulseaudio.h>
25 #include <freerdp/utils/memory.h>
27 #include "tsmf_audio.h"
29 typedef struct _TSMFPulseAudioDevice
31 ITSMFAudioDevice iface;
34 pa_threaded_mainloop* mainloop;
36 pa_sample_spec sample_spec;
38 } TSMFPulseAudioDevice;
40 static void tsmf_pulse_context_state_callback(pa_context* context, void* userdata)
42 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*) userdata;
43 pa_context_state_t state;
45 state = pa_context_get_state(context);
48 case PA_CONTEXT_READY:
49 DEBUG_DVC("PA_CONTEXT_READY");
50 pa_threaded_mainloop_signal(pulse->mainloop, 0);
53 case PA_CONTEXT_FAILED:
54 case PA_CONTEXT_TERMINATED:
55 DEBUG_DVC("state %d", (int)state);
56 pa_threaded_mainloop_signal(pulse->mainloop, 0);
60 DEBUG_DVC("state %d", (int)state);
65 static boolean tsmf_pulse_connect(TSMFPulseAudioDevice* pulse)
67 pa_context_state_t state;
72 if (pa_context_connect(pulse->context, NULL, 0, NULL))
74 DEBUG_WARN("pa_context_connect failed (%d)",
75 pa_context_errno(pulse->context));
78 pa_threaded_mainloop_lock(pulse->mainloop);
79 if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
81 pa_threaded_mainloop_unlock(pulse->mainloop);
82 DEBUG_WARN("pa_threaded_mainloop_start failed (%d)",
83 pa_context_errno(pulse->context));
88 state = pa_context_get_state(pulse->context);
89 if (state == PA_CONTEXT_READY)
91 if (!PA_CONTEXT_IS_GOOD(state))
93 DEBUG_DVC("bad context state (%d)",
94 pa_context_errno(pulse->context));
97 pa_threaded_mainloop_wait(pulse->mainloop);
99 pa_threaded_mainloop_unlock(pulse->mainloop);
100 if (state == PA_CONTEXT_READY)
102 DEBUG_DVC("connected");
107 pa_context_disconnect(pulse->context);
112 static boolean tsmf_pulse_open(ITSMFAudioDevice* audio, const char* device)
114 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*) audio;
118 strcpy(pulse->device, device);
121 pulse->mainloop = pa_threaded_mainloop_new();
122 if (!pulse->mainloop)
124 DEBUG_WARN("pa_threaded_mainloop_new failed");
127 pulse->context = pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop), "freerdp");
130 DEBUG_WARN("pa_context_new failed");
133 pa_context_set_state_callback(pulse->context, tsmf_pulse_context_state_callback, pulse);
134 if (tsmf_pulse_connect(pulse))
136 DEBUG_WARN("tsmf_pulse_connect failed");
140 DEBUG_DVC("open device %s", pulse->device);
144 static void tsmf_pulse_stream_success_callback(pa_stream* stream, int success, void* userdata)
146 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*) userdata;
148 pa_threaded_mainloop_signal(pulse->mainloop, 0);
151 static void tsmf_pulse_wait_for_operation(TSMFPulseAudioDevice* pulse, pa_operation* operation)
153 if (operation == NULL)
155 while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING)
157 pa_threaded_mainloop_wait(pulse->mainloop);
159 pa_operation_unref(operation);
162 static void tsmf_pulse_stream_state_callback(pa_stream* stream, void* userdata)
164 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*) userdata;
165 pa_stream_state_t state;
167 state = pa_stream_get_state(stream);
170 case PA_STREAM_READY:
171 DEBUG_DVC("PA_STREAM_READY");
172 pa_threaded_mainloop_signal (pulse->mainloop, 0);
175 case PA_STREAM_FAILED:
176 case PA_STREAM_TERMINATED:
177 DEBUG_DVC("state %d", (int)state);
178 pa_threaded_mainloop_signal (pulse->mainloop, 0);
182 DEBUG_DVC("state %d", (int)state);
187 static void tsmf_pulse_stream_request_callback(pa_stream* stream, size_t length, void* userdata)
189 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*) userdata;
191 DEBUG_DVC("%d", (int) length);
193 pa_threaded_mainloop_signal(pulse->mainloop, 0);
196 static boolean tsmf_pulse_close_stream(TSMFPulseAudioDevice* pulse)
198 if (!pulse->context || !pulse->stream)
203 pa_threaded_mainloop_lock(pulse->mainloop);
204 pa_stream_set_write_callback(pulse->stream, NULL, NULL);
205 tsmf_pulse_wait_for_operation(pulse,
206 pa_stream_drain(pulse->stream, tsmf_pulse_stream_success_callback, pulse));
207 pa_stream_disconnect(pulse->stream);
208 pa_stream_unref(pulse->stream);
209 pulse->stream = NULL;
210 pa_threaded_mainloop_unlock(pulse->mainloop);
215 static boolean tsmf_pulse_open_stream(TSMFPulseAudioDevice* pulse)
217 pa_stream_state_t state;
218 pa_buffer_attr buffer_attr = { 0 };
225 pa_threaded_mainloop_lock(pulse->mainloop);
226 pulse->stream = pa_stream_new(pulse->context, "freerdp",
227 &pulse->sample_spec, NULL);
230 pa_threaded_mainloop_unlock(pulse->mainloop);
231 DEBUG_WARN("pa_stream_new failed (%d)",
232 pa_context_errno(pulse->context));
235 pa_stream_set_state_callback(pulse->stream,
236 tsmf_pulse_stream_state_callback, pulse);
237 pa_stream_set_write_callback(pulse->stream,
238 tsmf_pulse_stream_request_callback, pulse);
239 buffer_attr.maxlength = pa_usec_to_bytes(500000, &pulse->sample_spec);
240 buffer_attr.tlength = pa_usec_to_bytes(250000, &pulse->sample_spec);
241 buffer_attr.prebuf = (uint32_t) -1;
242 buffer_attr.minreq = (uint32_t) -1;
243 buffer_attr.fragsize = (uint32_t) -1;
244 if (pa_stream_connect_playback(pulse->stream,
245 pulse->device[0] ? pulse->device : NULL, &buffer_attr,
246 PA_STREAM_ADJUST_LATENCY | PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE,
249 pa_threaded_mainloop_unlock(pulse->mainloop);
250 DEBUG_WARN("pa_stream_connect_playback failed (%d)",
251 pa_context_errno(pulse->context));
257 state = pa_stream_get_state(pulse->stream);
258 if (state == PA_STREAM_READY)
260 if (!PA_STREAM_IS_GOOD(state))
262 DEBUG_WARN("bad stream state (%d)",
263 pa_context_errno(pulse->context));
266 pa_threaded_mainloop_wait(pulse->mainloop);
268 pa_threaded_mainloop_unlock(pulse->mainloop);
269 if (state == PA_STREAM_READY)
271 DEBUG_DVC("connected");
276 tsmf_pulse_close_stream(pulse);
281 static boolean tsmf_pulse_set_format(ITSMFAudioDevice* audio,
282 uint32 sample_rate, uint32 channels, uint32 bits_per_sample)
284 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*) audio;
286 DEBUG_DVC("sample_rate %d channels %d bits_per_sample %d",
287 sample_rate, channels, bits_per_sample);
289 pulse->sample_spec.rate = sample_rate;
290 pulse->sample_spec.channels = channels;
291 pulse->sample_spec.format = PA_SAMPLE_S16LE;
293 return tsmf_pulse_open_stream(pulse);
296 static boolean tsmf_pulse_play(ITSMFAudioDevice* audio, uint8* data, uint32 data_size)
298 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*) audio;
303 DEBUG_DVC("data_size %d", data_size);
307 pa_threaded_mainloop_lock(pulse->mainloop);
310 while (data_size > 0)
312 while ((len = pa_stream_writable_size(pulse->stream)) == 0)
314 DEBUG_DVC("waiting");
315 pa_threaded_mainloop_wait(pulse->mainloop);
321 ret = pa_stream_write(pulse->stream, src, len, NULL, 0LL, PA_SEEK_RELATIVE);
324 DEBUG_DVC("pa_stream_write failed (%d)",
325 pa_context_errno(pulse->context));
332 pa_threaded_mainloop_unlock(pulse->mainloop);
339 static uint64 tsmf_pulse_get_latency(ITSMFAudioDevice* audio)
343 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*) audio;
345 if (pulse->stream && pa_stream_get_latency(pulse->stream, &usec, NULL) == 0)
347 latency = ((uint64)usec) * 10LL;
352 static void tsmf_pulse_flush(ITSMFAudioDevice* audio)
354 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*) audio;
356 pa_threaded_mainloop_lock(pulse->mainloop);
357 tsmf_pulse_wait_for_operation(pulse,
358 pa_stream_flush(pulse->stream, tsmf_pulse_stream_success_callback, pulse));
359 pa_threaded_mainloop_unlock(pulse->mainloop);
362 static void tsmf_pulse_free(ITSMFAudioDevice* audio)
364 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*) audio;
368 tsmf_pulse_close_stream(pulse);
371 pa_threaded_mainloop_stop(pulse->mainloop);
375 pa_context_disconnect(pulse->context);
376 pa_context_unref(pulse->context);
377 pulse->context = NULL;
381 pa_threaded_mainloop_free(pulse->mainloop);
382 pulse->mainloop = NULL;
387 ITSMFAudioDevice* TSMFAudioDeviceEntry(void)
389 TSMFPulseAudioDevice* pulse;
391 pulse = xnew(TSMFPulseAudioDevice);
393 pulse->iface.Open = tsmf_pulse_open;
394 pulse->iface.SetFormat = tsmf_pulse_set_format;
395 pulse->iface.Play = tsmf_pulse_play;
396 pulse->iface.GetLatency = tsmf_pulse_get_latency;
397 pulse->iface.Flush = tsmf_pulse_flush;
398 pulse->iface.Free = tsmf_pulse_free;
400 return (ITSMFAudioDevice*) pulse;