~ubuntu-branches/ubuntu/vivid/freerdp/vivid

« back to all changes in this revision

Viewing changes to channels/rdpsnd/pulse/rdpsnd_pulse.c

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2014-11-11 12:20:50 UTC
  • mfrom: (1.1.9) (9.1.17 sid)
  • Revision ID: package-import@ubuntu.com-20141111122050-wyr8hrnwco9fcmum
Tags: 1.1.0~git20140921.1.440916e+dfsg1-2ubuntu1
* Merge with Debian unstable, remaining changes
  - Disable ffmpeg support
* Disable gstreamer support, this relies on gstreamer 0.10 and we don't want
  to add any more deps on that.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * FreeRDP: A Remote Desktop Protocol client.
3
 
 * Audio Output Virtual Channel
4
 
 *
5
 
 * Copyright 2011 Vic Lee
6
 
 *
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
10
 
 *
11
 
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 
 *
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.
18
 
 */
19
 
 
20
 
#include <stdio.h>
21
 
#include <stdlib.h>
22
 
#include <string.h>
23
 
#include <pulse/pulseaudio.h>
24
 
#include <freerdp/types.h>
25
 
#include <freerdp/utils/memory.h>
26
 
#include <freerdp/utils/dsp.h>
27
 
#include <freerdp/utils/svc_plugin.h>
28
 
 
29
 
#include "rdpsnd_main.h"
30
 
 
31
 
typedef struct rdpsnd_pulse_plugin rdpsndPulsePlugin;
32
 
struct rdpsnd_pulse_plugin
33
 
{
34
 
        rdpsndDevicePlugin device;
35
 
 
36
 
        char* device_name;
37
 
        pa_threaded_mainloop *mainloop;
38
 
        pa_context *context;
39
 
        pa_sample_spec sample_spec;
40
 
        pa_stream *stream;
41
 
        int format;
42
 
        int block_size;
43
 
        int latency;
44
 
        ADPCM adpcm;
45
 
};
46
 
 
47
 
static void rdpsnd_pulse_context_state_callback(pa_context* context, void* userdata)
48
 
{
49
 
        rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
50
 
        pa_context_state_t state;
51
 
 
52
 
        state = pa_context_get_state(context);
53
 
        switch (state)
54
 
        {
55
 
                case PA_CONTEXT_READY:
56
 
                        DEBUG_SVC("PA_CONTEXT_READY");
57
 
                        pa_threaded_mainloop_signal(pulse->mainloop, 0);
58
 
                        break;
59
 
 
60
 
                case PA_CONTEXT_FAILED:
61
 
                case PA_CONTEXT_TERMINATED:
62
 
                        DEBUG_SVC("PA_CONTEXT_FAILED/PA_CONTEXT_TERMINATED %d", (int)state);
63
 
                        pa_threaded_mainloop_signal(pulse->mainloop, 0);
64
 
                        break;
65
 
 
66
 
                default:
67
 
                        DEBUG_SVC("state %d", (int)state);
68
 
                        break;
69
 
        }
70
 
}
71
 
 
72
 
static boolean rdpsnd_pulse_connect(rdpsndDevicePlugin* device)
73
 
{
74
 
        rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
75
 
        pa_context_state_t state;
76
 
 
77
 
        if (!pulse->context)
78
 
                return false;
79
 
 
80
 
        if (pa_context_connect(pulse->context, NULL, 0, NULL))
81
 
        {
82
 
                DEBUG_WARN("pa_context_connect failed (%d)", pa_context_errno(pulse->context));
83
 
                return false;
84
 
        }
85
 
        pa_threaded_mainloop_lock(pulse->mainloop);
86
 
        if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
87
 
        {
88
 
                pa_threaded_mainloop_unlock(pulse->mainloop);
89
 
                DEBUG_WARN("pa_threaded_mainloop_start failed (%d)", pa_context_errno(pulse->context));
90
 
                return false;
91
 
        }
92
 
        for (;;)
93
 
        {
94
 
                state = pa_context_get_state(pulse->context);
95
 
                if (state == PA_CONTEXT_READY)
96
 
                        break;
97
 
                if (!PA_CONTEXT_IS_GOOD(state))
98
 
                {
99
 
                        DEBUG_WARN("bad context state (%d)", pa_context_errno(pulse->context));
100
 
                        break;
101
 
                }
102
 
                pa_threaded_mainloop_wait(pulse->mainloop);
103
 
        }
104
 
        pa_threaded_mainloop_unlock(pulse->mainloop);
105
 
        if (state == PA_CONTEXT_READY)
106
 
        {
107
 
                DEBUG_SVC("connected");
108
 
                return true;
109
 
        }
110
 
        else
111
 
        {
112
 
                pa_context_disconnect(pulse->context);
113
 
                return false;
114
 
        }
115
 
}
116
 
 
117
 
