summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorColin Guthrie <pulse@colin.guthr.ie>2008-05-07 01:10:31 +0000
committerColin Guthrie <pulse@colin.guthr.ie>2008-10-08 20:32:07 +0100
commite596f42f39232e4e0d36c3764474f73a7ff48fbb (patch)
tree991ccfc78a082c69c291f752deafc6a5fce702d7
parent6510d97315b9bdf7b1afc204c3dca0a2b0a3a528 (diff)
Wrap the io_callback to ensure that all data is written before asking for more.
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
-rw-r--r--src/modules/rtp/raop_client.c82
-rw-r--r--src/modules/rtp/raop_client.h2
2 files changed, 62 insertions, 22 deletions
diff --git a/src/modules/rtp/raop_client.c b/src/modules/rtp/raop_client.c
index 8f6f2594..2dd2de94 100644
--- a/src/modules/rtp/raop_client.c
+++ b/src/modules/rtp/raop_client.c
@@ -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);
diff --git a/src/modules/rtp/raop_client.h b/src/modules/rtp/raop_client.h
index 99c75fdb..1dcf779f 100644
--- a/src/modules/rtp/raop_client.h
+++ b/src/modules/rtp/raop_client.h
@@ -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);