]> code.delx.au - pulseaudio/blobdiff - polyp/protocol-esound.c
Merge Pierre's changes
[pulseaudio] / polyp / protocol-esound.c
index 11a33315a7869428ba917d90b7fa0fc4280adf78..18ecb0ac1f69c276d458c1b429c6af3ba5c683b8 100644 (file)
@@ -63,6 +63,8 @@
 
 #define SCACHE_PREFIX "esound."
 
+#define PA_TYPEID_ESOUND PA_TYPEID_MAKE('E', 'S', 'D', 'P')
+
 /* This is heavily based on esound's code */
 
 struct connection {
@@ -237,11 +239,14 @@ static void* connection_write(struct connection *c, size_t length) {
     return (uint8_t*) c->write_data+i;
 }
 
-static void format_esd2native(int format, struct pa_sample_spec *ss) {
+static void format_esd2native(int format, int swap_bytes, struct pa_sample_spec *ss) {
     assert(ss);
 
     ss->channels = ((format & ESD_MASK_CHAN) == ESD_STEREO) ? 2 : 1;
-    ss->format = ((format & ESD_MASK_BITS) == ESD_BITS16) ? PA_SAMPLE_S16NE : PA_SAMPLE_U8;
+    if ((format & ESD_MASK_BITS) == ESD_BITS16)
+        ss->format = swap_bytes ? PA_SAMPLE_S16RE : PA_SAMPLE_S16NE;
+    else
+        ss->format = PA_SAMPLE_U8;
 }
 
 static int format_native2esd(struct pa_sample_spec *ss) {
@@ -267,8 +272,10 @@ static int esd_proto_connect(struct connection *c, esd_proto_t request, const vo
         }
 
         c->authorized = 1;
-        if (c->auth_timeout_event)
+        if (c->auth_timeout_event) {
             c->protocol->core->mainloop->time_free(c->auth_timeout_event);
+            c->auth_timeout_event = NULL;
+        }
     }
     
     ekey = *(uint32_t*)((uint8_t*) data+ESD_KEY_LEN);
@@ -299,13 +306,15 @@ static int esd_proto_stream_play(struct connection *c, esd_proto_t request, cons
     rate = maybe_swap_endian_32(c->swap_byte_order, *((int*)data + 1));
 
     ss.rate = rate;
-    format_esd2native(format, &ss);
+    format_esd2native(format, c->swap_byte_order, &ss);
 
-    if (!pa_sample_spec_valid(&ss))
+    if (!pa_sample_spec_valid(&ss)) {
+        pa_log(__FILE__": invalid sample specification\n");
         return -1;
+    }
 
     if (!(sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1))) {
-        pa_log(__FILE__": No output sink\n");
+        pa_log(__FILE__": no such sink\n");
         return -1;
     }
     
@@ -314,17 +323,17 @@ static int esd_proto_stream_play(struct connection *c, esd_proto_t request, cons
 
     pa_client_set_name(c->client, name);
 
-    assert(!c->input_memblockq);
+    assert(!c->sink_input && !c->input_memblockq);
+
+    if (!(c->sink_input = pa_sink_input_new(sink, PA_TYPEID_ESOUND, name, &ss, 0, -1))) {
+        pa_log(__FILE__": failed to create sink input.\n");
+        return -1;
+    }
 
     l = (size_t) (pa_bytes_per_second(&ss)*PLAYBACK_BUFFER_SECONDS); 
     c->input_memblockq = pa_memblockq_new(l, 0, pa_frame_size(&ss), l/2, l/PLAYBACK_BUFFER_FRAGMENTS, c->protocol->core->memblock_stat);
-    assert(c->input_memblockq);
     pa_iochannel_socket_set_rcvbuf(c->io, l/PLAYBACK_BUFFER_FRAGMENTS*2);
     c->playback.fragment_size = l/10;
-    
-    assert(!c->sink_input);
-    c->sink_input = pa_sink_input_new(sink, name, &ss, 0, -1);
-    assert(c->sink_input);
 
     c->sink_input->owner = c->protocol->module;
     c->sink_input->client = c->client;
@@ -353,24 +362,32 @@ static int esd_proto_stream_record(struct connection *c, esd_proto_t request, co
     rate = maybe_swap_endian_32(c->swap_byte_order, *((int*)data + 1));
 
     ss.rate = rate;
-    format_esd2native(format, &ss);
+    format_esd2native(format, c->swap_byte_order, &ss);
 
-    if (!pa_sample_spec_valid(&ss))
+    if (!pa_sample_spec_valid(&ss)) {
+        pa_log(__FILE__": invalid sample specification.\n");
         return -1;
+    }
 
     if (request == ESD_PROTO_STREAM_MON) {
         struct pa_sink* sink;
 
-        if (!(sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1)))
+        if (!(sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1))) {
+            pa_log(__FILE__": no such sink.\n");
             return -1;
+        }
 
-        if (!(source = sink->monitor_source))
+        if (!(source = sink->monitor_source)) {
+            pa_log(__FILE__": no such monitor source.\n");
             return -1;
+        }
     } else {
         assert(request == ESD_PROTO_STREAM_REC);
         
-        if (!(source = pa_namereg_get(c->protocol->core, c->protocol->source_name, PA_NAMEREG_SOURCE, 1)))
+        if (!(source = pa_namereg_get(c->protocol->core, c->protocol->source_name, PA_NAMEREG_SOURCE, 1))) {
+            pa_log(__FILE__": no such source.\n");
             return -1;
+        }
     }
     
     strncpy(name, (char*) data + sizeof(int)*2, sizeof(name));
@@ -378,17 +395,17 @@ static int esd_proto_stream_record(struct connection *c, esd_proto_t request, co
 
     pa_client_set_name(c->client, name);
 
-    assert(!c->output_memblockq);
+    assert(!c->output_memblockq && !c->source_output);
+
+    if (!(c->source_output = pa_source_output_new(source, PA_TYPEID_ESOUND, name, &ss, -1))) {
+        pa_log(__FILE__": failed to create source output\n");
+        return -1;
+    }
 
     l = (size_t) (pa_bytes_per_second(&ss)*RECORD_BUFFER_SECONDS); 
     c->output_memblockq = pa_memblockq_new(l, 0, pa_frame_size(&ss), 0, 0, c->protocol->core->memblock_stat);
-    assert(c->output_memblockq);
     pa_iochannel_socket_set_sndbuf(c->io, l/RECORD_BUFFER_FRAGMENTS*2);
     
-    assert(!c->source_output);
-    c->source_output = pa_source_output_new(source, name, &ss, -1);
-    assert(c->source_output);
-    
     c->source_output->owner = c->protocol->module;
     c->source_output->client = c->client;
     c->source_output->push = source_output_push_cb;
@@ -412,7 +429,6 @@ static int esd_proto_get_latency(struct connection *c, esd_proto_t request, cons
         latency = 0;
     else {
         double usec = pa_sink_get_latency(sink);
-        usec += PLAYBACK_BUFFER_SECONDS*1000000;          /* A better estimation would be a good idea! */
         latency = (int) ((usec*44100)/1000000);
     }
     
@@ -589,7 +605,7 @@ static int esd_proto_sample_cache(struct connection *c, esd_proto_t request, con
     rate = maybe_swap_endian_32(c->swap_byte_order, *((int*)data + 1));
     
     ss.rate = rate;
-    format_esd2native(format, &ss);
+    format_esd2native(format, c->swap_byte_order, &ss);
 
     sc_length = (size_t) maybe_swap_endian_32(c->swap_byte_order, (*((int*)data + 2)));
 
@@ -700,7 +716,8 @@ static int do_read(struct connection *c) {
         assert(c->read_data_length < sizeof(c->request));
 
         if ((r = pa_iochannel_read(c->io, ((uint8_t*) &c->request) + c->read_data_length, sizeof(c->request) - c->read_data_length)) <= 0) {
-            pa_log(__FILE__": read() failed: %s\n", r == 0 ? "EOF" : strerror(errno));
+            if (r != 0)
+                pa_log_warn(__FILE__": read() failed: %s\n", strerror(errno));
             return -1;
         }
 
@@ -749,7 +766,8 @@ static int do_read(struct connection *c) {
         assert(c->read_data && c->read_data_length < handler->data_length);
 
         if ((r = pa_iochannel_read(c->io, (uint8_t*) c->read_data + c->read_data_length, handler->data_length - c->read_data_length)) <= 0) {
-            pa_log(__FILE__": read() failed: %s\n", r == 0 ? "EOF" : strerror(errno));
+            if (r != 0)
+                pa_log_warn(__FILE__": read() failed: %s\n", strerror(errno));
             return -1;
         }
 
@@ -769,7 +787,8 @@ static int do_read(struct connection *c) {
         assert(c->scache.memchunk.memblock && c->scache.name && c->scache.memchunk.index < c->scache.memchunk.length);
         
         if ((r = pa_iochannel_read(c->io, (uint8_t*) c->scache.memchunk.memblock->data+c->scache.memchunk.index, c->scache.memchunk.length-c->scache.memchunk.index)) <= 0) {
-            pa_log(__FILE__": read() failed: %s\n", r == 0 ? "EOF" : strerror(errno));
+            if (r!= 0)
+                pa_log_warn(__FILE__": read() failed: %s\n", strerror(errno));
             return -1;
         }
 
@@ -826,11 +845,12 @@ static int do_read(struct connection *c) {
         }
 
         if ((r = pa_iochannel_read(c->io, (uint8_t*) c->playback.current_memblock->data+c->playback.memblock_index, l)) <= 0) {
-            pa_log(__FILE__": read() failed: %s\n", r == 0 ? "EOF" : strerror(errno));
+            if (r != 0)
+                pa_log(__FILE__": read() failed: %s\n", strerror(errno));
             return -1;
         }
-
-/*         pa_log(__FILE__": read %u\n", r); */
+        
+/*         pa_log(__FILE__": read %u\n", r);  */
         
         chunk.memblock = c->playback.current_memblock;
         chunk.index = c->playback.memblock_index;
@@ -880,7 +900,7 @@ static int do_write(struct connection *c) {
             pa_log(__FILE__": write(): %s\n", strerror(errno));
             return -1;
         }
-    
+
         pa_memblockq_drop(c->output_memblockq, &chunk, r);
         pa_memblock_unref(chunk.memblock);
     }
@@ -894,18 +914,21 @@ static void do_work(struct connection *c) {
     assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
     c->protocol->core->mainloop->defer_enable(c->defer_event, 0);
 
-/*     pa_log("DOWORK\n");  */
-    
-    if (c->dead || !c->io)
-        return;
+/*     pa_log("DOWORK %i\n", pa_iochannel_is_hungup(c->io));   */
 
-    if (pa_iochannel_is_readable(c->io))
+    if (!c->dead && pa_iochannel_is_readable(c->io))
         if (do_read(c) < 0)
             goto fail;
-    
-    if (pa_iochannel_is_writable(c->io))
+
+    if (!c->dead && pa_iochannel_is_writable(c->io))
         if (do_write(c) < 0)
             goto fail;
+
+    /* In case the line was hungup, make sure to rerun this function
+       as soon as possible, until all data has been read. */
+
+    if (!c->dead && pa_iochannel_is_hungup(c->io))
+        c->protocol->core->mainloop->defer_enable(c->defer_event, 1);
     
     return;
 
@@ -1043,7 +1066,7 @@ static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, vo
 
     pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
     assert(p->core);
-    c->client = pa_client_new(p->core, "ESOUND", cname);
+    c->client = pa_client_new(p->core, PA_TYPEID_ESOUND, cname);
     assert(c->client);
     c->client->owner = p->module;
     c->client->kill = client_kill_cb;
@@ -1078,7 +1101,7 @@ static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, vo
 
     if (!c->authorized) {
         struct timeval tv;
-        gettimeofday(&tv, NULL);
+        pa_gettimeofday(&tv);
         tv.tv_sec += AUTH_TIMEOUT;
         c->auth_timeout_event = p->core->mainloop->time_new(p->core->mainloop, &tv, auth_timeout, c);
     } else