static void rdpsnd_pulse_stream_success_callback(pa_stream* stream, int success, void* userdata)
118
 
{
119
 
        rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
120
 
 
121
 
        pa_threaded_mainloop_signal(pulse->mainloop, 0);
122
 
}
123
 
 
124
 
static void rdpsnd_pulse_wait_for_operation(rdpsndPulsePlugin* pulse, pa_operation* operation)
125
 
{
126
 
        if (operation == NULL)
127
 
                return;
128
 
        while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING)
129
 
        {
130
 
                pa_threaded_mainloop_wait(pulse->mainloop);
131
 
        }
132
 
        pa_operation_unref(operation);
133
 
}
134
 
 
135
 
static void rdpsnd_pulse_stream_state_callback(pa_stream* stream, void* userdata)
136
 
{
137
 
        rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
138
 
        pa_stream_state_t state;
139
 
 
140
 
        state = pa_stream_get_state(stream);
141
 
        switch (state)
142
 
        {
143
 
                case PA_STREAM_READY:
144
 
                        DEBUG_SVC("PA_STREAM_READY");
145
 
                        pa_threaded_mainloop_signal(pulse->mainloop, 0);
146
 
                        break;
147
 
 
148
 
                case PA_STREAM_FAILED:
149
 
                case PA_STREAM_TERMINATED:
150
 
                        DEBUG_SVC("state %d", (int)state);
151
 
                        pa_threaded_mainloop_signal(pulse->mainloop, 0);
152
 
                        break;
153
 
 
154
 
                default:
155
 
                        DEBUG_SVC("state %d", (int)state);
156
 
                        break;
157
 
        }
158
 
}
159
 
 
160
 
static void rdpsnd_pulse_stream_request_callback(pa_stream* stream, size_t length, void* userdata)
161
 
{
162
 
        rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
163
 
 
164
 
        pa_threaded_mainloop_signal(pulse->mainloop, 0);
165
 
}
166
 
 
167
 
static void rdpsnd_pulse_close(rdpsndDevicePlugin* device)
168
 
{
169
 
        rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
170
 
 
171
 
        if (!pulse->context || !pulse->stream)
172
 
                return;
173
 
 
174
 
        pa_threaded_mainloop_lock(pulse->mainloop);
175
 
        rdpsnd_pulse_wait_for_operation(pulse,
176
 
                pa_stream_drain(pulse->stream, rdpsnd_pulse_stream_success_callback, pulse));
177
 
        pa_stream_disconnect(pulse->stream);
178
 
        pa_stream_unref(pulse->stream);
179
 
        pulse->stream = NULL;
180
 
        pa_threaded_mainloop_unlock(pulse->mainloop);
181
 
}
182
 
 
183
 
static void rdpsnd_pulse_set_format_spec(rdpsndPulsePlugin* pulse, rdpsndFormat* format)
184
 
{
185
 
        pa_sample_spec sample_spec = { 0 };
186
 
 
187
 
        if (!pulse->context)
188
 
                return;
189
 
 
190
 
        sample_spec.rate = format->nSamplesPerSec;
191
 
        sample_spec.channels = format->nChannels;
192
 
        switch (format->wFormatTag)
193
 
        {
194
 
                case 1: /* PCM */
195
 
                        switch (format->wBitsPerSample)
196
 
                        {
197
 
                                case 8:
198
 
                                        sample_spec.format = PA_SAMPLE_U8;
199
 
                                        break;
200
 
                                case 16:
201
 
                                        sample_spec.format = PA_SAMPLE_S16LE;
202
 
                                        break;
203
 
                        }
204
 
                        break;
205
 
 
206
 
                case 6: /* A-LAW */
207
 
                        sample_spec.format = PA_SAMPLE_ALAW;
208
 
                        break;
209
 
 
210
 
                case 7: /* U-LAW */
211
 
                        sample_spec.format = PA_SAMPLE_ULAW;
212
 
                        break;
213
 
 
214
 
                case 0x11: /* IMA ADPCM */
215
 
                        sample_spec.format = PA_SAMPLE_S16LE;
216
 
                        break;
217
 
        }
218
 
 
219
 
        pulse->sample_spec = sample_spec;
220
 
        pulse->format = format->wFormatTag;
221
 
        pulse->block_size = format->nBlockAlign;
222
 
}
223
 
 
224
 
