[pulseaudio-commits] r2394 - in /branches/coling/airtunes/src/modules/rtp: raop_client.c raop_client.h

svnmailer-noreply at 0pointer.de svnmailer-noreply at 0pointer.de
Sat May 10 16:01:38 PDT 2008


Author: coling
Date: Sun May 11 01:01:37 2008
New Revision: 2394

URL: http://0pointer.de/cgi-bin/viewcvs.cgi?rev=2394&root=pulseaudio&view=rev
Log:
Combine pa_raop_client_new and pa_raop_client_connect (no point in having them separate)
Convert the iochannel to an fd and do not call a pa_iochannel_cb_t callback but rather trigger the callback on connection and pass the fd.
Change pa_raop_client_send_sample to pa_raop_client_encode_sample and work with memchunks.
Fix a subtle size bug in the bit writer that techincally isn't triggered in normal operation.
Clean up the _free function to actually free stuff.
Do the actual ALAC encoding.

Modified:
    branches/coling/airtunes/src/modules/rtp/raop_client.c
    branches/coling/airtunes/src/modules/rtp/raop_client.h

Modified: branches/coling/airtunes/src/modules/rtp/raop_client.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/coling/airtunes/src/modules/rtp/raop_client.c?rev=2394&root=pulseaudio&r1=2393&r2=2394&view=diff
==============================================================================
--- branches/coling/airtunes/src/modules/rtp/raop_client.c (original)
+++ branches/coling/airtunes/src/modules/rtp/raop_client.c Sun May 11 01:01:37 2008
@@ -88,8 +88,8 @@
     uint8_t aes_key[AES_CHUNKSIZE]; /* key for aes-cbc */
 
     pa_socket_client *sc;
-    pa_iochannel *io;
-    pa_iochannel_cb_t callback;
+    int fd;
+    pa_raop_client_cb_t callback;
     void* userdata;
 
     uint8_t *buffer;
@@ -115,7 +115,7 @@
 
     /* If bit pos is zero, we will definatly use at least one bit from the current byte so size increments. */
     if (!*bit_pos)
-        *size = 1;
+        *size += 1;
 
     /* Calc the number of bits left in the current byte of buffer */
     bits_left = 7 - *bit_pos  + 1;
@@ -195,18 +195,6 @@
     return i;
 }
 
-pa_raop_client* pa_raop_client_new(void)
-{
-    pa_raop_client* c = pa_xnew0(pa_raop_client, 1);
-    return c;
-}
-
-void pa_raop_client_free(pa_raop_client* c)
-{
-    pa_assert(c);
-    pa_xfree(c);
-}
-
 static inline void rtrimchar(char *str, char rc)
 {
     char *sp = str + strlen(str) - 1;
@@ -216,50 +204,14 @@
     }
 }
 
-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) {
     pa_raop_client *c = userdata;
 
     pa_assert(sc);
     pa_assert(c);
     pa_assert(c->sc == sc);
+    pa_assert(c->fd < 0);
+    pa_assert(c->callback);
 
     pa_socket_client_unref(c->sc);
     c->sc = NULL;
@@ -268,9 +220,16 @@
         pa_log("Connection failed: %s", pa_cstrerror(errno));
         return;
     }
-    pa_assert(!c->io);
-    c->io = io;
-    pa_iochannel_set_callback(c->io, io_callback, c);
+
+    c->fd = pa_iochannel_get_send_fd(io);
+
+    pa_iochannel_set_noclose(io, TRUE);
+    pa_iochannel_free(io);
+
+    pa_make_tcp_socket_low_delay(c->fd);
+
+    pa_log_debug("Connection established");
+    c->callback(c->fd, c->userdata);
 }
 
 static void rtsp_cb(pa_rtsp_client *rtsp, pa_rtsp_state state, pa_headerlist* headers, void *userdata)
@@ -382,7 +341,7 @@
     }
 }
 
