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 00:52:09 +00:00
|
|
|
*
|
2016-03-25 19:59:40 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-08-09 00:52:09 +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"
|
2013-08-09 00:52:09 +00:00
|
|
|
|
2015-10-01 18:49:25 +00:00
|
|
|
#include "raw_encoder.h"
|
2014-01-01 22:44:28 +00:00
|
|
|
|
|
|
|
#include <guacamole/audio.h>
|
2013-08-09 00:52:09 +00:00
|
|
|
#include <guacamole/client.h>
|
2014-01-01 22:44:28 +00:00
|
|
|
#include <guacamole/protocol.h>
|
2013-08-09 00:52:09 +00:00
|
|
|
#include <guacamole/stream.h>
|
2016-03-01 17:39:34 +00:00
|
|
|
#include <guacamole/user.h>
|
2013-08-09 00:52:09 +00:00
|
|
|
|
2014-06-10 23:54:08 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-08-09 00:57:07 +00:00
|
|
|
|
2016-12-26 21:49:04 +00:00
|
|
|
/**
|
|
|
|
* Sets the encoder associated with the given guac_audio_stream, automatically
|
|
|
|
* invoking its begin_handler. The guac_audio_stream MUST NOT already be
|
|
|
|
* associated with an encoder.
|
|
|
|
*
|
|
|
|
* @param audio
|
|
|
|
* The guac_audio_stream whose encoder is being set.
|
|
|
|
*
|
|
|
|
* @param encoder
|
|
|
|
* The encoder to associate with the given guac_audio_stream.
|
|
|
|
*/
|
|
|
|
static void guac_audio_stream_set_encoder(guac_audio_stream* audio,
|
|
|
|
guac_audio_encoder* encoder) {
|
|
|
|
|
|
|
|
/* Call handler, if defined */
|
|
|
|
if (encoder != NULL && encoder->begin_handler)
|
|
|
|
encoder->begin_handler(audio);
|
|
|
|
|
|
|
|
/* Assign encoder, which may be NULL */
|
|
|
|
audio->encoder = encoder;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 17:39:34 +00:00
|
|
|
/**
|
|
|
|
* Assigns a new audio encoder to the given guac_audio_stream based on the
|
|
|
|
* audio mimetypes declared as supported by the given user. If no audio encoder
|
|
|
|
* can be found, no new audio encoder is assigned, and the existing encoder is
|
|
|
|
* left untouched (if any).
|
|
|
|
*
|
2016-12-26 21:49:04 +00:00
|
|
|
* @param user
|
2016-03-01 17:39:34 +00:00
|
|
|
* The user whose supported audio mimetypes should determine the audio
|
2016-12-26 21:49:04 +00:00
|
|
|
* encoder selected.
|
2016-03-01 17:39:34 +00:00
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
* The guac_audio_stream to which the new encoder should be assigned.
|
|
|
|
* Existing properties set on this audio stream, such as the bits per
|
|
|
|
* sample, may affect the encoder chosen.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* The assigned audio encoder. If no new audio encoder can be assigned,
|
|
|
|
* this will be the currently-assigned audio encoder (which may be NULL).
|
|
|
|
*/
|
2016-12-26 21:49:04 +00:00
|
|
|
static void* guac_audio_assign_encoder(guac_user* user, void* data) {
|
2013-08-09 00:52:09 +00:00
|
|
|
|
2016-03-01 17:39:34 +00:00
|
|
|
int i;
|
2013-08-09 00:57:07 +00:00
|
|
|
|
2016-03-01 17:39:34 +00:00
|
|
|
guac_audio_stream* audio = (guac_audio_stream*) data;
|
|
|
|
int bps = audio->bps;
|
2013-08-09 00:57:07 +00:00
|
|
|
|
2016-12-26 21:49:04 +00:00
|
|
|
/* If no user is provided, or an encoder has already been assigned,
|
|
|
|
* do not attempt to assign a new encoder */
|
|
|
|
if (user == NULL || audio->encoder != NULL)
|
2016-03-01 17:39:34 +00:00
|
|
|
return audio->encoder;
|
2013-08-09 00:57:07 +00:00
|
|
|
|
2016-03-01 17:39:34 +00:00
|
|
|
/* For each supported mimetype, check for an associated encoder */
|
2016-12-26 21:49:04 +00:00
|
|
|
for (i=0; user->info.audio_mimetypes[i] != NULL; i++) {
|
2013-08-09 00:57:07 +00:00
|
|
|
|
2016-12-26 21:49:04 +00:00
|
|
|
const char* mimetype = user->info.audio_mimetypes[i];
|
2013-08-09 00:57:07 +00:00
|
|
|
|
2016-03-01 17:39:34 +00:00
|
|
|
/* If 16-bit raw audio is supported, done. */
|
|
|
|
if (bps == 16 && strcmp(mimetype, raw16_encoder->mimetype) == 0) {
|
2016-12-26 21:49:04 +00:00
|
|
|
guac_audio_stream_set_encoder(audio, raw16_encoder);
|
2016-03-01 17:39:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-10-01 18:49:25 +00:00
|
|
|
|
2016-03-01 17:39:34 +00:00
|
|
|
/* If 8-bit raw audio is supported, done. */
|
|
|
|
if (bps == 8 && strcmp(mimetype, raw8_encoder->mimetype) == 0) {
|
2016-12-26 21:49:04 +00:00
|
|
|
guac_audio_stream_set_encoder(audio, raw8_encoder);
|
2016-03-01 17:39:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-08-09 00:57:07 +00:00
|
|
|
|
2016-03-01 17:39:34 +00:00
|
|
|
} /* end for each mimetype */
|
2013-08-09 00:57:07 +00:00
|
|
|
|
2016-03-01 17:39:34 +00:00
|
|
|
/* Return assigned encoder, if any */
|
|
|
|
return audio->encoder;
|
2013-08-09 01:32:00 +00:00
|
|
|
|
2016-03-01 17:39:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
guac_audio_stream* guac_audio_stream_alloc(guac_client* client,
|
|
|
|
guac_audio_encoder* encoder, int rate, int channels, int bps) {
|
|
|
|
|
|
|
|
guac_audio_stream* audio;
|
2013-08-09 00:57:07 +00:00
|
|
|
|
2013-08-09 00:52:09 +00:00
|
|
|
/* Allocate stream */
|
2015-10-01 18:49:25 +00:00
|
|
|
audio = (guac_audio_stream*) calloc(1, sizeof(guac_audio_stream));
|
2013-08-09 00:52:09 +00:00
|
|
|
audio->client = client;
|
|
|
|
audio->stream = guac_client_alloc_stream(client);
|
|
|
|
|
|
|
|
/* Load PCM properties */
|
|
|
|
audio->rate = rate;
|
|
|
|
audio->channels = channels;
|
|
|
|
audio->bps = bps;
|
|
|
|
|
2016-12-26 21:49:04 +00:00
|
|
|
/* Assign encoder if explicitly provided */
|
|
|
|
if (encoder != NULL)
|
|
|
|
guac_audio_stream_set_encoder(audio, encoder);
|
2016-03-01 17:39:34 +00:00
|
|
|
|
2016-12-26 21:49:04 +00:00
|
|
|
/* Otherwise, attempt to automatically assign encoder for owner */
|
|
|
|
if (audio->encoder == NULL)
|
|
|
|
guac_client_for_owner(client, guac_audio_assign_encoder, audio);
|
|
|
|
|
|
|
|
/* Failing that, attempt to assign encoder for ANY connected user */
|
|
|
|
if (audio->encoder == NULL)
|
|
|
|
guac_client_foreach_user(client, guac_audio_assign_encoder, audio);
|
2013-08-09 00:52:09 +00:00
|
|
|
|
2015-10-01 18:49:25 +00:00
|
|
|
return audio;
|
2013-08-09 00:52:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-01 18:49:25 +00:00
|
|
|
void guac_audio_stream_reset(guac_audio_stream* audio,
|
|
|
|
guac_audio_encoder* encoder, int rate, int channels, int bps) {
|
2013-08-09 00:52:09 +00:00
|
|
|
|
2016-12-26 21:49:04 +00:00
|
|
|
/* Pull assigned encoder if no other encoder is requested */
|
|
|
|
if (encoder == NULL)
|
|
|
|
encoder = audio->encoder;
|
|
|
|
|
2015-10-01 18:49:25 +00:00
|
|
|
/* Do nothing if nothing is changing */
|
2016-12-26 21:49:04 +00:00
|
|
|
if (encoder == audio->encoder
|
2015-10-01 18:49:25 +00:00
|
|
|
&& rate == audio->rate
|
|
|
|
&& channels == audio->channels
|
|
|
|
&& bps == audio->bps) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-09 00:52:09 +00:00
|
|
|
|
2015-10-01 18:49:25 +00:00
|
|
|
/* Free old encoder data */
|
2016-12-26 21:49:04 +00:00
|
|
|
if (audio->encoder != NULL && audio->encoder->end_handler)
|
2015-10-01 18:49:25 +00:00
|
|
|
audio->encoder->end_handler(audio);
|
2013-09-24 19:01:02 +00:00
|
|
|
|
2015-10-01 18:49:25 +00:00
|
|
|
/* Set PCM properties */
|
|
|
|
audio->rate = rate;
|
|
|
|
audio->channels = channels;
|
|
|
|
audio->bps = bps;
|
2013-08-09 00:52:09 +00:00
|
|
|
|
2016-12-26 21:49:04 +00:00
|
|
|
/* Re-init encoder */
|
|
|
|
guac_audio_stream_set_encoder(audio, encoder);
|
2013-08-09 00:52:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 17:39:34 +00:00
|
|
|
void guac_audio_stream_add_user(guac_audio_stream* audio, guac_user* user) {
|
|
|
|
|
2016-12-26 21:49:04 +00:00
|
|
|
/* Attempt to assign encoder if no encoder has yet been assigned */
|
|
|
|
if (audio->encoder == NULL)
|
|
|
|
guac_audio_assign_encoder(user, audio);
|
2016-03-01 17:39:34 +00:00
|
|
|
|
|
|
|
/* Notify encoder that a new user is present */
|
2016-12-26 21:49:04 +00:00
|
|
|
if (audio->encoder != NULL && audio->encoder->join_handler)
|
|
|
|
audio->encoder->join_handler(audio, user);
|
2016-03-01 17:39:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-09 01:32:00 +00:00
|
|
|
void guac_audio_stream_free(guac_audio_stream* audio) {
|
2013-08-09 00:52:09 +00:00
|
|
|
|
2015-10-01 18:49:25 +00:00
|
|
|
/* Flush stream encoding */
|
|
|
|
guac_audio_stream_flush(audio);
|
2013-08-09 00:52:09 +00:00
|
|
|
|
2015-10-01 18:49:25 +00:00
|
|
|
/* Clean up encoder */
|
2016-12-26 21:49:04 +00:00
|
|
|
if (audio->encoder != NULL && audio->encoder->end_handler)
|
2015-10-01 18:49:25 +00:00
|
|
|
audio->encoder->end_handler(audio);
|
2013-08-09 00:52:09 +00:00
|
|
|
|
2015-10-01 18:49:25 +00:00
|
|
|
/* Free associated data */
|
|
|
|
free(audio);
|
2013-08-09 00:52:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-01 18:49:25 +00:00
|
|
|
void guac_audio_stream_write_pcm(guac_audio_stream* audio,
|
2013-08-09 23:52:25 +00:00
|
|
|
const unsigned char* data, int length) {
|
2013-08-09 00:52:09 +00:00
|
|
|
|
2015-10-01 18:49:25 +00:00
|
|
|
/* Write data */
|
2016-12-26 21:49:04 +00:00
|
|
|
if (audio->encoder != NULL && audio->encoder->write_handler)
|
2015-10-01 18:49:25 +00:00
|
|
|
audio->encoder->write_handler(audio, data, length);
|
2013-08-09 00:52:09 +00:00
|
|
|
|
2015-10-01 18:49:25 +00:00
|
|
|
}
|
2013-08-09 00:52:09 +00:00
|
|
|
|
2015-10-01 18:49:25 +00:00
|
|
|
void guac_audio_stream_flush(guac_audio_stream* audio) {
|
2013-08-09 00:52:09 +00:00
|
|
|
|
2015-10-01 18:49:25 +00:00
|
|
|
/* Flush any buffered data */
|
2016-12-26 21:49:04 +00:00
|
|
|
if (audio->encoder != NULL && audio->encoder->flush_handler)
|
2015-10-01 18:49:25 +00:00
|
|
|
audio->encoder->flush_handler(audio);
|
2013-08-09 00:52:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|