2013-12-29 04:53:12 +00:00
|
|
|
/*
|
2016-03-25 19:59:40 +00:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you 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
|
2013-08-09 18:58:29 +00:00
|
|
|
*
|
2016-03-25 19:59:40 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-08-09 18:58:29 +00:00
|
|
|
*
|
2016-03-25 19:59:40 +00:00
|
|
|
* 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.
|
2013-12-29 04:53:12 +00:00
|
|
|
*/
|
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "pulse.h"
|
2016-03-01 05:42:57 +00:00
|
|
|
#include "vnc.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>
|
2015-09-05 01:58:21 +00:00
|
|
|
#include <guacamole/socket.h>
|
2013-08-09 22:11:31 +00:00
|
|
|
#include <pulse/pulseaudio.h>
|
2013-08-09 20:03:01 +00:00
|
|
|
|
2015-08-22 03:35:13 +00:00
|
|
|
/**
|
|
|
|
* Returns whether the given buffer contains only silence (only null bytes).
|
|
|
|
*
|
|
|
|
* @param buffer
|
|
|
|
* The audio buffer to check.
|
|
|
|
*
|
|
|
|
* @param length
|
|
|
|
* The length of the buffer to check.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Non-zero if the audio buffer contains silence, zero otherwise.
|
|
|
|
*/
|
|
|
|
static int guac_pa_is_silence(const void* buffer, size_t length) {
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
2015-09-04 20:47:21 +00:00
|
|
|
const unsigned char* current = (const unsigned char*) buffer;
|
2015-08-22 03:35:13 +00:00
|
|
|
|
|
|
|
/* For each byte in buffer */
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
|
|
|
|
/* If current value non-zero, then not silence */
|
|
|
|
if (*(current++))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, the buffer contains 100% silence */
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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;
|
2016-03-01 05:42:57 +00:00
|
|
|
guac_vnc_client* vnc_client = (guac_vnc_client*) client->data;
|
|
|
|
guac_audio_stream* audio = vnc_client->audio;
|
2013-08-10 00:01:06 +00:00
|
|
|
|
2013-08-09 23:43:30 +00:00
|
|
|
const void* buffer;
|
|
|
|
|
|
|
|
/* Read data */
|
|
|
|
pa_stream_peek(stream, &buffer, &length);
|
|
|
|
|
2015-10-01 18:49:25 +00:00
|
|
|
/* Continuously write received PCM data */
|
|
|
|
if (!guac_pa_is_silence(buffer, length))
|
2015-08-22 03:35:13 +00:00
|
|
|
guac_audio_stream_write_pcm(audio, buffer, length);
|
|
|
|
|
2015-10-01 18:49:25 +00:00
|
|
|
/* Flush upon silence */
|
|
|
|
else
|
|
|
|
guac_audio_stream_flush(audio);
|
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
|
|
|
|
2016-03-01 05:42:57 +00:00
|
|
|
guac_vnc_client* vnc_client = (guac_vnc_client*) client->data;
|
|
|
|
guac_vnc_settings* settings = vnc_client->settings;
|
|
|
|
|
2013-08-09 22:47:59 +00:00
|
|
|
pa_context* context;
|
|
|
|
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "Starting audio stream");
|
2013-08-09 20:03:01 +00:00
|
|
|
|
2013-08-09 22:47:59 +00:00
|
|
|
/* Init main loop */
|
2016-03-01 05:42:57 +00:00
|
|
|
vnc_client->pa_mainloop = pa_threaded_mainloop_new();
|
2013-08-09 22:47:59 +00:00
|
|
|
|
|
|
|
/* Create context */
|
|
|
|
context = pa_context_new(
|
2016-03-01 05:42:57 +00:00
|
|
|
pa_threaded_mainloop_get_api(vnc_client->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);
|
2016-03-01 05:42:57 +00:00
|
|
|
pa_context_connect(context, settings->pa_servername,
|
2013-08-10 01:21:32 +00:00
|
|
|
PA_CONTEXT_NOAUTOSPAWN, NULL);
|
2013-08-09 22:47:59 +00:00
|
|
|
|
|
|
|
/* Start loop */
|
2016-03-01 05:42:57 +00:00
|
|
|
pa_threaded_mainloop_start(vnc_client->pa_mainloop);
|
2013-08-09 22:47:59 +00:00
|
|
|
|
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
|
|
|
|
2016-03-01 05:42:57 +00:00
|
|
|
guac_vnc_client* vnc_client = (guac_vnc_client*) client->data;
|
2013-08-09 22:47:59 +00:00
|
|
|
|
|
|
|
/* Stop loop */
|
2016-03-01 05:42:57 +00:00
|
|
|
pa_threaded_mainloop_stop(vnc_client->pa_mainloop);
|
2013-08-09 22:47:59 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|