2013-12-29 04:53:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Glyptodon LLC
|
2013-08-09 18:58:29 +00:00
|
|
|
*
|
2013-12-29 04:53:12 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2013-08-09 18:58:29 +00:00
|
|
|
*
|
2013-12-29 04:53:12 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2013-08-09 18:58:29 +00:00
|
|
|
*
|
2013-12-29 04:53:12 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "client.h"
|
|
|
|
#include "pulse.h"
|
2013-08-09 18:58:29 +00:00
|
|
|
|
2013-08-10 00:01:06 +00:00
|
|
|
#include <guacamole/audio.h>
|
2013-08-09 18:58:29 +00:00
|
|
|
#include <guacamole/client.h>
|
2013-08-09 22:11:31 +00:00
|
|
|
#include <pulse/pulseaudio.h>
|
2013-08-09 20:03:01 +00:00
|
|
|
|
2013-08-09 23:43:30 +00:00
|
|
|
static void __stream_read_callback(pa_stream* stream, size_t length,
|
|
|
|
void* data) {
|
|
|
|
|
|
|
|
guac_client* client = (guac_client*) data;
|
2013-08-10 00:01:06 +00:00
|
|
|
vnc_guac_client_data* client_data = (vnc_guac_client_data*) client->data;
|
|
|
|
guac_audio_stream* audio = client_data->audio;
|
|
|
|
|
2013-08-09 23:43:30 +00:00
|
|
|
const void* buffer;
|
|
|
|
|
|
|
|
/* Read data */
|
|
|
|
pa_stream_peek(stream, &buffer, &length);
|
|
|
|
|
2013-08-10 00:01:06 +00:00
|
|
|
/* Write data */
|
|
|
|
guac_audio_stream_write_pcm(audio, buffer, length);
|
|
|
|
|
|
|
|
/* Flush occasionally */
|
2013-08-10 01:21:32 +00:00
|
|
|
if (audio->pcm_bytes_written > GUAC_VNC_PCM_WRITE_RATE) {
|
2013-08-10 00:01:06 +00:00
|
|
|
guac_audio_stream_end(audio);
|
2013-08-10 01:21:32 +00:00
|
|
|
guac_audio_stream_begin(client_data->audio,
|
|
|
|
GUAC_VNC_AUDIO_RATE,
|
|
|
|
GUAC_VNC_AUDIO_CHANNELS,
|
|
|
|
GUAC_VNC_AUDIO_BPS);
|
2013-08-10 00:01:06 +00:00
|
|
|
}
|
2013-08-09 23:43:30 +00:00
|
|
|
|
|
|
|
/* Advance buffer */
|
|
|
|
pa_stream_drop(stream);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __stream_state_callback(pa_stream* stream, void* data) {
|
|
|
|
|
|
|
|
guac_client* client = (guac_client*) data;
|
|
|
|
|
|
|
|
switch (pa_stream_get_state(stream)) {
|
|
|
|
|
|
|
|
case PA_STREAM_UNCONNECTED:
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO,
|
2013-08-09 23:43:30 +00:00
|
|
|
"PulseAudio stream currently unconnected");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PA_STREAM_CREATING:
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "PulseAudio stream being created...");
|
2013-08-09 23:43:30 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PA_STREAM_READY:
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "PulseAudio stream now ready");
|
2013-08-09 23:43:30 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PA_STREAM_FAILED:
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "PulseAudio stream connection failed");
|
2013-08-09 23:43:30 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PA_STREAM_TERMINATED:
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "PulseAudio stream terminated");
|
2013-08-09 23:43:30 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO,
|
2013-08-09 23:43:30 +00:00
|
|
|
"Unknown PulseAudio stream state: 0x%x",
|
|
|
|
pa_stream_get_state(stream));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-09 23:04:58 +00:00
|
|
|
static void __context_get_sink_info_callback(pa_context* context,
|
|
|
|
const pa_sink_info* info, int is_last, void* data) {
|
|
|
|
|
|
|
|
guac_client* client = (guac_client*) data;
|
2013-08-09 23:43:30 +00:00
|
|
|
pa_stream* stream;
|
|
|
|
pa_sample_spec spec;
|
2013-08-10 01:21:32 +00:00
|
|
|
pa_buffer_attr attr;
|
2013-08-09 23:04:58 +00:00
|
|
|
|
2013-08-09 23:43:30 +00:00
|
|
|
/* Stop if end of list reached */
|
|
|
|
if (is_last)
|
2013-08-09 23:04:58 +00:00
|
|
|
return;
|
2013-08-09 23:43:30 +00:00
|
|
|
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "Starting streaming from \"%s\"",
|
2013-08-09 23:43:30 +00:00
|
|
|
info->description);
|
|
|
|
|
|
|
|
/* Set format */
|
|
|
|
spec.format = PA_SAMPLE_S16LE;
|
2013-08-10 01:21:32 +00:00
|
|
|
spec.rate = GUAC_VNC_AUDIO_RATE;
|
|
|
|
spec.channels = GUAC_VNC_AUDIO_CHANNELS;
|
|
|
|
|
2013-08-10 19:30:46 +00:00
|
|
|
attr.maxlength = -1;
|
|
|
|
attr.fragsize = GUAC_VNC_AUDIO_FRAGMENT_SIZE;
|
2013-08-09 23:43:30 +00:00
|
|
|
|
|
|
|
/* Create stream */
|
2013-08-09 23:46:23 +00:00
|
|
|
stream = pa_stream_new(context, "Guacamole Audio", &spec, NULL);
|
2013-08-09 23:43:30 +00:00
|
|
|
|
|
|
|
/* Set stream callbacks */
|
|
|
|
pa_stream_set_state_callback(stream, __stream_state_callback, client);
|
|
|
|
pa_stream_set_read_callback(stream, __stream_read_callback, client);
|
2013-08-09 23:04:58 +00:00
|
|
|
|
|
|
|
/* Start stream */
|
2013-08-10 01:21:32 +00:00
|
|
|
pa_stream_connect_record(stream, info->monitor_source_name, &attr,
|
|
|
|
PA_STREAM_DONT_INHIBIT_AUTO_SUSPEND
|
|
|
|
| PA_STREAM_ADJUST_LATENCY);
|
2013-08-09 23:04:58 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __context_get_server_info_callback(pa_context* context,
|
|
|
|
const pa_server_info* info, void* data) {
|
|
|
|
|
|
|
|
guac_client* client = (guac_client*) data;
|
|
|
|
|
|
|
|
/* If no default sink, cannot continue */
|
|
|
|
if (info->default_sink_name == NULL) {
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_ERROR, "No default sink. Cannot stream audio.");
|
2013-08-09 23:04:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "Will use default sink: \"%s\"",
|
2013-08-09 23:04:58 +00:00
|
|
|
info->default_sink_name);
|
|
|
|
|
|
|
|
/* Wait for default sink information */
|
|
|
|
pa_operation_unref(
|
|
|
|
pa_context_get_sink_info_by_name(context,
|
|
|
|
info->default_sink_name, __context_get_sink_info_callback,
|
|
|
|
client));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-09 22:47:59 +00:00
|
|
|
static void __context_state_callback(pa_context* context, void* data) {
|
|
|
|
|
|
|
|
guac_client* client = (guac_client*) data;
|
|
|
|
|
|
|
|
switch (pa_context_get_state(context)) {
|
|
|
|
|
|
|
|
case PA_CONTEXT_UNCONNECTED:
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO,
|
2013-08-09 22:47:59 +00:00
|
|
|
"PulseAudio reports it is unconnected");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PA_CONTEXT_CONNECTING:
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "Connecting to PulseAudio...");
|
2013-08-09 22:47:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PA_CONTEXT_AUTHORIZING:
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO,
|
2013-08-09 22:47:59 +00:00
|
|
|
"Authorizing PulseAudio connection...");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PA_CONTEXT_SETTING_NAME:
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "Sending client name...");
|
2013-08-09 22:47:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PA_CONTEXT_READY:
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "PulseAudio now ready");
|
2013-08-09 23:04:58 +00:00
|
|
|
pa_operation_unref(pa_context_get_server_info(context,
|
|
|
|
__context_get_server_info_callback, client));
|
2013-08-09 22:47:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PA_CONTEXT_FAILED:
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "PulseAudio connection failed");
|
2013-08-09 22:47:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PA_CONTEXT_TERMINATED:
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "PulseAudio connection terminated");
|
2013-08-09 22:47:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO,
|
2013-08-09 22:47:59 +00:00
|
|
|
"Unknown PulseAudio context state: 0x%x",
|
|
|
|
pa_context_get_state(context));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-09 22:11:31 +00:00
|
|
|
void guac_pa_start_stream(guac_client* client) {
|
2013-08-09 18:58:29 +00:00
|
|
|
|
2013-08-09 22:47:59 +00:00
|
|
|
vnc_guac_client_data* client_data = (vnc_guac_client_data*) client->data;
|
|
|
|
pa_context* context;
|
|
|
|
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "Starting audio stream");
|
2013-08-10 01:21:32 +00:00
|
|
|
guac_audio_stream_begin(client_data->audio,
|
|
|
|
GUAC_VNC_AUDIO_RATE,
|
|
|
|
GUAC_VNC_AUDIO_CHANNELS,
|
|
|
|
GUAC_VNC_AUDIO_BPS);
|
2013-08-09 20:03:01 +00:00
|
|
|
|
2013-08-09 22:47:59 +00:00
|
|
|
/* Init main loop */
|
|
|
|
client_data->pa_mainloop = pa_threaded_mainloop_new();
|
|
|
|
|
|
|
|
/* Create context */
|
|
|
|
context = pa_context_new(
|
|
|
|
pa_threaded_mainloop_get_api(client_data->pa_mainloop),
|
2013-08-09 23:43:30 +00:00
|
|
|
"Guacamole Audio");
|
2013-08-09 22:47:59 +00:00
|
|
|
|
|
|
|
/* Set up context */
|
|
|
|
pa_context_set_state_callback(context, __context_state_callback, client);
|
2013-08-10 01:21:32 +00:00
|
|
|
pa_context_connect(context, client_data->pa_servername,
|
|
|
|
PA_CONTEXT_NOAUTOSPAWN, NULL);
|
2013-08-09 22:47:59 +00:00
|
|
|
|
|
|
|
/* Start loop */
|
|
|
|
pa_threaded_mainloop_start(client_data->pa_mainloop);
|
|
|
|
|
2013-08-09 22:11:31 +00:00
|
|
|
}
|
2013-08-09 20:03:01 +00:00
|
|
|
|
2013-08-09 22:11:31 +00:00
|
|
|
void guac_pa_stop_stream(guac_client* client) {
|
2013-08-09 20:03:01 +00:00
|
|
|
|
2013-08-09 22:47:59 +00:00
|
|
|
vnc_guac_client_data* client_data = (vnc_guac_client_data*) client->data;
|
|
|
|
|
|
|
|
/* Stop loop */
|
|
|
|
pa_threaded_mainloop_stop(client_data->pa_mainloop);
|
|
|
|
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "Audio stream finished");
|
2013-08-09 18:58:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|