guacamole-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmuehl...@apache.org
Subject [06/26] incubator-guacamole-server git commit: GUACAMOLE-25: Delay sending of "ack" until RDP has opened the AUDIO_INPUT stream. Close audio stream with another "ack" when RDP has closed the AUDIO_INPUT stream.
Date Thu, 02 Jun 2016 18:45:13 GMT
GUACAMOLE-25: Delay sending of "ack" until RDP has opened the AUDIO_INPUT stream. Close audio
stream with another "ack" when RDP has closed the AUDIO_INPUT stream.


Project: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/commit/46bdf069
Tree: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/tree/46bdf069
Diff: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/diff/46bdf069

Branch: refs/heads/master
Commit: 46bdf0692fca50d74c964701f163cc0df58ba76b
Parents: 5030c06
Author: Michael Jumper <mjumper@apache.org>
Authored: Mon May 2 00:11:38 2016 -0700
Committer: Michael Jumper <mjumper@apache.org>
Committed: Wed May 25 13:50:28 2016 -0700

----------------------------------------------------------------------
 src/protocols/rdp/audio_input.c | 97 ++++++++++++++++++++++++++++++++++--
 src/protocols/rdp/audio_input.h | 39 +++++++++++++++
 2 files changed, 131 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/46bdf069/src/protocols/rdp/audio_input.c
----------------------------------------------------------------------
diff --git a/src/protocols/rdp/audio_input.c b/src/protocols/rdp/audio_input.c
index 8839d35..93c492b 100644
--- a/src/protocols/rdp/audio_input.c
+++ b/src/protocols/rdp/audio_input.c
@@ -30,19 +30,22 @@
 #include <guacamole/user.h>
 
 #include <stdlib.h>
