/* Called from main context */ int pa_source_output_move_to(pa_source_output *o, pa_source *dest, pa_bool_t save) { int r; pa_source_output_assert_ref(o); pa_assert_ctl_context(); pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state)); pa_assert(o->source); pa_source_assert_ref(dest); if (dest == o->source) return 0; if (!pa_source_output_may_move_to(o, dest)) return -PA_ERR_NOTSUPPORTED; pa_source_output_ref(o); if ((r = pa_source_output_start_move(o)) < 0) { pa_source_output_unref(o); return r; } if ((r = pa_source_output_finish_move(o, dest, save)) < 0) { pa_source_output_fail_move(o); pa_source_output_unref(o); return r; } pa_source_output_unref(o); return 0; }
/* Called from main context */ void pa_source_output_unlink(pa_source_output*o) { pa_bool_t linked; pa_assert(o); pa_assert_ctl_context(); /* See pa_sink_unlink() for a couple of comments how this function * works */ pa_source_output_ref(o); linked = PA_SOURCE_OUTPUT_IS_LINKED(o->state); if (linked) pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK], o); if (o->direct_on_input) pa_idxset_remove_by_data(o->direct_on_input->direct_outputs, o, NULL); pa_idxset_remove_by_data(o->core->source_outputs, o, NULL); if (o->source) if (pa_idxset_remove_by_data(o->source->outputs, o, NULL)) pa_source_output_unref(o); if (o->client) pa_idxset_remove_by_data(o->client->source_outputs, o, NULL); update_n_corked(o, PA_SOURCE_OUTPUT_UNLINKED); o->state = PA_SOURCE_OUTPUT_UNLINKED; if (linked && o->source) if (o->source->asyncmsgq) pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_REMOVE_OUTPUT, o, 0, NULL) == 0); reset_callbacks(o); if (linked) { pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_REMOVE, o->index); pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST], o); } if (o->source) { pa_source_update_status(o->source); o->source = NULL; } pa_core_maybe_vacuum(o->core); pa_source_output_unref(o); }
/* Called from main context */ static void teardown(struct userdata *u) { pa_assert(u); pa_assert_ctl_context(); u->adjust_time = 0; enable_adjust_timer(u, false); /* Handling the asyncmsgq between the source output and the sink input * requires some care. When the source output is unlinked, nothing needs * to be done for the asyncmsgq, because the source output is the sending * end. But when the sink input is unlinked, we should ensure that the * asyncmsgq is emptied, because the messages in the queue hold references * to the sink input. Also, we need to ensure that new messages won't be * written to the queue after we have emptied it. * * Emptying the queue can be done in the state_changed() callback of the * sink input, when the new state is "unlinked". * * Preventing new messages from being written to the queue can be achieved * by unlinking the source output before unlinking the sink input. There * are no other writers for that queue, so this is sufficient. */ if (u->source_output) { pa_source_output_unlink(u->source_output); pa_source_output_unref(u->source_output); u->source_output = NULL; } if (u->sink_input) { pa_sink_input_unlink(u->sink_input); pa_sink_input_unref(u->sink_input); u->sink_input = NULL; } }
void pa__done(pa_module*m) { struct userdata *u; pa_assert(m); if (!(u = m->userdata)) return; /* See comments in source_output_kill_cb() above regarding * destruction order! */ if (u->source_output) pa_source_output_unlink(u->source_output); if (u->source) pa_source_unlink(u->source); if (u->source_output) pa_source_output_unref(u->source_output); if (u->source) pa_source_unref(u->source); pa_xfree(u); }
void pa__done(pa_module*m) { struct userdata *u; pa_assert(m); if (!(u = m->userdata)) return; if (u->sap_event) m->core->mainloop->time_free(u->sap_event); if (u->source_output) { pa_source_output_unlink(u->source_output); pa_source_output_unref(u->source_output); } pa_rtp_context_destroy(&u->rtp_context); pa_sap_send(&u->sap_context, 1); pa_sap_context_destroy(&u->sap_context); if (u->memblockq) pa_memblockq_free(u->memblockq); pa_xfree(u); }
/* Called from main context */ int pa_source_output_start_move(pa_source_output *o) { pa_source *origin; int r; pa_source_output_assert_ref(o); pa_assert_ctl_context(); pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state)); pa_assert(o->source); if (!pa_source_output_may_move(o)) return -PA_ERR_NOTSUPPORTED; if ((r = pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_START], o)) < 0) return r; origin = o->source; pa_idxset_remove_by_data(o->source->outputs, o, NULL); if (pa_source_output_get_state(o) == PA_SOURCE_OUTPUT_CORKED) pa_assert_se(origin->n_corked-- >= 1); pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_REMOVE_OUTPUT, o, 0, NULL) == 0); pa_source_update_status(o->source); o->source = NULL; pa_source_output_unref(o); return 0; }
/* Called from main context */ static void source_output_kill_cb(pa_source_output* o) { struct userdata *u; pa_source_output_assert_ref(o); pa_assert_se(u = o->userdata); pa_module_unload_request(u->module, true); pa_source_output_unlink(u->source_output); pa_source_output_unref(u->source_output); u->source_output = NULL; }
/* Called from main context */ static void cmtspeech_source_output_kill_cb(pa_source_output* o) { struct userdata *u; pa_source_output_assert_ref(o); pa_assert_se(u = o->userdata); pa_assert(u->source_output == o); pa_log_warn("Kill called for cmtspeech source output"); cmtspeech_trigger_unload(u); pa_source_output_unref(u->source_output); u->source_output = NULL; }
void cmtspeech_delete_source_output(struct userdata *u) { pa_assert(u); ENTER(); if (!u->source_output) { pa_log_warn("Delete called but no source output exists"); return; } pa_source_output_unlink(u->source_output); pa_source_output_unref(u->source_output); u->source_output = NULL; pa_log_info("cmtspeech source-output deleted"); }
/* Called from main context */ static void teardown(struct userdata *u) { pa_assert(u); pa_assert_ctl_context(); if (u->sink_input) pa_sink_input_unlink(u->sink_input); if (u->source_output) pa_source_output_unlink(u->source_output); if (u->sink_input) { pa_sink_input_unref(u->sink_input); u->sink_input = NULL; } if (u->source_output) { pa_source_output_unref(u->source_output); u->source_output = NULL; } }
/* Called from main thread */ static void source_output_kill_cb(pa_source_output *o) { struct userdata *u; pa_source_output_assert_ref(o); pa_assert_ctl_context(); pa_assert_se(u = o->userdata); /* The order here matters! We first kill the source output, followed * by the source. That means the source callbacks must be protected * against an unconnected source output! */ pa_source_output_unlink(u->source_output); pa_source_unlink(u->source); pa_source_output_unref(u->source_output); u->source_output = NULL; pa_source_unref(u->source); u->source = NULL; pa_module_unload_request(u->module, true); }
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); }
int pa__init(pa_module*m) { struct userdata *u; pa_modargs *ma = NULL; const char *dst_addr; const char *src_addr; uint32_t port = DEFAULT_PORT, mtu; uint32_t ttl = DEFAULT_TTL; sa_family_t af; int fd = -1, sap_fd = -1; pa_source *s; pa_sample_spec ss; pa_channel_map cm; struct sockaddr_in dst_sa4, dst_sap_sa4, src_sa4, src_sap_sa4; #ifdef HAVE_IPV6 struct sockaddr_in6 dst_sa6, dst_sap_sa6, src_sa6, src_sap_sa6; #endif struct sockaddr_storage sa_dst; pa_source_output *o = NULL; uint8_t payload; char *p; int r, j; socklen_t k; char hn[128], *n; pa_bool_t loop = FALSE; pa_source_output_new_data data; pa_assert(m); if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { pa_log("Failed to parse module arguments"); goto fail; } if (!(s = pa_namereg_get(m->core, pa_modargs_get_value(ma, "source", NULL), PA_NAMEREG_SOURCE))) { pa_log("Source does not exist."); goto fail; } if (pa_modargs_get_value_boolean(ma, "loop", &loop) < 0) { pa_log("Failed to parse \"loop\" parameter."); goto fail; } ss = s->sample_spec; pa_rtp_sample_spec_fixup(&ss); cm = s->channel_map; if (pa_modargs_get_sample_spec(ma, &ss) < 0) { pa_log("Failed to parse sample specification"); goto fail; } if (!pa_rtp_sample_spec_valid(&ss)) { pa_log("Specified sample type not compatible with RTP"); goto fail; } if (ss.channels != cm.channels) pa_channel_map_init_auto(&cm, ss.channels, PA_CHANNEL_MAP_AIFF); payload = pa_rtp_payload_from_sample_spec(&ss); mtu = (uint32_t) pa_frame_align(DEFAULT_MTU, &ss); if (pa_modargs_get_value_u32(ma, "mtu", &mtu) < 0 || mtu < 1 || mtu % pa_frame_size(&ss) != 0) { pa_log("Invalid MTU."); goto fail; } port = DEFAULT_PORT + ((uint32_t) (rand() % 512) << 1); if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) { pa_log("port= expects a numerical argument between 1 and 65535."); goto fail; } if (port & 1) pa_log_warn("Port number not even as suggested in RFC3550!"); if (pa_modargs_get_value_u32(ma, "ttl", &ttl) < 0 || ttl < 1 || ttl > 0xFF) { pa_log("ttl= expects a numerical argument between 1 and 255."); goto fail; } src_addr = pa_modargs_get_value(ma, "source_ip", DEFAULT_SOURCE_IP); if (inet_pton(AF_INET, src_addr, &src_sa4.sin_addr) > 0) { src_sa4.sin_family = af = AF_INET; src_sa4.sin_port = htons(0); src_sap_sa4 = src_sa4; #ifdef HAVE_IPV6 } else if (inet_pton(AF_INET6, src_addr, &src_sa6.sin6_addr) > 0) { src_sa6.sin6_family = af = AF_INET6; src_sa6.sin6_port = htons(0); src_sap_sa6 = src_sa6; #endif } else { pa_log("Invalid source address '%s'", src_addr); goto fail; } dst_addr = pa_modargs_get_value(ma, "destination", NULL); if (dst_addr == NULL) dst_addr = pa_modargs_get_value(ma, "destination_ip", DEFAULT_DESTINATION_IP); if (inet_pton(AF_INET, dst_addr, &dst_sa4.sin_addr) > 0) { dst_sa4.sin_family = af = AF_INET; dst_sa4.sin_port = htons((uint16_t) port); dst_sap_sa4 = dst_sa4; dst_sap_sa4.sin_port = htons(SAP_PORT); #ifdef HAVE_IPV6 } else if (inet_pton(AF_INET6, dst_addr, &dst_sa6.sin6_addr) > 0) { dst_sa6.sin6_family = af = AF_INET6; dst_sa6.sin6_port = htons((uint16_t) port); dst_sap_sa6 = dst_sa6; dst_sap_sa6.sin6_port = htons(SAP_PORT); #endif } else { pa_log("Invalid destination '%s'", dst_addr); goto fail; } if ((fd = pa_socket_cloexec(af, SOCK_DGRAM, 0)) < 0) { pa_log("socket() failed: %s", pa_cstrerror(errno)); goto fail; } if (af == AF_INET && bind(fd, (struct sockaddr*) &src_sa4, sizeof(src_sa4)) < 0) { pa_log("bind() failed: %s", pa_cstrerror(errno)); goto fail; #ifdef HAVE_IPV6 } else if (af == AF_INET6 && bind(fd, (struct sockaddr*) &src_sa6, sizeof(src_sa6)) < 0) { pa_log("bind() failed: %s", pa_cstrerror(errno)); goto fail; #endif } if (af == AF_INET && connect(fd, (struct sockaddr*) &dst_sa4, sizeof(dst_sa4)) < 0) { pa_log("connect() failed: %s", pa_cstrerror(errno)); goto fail; #ifdef HAVE_IPV6 } else if (af == AF_INET6 && connect(fd, (struct sockaddr*) &dst_sa6, sizeof(dst_sa6)) < 0) { pa_log("connect() failed: %s", pa_cstrerror(errno)); goto fail; #endif } if ((sap_fd = pa_socket_cloexec(af, SOCK_DGRAM, 0)) < 0) { pa_log("socket() failed: %s", pa_cstrerror(errno)); goto fail; } if (af == AF_INET && bind(sap_fd, (struct sockaddr*) &src_sap_sa4, sizeof(src_sap_sa4)) < 0) { pa_log("bind() failed: %s", pa_cstrerror(errno)); goto fail; #ifdef HAVE_IPV6 } else if (af == AF_INET6 && bind(sap_fd, (struct sockaddr*) &src_sap_sa6, sizeof(src_sap_sa6)) < 0) { pa_log("bind() failed: %s", pa_cstrerror(errno)); goto fail; #endif } if (af == AF_INET && connect(sap_fd, (struct sockaddr*) &dst_sap_sa4, sizeof(dst_sap_sa4)) < 0) { pa_log("connect() failed: %s", pa_cstrerror(errno)); goto fail; #ifdef HAVE_IPV6 } else if (af == AF_INET6 && connect(sap_fd, (struct sockaddr*) &dst_sap_sa6, sizeof(dst_sap_sa6)) < 0) { pa_log("connect() failed: %s", pa_cstrerror(errno)); goto fail; #endif } j = !!loop; if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &j, sizeof(j)) < 0 || setsockopt(sap_fd, IPPROTO_IP, IP_MULTICAST_LOOP, &j, sizeof(j)) < 0) { pa_log("IP_MULTICAST_LOOP failed: %s", pa_cstrerror(errno)); goto fail; } if (ttl != DEFAULT_TTL) { int _ttl = (int) ttl; if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &_ttl, sizeof(_ttl)) < 0) { pa_log("IP_MULTICAST_TTL failed: %s", pa_cstrerror(errno)); goto fail; } if (setsockopt(sap_fd, IPPROTO_IP, IP_MULTICAST_TTL, &_ttl, sizeof(_ttl)) < 0) { pa_log("IP_MULTICAST_TTL (sap) failed: %s", pa_cstrerror(errno)); goto fail; } } /* If the socket queue is full, let's drop packets */ pa_make_fd_nonblock(fd); pa_make_udp_socket_low_delay(fd); pa_source_output_new_data_init(&data); pa_proplist_sets(data.proplist, PA_PROP_MEDIA_NAME, "RTP Monitor Stream"); pa_proplist_sets(data.proplist, "rtp.source", src_addr); pa_proplist_sets(data.proplist, "rtp.destination", dst_addr); pa_proplist_setf(data.proplist, "rtp.mtu", "%lu", (unsigned long) mtu); pa_proplist_setf(data.proplist, "rtp.port", "%lu", (unsigned long) port); pa_proplist_setf(data.proplist, "rtp.ttl", "%lu", (unsigned long) ttl); data.driver = __FILE__; data.module = m; pa_source_output_new_data_set_source(&data, s, FALSE); pa_source_output_new_data_set_sample_spec(&data, &ss); pa_source_output_new_data_set_channel_map(&data, &cm); data.flags = PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND; pa_source_output_new(&o, m->core, &data); pa_source_output_new_data_done(&data); if (!o) { pa_log("failed to create source output."); goto fail; } o->parent.process_msg = source_output_process_msg; o->push = source_output_push; o->kill = source_output_kill; pa_log_info("Configured source latency of %llu ms.", (unsigned long long) pa_source_output_set_requested_latency(o, pa_bytes_to_usec(mtu, &o->sample_spec)) / PA_USEC_PER_MSEC); m->userdata = o->userdata = u = pa_xnew(struct userdata, 1); u->module = m; u->source_output = o; u->memblockq = pa_memblockq_new( "module-rtp-send memblockq", 0, MEMBLOCKQ_MAXLENGTH, MEMBLOCKQ_MAXLENGTH, &ss, 1, 0, 0, NULL); u->mtu = mtu; k = sizeof(sa_dst); pa_assert_se((r = getsockname(fd, (struct sockaddr*) &sa_dst, &k)) >= 0); n = pa_sprintf_malloc("PulseAudio RTP Stream on %s", pa_get_fqdn(hn, sizeof(hn))); if (af == AF_INET) { p = pa_sdp_build(af, (void*) &((struct sockaddr_in*) &sa_dst)->sin_addr, (void*) &dst_sa4.sin_addr, n, (uint16_t) port, payload, &ss); #ifdef HAVE_IPV6 } else { p = pa_sdp_build(af, (void*) &((struct sockaddr_in6*) &sa_dst)->sin6_addr, (void*) &dst_sa6.sin6_addr, n, (uint16_t) port, payload, &ss); #endif } pa_xfree(n); pa_rtp_context_init_send(&u->rtp_context, fd, m->core->cookie, payload, pa_frame_size(&ss)); pa_sap_context_init_send(&u->sap_context, sap_fd, p); pa_log_info("RTP stream initialized with mtu %u on %s:%u from %s ttl=%u, SSRC=0x%08x, payload=%u, initial sequence #%u", mtu, dst_addr, port, src_addr, ttl, u->rtp_context.ssrc, payload, u->rtp_context.sequence); pa_log_info("SDP-Data:\n%s\nEOF", p); pa_sap_send(&u->sap_context, 0); u->sap_event = pa_core_rttime_new(m->core, pa_rtclock_now() + SAP_INTERVAL, sap_event_cb, u); pa_source_output_put(u->source_output); pa_modargs_free(ma); return 0; fail: if (ma) pa_modargs_free(ma); if (fd >= 0) pa_close(fd); if (sap_fd >= 0) pa_close(sap_fd); if (o) { pa_source_output_unlink(o); pa_source_output_unref(o); } return -1; }
/*** Deallocate stuff ***/ void voice_clear_up(struct userdata *u) { pa_assert(u); if (u->mainloop_handler) { u->mainloop_handler->parent.free((pa_object *)u->mainloop_handler); u->mainloop_handler = NULL; } if (u->hw_sink_input) { pa_sink_input_unlink(u->hw_sink_input); pa_sink_input_unref(u->hw_sink_input); u->hw_sink_input = NULL; } if (u->raw_sink) { pa_sink_unlink(u->raw_sink); pa_sink_unref(u->raw_sink); u->raw_sink = NULL; } if (u->dl_memblockq) { pa_memblockq_free(u->dl_memblockq); u->dl_memblockq = NULL; } if (u->voip_sink) { pa_sink_unlink(u->voip_sink); pa_sink_unref(u->voip_sink); u->voip_sink = NULL; } if (u->hw_source_output) { pa_source_output_unlink(u->hw_source_output); pa_source_output_unref(u->hw_source_output); u->hw_source_output = NULL; } if (u->voip_source) { pa_source_unlink(u->voip_source); pa_source_unref(u->voip_source); u->voip_source = NULL; } if (u->raw_source) { pa_source_unlink(u->raw_source); pa_source_unref(u->raw_source); u->raw_source = NULL; } if (u->hw_source_memblockq) { pa_memblockq_free(u->hw_source_memblockq); u->hw_source_memblockq = NULL; } if (u->ul_memblockq) { pa_memblockq_free(u->ul_memblockq); u->ul_memblockq = NULL; } if (u->dl_sideinfo_queue) { pa_queue_free(u->dl_sideinfo_queue, NULL, u); u->dl_sideinfo_queue = NULL; } voice_aep_ear_ref_unload(u); if (u->aep_silence_memchunk.memblock) { pa_memblock_unref(u->aep_silence_memchunk.memblock); pa_memchunk_reset(&u->aep_silence_memchunk); } if (u->sink_temp_buff) { pa_xfree(u->sink_temp_buff); u->sink_temp_buff = NULL; } if (u->sink_subscription) { pa_subscription_free(u->sink_subscription); u->sink_subscription = NULL; } if (u->sink_proplist_changed_slot) { pa_hook_slot_free(u->sink_proplist_changed_slot); u->sink_proplist_changed_slot = NULL; } if (u->source_proplist_changed_slot) { pa_hook_slot_free(u->source_proplist_changed_slot); u->source_proplist_changed_slot = NULL; } voice_convert_free(u); voice_memchunk_pool_unload(u); voice_unload_event_forwarder(u); }