]> code.delx.au - pulseaudio/commitdiff
Wrap the io_callback to ensure that all data is written before asking for more.
authorColin Guthrie <pulse@colin.guthr.ie>
Wed, 7 May 2008 01:10:31 +0000 (01:10 +0000)
committerColin Guthrie <pulse@colin.guthr.ie>
Wed, 8 Oct 2008 19:32:07 +0000 (20:32 +0100)
Fix the length type for send_sample (restrict to 16bit value)

git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/coling@2374 fefdeb5f-60dc-0310-8127-8f9354f1896f

src/modules/rtp/raop_client.c
src/modules/rtp/raop_client.h

index 8f6f2594e29a9e78965ba540f0bdbac83b26d605..2dd2de949467b8cbf91d0c1f1a63606a33e56ca5 100644 (file)
@@ -93,6 +93,8 @@ struct pa_raop_client {
     void* userdata;
 
     uint8_t *buffer;
+    uint8_t *buffer_index;
+    uint16_t buffer_count;
     /*pa_memchunk memchunk;*/
 };
 
@@ -205,22 +207,51 @@ void pa_raop_client_free(pa_raop_client* c)
     pa_xfree(c);
 }
 
-static int remove_char_from_string(char *str, char rc)
+static inline void rtrimchar(char *str, char rc)
 {
-  int i=0, j=0, len;
-  int num = 0;
-  len = strlen(str);
-  while (i<len) {
-      if (str[i] == rc) {
-          for (j=i; j<len; j++)
-              str[j] = str[j+1];
-          len--;
-          num++;
-      } else {
-          i++;
-      }
-  }
-  return num;
+    char *sp = str + strlen(str) - 1;
+    while (sp >= str && *sp == rc) {
+        *sp = '\0';
+        sp -= 1;
+    }
+}
+
+static int pa_raop_client_process(pa_raop_client* c)
+{
+    ssize_t l;
+
+    pa_assert(c);
+
+    if (!c->buffer_index || !c->buffer_count)
+        return 1;
+
+    if (!pa_iochannel_is_writable(c->io))
+        return 0;
+    l = pa_iochannel_write(c->io, c->buffer_index, c->buffer_count);
+    /*pa_log_debug("Wrote %d bytes (from buffer)", (int)l);*/
+    if (l == c->buffer_count) {
+        c->buffer_index = NULL;
+        c->buffer_count = 0;
+        return 1;
+    }
+    c->buffer_index += l;
+    c->buffer_count -= l;
+    /*pa_log_debug("Sill have %d bytes (in buffer)", c->buffer_count);*/
+
+    return 0;
+}
+
+static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void *userdata)
+{
+    pa_raop_client *c = userdata;
+
+    pa_assert(c);
+    pa_assert(c->io == io);
+    pa_assert(c->callback);
+
+    if (pa_raop_client_process(c)) {
+        c->callback(c->io, c->userdata);
+    }
 }
 
 static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata) {
@@ -239,7 +270,7 @@ static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata
     }
     pa_assert(!c->io);
     c->io = io;
-    pa_iochannel_set_callback(c->io, c->callback, c->userdata);
+    pa_iochannel_set_callback(c->io, io_callback, c);
 }
 
 static void rtsp_cb(pa_rtsp_context *rtsp, pa_rtsp_state state, pa_headerlist* headers, void *userdata)
@@ -268,13 +299,13 @@ static void rtsp_cb(pa_rtsp_context *rtsp, pa_rtsp_state state, pa_headerlist* h
             /* Now encrypt our aes_public key to send to the device */
             i = rsa_encrypt(c->aes_key, AES_CHUNKSIZE, rsakey);
             pa_base64_encode(rsakey, i, &key);
-            remove_char_from_string(key, '=');
+            rtrimchar(key, '=');
             pa_base64_encode(c->aes_iv, AES_CHUNKSIZE, &iv);
-            remove_char_from_string(iv, '=');
+            rtrimchar(iv, '=');
 
             pa_random(&rand_data, sizeof(rand_data));
             pa_base64_encode(&rand_data, AES_CHUNKSIZE, &sac);
-            remove_char_from_string(sac, '=');
+            rtrimchar(sac, '=');
             pa_rtsp_add_header(c->rtsp, "Apple-Challenge", sac);
             sdp = pa_sprintf_malloc(
                 "v=0\r\n"
@@ -388,7 +419,7 @@ void pa_raop_client_disconnect(pa_raop_client* c)
 
 }
 
-void pa_raop_client_send_sample(pa_raop_client* c, const uint8_t* buffer, unsigned int count)
+void pa_raop_client_send_sample(pa_raop_client* c, const uint8_t* buffer, uint16_t count)
 {
     ssize_t l;
     uint16_t len;
@@ -406,7 +437,7 @@ void pa_raop_client_send_sample(pa_raop_client* c, const uint8_t* buffer, unsign
 
     c->buffer = pa_xrealloc(c->buffer, (count + header_size + 16));
     memcpy(c->buffer, header, header_size);
-    len = count + header_size - 4;
+    len = header_size + count - 4;
 
     /* store the lenght (endian swapped: make this better) */
     *(c->buffer + 2) = len >> 8;
@@ -417,9 +448,18 @@ void pa_raop_client_send_sample(pa_raop_client* c, const uint8_t* buffer, unsign
     len = header_size + count;
 
     /* TODO: move this into a memchunk/memblock and write only in callback */
+    /*pa_log_debug("Channel status: %d", pa_iochannel_is_writable(c->io));
+    pa_log_debug("Writing %d bytes", len);*/
     l = pa_iochannel_write(c->io, c->buffer, len);
+    /*pa_log_debug("Wrote %d bytes", (int)l);*/
+    if (l != len) {
+        c->buffer_index = c->buffer + l;
+        c->buffer_count = len - l;
+    }
+    /*pa_log_debug("Sill have %d bytes (in buffer)", c->buffer_count);*/
 }
 
+
 void pa_raop_client_set_callback(pa_raop_client* c, pa_iochannel_cb_t callback, void *userdata)
 {
     pa_assert(c);
index 99c75fdb57e9b383d17e9f3132fb06ba615554f2..1dcf779f923fb50986b610f27b374da72a27f3a6 100644 (file)
@@ -36,7 +36,7 @@ int pa_raop_client_connect(pa_raop_client* c, pa_mainloop_api *mainloop, const c
 
 void pa_raop_client_disconnect(pa_raop_client* c);
 
-void pa_raop_client_send_sample(pa_raop_client* c, const uint8_t* buffer, unsigned int count);
+void pa_raop_client_send_sample(pa_raop_client* c, const uint8_t* buffer, uint16_t count);
 
 void pa_raop_client_set_callback(pa_raop_client* c, pa_iochannel_cb_t callback, void *userdata);