-int pa_raop_client_connect(pa_raop_client* c, pa_mainloop_api *mainloop, const char* host)
+pa_raop_client* pa_raop_client_new(pa_mainloop_api *mainloop, const char* host)
 {
     char *sci;
     struct {
@@ -390,11 +349,12 @@
         uint32_t b;
         uint32_t c;
     } rand_data;
-
-    pa_assert(c);
+    pa_raop_client* c = pa_xnew0(pa_raop_client, 1);
+
     pa_assert(host);
 
     c->mainloop = mainloop;
+    c->fd = -1;
     c->host = host;
     c->rtsp = pa_rtsp_client_new("iTunes/4.6 (Macintosh; U; PPC Mac OS X 10.3)");
 
@@ -411,18 +371,41 @@
     sci = pa_sprintf_malloc("%08x%08x",rand_data.b, rand_data.c);
     pa_rtsp_add_header(c->rtsp, "Client-Instance", sci);
     pa_rtsp_set_callback(c->rtsp, rtsp_cb, c);
-    return pa_rtsp_connect(c->rtsp, mainloop, host, 5000);
-}
-
-void pa_raop_client_disconnect(pa_raop_client* c)
-{
-
-}
-
-void pa_raop_client_send_sample(pa_raop_client* c, const uint8_t* buffer, uint16_t count)
-{
-    ssize_t l;
-    uint16_t len;
+    if (pa_rtsp_connect(c->rtsp, mainloop, host, 5000)) {
+        pa_rtsp_client_free(c->rtsp);
+        pa_xfree(c->aes_iv);
+        pa_xfree(c->aes_nv);
+        pa_xfree(c->aes_key);
+        return NULL;
+    }
+    return c;
+}
+
+
+void pa_raop_client_free(pa_raop_client* c)
+{
+    pa_assert(c);
+
+    pa_rtsp_client_free(c->rtsp);
+    pa_xfree(c->aes_iv);
+    pa_xfree(c->aes_nv);
+    pa_xfree(c->aes_key);
+    pa_xfree(c);
+}
+
+
+static void noop(PA_GCC_UNUSED void* p) {}
+
+pa_memchunk pa_raop_client_encode_sample(pa_raop_client* c, pa_mempool* mempool, pa_memchunk* raw)
+{
+    uint16_t len, bufmax;
+    uint8_t *bp, bpos;
+    uint8_t *ibp, *maxibp;
+    int size;
+    uint8_t *p;
+    uint16_t bsize;
+    pa_memchunk rv;
+    size_t length;
     static uint8_t header[] = {
         0x24, 0x00, 0x00, 0x00,
         0xF0, 0xFF, 0x00, 0x00,
@@ -432,35 +415,66 @@
     const int header_size = sizeof(header);
 
     pa_assert(c);
-    pa_assert(buffer);
-    pa_assert(count > 0);
-
-    c->buffer = pa_xrealloc(c->buffer, (count + header_size + 16));
+    pa_assert(c->fd > 0);
+    pa_assert(raw);
+    pa_assert(raw->memblock);
+    pa_assert(raw->length > 0);
+
+    /* We have to send 4 byte chunks */
+    bsize = (int)(raw->length / 4);
+    length = bsize * 4;
+
+    /* Leave 16 bytes extra to allow for the ALAC header which is about 55 bits */
+    bufmax = length + header_size + 16;
+    c->buffer = pa_xrealloc(c->buffer, bufmax);
     memcpy(c->buffer, header, header_size);
-    len = header_size + count - 4;
+    pa_memchunk_reset(&rv);
+    rv.memblock = pa_memblock_new_user(mempool, c->buffer, (header_size + length), noop, 1);
+
+    /* Now write the actual samples */
+    bp = c->buffer + header_size;
+    size = bpos = 0;
+    bit_writer(&bp,&bpos,&size,1,3); // channel=1, stereo
+    bit_writer(&bp,&bpos,&size,0,4); // unknown
+    bit_writer(&bp,&bpos,&size,0,8); // unknown
+    bit_writer(&bp,&bpos,&size,0,4); // unknown
+    bit_writer(&bp,&bpos,&size,1,1); // hassize
+    bit_writer(&bp,&bpos,&size,0,2); // unused
+    bit_writer(&bp,&bpos,&size,1,1); // is-not-compressed
+
+    /* size of data, integer, big endian */
+    bit_writer(&bp,&bpos,&size,(bsize>>24)&0xff,8);
+    bit_writer(&bp,&bpos,&size,(bsize>>16)&0xff,8);
+    bit_writer(&bp,&bpos,&size,(bsize>>8)&0xff,8);
+    bit_writer(&bp,&bpos,&size,(bsize)&0xff,8);
+
+    ibp = p = pa_memblock_acquire(raw->memblock);
+    maxibp = p + raw->length - 4;
+    while (ibp <= maxibp) {
+        /* Byte swap stereo data */
+        bit_writer(&bp,&bpos,&size,*(ibp+1),8);
+        bit_writer(&bp,&bpos,&size,*(ibp+0),8);
+        bit_writer(&bp,&bpos,&size,*(ibp+3),8);
+        bit_writer(&bp,&bpos,&size,*(ibp+2),8);
+        ibp += 4;
+        raw->index += 4;
+        raw->length -= 4;
+    }
+    pa_memblock_release(raw->memblock);
+    rv.length = header_size + size;
 
     /* store the lenght (endian swapped: make this better) */
+    len = size + header_size - 4;
     *(c->buffer + 2) = len >> 8;
     *(c->buffer + 3) = len & 0xff;
 
-    memcpy((c->buffer+header_size), buffer, count);
-    aes_encrypt(c, (c->buffer + header_size), count);
-    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)
+    /* encrypt our data */
+    aes_encrypt(c, (c->buffer + header_size), size);
+    return rv;
+}
+
+
+void pa_raop_client_set_callback(pa_raop_client* c, pa_raop_client_cb_t callback, void *userdata)
 {
     pa_assert(c);
 

Modified: branches/coling/airtunes/src/modules/rtp/raop_client.h
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/coling/airtunes/src/modules/rtp/raop_client.h?rev=2394&root=pulseaudio&r1=2393&r2=2394&view=diff
==============================================================================
--- branches/coling/airtunes/src/modules/rtp/raop_client.h (original)
+++ branches/coling/airtunes/src/modules/rtp/raop_client.h Sun May 11 01:01:37 2008
@@ -26,18 +26,16 @@
 
 #include <pulse/mainloop-api.h>
 #include <pulsecore/iochannel.h>
+#include <pulsecore/memchunk.h>
 
 typedef struct pa_raop_client pa_raop_client;
 
-pa_raop_client* pa_raop_client_new(void);
+pa_raop_client* pa_raop_client_new(pa_mainloop_api *mainloop, const char* host);
 void pa_raop_client_free(pa_raop_client* c);
 
-int pa_raop_client_connect(pa_raop_client* c, pa_mainloop_api *mainloop, const char* host);
+pa_memchunk pa_raop_client_encode_sample(pa_raop_client* c, pa_mempool* mempool, pa_memchunk* raw);
 
-void pa_raop_client_disconnect(pa_raop_client* c);
-
-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);
+typedef void (*pa_raop_client_cb_t)(int fd, void *userdata);
+void pa_raop_client_set_callback(pa_raop_client* c, pa_raop_client_cb_t callback, void *userdata);
 
 #endif




More information about the pulseaudio-commits mailing list