static void rdpsnd_pulse_open(rdpsndDevicePlugin* device, rdpsndFormat* format, int latency)
225
 
{
226
 
        rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
227
 
        pa_stream_state_t state;
228
 
        pa_stream_flags_t flags;
229
 
        pa_buffer_attr buffer_attr = { 0 };
230
 
        char ss[PA_SAMPLE_SPEC_SNPRINT_MAX];
231
 
 
232
 
        if (!pulse->context || pulse->stream)
233
 
        {
234
 
                DEBUG_WARN("pulse stream has been created.");
235
 
                return;
236
 
        }
237
 
 
238
 
        rdpsnd_pulse_set_format_spec(pulse, format);
239
 
        pulse->latency = latency;
240
 
 
241
 
        if (pa_sample_spec_valid(&pulse->sample_spec) == 0)
242
 
        {
243
 
                pa_sample_spec_snprint(ss, sizeof(ss), &pulse->sample_spec);
244
 
                DEBUG_WARN("Invalid sample spec %s", ss);
245
 
                return;
246
 
        }
247
 
 
248
 
        pa_threaded_mainloop_lock(pulse->mainloop);
249
 
        pulse->stream = pa_stream_new(pulse->context, "freerdp",
250
 
                &pulse->sample_spec, NULL);
251
 
        if (!pulse->stream)
252
 
        {
253
 
                pa_threaded_mainloop_unlock(pulse->mainloop);
254
 
                DEBUG_WARN("pa_stream_new failed (%d)",
255
 
                        pa_context_errno(pulse->context));
256
 
                return;
257
 
        }
258
 
 
259
 
        /* install essential callbacks */
260
 
        pa_stream_set_state_callback(pulse->stream,
261
 
                rdpsnd_pulse_stream_state_callback, pulse);
262
 
        pa_stream_set_write_callback(pulse->stream,
263
 
                rdpsnd_pulse_stream_request_callback, pulse);
264
 
 
265
 
        flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
266
 
        if (pulse->latency > 0)
267
 
        {
268
 
                buffer_attr.maxlength = pa_usec_to_bytes(pulse->latency * 2 * 1000, &pulse->sample_spec);
269
 
                buffer_attr.tlength = pa_usec_to_bytes(pulse->latency * 1000, &pulse->sample_spec);
270
 
                buffer_attr.prebuf = (uint32_t) -1;
271
 
                buffer_attr.minreq = (uint32_t) -1;
272
 
                buffer_attr.fragsize = (uint32_t) -1;
273
 
                flags |= PA_STREAM_ADJUST_LATENCY;
274
 
        }
275
 
        if (pa_stream_connect_playback(pulse->stream,
276
 
                pulse->device_name, pulse->latency > 0 ? &buffer_attr : NULL, flags, NULL, NULL) < 0)
277
 
        {
278
 
                pa_threaded_mainloop_unlock(pulse->mainloop);
279
 
                DEBUG_WARN("pa_stream_connect_playback failed (%d)",
280
 
                        pa_context_errno(pulse->context));
281
 
                return;
282
 
        }
283
 
 
284
 
        for (;;)
285
 
        {
286
 
                state = pa_stream_get_state(pulse->stream);
287
 
                if (state == PA_STREAM_READY)
288
 
                        break;
289
 
                if (!PA_STREAM_IS_GOOD(state))
290
 
                {
291
 
                        DEBUG_WARN("bad stream state (%d)",
292
 
                                pa_context_errno(pulse->context));
293
 
                        break;
294
 
                }
295
 
                pa_threaded_mainloop_wait(pulse->mainloop);
296
 
        }
297
 
        pa_threaded_mainloop_unlock(pulse->mainloop);
298
 
        if (state == PA_STREAM_READY)
299
 
        {
300
 
                memset(&pulse->adpcm, 0, sizeof(ADPCM));
301
 
                DEBUG_SVC("connected");
302
 
        }
303
 
        else
304
 
        {
305
 
                rdpsnd_pulse_close(device);
306
 
        }
307
 
}
308
 
 
309
 
