guacamole-spice-protocol/src/protocols/vnc/pulse.c

268 lines
7.9 KiB
C
Raw Normal View History

/*
* Copyright (C) 2013 Glyptodon LLC
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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-10 00:01:06 +00:00
#include <guacamole/audio.h>
#include <guacamole/client.h>
#include <guacamole/socket.h>
2013-08-09 22:11:31 +00:00
#include <pulse/pulseaudio.h>
2013-08-09 20:03:01 +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;
/* 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;
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);
/* Continuously write received PCM data */
if (!guac_pa_is_silence(buffer, length))
guac_audio_stream_write_pcm(audio, buffer, length);
/* 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:
guac_client_log(client, GUAC_LOG_INFO,
2013-08-09 23:43:30 +00:00
"PulseAudio stream currently unconnected");
break;
case PA_STREAM_CREATING:
guac_client_log(client, GUAC_LOG_INFO, "PulseAudio stream being created...");
2013-08-09 23:43:30 +00:00
break;
case PA_STREAM_READY:
guac_client_log(client, GUAC_LOG_INFO, "PulseAudio stream now ready");
2013-08-09 23:43:30 +00:00
break;
case PA_STREAM_FAILED:
guac_client_log(client, GUAC_LOG_INFO, "PulseAudio stream connection failed");
2013-08-09 23:43:30 +00:00
break;
case PA_STREAM_TERMINATED:
guac_client_log(client, GUAC_LOG_INFO, "PulseAudio stream terminated");
2013-08-09 23:43:30 +00:00
break;
default:
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
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;
attr.maxlength = -1;
attr.fragsize = GUAC_VNC_AUDIO_FRAGMENT_SIZE;
2013-08-09 23:43:30 +00:00
/* Create stream */
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) {
guac_client_log(client, GUAC_LOG_ERROR, "No default sink. Cannot stream audio.");
2013-08-09 23:04:58 +00:00
return;
}
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));
}
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:
guac_client_log(client, GUAC_LOG_INFO,
"PulseAudio reports it is unconnected");
break;
case PA_CONTEXT_CONNECTING:
guac_client_log(client, GUAC_LOG_INFO, "Connecting to PulseAudio...");
break;
case PA_CONTEXT_AUTHORIZING:
guac_client_log(client, GUAC_LOG_INFO,
"Authorizing PulseAudio connection...");
break;
case PA_CONTEXT_SETTING_NAME:
guac_client_log(client, GUAC_LOG_INFO, "Sending client name...");
break;
case PA_CONTEXT_READY:
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));
break;
case PA_CONTEXT_FAILED:
guac_client_log(client, GUAC_LOG_INFO, "PulseAudio connection failed");
break;
case PA_CONTEXT_TERMINATED:
guac_client_log(client, GUAC_LOG_INFO, "PulseAudio connection terminated");
break;
default:
guac_client_log(client, GUAC_LOG_INFO,
"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) {
vnc_guac_client_data* client_data = (vnc_guac_client_data*) client->data;
pa_context* context;
guac_client_log(client, GUAC_LOG_INFO, "Starting audio stream");
2013-08-09 20:03:01 +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");
/* 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);
/* 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
vnc_guac_client_data* client_data = (vnc_guac_client_data*) client->data;
/* Stop loop */
pa_threaded_mainloop_stop(client_data->pa_mainloop);
guac_client_log(client, GUAC_LOG_INFO, "Audio stream finished");
}