FreeRDP/channels/tsmf/client/pulse/tsmf_pulse.c

378 lines
10 KiB
C
Raw Normal View History

/**
2012-10-09 07:02:04 +04:00
* FreeRDP: A Remote Desktop Protocol Implementation
* Video Redirection Virtual Channel - PulseAudio Device
*
* Copyright 2010-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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <winpr/crt.h>
#include <pulse/pulseaudio.h>
#include "tsmf_audio.h"
typedef struct _TSMFPulseAudioDevice
{
ITSMFAudioDevice iface;
char device[32];
2014-05-23 15:12:34 +04:00
pa_threaded_mainloop *mainloop;
pa_context *context;
pa_sample_spec sample_spec;
2014-05-23 15:12:34 +04:00
pa_stream *stream;
} TSMFPulseAudioDevice;
2014-05-23 15:12:34 +04:00
static void tsmf_pulse_context_state_callback(pa_context *context, void *userdata)
{
2014-05-23 15:12:34 +04:00
TSMFPulseAudioDevice *pulse = (TSMFPulseAudioDevice *) userdata;
pa_context_state_t state;
state = pa_context_get_state(context);
2014-05-23 15:12:34 +04:00
switch(state)
{
case PA_CONTEXT_READY:
2014-05-23 15:12:34 +04:00
DEBUG_TSMF("PA_CONTEXT_READY");
pa_threaded_mainloop_signal(pulse->mainloop, 0);
break;
case PA_CONTEXT_FAILED:
case PA_CONTEXT_TERMINATED:
2014-05-23 15:12:34 +04:00
DEBUG_TSMF("state %d", (int)state);
pa_threaded_mainloop_signal(pulse->mainloop, 0);
break;
default:
2014-05-23 15:12:34 +04:00
DEBUG_TSMF("state %d", (int)state);
break;
}
}
2014-05-23 15:12:34 +04:00
static BOOL tsmf_pulse_connect(TSMFPulseAudioDevice *pulse)
{
pa_context_state_t state;
2014-05-23 15:12:34 +04:00
if(!pulse->context)
return FALSE;
2014-05-23 15:12:34 +04:00
if(pa_context_connect(pulse->context, NULL, 0, NULL))
{
WLog_ERR(TAG, "pa_context_connect failed (%d)",
2014-05-23 15:12:34 +04:00
pa_context_errno(pulse->context));
return FALSE;
}
pa_threaded_mainloop_lock(pulse->mainloop);
2014-05-23 15:12:34 +04:00
if(pa_threaded_mainloop_start(pulse->mainloop) < 0)
{
pa_threaded_mainloop_unlock(pulse->mainloop);
WLog_ERR(TAG, "pa_threaded_mainloop_start failed (%d)",
2014-05-23 15:12:34 +04:00
pa_context_errno(pulse->context));
return FALSE;
}
2014-05-23 15:12:34 +04:00
for(;;)
{
state = pa_context_get_state(pulse->context);
2014-05-23 15:12:34 +04:00
if(state == PA_CONTEXT_READY)
break;
2014-05-23 15:12:34 +04:00
if(!PA_CONTEXT_IS_GOOD(state))
{
2014-05-23 15:12:34 +04:00
DEBUG_TSMF("bad context state (%d)",
pa_context_errno(pulse->context));
break;
}
pa_threaded_mainloop_wait(pulse->mainloop);
}
pa_threaded_mainloop_unlock(pulse->mainloop);
2014-05-23 15:12:34 +04:00
if(state == PA_CONTEXT_READY)
{
2014-05-23 15:12:34 +04:00
DEBUG_TSMF("connected");
return TRUE;
}
else
{
pa_context_disconnect(pulse->context);
return FALSE;
}
}
2014-05-23 15:12:34 +04:00
static BOOL tsmf_pulse_open(ITSMFAudioDevice *audio, const char *device)
{
2014-05-23 15:12:34 +04:00
TSMFPulseAudioDevice *pulse = (TSMFPulseAudioDevice *) audio;
if(device)
{
strcpy(pulse->device, device);
}
pulse->mainloop = pa_threaded_mainloop_new();
2014-05-23 15:12:34 +04:00
if(!pulse->mainloop)
{
WLog_ERR(TAG, "pa_threaded_mainloop_new failed");
return FALSE;
}
pulse->context = pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop), "freerdp");
2014-05-23 15:12:34 +04:00
if(!pulse->context)
{
WLog_ERR(TAG, "pa_context_new failed");
return FALSE;
}
pa_context_set_state_callback(pulse->context, tsmf_pulse_context_state_callback, pulse);
2014-05-23 15:12:34 +04:00
if(tsmf_pulse_connect(pulse))
{
WLog_ERR(TAG, "tsmf_pulse_connect failed");
return FALSE;
}
2014-05-23 15:12:34 +04:00
DEBUG_TSMF("open device %s", pulse->device);
return TRUE;
}
2014-05-23 15:12:34 +04:00
static void tsmf_pulse_stream_success_callback(pa_stream *stream, int success, void *userdata)
{
2014-05-23 15:12:34 +04:00
TSMFPulseAudioDevice *pulse = (TSMFPulseAudioDevice *) userdata;
pa_threaded_mainloop_signal(pulse->mainloop, 0);
}
2014-05-23 15:12:34 +04:00
static void tsmf_pulse_wait_for_operation(TSMFPulseAudioDevice *pulse, pa_operation *operation)
{
2014-05-23 15:12:34 +04:00
if(operation == NULL)
return;
2014-05-23 15:12:34 +04:00
while(pa_operation_get_state(operation) == PA_OPERATION_RUNNING)
{
pa_threaded_mainloop_wait(pulse->mainloop);
}
pa_operation_unref(operation);
}
2014-05-23 15:12:34 +04:00
static void tsmf_pulse_stream_state_callback(pa_stream *stream, void *userdata)
{
2014-05-23 15:12:34 +04:00
TSMFPulseAudioDevice *pulse = (TSMFPulseAudioDevice *) userdata;
pa_stream_state_t state;
state = pa_stream_get_state(stream);
2014-05-23 15:12:34 +04:00
switch(state)
{
case PA_STREAM_READY:
2014-05-23 15:12:34 +04:00
DEBUG_TSMF("PA_STREAM_READY");
pa_threaded_mainloop_signal(pulse->mainloop, 0);
break;
case PA_STREAM_FAILED:
case PA_STREAM_TERMINATED:
2014-05-23 15:12:34 +04:00
DEBUG_TSMF("state %d", (int)state);
pa_threaded_mainloop_signal(pulse->mainloop, 0);
break;
default:
2014-05-23 15:12:34 +04:00
DEBUG_TSMF("state %d", (int)state);
break;
}
}
2014-05-23 15:12:34 +04:00
static void tsmf_pulse_stream_request_callback(pa_stream *stream, size_t length, void *userdata)
{
2014-05-23 15:12:34 +04:00
TSMFPulseAudioDevice *pulse = (TSMFPulseAudioDevice *) userdata;
DEBUG_TSMF("%d", (int) length);
pa_threaded_mainloop_signal(pulse->mainloop, 0);
}
2014-05-23 15:12:34 +04:00
static BOOL tsmf_pulse_close_stream(TSMFPulseAudioDevice *pulse)
{
2014-05-23 15:12:34 +04:00
if(!pulse->context || !pulse->stream)
return FALSE;
2014-05-23 15:12:34 +04:00
DEBUG_TSMF("");
pa_threaded_mainloop_lock(pulse->mainloop);
pa_stream_set_write_callback(pulse->stream, NULL, NULL);
tsmf_pulse_wait_for_operation(pulse,
2014-05-23 15:12:34 +04:00
pa_stream_drain(pulse->stream, tsmf_pulse_stream_success_callback, pulse));
pa_stream_disconnect(pulse->stream);
pa_stream_unref(pulse->stream);
pulse->stream = NULL;
pa_threaded_mainloop_unlock(pulse->mainloop);
return TRUE;
}
2014-05-23 15:12:34 +04:00
static BOOL tsmf_pulse_open_stream(TSMFPulseAudioDevice *pulse)
{
pa_stream_state_t state;
pa_buffer_attr buffer_attr = { 0 };
2014-05-23 15:12:34 +04:00
if(!pulse->context)
return FALSE;
2014-05-23 15:12:34 +04:00
DEBUG_TSMF("");
pa_threaded_mainloop_lock(pulse->mainloop);
pulse->stream = pa_stream_new(pulse->context, "freerdp",
2014-05-23 15:12:34 +04:00
&pulse->sample_spec, NULL);
if(!pulse->stream)
{
pa_threaded_mainloop_unlock(pulse->mainloop);
WLog_ERR(TAG, "pa_stream_new failed (%d)",
2014-05-23 15:12:34 +04:00
pa_context_errno(pulse->context));
return FALSE;
}
pa_stream_set_state_callback(pulse->stream,
2014-05-23 15:12:34 +04:00
tsmf_pulse_stream_state_callback, pulse);
pa_stream_set_write_callback(pulse->stream,
2014-05-23 15:12:34 +04:00
tsmf_pulse_stream_request_callback, pulse);
buffer_attr.maxlength = pa_usec_to_bytes(500000, &pulse->sample_spec);
buffer_attr.tlength = pa_usec_to_bytes(250000, &pulse->sample_spec);
buffer_attr.prebuf = (UINT32) -1;
buffer_attr.minreq = (UINT32) -1;
buffer_attr.fragsize = (UINT32) -1;
2014-05-23 15:12:34 +04:00
if(pa_stream_connect_playback(pulse->stream,
pulse->device[0] ? pulse->device : NULL, &buffer_attr,
PA_STREAM_ADJUST_LATENCY | PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE,
NULL, NULL) < 0)
{
pa_threaded_mainloop_unlock(pulse->mainloop);
WLog_ERR(TAG, "pa_stream_connect_playback failed (%d)",
2014-05-23 15:12:34 +04:00
pa_context_errno(pulse->context));
return FALSE;
}
2014-05-23 15:12:34 +04:00
for(;;)
{
state = pa_stream_get_state(pulse->stream);
2014-05-23 15:12:34 +04:00
if(state == PA_STREAM_READY)
break;
2014-05-23 15:12:34 +04:00
if(!PA_STREAM_IS_GOOD(state))
{
WLog_ERR(TAG, "bad stream state (%d)",
2014-05-23 15:12:34 +04:00
pa_context_errno(pulse->context));
break;
}
pa_threaded_mainloop_wait(pulse->mainloop);
}
pa_threaded_mainloop_unlock(pulse->mainloop);
2014-05-23 15:12:34 +04:00
if(state == PA_STREAM_READY)
{
2014-05-23 15:12:34 +04:00
DEBUG_TSMF("connected");
return TRUE;
}
else
{
tsmf_pulse_close_stream(pulse);
return FALSE;
}
}
2014-05-23 15:12:34 +04:00
static BOOL tsmf_pulse_set_format(ITSMFAudioDevice *audio,
UINT32 sample_rate, UINT32 channels, UINT32 bits_per_sample)
{
2014-05-23 15:12:34 +04:00
TSMFPulseAudioDevice *pulse = (TSMFPulseAudioDevice *) audio;
DEBUG_TSMF("sample_rate %d channels %d bits_per_sample %d",
sample_rate, channels, bits_per_sample);
pulse->sample_spec.rate = sample_rate;
pulse->sample_spec.channels = channels;
pulse->sample_spec.format = PA_SAMPLE_S16LE;
return tsmf_pulse_open_stream(pulse);
}
2014-05-23 15:12:34 +04:00
static BOOL tsmf_pulse_play(ITSMFAudioDevice *audio, BYTE *data, UINT32 data_size)
{
2014-05-23 15:12:34 +04:00
TSMFPulseAudioDevice *pulse = (TSMFPulseAudioDevice *) audio;
BYTE *src;
int len;
int ret;
2014-05-23 15:12:34 +04:00
DEBUG_TSMF("data_size %d", data_size);
if(pulse->stream)
{
pa_threaded_mainloop_lock(pulse->mainloop);
src = data;
2014-05-23 15:12:34 +04:00
while(data_size > 0)
{
2014-05-23 15:12:34 +04:00
while((len = pa_stream_writable_size(pulse->stream)) == 0)
{
2014-05-23 15:12:34 +04:00
DEBUG_TSMF("waiting");
pa_threaded_mainloop_wait(pulse->mainloop);
}
2014-05-23 15:12:34 +04:00
if(len < 0)
break;
2014-05-23 15:12:34 +04:00
if(len > data_size)
len = data_size;
ret = pa_stream_write(pulse->stream, src, len, NULL, 0LL, PA_SEEK_RELATIVE);
2014-05-23 15:12:34 +04:00
if(ret < 0)
{
2014-05-23 15:12:34 +04:00
DEBUG_TSMF("pa_stream_write failed (%d)",
pa_context_errno(pulse->context));
break;
}
src += len;
data_size -= len;
}
pa_threaded_mainloop_unlock(pulse->mainloop);
}
free(data);
return TRUE;
}
2014-05-23 15:12:34 +04:00
static UINT64 tsmf_pulse_get_latency(ITSMFAudioDevice *audio)
{
pa_usec_t usec;
2012-10-09 11:26:39 +04:00
UINT64 latency = 0;
2014-05-23 15:12:34 +04:00
TSMFPulseAudioDevice *pulse = (TSMFPulseAudioDevice *) audio;
if(pulse->stream && pa_stream_get_latency(pulse->stream, &usec, NULL) == 0)
{
2012-10-09 11:26:39 +04:00
latency = ((UINT64)usec) * 10LL;
}
return latency;
}
2014-05-23 15:12:34 +04:00
static void tsmf_pulse_flush(ITSMFAudioDevice *audio)
{
2014-05-23 15:12:34 +04:00
TSMFPulseAudioDevice *pulse = (TSMFPulseAudioDevice *) audio;
pa_threaded_mainloop_lock(pulse->mainloop);
tsmf_pulse_wait_for_operation(pulse,
2014-05-23 15:12:34 +04:00
pa_stream_flush(pulse->stream, tsmf_pulse_stream_success_callback, pulse));
pa_threaded_mainloop_unlock(pulse->mainloop);
}
2014-05-23 15:12:34 +04:00
static void tsmf_pulse_free(ITSMFAudioDevice *audio)
{
2014-05-23 15:12:34 +04:00
TSMFPulseAudioDevice *pulse = (TSMFPulseAudioDevice *) audio;
DEBUG_TSMF("");
tsmf_pulse_close_stream(pulse);
2014-05-23 15:12:34 +04:00
if(pulse->mainloop)
{
pa_threaded_mainloop_stop(pulse->mainloop);
}
2014-05-23 15:12:34 +04:00
if(pulse->context)
{
pa_context_disconnect(pulse->context);
pa_context_unref(pulse->context);
pulse->context = NULL;
}
2014-05-23 15:12:34 +04:00
if(pulse->mainloop)
{
pa_threaded_mainloop_free(pulse->mainloop);
pulse->mainloop = NULL;
}
free(pulse);
}
#ifdef STATIC_CHANNELS
#define freerdp_tsmf_client_audio_subsystem_entry pulse_freerdp_tsmf_client_audio_subsystem_entry
#endif
2014-05-23 15:12:34 +04:00
ITSMFAudioDevice *freerdp_tsmf_client_audio_subsystem_entry(void)
{
2014-05-23 15:12:34 +04:00
TSMFPulseAudioDevice *pulse;
pulse = (TSMFPulseAudioDevice *) malloc(sizeof(TSMFPulseAudioDevice));
ZeroMemory(pulse, sizeof(TSMFPulseAudioDevice));
pulse->iface.Open = tsmf_pulse_open;
pulse->iface.SetFormat = tsmf_pulse_set_format;
pulse->iface.Play = tsmf_pulse_play;
pulse->iface.GetLatency = tsmf_pulse_get_latency;
pulse->iface.Flush = tsmf_pulse_flush;
pulse->iface.Free = tsmf_pulse_free;
2014-05-23 15:12:34 +04:00
return (ITSMFAudioDevice *) pulse;
}