static void rdpsnd_pulse_free(rdpsndDevicePlugin* device)
310
 
{
311
 
        rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
312
 
 
313
 
        if (!pulse)
314
 
                return;
315
 
        rdpsnd_pulse_close(device);
316
 
        if (pulse->mainloop)
317
 
        {
318
 
                pa_threaded_mainloop_stop(pulse->mainloop);
319
 
        }
320
 
        if (pulse->context)
321
 
        {
322
 
                pa_context_disconnect(pulse->context);
323
 
                pa_context_unref(pulse->context);
324
 
                pulse->context = NULL;
325
 
        }
326
 
        if (pulse->mainloop)
327
 
        {
328
 
                pa_threaded_mainloop_free(pulse->mainloop);
329
 
                pulse->mainloop = NULL;
330
 
        }
331
 
        xfree(pulse->device_name);
332
 
        xfree(pulse);
333
 
}
334
 
 
335
 
static boolean rdpsnd_pulse_format_supported(rdpsndDevicePlugin* device, rdpsndFormat* format)
336
 
{
337
 
        rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
338
 
 
339
 
        if (!pulse->context)
340
 
                return false;
341
 
 
342
 
        switch (format->wFormatTag)
343
 
        {
344
 
                case 1: /* PCM */
345
 
                        if (format->cbSize == 0 &&
346
 
                                (format->nSamplesPerSec <= PA_RATE_MAX) &&
347
 
                                (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
348
 
                                (format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
349
 
                        {
350
 
                                return true;
351
 
                        }
352
 
                        break;
353
 
 
354
 
                case 6: /* A-LAW */
355
 
                case 7: /* U-LAW */
356
 
                        if (format->cbSize == 0 &&
357
 
                                (format->nSamplesPerSec <= PA_RATE_MAX) &&
358
 
                                (format->wBitsPerSample == 8) &&
359
 
                                (format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
360
 
                        {
361
 
                                return true;
362
 
                        }
363
 
                        break;
364
 
 
365
 
                case 0x11: /* IMA ADPCM */
366
 
                        if ((format->nSamplesPerSec <= PA_RATE_MAX) &&
367
 
                                (format->wBitsPerSample == 4) &&
368
 
                                (format->nChannels == 1 || format->nChannels == 2))
369
 
                        {
370
 
                                return true;
371
 
                        }
372
 
                        break;
373
 
        }
374
 
        return false;
375
 
}
376
 
 
377
 
static void rdpsnd_pulse_set_format(rdpsndDevicePlugin* device, rdpsndFormat* format, int latency)
378
 
{
379
 
        rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
380
 
 
381
 
        if (pulse->stream)
382
 
        {
383
 
                pa_threaded_mainloop_lock(pulse->mainloop);
384
 
                pa_stream_disconnect(pulse->stream);
385
 
                pa_stream_unref(pulse->stream);
386
 
                pulse->stream = NULL;
387
 
                pa_threaded_mainloop_unlock(pulse->mainloop);
388
 
        }
389
 
        rdpsnd_pulse_open(device, format, latency);
390
 
}
391
 
 
392
 
static void rdpsnd_pulse_set_volume(rdpsndDevicePlugin* device, uint32 value)
393
 
{
394
 
}
395
 
 
396
 
static void rdpsnd_pulse_play(rdpsndDevicePlugin* device, uint8* data, int size)
397
 
{
398
 
        rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
399
 
        int len;
400
 
        int ret;
401
 
        uint8* decoded_data;
402
 
        uint8* src;
403
 
        int decoded_size;
404
 
 
405
 
        if (!pulse->stream)
406
 
                return;
407
 
 
408
 
        if (pulse->format == 0x11)
409
 
        {
410
 
                decoded_data = dsp_decode_ima_adpcm(&pulse->adpcm,
411
 
                        data, size, pulse->sample_spec.channels, pulse->block_size, &decoded_size);
412
 
                size = decoded_size;
413
 
                src = decoded_data;
414
 
        }
415
 
        else
416
 
        {
417
 
                decoded_data = NULL;
418
 
                src = data;
419
 
        }
420
 
 
421
 
        pa_threaded_mainloop_lock(pulse->mainloop);
422
 
        while (size > 0)
423
 
        {
424
 
                while ((len = pa_stream_writable_size(pulse->stream)) == 0)
425
 
                {
426
 
                        pa_threaded_mainloop_wait(pulse->mainloop);
427
 
                }
428
 
                if (len < 0)
429
 
                        break;
430
 
                if (len > size)
431
 
                        len = size;
432
 
                ret = pa_stream_write(pulse->stream, src, len, NULL, 0LL, PA_SEEK_RELATIVE);
433
 
                if (ret < 0)
434
 
                {
435
 
                        DEBUG_WARN("pa_stream_write failed (%d)",
436
 
                                pa_context_errno(pulse->context));
437
 
                        break;
438
 
                }
439
 
                src += len;
440
 
                size -= len;
441
 
        }
442
 
        pa_threaded_mainloop_unlock(pulse->mainloop);
443
 
 
444
 
        if (decoded_data)
445
 
                xfree(decoded_data);
446
 
}
447
 
 
448
 
static void rdpsnd_pulse_start(rdpsndDevicePlugin* device)
449
 
{
450
 
        rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
451
 
 
452
 
        if (!pulse->stream)
453
 
                return;
454
 
 
455
 
        pa_stream_trigger(pulse->stream, NULL, NULL);
456
 
}
457
 
 
458
 
int FreeRDPRdpsndDeviceEntry(PFREERDP_RDPSND_DEVICE_ENTRY_POINTS pEntryPoints)
459
 
{
460
 
        rdpsndPulsePlugin* pulse;
461
 
        RDP_PLUGIN_DATA* data;
462
 
 
463
 
        pulse = xnew(rdpsndPulsePlugin);
464
 
 
465
 
        pulse->device.Open = rdpsnd_pulse_open;
466
 
        pulse->device.FormatSupported = rdpsnd_pulse_format_supported;
467
 
        pulse->device.SetFormat = rdpsnd_pulse_set_format;
468
 
        pulse->device.SetVolume = rdpsnd_pulse_set_volume;
469
 
        pulse->device.Play = rdpsnd_pulse_play;
470
 
        pulse->device.Start = rdpsnd_pulse_start;
471
 
        pulse->device.Close = rdpsnd_pulse_close;
472
 
        pulse->device.Free = rdpsnd_pulse_free;
473
 
 
474
 
        data = pEntryPoints->plugin_data;
475
 
        if (data && strcmp((char*)data->data[0], "pulse") == 0)
476
 
        {
477
 
                if(strlen((char*)data->data[1]) > 0) 
478
 
                        pulse->device_name = xstrdup((char*)data->data[1]);
479
 
                else
480
 
                        pulse->device_name = NULL;
481
 
        }
482
 
 
483
 
        pulse->mainloop = pa_threaded_mainloop_new();
484
 
        if (!pulse->mainloop)
485
 
        {
486
 
                DEBUG_WARN("pa_threaded_mainloop_new failed");
487
 
                rdpsnd_pulse_free((rdpsndDevicePlugin*)pulse);
488
 
                return 1;
489
 
        }
490
 
        pulse->context = pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop), "freerdp");
491
 
        if (!pulse->context)
492
 
        {
493
 
                DEBUG_WARN("pa_context_new failed");
494
 
                rdpsnd_pulse_free((rdpsndDevicePlugin*)pulse);
495
 
                return 1;
496
 
        }
497
 
        pa_context_set_state_callback(pulse->context, rdpsnd_pulse_context_state_callback, pulse);
498
 
        if (!rdpsnd_pulse_connect((rdpsndDevicePlugin*)pulse))
499
 
        {
500
 
                DEBUG_WARN("rdpsnd_pulse_connect failed");
501
 
                rdpsnd_pulse_free((rdpsndDevicePlugin*)pulse);
502
 
                return 1;
503
 
        }
504
 
 
505
 
        pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*)pulse);
506
 
 
507
 
        return 0;
508
 
}
509