+#include <pthread.h>
 
 int guac_rdp_audio_handler(guac_user* user, guac_stream* stream,
         char* mimetype) {
 
+    guac_client* client = user->client;
+    guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
+
     /* FIXME: Assuming mimetype of "audio/L16;rate=44100,channels=2" */
 
     /* Init stream data */
     stream->blob_handler = guac_rdp_audio_blob_handler;
     stream->end_handler = guac_rdp_audio_end_handler;
 
-    guac_protocol_send_ack(user->socket, stream,
-            "OK", GUAC_PROTOCOL_STATUS_SUCCESS);
-    guac_socket_flush(user->socket);
+    /* Associate stream with audio buffer */
+    guac_rdp_audio_buffer_set_stream(rdp_client->audio_input, user, stream);
 
     return 0;
 
@@ -83,13 +86,72 @@ void guac_rdp_audio_load_plugin(rdpContext* context) {
 }
 
 guac_rdp_audio_buffer* guac_rdp_audio_buffer_alloc() {
-    return calloc(1, sizeof(guac_rdp_audio_buffer));
+    guac_rdp_audio_buffer* buffer = calloc(1, sizeof(guac_rdp_audio_buffer));
+    pthread_mutex_init(&(buffer->lock), NULL);
+    return buffer;
+}
+
+/**
+ * Sends an "ack" instruction over the socket associated with the Guacamole
+ * stream over which audio data is being received. The "ack" instruction will
+ * only be sent if the Guacamole audio stream has been established (through
+ * receipt of an "audio" instruction), is still open (has not received an "end"
+ * instruction nor been associated with an "ack" having an error code), and is
+ * associated with an active RDP AUDIO_INPUT channel.
+ *
+ * @param audio_buffer
+ *     The audio buffer associated with the guac_stream for which the "ack"
+ *     instruction should be sent, if any. If there is no associated
+ *     guac_stream, this function has no effect.
+ *
+ * @param message
+ *     An arbitrary human-readable message to send along with the "ack".
+ *
+ * @param status
+ *     The Guacamole protocol status code to send with the "ack". This should
+ *     be GUAC_PROTOCOL_STATUS_SUCCESS if the audio stream has been set up
+ *     successfully or GUAC_PROTOCOL_STATUS_RESOURCE_CLOSED if the audio stream
+ *     has been closed (but may usable again if reopened).
+ */
+static void guac_rdp_audio_buffer_ack(guac_rdp_audio_buffer* audio_buffer,
+        const char* message, guac_protocol_status status) {
+
+    guac_user* user = audio_buffer->user;
+    guac_stream* stream = audio_buffer->stream;
+
+    /* Do not send ack unless both sides of the audio stream are ready */
+    if (user == NULL || stream == NULL || audio_buffer->packet == NULL)
+        return;
+
+    /* Send ack instruction */
+    guac_protocol_send_ack(user->socket, stream, message, status);
+    guac_socket_flush(user->socket);
+
+}
+
+void guac_rdp_audio_buffer_set_stream(guac_rdp_audio_buffer* audio_buffer,
+        guac_user* user, guac_stream* stream) {
+
+    pthread_mutex_lock(&(audio_buffer->lock));
+
+    /* Associate received stream */
+    audio_buffer->user = user;
+    audio_buffer->stream = stream;
+
+    /* Acknowledge stream creation (if buffer is ready to receive) */
+    guac_rdp_audio_buffer_ack(audio_buffer,
+            "OK", GUAC_PROTOCOL_STATUS_SUCCESS);
+
+    pthread_mutex_unlock(&(audio_buffer->lock));
+
 }
 
 void guac_rdp_audio_buffer_begin(guac_rdp_audio_buffer* audio_buffer,
         int packet_size, guac_rdp_audio_buffer_flush_handler* flush_handler,
         void* data) {
 
+    pthread_mutex_lock(&(audio_buffer->lock));
+
     /* Reset buffer state to provided values */
     audio_buffer->bytes_written = 0;
     audio_buffer->packet_size = packet_size;
@@ -100,14 +162,24 @@ void guac_rdp_audio_buffer_begin(guac_rdp_audio_buffer* audio_buffer,
     free(audio_buffer->packet);
     audio_buffer->packet = malloc(packet_size);
 
+    /* Acknowledge stream creation (if stream is ready to receive) */
+    guac_rdp_audio_buffer_ack(audio_buffer,
+            "OK", GUAC_PROTOCOL_STATUS_SUCCESS);
+
+    pthread_mutex_unlock(&(audio_buffer->lock));
+
 }
 
 void guac_rdp_audio_buffer_write(guac_rdp_audio_buffer* audio_buffer,
         char* buffer, int length) {
 
+    pthread_mutex_lock(&(audio_buffer->lock));
+
     /* Ignore packet if there is no buffer */
-    if (audio_buffer->packet_size == 0 || audio_buffer->packet == NULL)
+    if (audio_buffer->packet_size == 0 || audio_buffer->packet == NULL) {
+        pthread_mutex_unlock(&(audio_buffer->lock));
         return;
+    }
 
     /* Continuously write packets until no data remains */
     while (length > 0) {
@@ -146,10 +218,22 @@ void guac_rdp_audio_buffer_write(guac_rdp_audio_buffer* audio_buffer,
 
     } /* end packet write loop */
 
+    pthread_mutex_unlock(&(audio_buffer->lock));
+
 }
 
 void guac_rdp_audio_buffer_end(guac_rdp_audio_buffer* audio_buffer) {
 
+    pthread_mutex_lock(&(audio_buffer->lock));
+
+    /* The stream is now closed */
+    guac_rdp_audio_buffer_ack(audio_buffer,
+            "CLOSED", GUAC_PROTOCOL_STATUS_RESOURCE_CLOSED);
+
+    /* Unset user and stream */
+    audio_buffer->user = NULL;
+    audio_buffer->stream = NULL;
+
     /* Reset buffer state */
     audio_buffer->bytes_written = 0;
     audio_buffer->packet_size = 0;
@@ -159,9 +243,12 @@ void guac_rdp_audio_buffer_end(guac_rdp_audio_buffer* audio_buffer) {
     free(audio_buffer->packet);
     audio_buffer->packet = NULL;
 
+    pthread_mutex_unlock(&(audio_buffer->lock));
+
 }
 
 void guac_rdp_audio_buffer_free(guac_rdp_audio_buffer* audio_buffer) {
+    pthread_mutex_destroy(&(audio_buffer->lock));
     free(audio_buffer->packet);
     free(audio_buffer);
 }

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/46bdf069/src/protocols/rdp/audio_input.h
----------------------------------------------------------------------
diff --git a/src/protocols/rdp/audio_input.h b/src/protocols/rdp/audio_input.h
index c89de15..776f0a5 100644
--- a/src/protocols/rdp/audio_input.h
+++ b/src/protocols/rdp/audio_input.h
@@ -23,8 +23,11 @@
 #include "config.h"
 
 #include <freerdp/freerdp.h>
+#include <guacamole/stream.h>
 #include <guacamole/user.h>
 
+#include <pthread.h>
+
 /**
  * Handler which is invoked when a guac_rdp_audio_buffer's internal packet
  * buffer has reached capacity and must be flushed.
@@ -52,6 +55,24 @@ typedef void guac_rdp_audio_buffer_flush_handler(char* buffer, int length,
 typedef struct guac_rdp_audio_buffer {
 
     /**
+     * Lock which is acquired/released to ensure accesses to the audio buffer
+     * are atomic.
+     */
+    pthread_mutex_t lock;
+
+    /**
+     * The user from which this audio buffer will receive data. If no user has
+     * yet opened an associated audio stream, this will be NULL.
+     */
+    guac_user* user;
+
+    /**
+     * The stream from which this audio buffer will receive data. If no user
+     * has yet opened an associated audio stream, this will be NULL.
+     */
+    guac_stream* stream;
+
+    /**
      * The size that each audio packet must be, in bytes. The packet buffer
      * within this structure will be at least this size.
      */
@@ -92,6 +113,24 @@ typedef struct guac_rdp_audio_buffer {
 guac_rdp_audio_buffer* guac_rdp_audio_buffer_alloc();
 
 /**
+ * Associates the given audio buffer with the underlying audio stream which
+ * has been received from the given Guacamole user. Once both the Guacamole
+ * audio stream and the RDP audio stream are ready, an appropriate "ack"
+ * message will be sent.
+ *
+ * @param audio_buffer
+ *     The audio buffer associated with the audio stream just received.
+ *
+ * @param user
+ *     The Guacamole user that created the audio stream.
+ *
+ * @param stream
+ *     The guac_stream object representing the audio stream.
+ */
+void guac_rdp_audio_buffer_set_stream(guac_rdp_audio_buffer* audio_buffer,
+        guac_user* user, guac_stream* stream);
+
+/**
  * Begins handling of audio data received via guac_rdp_audio_buffer_write() and
  * allocates the necessary underlying packet buffer. Audio packets of exactly
  * packet_size bytes will be flushed as available using the provided


Mime
View raw message