2011-08-15 14:21:58 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-08-15 14:21:58 +04:00
|
|
|
* Audio Output Virtual Channel
|
|
|
|
*
|
|
|
|
* Copyright 2011 Vic Lee
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2012-10-09 07:42:01 +04:00
|
|
|
#include <winpr/crt.h>
|
2013-11-22 21:11:39 +04:00
|
|
|
#include <winpr/stream.h>
|
2012-11-19 02:32:18 +04:00
|
|
|
#include <winpr/cmdline.h>
|
2012-10-09 07:42:01 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
#include <pulse/pulseaudio.h>
|
2012-10-09 07:42:01 +04:00
|
|
|
|
2013-11-22 21:11:39 +04:00
|
|
|
#ifdef WITH_GSM
|
|
|
|
#include <gsm/gsm.h>
|
|
|
|
#endif
|
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
#include <freerdp/types.h>
|
2013-02-20 02:47:55 +04:00
|
|
|
#include <freerdp/codec/dsp.h>
|
2011-08-15 14:21:58 +04:00
|
|
|
|
|
|
|
#include "rdpsnd_main.h"
|
|
|
|
|
|
|
|
typedef struct rdpsnd_pulse_plugin rdpsndPulsePlugin;
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
struct rdpsnd_pulse_plugin
|
|
|
|
{
|
|
|
|
rdpsndDevicePlugin device;
|
|
|
|
|
|
|
|
char* device_name;
|
2013-02-21 12:38:36 +04:00
|
|
|
pa_threaded_mainloop* mainloop;
|
|
|
|
pa_context* context;
|
2011-08-15 14:21:58 +04:00
|
|
|
pa_sample_spec sample_spec;
|
2013-02-21 12:38:36 +04:00
|
|
|
pa_stream* stream;
|
2011-08-15 14:21:58 +04:00
|
|
|
int format;
|
|
|
|
int block_size;
|
2011-09-25 07:41:37 +04:00
|
|
|
int latency;
|
2012-05-09 12:15:23 +04:00
|
|
|
|
|
|
|
FREERDP_DSP_CONTEXT* dsp_context;
|
2013-11-22 21:11:39 +04:00
|
|
|
|
|
|
|
#ifdef WITH_GSM
|
|
|
|
gsm gsm_context;
|
|
|
|
wStream* gsmBuffer;
|
|
|
|
#endif
|
2011-08-15 14:21:58 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static void rdpsnd_pulse_context_state_callback(pa_context* context, void* userdata)
|
|
|
|
{
|
|
|
|
pa_context_state_t state;
|
2012-10-03 07:01:16 +04:00
|
|
|
rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*) userdata;
|
2011-08-15 14:21:58 +04:00
|
|
|
|
|
|
|
state = pa_context_get_state(context);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case PA_CONTEXT_READY:
|
|
|
|
pa_threaded_mainloop_signal(pulse->mainloop, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PA_CONTEXT_FAILED:
|
|
|
|
case PA_CONTEXT_TERMINATED:
|
|
|
|
pa_threaded_mainloop_signal(pulse->mainloop, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
static BOOL rdpsnd_pulse_connect(rdpsndDevicePlugin* device)
|
2011-08-15 14:21:58 +04:00
|
|
|
{
|
|
|
|
pa_context_state_t state;
|
2012-10-03 07:01:16 +04:00
|
|
|
rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*) device;
|
2011-08-15 14:21:58 +04:00
|
|
|
|
|
|
|
if (!pulse->context)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-15 14:21:58 +04:00
|
|
|
|
|
|
|
if (pa_context_connect(pulse->context, NULL, 0, NULL))
|
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-15 14:21:58 +04:00
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
pa_threaded_mainloop_lock(pulse->mainloop);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
|
|
|
|
{
|
|
|
|
pa_threaded_mainloop_unlock(pulse->mainloop);
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-15 14:21:58 +04:00
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
state = pa_context_get_state(pulse->context);
|
2013-02-21 12:38:36 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
if (state == PA_CONTEXT_READY)
|
|
|
|
break;
|
2013-02-21 12:38:36 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
if (!PA_CONTEXT_IS_GOOD(state))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2013-02-21 12:38:36 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
pa_threaded_mainloop_wait(pulse->mainloop);
|
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
pa_threaded_mainloop_unlock(pulse->mainloop);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
if (state == PA_CONTEXT_READY)
|
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-15 14:21:58 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pa_context_disconnect(pulse->context);
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-15 14:21:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rdpsnd_pulse_stream_success_callback(pa_stream* stream, int success, void* userdata)
|
|
|
|
{
|
2012-10-03 07:01:16 +04:00
|
|
|
rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*) userdata;
|
2011-08-15 14:21:58 +04:00
|
|
|
|
|
|
|
pa_threaded_mainloop_signal(pulse->mainloop, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rdpsnd_pulse_wait_for_operation(rdpsndPulsePlugin* pulse, pa_operation* operation)
|
|
|
|
{
|
2013-02-21 12:38:36 +04:00
|
|
|
if (!operation)
|
2011-08-30 06:59:30 +04:00
|
|
|
return;
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING)
|
|
|
|
{
|
|
|
|
pa_threaded_mainloop_wait(pulse->mainloop);
|
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
pa_operation_unref(operation);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rdpsnd_pulse_stream_state_callback(pa_stream* stream, void* userdata)
|
|
|
|
{
|
|
|
|
pa_stream_state_t state;
|
2012-10-03 07:01:16 +04:00
|
|
|
rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*) userdata;
|
2011-08-15 14:21:58 +04:00
|
|
|
|
|
|
|
state = pa_stream_get_state(stream);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case PA_STREAM_READY:
|
|
|
|
pa_threaded_mainloop_signal(pulse->mainloop, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PA_STREAM_FAILED:
|
|
|
|
case PA_STREAM_TERMINATED:
|
|
|
|
pa_threaded_mainloop_signal(pulse->mainloop, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rdpsnd_pulse_stream_request_callback(pa_stream* stream, size_t length, void* userdata)
|
|
|
|
{
|
2012-10-03 07:01:16 +04:00
|
|
|
rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*) userdata;
|
2011-08-15 14:21:58 +04:00
|
|
|
|
|
|
|
pa_threaded_mainloop_signal(pulse->mainloop, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rdpsnd_pulse_close(rdpsndDevicePlugin* device)
|
|
|
|
{
|
2012-10-03 07:01:16 +04:00
|
|
|
rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*) device;
|
2011-08-15 14:21:58 +04:00
|
|
|
|
|
|
|
if (!pulse->context || !pulse->stream)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(pulse->mainloop);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
|
|
|
rdpsnd_pulse_wait_for_operation(pulse, pa_stream_drain(pulse->stream, rdpsnd_pulse_stream_success_callback, pulse));
|
2011-08-15 14:21:58 +04:00
|
|
|
pa_stream_disconnect(pulse->stream);
|
|
|
|
pa_stream_unref(pulse->stream);
|
|
|
|
pulse->stream = NULL;
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
pa_threaded_mainloop_unlock(pulse->mainloop);
|
|
|
|
}
|
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
static void rdpsnd_pulse_set_format_spec(rdpsndPulsePlugin* pulse, AUDIO_FORMAT* format)
|
2011-08-15 14:21:58 +04:00
|
|
|
{
|
|
|
|
pa_sample_spec sample_spec = { 0 };
|
|
|
|
|
|
|
|
if (!pulse->context)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sample_spec.rate = format->nSamplesPerSec;
|
|
|
|
sample_spec.channels = format->nChannels;
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
switch (format->wFormatTag)
|
|
|
|
{
|
2013-02-19 20:02:45 +04:00
|
|
|
case WAVE_FORMAT_PCM:
|
2011-08-15 14:21:58 +04:00
|
|
|
switch (format->wBitsPerSample)
|
|
|
|
{
|
|
|
|
case 8:
|
|
|
|
sample_spec.format = PA_SAMPLE_U8;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
sample_spec.format = PA_SAMPLE_S16LE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-02-19 20:02:45 +04:00
|
|
|
case WAVE_FORMAT_ADPCM:
|
2013-02-21 12:38:36 +04:00
|
|
|
case WAVE_FORMAT_DVI_ADPCM:
|
2013-02-19 20:02:45 +04:00
|
|
|
sample_spec.format = PA_SAMPLE_S16LE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WAVE_FORMAT_ALAW:
|
2011-08-15 14:21:58 +04:00
|
|
|
sample_spec.format = PA_SAMPLE_ALAW;
|
|
|
|
break;
|
|
|
|
|
2013-02-19 20:02:45 +04:00
|
|
|
case WAVE_FORMAT_MULAW:
|
2011-08-15 14:21:58 +04:00
|
|
|
sample_spec.format = PA_SAMPLE_ULAW;
|
|
|
|
break;
|
|
|
|
|
2013-02-19 20:02:45 +04:00
|
|
|
case WAVE_FORMAT_GSM610:
|
2013-11-22 21:11:39 +04:00
|
|
|
sample_spec.format = PA_SAMPLE_S16LE;
|
2011-08-15 14:21:58 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pulse->sample_spec = sample_spec;
|
|
|
|
pulse->format = format->wFormatTag;
|
|
|
|
pulse->block_size = format->nBlockAlign;
|
|
|
|
}
|
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
static void rdpsnd_pulse_open(rdpsndDevicePlugin* device, AUDIO_FORMAT* format, int latency)
|
2011-08-15 14:21:58 +04:00
|
|
|
{
|
|
|
|
pa_stream_state_t state;
|
2011-09-25 10:00:09 +04:00
|
|
|
pa_stream_flags_t flags;
|
|
|
|
pa_buffer_attr buffer_attr = { 0 };
|
2011-09-01 18:36:26 +04:00
|
|
|
char ss[PA_SAMPLE_SPEC_SNPRINT_MAX];
|
2012-10-03 07:01:16 +04:00
|
|
|
rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*) device;
|
2011-08-15 14:21:58 +04:00
|
|
|
|
2011-09-25 07:41:37 +04:00
|
|
|
if (!pulse->context || pulse->stream)
|
|
|
|
{
|
2011-08-15 14:21:58 +04:00
|
|
|
return;
|
2011-09-01 18:36:26 +04:00
|
|
|
}
|
2011-08-15 14:21:58 +04:00
|
|
|
|
|
|
|
rdpsnd_pulse_set_format_spec(pulse, format);
|
2011-09-25 07:41:37 +04:00
|
|
|
pulse->latency = latency;
|
2011-08-15 14:21:58 +04:00
|
|
|
|
2011-09-25 07:41:37 +04:00
|
|
|
if (pa_sample_spec_valid(&pulse->sample_spec) == 0)
|
|
|
|
{
|
|
|
|
pa_sample_spec_snprint(ss, sizeof(ss), &pulse->sample_spec);
|
|
|
|
return;
|
2011-09-01 18:36:26 +04:00
|
|
|
}
|
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
pa_threaded_mainloop_lock(pulse->mainloop);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
|
|
|
pulse->stream = pa_stream_new(pulse->context, "freerdp", &pulse->sample_spec, NULL);
|
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
if (!pulse->stream)
|
|
|
|
{
|
|
|
|
pa_threaded_mainloop_unlock(pulse->mainloop);
|
|
|
|
return;
|
|
|
|
}
|
2011-09-01 18:36:26 +04:00
|
|
|
|
2013-02-21 12:38:36 +04:00
|
|
|
/* register essential callbacks */
|
2012-10-03 07:01:16 +04:00
|
|
|
pa_stream_set_state_callback(pulse->stream, rdpsnd_pulse_stream_state_callback, pulse);
|
|
|
|
pa_stream_set_write_callback(pulse->stream, rdpsnd_pulse_stream_request_callback, pulse);
|
2011-09-01 18:36:26 +04:00
|
|
|
|
2011-09-25 10:00:09 +04:00
|
|
|
flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-09-25 10:00:09 +04:00
|
|
|
if (pulse->latency > 0)
|
|
|
|
{
|
|
|
|
buffer_attr.maxlength = pa_usec_to_bytes(pulse->latency * 2 * 1000, &pulse->sample_spec);
|
|
|
|
buffer_attr.tlength = pa_usec_to_bytes(pulse->latency * 1000, &pulse->sample_spec);
|
2012-10-09 22:00:28 +04:00
|
|
|
buffer_attr.prebuf = (UINT32) -1;
|
|
|
|
buffer_attr.minreq = (UINT32) -1;
|
|
|
|
buffer_attr.fragsize = (UINT32) -1;
|
2011-09-25 10:00:09 +04:00
|
|
|
flags |= PA_STREAM_ADJUST_LATENCY;
|
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
if (pa_stream_connect_playback(pulse->stream,
|
2011-09-25 10:00:09 +04:00
|
|
|
pulse->device_name, pulse->latency > 0 ? &buffer_attr : NULL, flags, NULL, NULL) < 0)
|
2011-08-15 14:21:58 +04:00
|
|
|
{
|
|
|
|
pa_threaded_mainloop_unlock(pulse->mainloop);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
state = pa_stream_get_state(pulse->stream);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
if (state == PA_STREAM_READY)
|
|
|
|
break;
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
if (!PA_STREAM_IS_GOOD(state))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
pa_threaded_mainloop_wait(pulse->mainloop);
|
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
pa_threaded_mainloop_unlock(pulse->mainloop);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
if (state == PA_STREAM_READY)
|
|
|
|
{
|
2012-05-09 12:15:23 +04:00
|
|
|
freerdp_dsp_context_reset_adpcm(pulse->dsp_context);
|
2013-11-22 21:11:39 +04:00
|
|
|
|
|
|
|
#ifdef WITH_GSM
|
|
|
|
if (pulse->gsm_context)
|
|
|
|
gsm_destroy(pulse->gsm_context);
|
|
|
|
|
|
|
|
pulse->gsm_context = gsm_create();
|
|
|
|
#endif
|
2011-08-15 14:21:58 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rdpsnd_pulse_close(device);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rdpsnd_pulse_free(rdpsndDevicePlugin* device)
|
|
|
|
{
|
2012-10-03 07:01:16 +04:00
|
|
|
rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*) device;
|
2011-08-15 14:21:58 +04:00
|
|
|
|
|
|
|
if (!pulse)
|
|
|
|
return;
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
rdpsnd_pulse_close(device);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
if (pulse->mainloop)
|
|
|
|
{
|
|
|
|
pa_threaded_mainloop_stop(pulse->mainloop);
|
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
if (pulse->context)
|
|
|
|
{
|
|
|
|
pa_context_disconnect(pulse->context);
|
|
|
|
pa_context_unref(pulse->context);
|
|
|
|
pulse->context = NULL;
|
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
if (pulse->mainloop)
|
|
|
|
{
|
|
|
|
pa_threaded_mainloop_free(pulse->mainloop);
|
|
|
|
pulse->mainloop = NULL;
|
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(pulse->device_name);
|
2012-05-09 12:15:23 +04:00
|
|
|
freerdp_dsp_context_free(pulse->dsp_context);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(pulse);
|
2011-08-15 14:21:58 +04:00
|
|
|
}
|
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
static BOOL rdpsnd_pulse_format_supported(rdpsndDevicePlugin* device, AUDIO_FORMAT* format)
|
2011-08-15 14:21:58 +04:00
|
|
|
{
|
|
|
|
rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
|
|
|
|
|
|
|
|
if (!pulse->context)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-15 14:21:58 +04:00
|
|
|
|
|
|
|
switch (format->wFormatTag)
|
|
|
|
{
|
2013-02-19 20:02:45 +04:00
|
|
|
case WAVE_FORMAT_PCM:
|
2011-08-15 14:21:58 +04:00
|
|
|
if (format->cbSize == 0 &&
|
|
|
|
(format->nSamplesPerSec <= PA_RATE_MAX) &&
|
|
|
|
(format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
|
|
|
|
(format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
|
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-15 14:21:58 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-02-19 20:02:45 +04:00
|
|
|
case WAVE_FORMAT_ALAW:
|
|
|
|
case WAVE_FORMAT_MULAW:
|
2011-08-15 14:21:58 +04:00
|
|
|
if (format->cbSize == 0 &&
|
|
|
|
(format->nSamplesPerSec <= PA_RATE_MAX) &&
|
|
|
|
(format->wBitsPerSample == 8) &&
|
|
|
|
(format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
|
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-15 14:21:58 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-02-19 20:02:45 +04:00
|
|
|
case WAVE_FORMAT_ADPCM:
|
|
|
|
case WAVE_FORMAT_DVI_ADPCM:
|
2011-08-15 14:21:58 +04:00
|
|
|
if ((format->nSamplesPerSec <= PA_RATE_MAX) &&
|
|
|
|
(format->wBitsPerSample == 4) &&
|
|
|
|
(format->nChannels == 1 || format->nChannels == 2))
|
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-15 14:21:58 +04:00
|
|
|
}
|
|
|
|
break;
|
2013-11-22 21:11:39 +04:00
|
|
|
|
|
|
|
#ifdef WITH_GSM
|
|
|
|
case WAVE_FORMAT_GSM610:
|
|
|
|
if ((format->nSamplesPerSec <= PA_RATE_MAX) &&
|
|
|
|
(format->nBlockAlign == 65) && (format->nChannels == 1))
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2011-08-15 14:21:58 +04:00
|
|
|
}
|
2013-11-22 21:11:39 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-15 14:21:58 +04:00
|
|
|
}
|
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
static void rdpsnd_pulse_set_format(rdpsndDevicePlugin* device, AUDIO_FORMAT* format, int latency)
|
2011-08-15 14:21:58 +04:00
|
|
|
{
|
2012-10-03 07:01:16 +04:00
|
|
|
rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*) device;
|
2011-08-15 14:21:58 +04:00
|
|
|
|
|
|
|
if (pulse->stream)
|
|
|
|
{
|
|
|
|
pa_threaded_mainloop_lock(pulse->mainloop);
|
|
|
|
pa_stream_disconnect(pulse->stream);
|
|
|
|
pa_stream_unref(pulse->stream);
|
|
|
|
pulse->stream = NULL;
|
|
|
|
pa_threaded_mainloop_unlock(pulse->mainloop);
|
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-09-25 07:41:37 +04:00
|
|
|
rdpsnd_pulse_open(device, format, latency);
|
2011-08-15 14:21:58 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
static void rdpsnd_pulse_set_volume(rdpsndDevicePlugin* device, UINT32 value)
|
2011-08-15 14:21:58 +04:00
|
|
|
{
|
2012-05-29 20:22:58 +04:00
|
|
|
pa_cvolume cv;
|
|
|
|
pa_volume_t left;
|
|
|
|
pa_volume_t right;
|
|
|
|
pa_operation* operation;
|
2012-10-03 07:01:16 +04:00
|
|
|
rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*) device;
|
2012-05-29 20:22:58 +04:00
|
|
|
|
|
|
|
if (!pulse->context || !pulse->stream)
|
|
|
|
return;
|
|
|
|
|
|
|
|
left = (pa_volume_t) (value & 0xFFFF);
|
|
|
|
right = (pa_volume_t) ((value >> 16) & 0xFFFF);
|
|
|
|
|
|
|
|
pa_cvolume_init(&cv);
|
|
|
|
cv.channels = 2;
|
|
|
|
cv.values[0] = PA_VOLUME_MUTED + (left * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / 0xFFFF;
|
|
|
|
cv.values[1] = PA_VOLUME_MUTED + (right * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / 0xFFFF;
|
2012-05-30 16:34:36 +04:00
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(pulse->mainloop);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2012-05-29 20:22:58 +04:00
|
|
|
operation = pa_context_set_sink_input_volume(pulse->context, pa_stream_get_index(pulse->stream), &cv, NULL, NULL);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
|
|
|
if (operation)
|
2012-05-29 20:22:58 +04:00
|
|
|
pa_operation_unref(operation);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2012-05-30 16:34:36 +04:00
|
|
|
pa_threaded_mainloop_unlock(pulse->mainloop);
|
2011-08-15 14:21:58 +04:00
|
|
|
}
|
|
|
|
|
2013-11-22 21:11:39 +04:00
|
|
|
static BYTE* rdpsnd_pulse_convert_audio(rdpsndDevicePlugin* device, BYTE* data, int* size)
|
2011-08-15 14:21:58 +04:00
|
|
|
{
|
2013-11-22 21:11:39 +04:00
|
|
|
BYTE* pcmData = NULL;
|
2012-10-03 07:01:16 +04:00
|
|
|
rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*) device;
|
2011-08-15 14:21:58 +04:00
|
|
|
|
2013-02-21 12:38:36 +04:00
|
|
|
if (pulse->format == WAVE_FORMAT_ADPCM)
|
2012-05-24 17:26:17 +04:00
|
|
|
{
|
|
|
|
pulse->dsp_context->decode_ms_adpcm(pulse->dsp_context,
|
2013-11-22 21:11:39 +04:00
|
|
|
data, *size, pulse->sample_spec.channels, pulse->block_size);
|
2013-02-21 12:38:36 +04:00
|
|
|
|
2013-11-22 21:11:39 +04:00
|
|
|
*size = pulse->dsp_context->adpcm_size;
|
|
|
|
pcmData = pulse->dsp_context->adpcm_buffer;
|
2012-05-24 17:26:17 +04:00
|
|
|
}
|
2013-02-21 12:38:36 +04:00
|
|
|
else if (pulse->format == WAVE_FORMAT_DVI_ADPCM)
|
2011-08-15 14:21:58 +04:00
|
|
|
{
|
2012-05-09 12:15:23 +04:00
|
|
|
pulse->dsp_context->decode_ima_adpcm(pulse->dsp_context,
|
2013-11-22 21:11:39 +04:00
|
|
|
data, *size, pulse->sample_spec.channels, pulse->block_size);
|
|
|
|
|
|
|
|
*size = pulse->dsp_context->adpcm_size;
|
|
|
|
pcmData = pulse->dsp_context->adpcm_buffer;
|
|
|
|
}
|
|
|
|
#ifdef WITH_GSM
|
|
|
|
else if (pulse->format == WAVE_FORMAT_GSM610)
|
|
|
|
{
|
|
|
|
int inPos = 0;
|
|
|
|
int inSize = *size;
|
|
|
|
UINT16 gsmBlockBuffer[160];
|
2013-02-21 12:38:36 +04:00
|
|
|
|
2013-11-22 21:11:39 +04:00
|
|
|
Stream_SetPosition(pulse->gsmBuffer, 0);
|
|
|
|
|
|
|
|
while (inSize)
|
|
|
|
{
|
|
|
|
ZeroMemory(gsmBlockBuffer, sizeof(gsmBlockBuffer));
|
|
|
|
gsm_decode(pulse->gsm_context, (gsm_byte*) &data[inPos], (gsm_signal*) gsmBlockBuffer);
|
|
|
|
|
|
|
|
if ((inPos % 65) == 0)
|
|
|
|
{
|
|
|
|
inPos += 33;
|
|
|
|
inSize -= 33;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inPos += 32;
|
|
|
|
inSize -= 32;
|
|
|
|
}
|
|
|
|
|
2015-03-30 18:15:45 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(pulse->gsmBuffer, 160 * 2))
|
2015-03-26 19:09:47 +03:00
|
|
|
return NULL;
|
2013-11-22 21:11:39 +04:00
|
|
|
Stream_Write(pulse->gsmBuffer, (void*) gsmBlockBuffer, 160 * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream_SealLength(pulse->gsmBuffer);
|
|
|
|
|
|
|
|
pcmData = Stream_Buffer(pulse->gsmBuffer);
|
|
|
|
*size = Stream_Length(pulse->gsmBuffer);
|
2011-08-15 14:21:58 +04:00
|
|
|
}
|
2013-11-22 21:11:39 +04:00
|
|
|
#endif
|
2011-08-15 14:21:58 +04:00
|
|
|
else
|
|
|
|
{
|
2013-11-22 21:11:39 +04:00
|
|
|
pcmData = data;
|
2011-08-15 14:21:58 +04:00
|
|
|
}
|
|
|
|
|
2013-11-22 21:11:39 +04:00
|
|
|
return pcmData;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rdpsnd_pulse_play(rdpsndDevicePlugin* device, BYTE* data, int size)
|
|
|
|
{
|
|
|
|
int length;
|
|
|
|
int status;
|
|
|
|
BYTE* pcmData;
|
|
|
|
rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*) device;
|
|
|
|
|
|
|
|
if (!pulse->stream)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pcmData = rdpsnd_pulse_convert_audio(device, data, &size);
|
2015-03-30 19:29:32 +03:00
|
|
|
if (!pcmData)
|
2015-03-26 19:09:47 +03:00
|
|
|
return;
|
2013-11-22 21:11:39 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
pa_threaded_mainloop_lock(pulse->mainloop);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
while (size > 0)
|
|
|
|
{
|
2013-11-22 21:11:39 +04:00
|
|
|
while ((length = pa_stream_writable_size(pulse->stream)) == 0)
|
2011-08-15 14:21:58 +04:00
|
|
|
{
|
|
|
|
pa_threaded_mainloop_wait(pulse->mainloop);
|
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2013-11-22 21:11:39 +04:00
|
|
|
if (length < 0)
|
2011-08-15 14:21:58 +04:00
|
|
|
break;
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2013-11-22 21:11:39 +04:00
|
|
|
if (length > size)
|
|
|
|
length = size;
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2013-11-22 21:11:39 +04:00
|
|
|
status = pa_stream_write(pulse->stream, pcmData, length, NULL, 0LL, PA_SEEK_RELATIVE);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2013-11-22 21:11:39 +04:00
|
|
|
if (status < 0)
|
2011-08-15 14:21:58 +04:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2013-11-22 21:11:39 +04:00
|
|
|
pcmData += length;
|
|
|
|
size -= length;
|
2011-08-15 14:21:58 +04:00
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
pa_threaded_mainloop_unlock(pulse->mainloop);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rdpsnd_pulse_start(rdpsndDevicePlugin* device)
|
|
|
|
{
|
2012-10-03 07:01:16 +04:00
|
|
|
rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*) device;
|
2011-08-15 14:21:58 +04:00
|
|
|
|
|
|
|
if (!pulse->stream)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pa_stream_trigger(pulse->stream, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2012-11-19 02:32:18 +04:00
|
|
|
COMMAND_LINE_ARGUMENT_A rdpsnd_pulse_args[] =
|
|
|
|
{
|
|
|
|
{ "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", NULL, NULL, -1, NULL, "device" },
|
|
|
|
{ NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void rdpsnd_pulse_parse_addin_args(rdpsndDevicePlugin* device, ADDIN_ARGV* args)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
DWORD flags;
|
|
|
|
COMMAND_LINE_ARGUMENT_A* arg;
|
|
|
|
rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*) device;
|
|
|
|
|
2015-03-13 01:28:44 +03:00
|
|
|
flags = COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
|
2012-11-19 02:32:18 +04:00
|
|
|
|
|
|
|
status = CommandLineParseArgumentsA(args->argc, (const char**) args->argv,
|
|
|
|
rdpsnd_pulse_args, flags, pulse, NULL, NULL);
|
2013-09-20 12:11:05 +04:00
|
|
|
if (status < 0)
|
2013-09-20 12:08:56 +04:00
|
|
|
return;
|
2012-11-19 02:32:18 +04:00
|
|
|
|
|
|
|
arg = rdpsnd_pulse_args;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CommandLineSwitchStart(arg)
|
|
|
|
|
|
|
|
CommandLineSwitchCase(arg, "dev")
|
|
|
|
{
|
|
|
|
pulse->device_name = _strdup(arg->Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandLineSwitchEnd(arg)
|
|
|
|
}
|
|
|
|
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
|
|
|
|
}
|
|
|
|
|
2012-11-06 04:55:15 +04:00
|
|
|
#ifdef STATIC_CHANNELS
|
|
|
|
#define freerdp_rdpsnd_client_subsystem_entry pulse_freerdp_rdpsnd_client_subsystem_entry
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int freerdp_rdpsnd_client_subsystem_entry(PFREERDP_RDPSND_DEVICE_ENTRY_POINTS pEntryPoints)
|
2011-08-15 14:21:58 +04:00
|
|
|
{
|
2012-11-19 02:32:18 +04:00
|
|
|
ADDIN_ARGV* args;
|
2011-08-15 14:21:58 +04:00
|
|
|
rdpsndPulsePlugin* pulse;
|
|
|
|
|
2014-06-11 23:09:28 +04:00
|
|
|
pulse = (rdpsndPulsePlugin*) calloc(1, sizeof(rdpsndPulsePlugin));
|
|
|
|
|
|
|
|
if (!pulse)
|
|
|
|
return -1;
|
2011-08-15 14:21:58 +04:00
|
|
|
|
|
|
|
pulse->device.Open = rdpsnd_pulse_open;
|
|
|
|
pulse->device.FormatSupported = rdpsnd_pulse_format_supported;
|
|
|
|
pulse->device.SetFormat = rdpsnd_pulse_set_format;
|
|
|
|
pulse->device.SetVolume = rdpsnd_pulse_set_volume;
|
|
|
|
pulse->device.Play = rdpsnd_pulse_play;
|
|
|
|
pulse->device.Start = rdpsnd_pulse_start;
|
|
|
|
pulse->device.Close = rdpsnd_pulse_close;
|
|
|
|
pulse->device.Free = rdpsnd_pulse_free;
|
|
|
|
|
2012-11-19 02:32:18 +04:00
|
|
|
args = pEntryPoints->args;
|
|
|
|
rdpsnd_pulse_parse_addin_args((rdpsndDevicePlugin*) pulse, args);
|
2011-08-15 14:21:58 +04:00
|
|
|
|
2012-05-09 12:15:23 +04:00
|
|
|
pulse->dsp_context = freerdp_dsp_context_new();
|
|
|
|
|
2013-11-22 21:11:39 +04:00
|
|
|
#ifdef WITH_GSM
|
|
|
|
pulse->gsmBuffer = Stream_New(NULL, 4096);
|
|
|
|
#endif
|
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
pulse->mainloop = pa_threaded_mainloop_new();
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
if (!pulse->mainloop)
|
|
|
|
{
|
|
|
|
rdpsnd_pulse_free((rdpsndDevicePlugin*)pulse);
|
|
|
|
return 1;
|
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
pulse->context = pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop), "freerdp");
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
if (!pulse->context)
|
|
|
|
{
|
|
|
|
rdpsnd_pulse_free((rdpsndDevicePlugin*)pulse);
|
|
|
|
return 1;
|
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
pa_context_set_state_callback(pulse->context, rdpsnd_pulse_context_state_callback, pulse);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-08-15 14:21:58 +04:00
|
|
|
if (!rdpsnd_pulse_connect((rdpsndDevicePlugin*)pulse))
|
|
|
|
{
|
|
|
|
rdpsnd_pulse_free((rdpsndDevicePlugin*)pulse);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-03 07:01:16 +04:00
|
|
|
pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*) pulse);
|
2011-08-15 14:21:58 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|