static void do_work(connection *c) { connection_assert_ref(c); if (c->dead) return; if (pa_iochannel_is_readable(c->io)) if (do_read(c) < 0) goto fail; if (!c->sink_input && pa_iochannel_is_hungup(c->io)) goto fail; if (pa_iochannel_is_writable(c->io)) if (do_write(c) < 0) goto fail; return; fail: if (c->sink_input) { /* If there is a sink input, we first drain what we already have read before shutting down the connection */ c->dead = TRUE; pa_iochannel_free(c->io); c->io = NULL; pa_asyncmsgq_post(c->sink_input->sink->asyncmsgq, PA_MSGOBJECT(c->sink_input), SINK_INPUT_MESSAGE_DISABLE_PREBUF, NULL, 0, NULL, NULL); } else connection_unlink(c); }
static int do_write(struct userdata *u) { ssize_t r; pa_assert(u); if (!pa_iochannel_is_writable(u->io)) return 0; if (u->write_data) { pa_assert(u->write_index < u->write_length); if ((r = pa_iochannel_write(u->io, (uint8_t*) u->write_data + u->write_index, u->write_length - u->write_index)) <= 0) { pa_log("write() failed: %s", pa_cstrerror(errno)); return -1; } u->write_index += (size_t) r; pa_assert(u->write_index <= u->write_length); if (u->write_index == u->write_length) { pa_xfree(u->write_data); u->write_data = NULL; u->write_index = u->write_length = 0; } } if (!u->write_data && u->state == STATE_PREPARE) { int so_sndbuf = 0; socklen_t sl = sizeof(int); /* OK, we're done with sending all control data we need to, so * let's hand the socket over to the IO thread now */ pa_assert(u->fd < 0); u->fd = pa_iochannel_get_send_fd(u->io); pa_iochannel_set_noclose(u->io, TRUE); pa_iochannel_free(u->io); u->io = NULL; pa_make_tcp_socket_low_delay(u->fd); if (getsockopt(u->fd, SOL_SOCKET, SO_SNDBUF, &so_sndbuf, &sl) < 0) pa_log_warn("getsockopt(SO_SNDBUF) failed: %s", pa_cstrerror(errno)); else { pa_log_debug("SO_SNDBUF is %zu.", (size_t) so_sndbuf); pa_sink_set_max_request(u->sink, PA_MAX((size_t) so_sndbuf, u->block_size)); } pa_log_debug("Connection authenticated, handing fd to IO thread..."); pa_asyncmsgq_post(u->thread_mq.inq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_PASS_SOCKET, NULL, 0, NULL, NULL); u->state = STATE_RUNNING; } return 0; }
static void io_callback(pa_iochannel *io, void*userdata) { struct userdata *u = userdata; pa_assert(u); if (do_read(u) < 0 || do_write(u) < 0) { if (u->io) { pa_iochannel_free(u->io); u->io = NULL; } pa_module_unload_request(u->module, TRUE); } }
void pa__done(pa_module*m) { struct userdata *u; pa_assert(m); if (!(u = m->userdata)) return; if (u->sink) pa_sink_unlink(u->sink); if (u->thread) { pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL); pa_thread_free(u->thread); } pa_thread_mq_done(&u->thread_mq); if (u->sink) pa_sink_unref(u->sink); if (u->io) pa_iochannel_free(u->io); if (u->rtpoll_item) pa_rtpoll_item_free(u->rtpoll_item); if (u->rtpoll) pa_rtpoll_free(u->rtpoll); if (u->memchunk.memblock) pa_memblock_unref(u->memchunk.memblock); if (u->client) pa_socket_client_unref(u->client); pa_xfree(u->read_data); pa_xfree(u->write_data); if (u->smoother) pa_smoother_free(u->smoother); if (u->fd >= 0) pa_close(u->fd); pa_xfree(u); }
void pa_cli_protocol_connect(pa_cli_protocol *p, pa_iochannel *io, pa_module *m) { pa_cli *c; pa_assert(p); pa_assert(io); pa_assert(m); if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) { pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS); pa_iochannel_free(io); return; } c = pa_cli_new(p->core, io, m); pa_cli_set_eof_callback(c, cli_eof_cb, p); pa_idxset_put(p->connections, c, NULL); }
static void connection_unlink(connection *c) { pa_assert(c); if (!c->protocol) return; if (c->options) { pa_simple_options_unref(c->options); c->options = NULL; } if (c->sink_input) { pa_sink_input_unlink(c->sink_input); pa_sink_input_unref(c->sink_input); c->sink_input = NULL; } if (c->source_output) { pa_source_output_unlink(c->source_output); pa_source_output_unref(c->source_output); c->source_output = NULL; } if (c->client) { pa_client_free(c->client); c->client = NULL; } if (c->io) { pa_iochannel_free(c->io); c->io = NULL; } pa_idxset_remove_by_data(c->protocol->connections, c, NULL); c->protocol = NULL; connection_unref(c); }
void pa_simple_protocol_connect(pa_simple_protocol *p, pa_iochannel *io, pa_simple_options *o) { connection *c = NULL; char pname[128]; pa_client_new_data client_data; pa_assert(p); pa_assert(io); pa_assert(o); if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) { pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS); pa_iochannel_free(io); return; } c = pa_msgobject_new(connection); c->parent.parent.free = connection_free; c->parent.process_msg = connection_process_msg; c->io = io; pa_iochannel_set_callback(c->io, io_callback, c); c->sink_input = NULL; c->source_output = NULL; c->input_memblockq = c->output_memblockq = NULL; c->protocol = p; c->options = pa_simple_options_ref(o); c->playback.current_memblock = NULL; c->playback.memblock_index = 0; c->dead = false; c->playback.underrun = true; pa_atomic_store(&c->playback.missing, 0); pa_client_new_data_init(&client_data); client_data.module = o->module; client_data.driver = __FILE__; pa_iochannel_socket_peer_to_string(io, pname, sizeof(pname)); pa_proplist_setf(client_data.proplist, PA_PROP_APPLICATION_NAME, "Simple client (%s)", pname); pa_proplist_sets(client_data.proplist, "simple-protocol.peer", pname); c->client = pa_client_new(p->core, &client_data); pa_client_new_data_done(&client_data); if (!c->client) goto fail; c->client->kill = client_kill_cb; c->client->userdata = c; if (o->playback) { pa_sink_input_new_data data; pa_memchunk silence; size_t l; pa_sink *sink; if (!(sink = pa_namereg_get(c->protocol->core, o->default_sink, PA_NAMEREG_SINK))) { pa_log("Failed to get sink."); goto fail; } pa_sink_input_new_data_init(&data); data.driver = __FILE__; data.module = o->module; data.client = c->client; pa_sink_input_new_data_set_sink(&data, sink, false); pa_proplist_update(data.proplist, PA_UPDATE_MERGE, c->client->proplist); pa_sink_input_new_data_set_sample_spec(&data, &o->sample_spec); pa_sink_input_new(&c->sink_input, p->core, &data); pa_sink_input_new_data_done(&data); if (!c->sink_input) { pa_log("Failed to create sink input."); goto fail; } c->sink_input->parent.process_msg = sink_input_process_msg; c->sink_input->pop = sink_input_pop_cb; c->sink_input->process_rewind = sink_input_process_rewind_cb; c->sink_input->update_max_rewind = sink_input_update_max_rewind_cb; c->sink_input->kill = sink_input_kill_cb; c->sink_input->userdata = c; pa_sink_input_set_requested_latency(c->sink_input, DEFAULT_SINK_LATENCY); l = (size_t) ((double) pa_bytes_per_second(&o->sample_spec)*PLAYBACK_BUFFER_SECONDS); pa_sink_input_get_silence(c->sink_input, &silence); c->input_memblockq = pa_memblockq_new( "simple protocol connection input_memblockq", 0, l, l, &o->sample_spec, (size_t) -1, l/PLAYBACK_BUFFER_FRAGMENTS, 0, &silence); pa_memblock_unref(silence.memblock); pa_iochannel_socket_set_rcvbuf(io, l); pa_atomic_store(&c->playback.missing, (int) pa_memblockq_pop_missing(c->input_memblockq)); pa_sink_input_put(c->sink_input); } if (o->record) { pa_source_output_new_data data; size_t l; pa_source *source; if (!(source = pa_namereg_get(c->protocol->core, o->default_source, PA_NAMEREG_SOURCE))) { pa_log("Failed to get source."); goto fail; } pa_source_output_new_data_init(&data); data.driver = __FILE__; data.module = o->module; data.client = c->client; pa_source_output_new_data_set_source(&data, source, false); pa_proplist_update(data.proplist, PA_UPDATE_MERGE, c->client->proplist); pa_source_output_new_data_set_sample_spec(&data, &o->sample_spec); pa_source_output_new(&c->source_output, p->core, &data); pa_source_output_new_data_done(&data); if (!c->source_output) { pa_log("Failed to create source output."); goto fail; } c->source_output->push = source_output_push_cb; c->source_output->kill = source_output_kill_cb; c->source_output->get_latency = source_output_get_latency_cb; c->source_output->userdata = c; pa_source_output_set_requested_latency(c->source_output, DEFAULT_SOURCE_LATENCY); l = (size_t) (pa_bytes_per_second(&o->sample_spec)*RECORD_BUFFER_SECONDS); c->output_memblockq = pa_memblockq_new( "simple protocol connection output_memblockq", 0, l, 0, &o->sample_spec, 1, 0, 0, NULL); pa_iochannel_socket_set_sndbuf(io, l); pa_source_output_put(c->source_output); } pa_idxset_put(p->connections, c, NULL); return; fail: connection_unlink(c); }