static struct service *get_service(struct userdata *u, pa_object *device) { struct service *s; char *hn, *un; const char *n; pa_assert(u); pa_object_assert_ref(device); if ((s = pa_hashmap_get(u->services, device))) return s; s = pa_xnew0(struct service, 1); s->userdata = u; s->device = device; if (pa_sink_isinstance(device)) { if (!(n = pa_proplist_gets(PA_SINK(device)->proplist, PA_PROP_DEVICE_DESCRIPTION))) n = PA_SINK(device)->name; } else { if (!(n = pa_proplist_gets(PA_SOURCE(device)->proplist, PA_PROP_DEVICE_DESCRIPTION))) n = PA_SOURCE(device)->name; } hn = pa_get_host_name_malloc(); un = pa_get_user_name_malloc(); s->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s: %s", un, hn, n), kDNSServiceMaxDomainName-1); pa_xfree(un); pa_xfree(hn); pa_hashmap_put(u->services, s->device, s); return s; }
static int publish_all_services(struct userdata *u) { pa_sink *sink; pa_source *source; int r = -1; uint32_t idx; pa_assert(u); pa_log_debug("Publishing services in Zeroconf"); for (sink = PA_SINK(pa_idxset_first(u->core->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(u->core->sinks, &idx))) if (!shall_ignore(PA_OBJECT(sink))) publish_service(get_service(u, PA_OBJECT(sink))); for (source = PA_SOURCE(pa_idxset_first(u->core->sources, &idx)); source; source = PA_SOURCE(pa_idxset_next(u->core->sources, &idx))) if (!shall_ignore(PA_OBJECT(source))) publish_service(get_service(u, PA_OBJECT(source))); if (publish_main_service(u) < 0) goto fail; r = 0; fail: return r; }
static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SINK(o)->userdata; switch (code) { case PA_SINK_MESSAGE_GET_LATENCY: *((pa_usec_t*) data) = sink_get_latency(u, &PA_SINK(o)->sample_spec); return 0; case PA_SINK_MESSAGE_SET_STATE: switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) { case PA_SINK_SUSPENDED: pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state)); pa_smoother_pause(u->smoother, pa_rtclock_now()); if (!u->source || u->source_suspended) { if (suspend(u) < 0) return -1; } u->sink_suspended = true; break; case PA_SINK_IDLE: case PA_SINK_RUNNING: if (u->sink->thread_info.state == PA_SINK_SUSPENDED) { pa_smoother_resume(u->smoother, pa_rtclock_now(), true); if (!u->source || u->source_suspended) { if (unsuspend(u) < 0) return -1; u->sink->get_volume(u->sink); u->sink->get_mute(u->sink); } u->sink_suspended = false; } break; case PA_SINK_INVALID_STATE: case PA_SINK_UNLINKED: case PA_SINK_INIT: ; } break; } return pa_sink_process_msg(o, code, data, offset, chunk); }
static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SINK(o)->userdata; switch (code) { case PA_SINK_MESSAGE_SET_STATE: if (PA_PTR_TO_UINT(data) == PA_SINK_RUNNING) pa_rtclock_get(&u->timestamp); break; case PA_SINK_MESSAGE_GET_LATENCY: { struct timeval now; pa_rtclock_get(&now); if (pa_timeval_cmp(&u->timestamp, &now) > 0) *((pa_usec_t*) data) = 0; else *((pa_usec_t*) data) = pa_timeval_diff(&u->timestamp, &now); break; } } return pa_sink_process_msg(o, code, data, offset, chunk); }
/* Called from I/O thread context */ static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SINK(o)->userdata; switch (code) { case PA_SINK_MESSAGE_GET_LATENCY: /* The sink is _put() before the sink input is, so let's * make sure we don't access it in that time. Also, the * sink input is first shut down, the sink second. */ if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) || !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) { *((pa_usec_t*) data) = 0; return 0; } *((pa_usec_t*) data) = /* Get the latency of the master sink */ pa_sink_get_latency_within_thread(u->sink_input->sink) + /* Add the latency internal to our sink input on top */ pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec); return 0; } return pa_sink_process_msg(o, code, data, offset, chunk); }
static int sink_process_msg(pa_msgobject * o, int code, void *data, int64_t offset, pa_memchunk * chunk) { int r; struct userdata *u = PA_SINK(o)->userdata; int state; switch (code) { case PA_SINK_MESSAGE_SET_STATE: state = PA_PTR_TO_UINT(data); r = pa_sink_process_msg(o, code, data, offset, chunk); if (r >= 0) { pa_log("sink cork req state =%d, now state=%d\n", state, (int) (u->sink->state)); } return r; case PA_SINK_MESSAGE_GET_LATENCY: { size_t n = 0; n += u->memchunk_sink.length; *((pa_usec_t *) data) = pa_bytes_to_usec(n, &u->sink->sample_spec); return 0; } } return pa_sink_process_msg(o, code, data, offset, chunk); }
/* Runs in PA mainloop context */ static void get_service_data(struct service *s, pa_object *device) { pa_assert(s); if (pa_sink_isinstance(device)) { pa_sink *sink = PA_SINK(device); s->is_sink = true; s->service_type = SERVICE_TYPE_SINK; s->ss = sink->sample_spec; s->cm = sink->channel_map; s->name = pa_xstrdup(sink->name); s->proplist = pa_proplist_copy(sink->proplist); s->subtype = sink->flags & PA_SINK_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL; } else if (pa_source_isinstance(device)) { pa_source *source = PA_SOURCE(device); s->is_sink = false; s->service_type = SERVICE_TYPE_SOURCE; s->ss = source->sample_spec; s->cm = source->channel_map; s->name = pa_xstrdup(source->name); s->proplist = pa_proplist_copy(source->proplist); s->subtype = source->monitor_of ? SUBTYPE_MONITOR : (source->flags & PA_SOURCE_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL); } else pa_assert_not_reached(); }
static int sink_process_msg( pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SINK(o)->userdata; switch (code) { case PA_SINK_MESSAGE_SET_STATE: pa_log("mesg set state"); if (PA_PTR_TO_INT(data) == PA_SINK_RUNNING) u->timestamp = pa_rtclock_now(); break; case PA_SINK_MESSAGE_GET_LATENCY: { pa_usec_t now; pa_log("mesg get latency"); now = pa_rtclock_now(); *((pa_usec_t*)data) = u->timestamp > now ? u->timestamp - now : 0; return 0; } } pa_log("sink message: %u", code); return pa_sink_process_msg(o, code, data, offset, chunk); }
static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SINK(o)->userdata; switch (code) { case PA_SINK_MESSAGE_GET_LATENCY: { pa_usec_t w, r; r = pa_smoother_get(u->smoother, pa_rtclock_now()); w = pa_bytes_to_usec((uint64_t) u->offset + u->memchunk.length, &u->sink->sample_spec); *((int64_t*) data) = (int64_t)w - r; return 0; } case SINK_MESSAGE_PASS_SOCKET: { struct pollfd *pollfd; pa_assert(!u->rtpoll_item); u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1); pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL); pollfd->fd = u->fd; pollfd->events = pollfd->revents = 0; return 0; } } return pa_sink_process_msg(o, code, data, offset, chunk); }
static int sink_process_msg( pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SINK(o)->userdata; switch (code) { case PA_SINK_MESSAGE_SET_STATE: if (pa_sink_get_state(u->sink) == PA_SINK_SUSPENDED || pa_sink_get_state(u->sink) == PA_SINK_INIT) { if (PA_PTR_TO_UINT(data) == PA_SINK_RUNNING || PA_PTR_TO_UINT(data) == PA_SINK_IDLE) u->timestamp = pa_rtclock_now(); } break; case PA_SINK_MESSAGE_GET_LATENCY: { pa_usec_t now; now = pa_rtclock_now(); *((int64_t*) data) = (int64_t)u->timestamp - (int64_t)now; return 0; } } return pa_sink_process_msg(o, code, data, offset, chunk); }
/* Called from I/O thread context */ static int raw_sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SINK(o)->userdata; if (!u->master_sink) return -1; switch (code) { case PA_SINK_MESSAGE_GET_LATENCY: { pa_usec_t usec = 0; if (PA_MSGOBJECT(u->master_sink)->process_msg(PA_MSGOBJECT(u->master_sink), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0) usec = 0; *((pa_usec_t*) data) = usec; return 0; } case PA_SINK_MESSAGE_ADD_INPUT: { pa_sink_input *i = PA_SINK_INPUT(data); if (i == u->hw_sink_input) { pa_log_error("Denied loop connection"); // TODO: This does not work... we should do something more deny connection.. return -1; } break; } } return pa_sink_process_msg(o, code, data, offset, chunk); }
static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struct userdata *u) { struct device_info *d; pa_source *source; pa_sink *sink; pa_assert(c); pa_object_assert_ref(o); pa_assert(u); source = pa_source_isinstance(o) ? PA_SOURCE(o) : NULL; sink = pa_sink_isinstance(o) ? PA_SINK(o) : NULL; /* Never suspend monitors */ if (source && source->monitor_of) return PA_HOOK_OK; pa_assert(source || sink); d = pa_xnew(struct device_info, 1); d->userdata = u; d->source = source ? pa_source_ref(source) : NULL; d->sink = sink ? pa_sink_ref(sink) : NULL; d->time_event = pa_core_rttime_new(c, PA_USEC_INVALID, timeout_cb, d); pa_hashmap_put(u->device_infos, o, d); if ((d->sink && pa_sink_check_suspend(d->sink) <= 0) || (d->source && pa_source_check_suspend(d->source) <= 0)) restart(d); return PA_HOOK_OK; }
static pa_hook_result_t device_state_changed_hook_cb(pa_core *c, pa_object *o, struct userdata *u) { struct device_info *d; pa_assert(c); pa_object_assert_ref(o); pa_assert(u); if (!(d = pa_hashmap_get(u->device_infos, o))) return PA_HOOK_OK; if (pa_sink_isinstance(o)) { pa_sink *s = PA_SINK(o); pa_sink_state_t state = pa_sink_get_state(s); if (pa_sink_check_suspend(s) <= 0) if (PA_SINK_IS_OPENED(state)) restart(d); } else if (pa_source_isinstance(o)) { pa_source *s = PA_SOURCE(o); pa_source_state_t state = pa_source_get_state(s); if (pa_source_check_suspend(s) <= 0) if (PA_SOURCE_IS_OPENED(state)) restart(d); } return PA_HOOK_OK; }
static void get_service_data(struct service *s, pa_sample_spec *ret_ss, pa_channel_map *ret_map, const char **ret_name, pa_proplist **ret_proplist, enum service_subtype *ret_subtype) { pa_assert(s); pa_assert(ret_ss); pa_assert(ret_proplist); pa_assert(ret_subtype); if (pa_sink_isinstance(s->device)) { pa_sink *sink = PA_SINK(s->device); *ret_ss = sink->sample_spec; *ret_map = sink->channel_map; *ret_name = sink->name; *ret_proplist = sink->proplist; *ret_subtype = sink->flags & PA_SINK_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL; } else if (pa_source_isinstance(s->device)) { pa_source *source = PA_SOURCE(s->device); *ret_ss = source->sample_spec; *ret_map = source->channel_map; *ret_name = source->name; *ret_proplist = source->proplist; *ret_subtype = source->monitor_of ? SUBTYPE_MONITOR : (source->flags & PA_SOURCE_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL); } else pa_assert_not_reached(); }
static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SINK(o)->userdata; switch (code) { case PA_SINK_MESSAGE_SET_STATE: switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) { case PA_SINK_SUSPENDED: pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state)); pa_smoother_pause(u->smoother, pa_rtclock_now()); break; case PA_SINK_IDLE: case PA_SINK_RUNNING: if (u->sink->thread_info.state == PA_SINK_SUSPENDED) pa_smoother_resume(u->smoother, pa_rtclock_now(), TRUE); break; case PA_SINK_UNLINKED: case PA_SINK_INIT: case PA_SINK_INVALID_STATE: ; } break; case PA_SINK_MESSAGE_GET_LATENCY: { pa_usec_t w, r; r = pa_smoother_get(u->smoother, pa_rtclock_now()); w = pa_bytes_to_usec((uint64_t) u->offset + u->memchunk.length, &u->sink->sample_spec); *((pa_usec_t*) data) = w > r ? w - r : 0; return 0; } case SINK_MESSAGE_PASS_SOCKET: { struct pollfd *pollfd; pa_assert(!u->rtpoll_item); u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1); pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL); pollfd->fd = u->fd; pollfd->events = pollfd->revents = 0; return 0; } } return pa_sink_process_msg(o, code, data, offset, chunk); }
static int publish_all_services(struct userdata *u) { pa_sink *sink; pa_source *source; uint32_t idx; pa_assert(u); pa_log_debug("Publishing services in Bonjour"); for (sink = PA_SINK(pa_idxset_first(u->core->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(u->core->sinks, &idx))) if (!shall_ignore(PA_OBJECT(sink))) publish_service(get_service(u, PA_OBJECT(sink))); for (source = PA_SOURCE(pa_idxset_first(u->core->sources, &idx)); source; source = PA_SOURCE(pa_idxset_next(u->core->sources, &idx))) if (!shall_ignore(PA_OBJECT(source))) publish_service(get_service(u, PA_OBJECT(source))); return publish_main_service(u); }
static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SINK(o)->userdata; switch (code) { case PA_SINK_MESSAGE_GET_LATENCY: { int negative; pa_usec_t remote_latency; if (!PA_SINK_IS_LINKED(u->sink->thread_info.state)) { *((pa_usec_t*) data) = 0; return 0; } if (!u->stream) { *((pa_usec_t*) data) = 0; return 0; } if (pa_stream_get_state(u->stream) != PA_STREAM_READY) { *((pa_usec_t*) data) = 0; return 0; } if (pa_stream_get_latency(u->stream, &remote_latency, &negative) < 0) { *((pa_usec_t*) data) = 0; return 0; } *((pa_usec_t*) data) = remote_latency; return 0; } case PA_SINK_MESSAGE_SET_STATE: if (!u->stream || pa_stream_get_state(u->stream) != PA_STREAM_READY) break; switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) { case PA_SINK_SUSPENDED: { cork_stream(u, true); break; } case PA_SINK_IDLE: case PA_SINK_RUNNING: { cork_stream(u, false); break; } case PA_SINK_INVALID_STATE: case PA_SINK_INIT: case PA_SINK_UNLINKED: break; } break; } return pa_sink_process_msg(o, code, data, offset, chunk); }
static bool shall_ignore(pa_object *o) { pa_object_assert_ref(o); if (pa_sink_isinstance(o)) return !!(PA_SINK(o)->flags & PA_SINK_NETWORK); if (pa_source_isinstance(o)) return PA_SOURCE(o)->monitor_of || (PA_SOURCE(o)->flags & PA_SOURCE_NETWORK); pa_assert_not_reached(); }
static pa_hook_result_t sink_state_changed_cb(pa_core *c, pa_object *o, struct pa_policy_activity_variable *var) { pa_sink *sink; pa_assert(c); pa_object_assert_ref(o); pa_assert(var); if (pa_sink_isinstance(o)) { sink = PA_SINK(o); perform_activity_action(sink, var, var->default_state); } return PA_HOOK_OK; }
static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struct userdata *u) { struct device_info *d; pa_source *source; pa_sink *sink; const char *timeout_str; int32_t timeout; bool timeout_valid; pa_assert(c); pa_object_assert_ref(o); pa_assert(u); source = pa_source_isinstance(o) ? PA_SOURCE(o) : NULL; sink = pa_sink_isinstance(o) ? PA_SINK(o) : NULL; /* Never suspend monitors */ if (source && source->monitor_of) return PA_HOOK_OK; pa_assert(source || sink); timeout_str = pa_proplist_gets(sink ? sink->proplist : source->proplist, "module-suspend-on-idle.timeout"); if (timeout_str && pa_atoi(timeout_str, &timeout) >= 0) timeout_valid = true; else timeout_valid = false; if (timeout_valid && timeout < 0) return PA_HOOK_OK; d = pa_xnew(struct device_info, 1); d->userdata = u; d->source = source ? pa_source_ref(source) : NULL; d->sink = sink ? pa_sink_ref(sink) : NULL; d->time_event = pa_core_rttime_new(c, PA_USEC_INVALID, timeout_cb, d); if (timeout_valid) d->timeout = timeout * PA_USEC_PER_SEC; else d->timeout = d->userdata->timeout; pa_hashmap_put(u->device_infos, o, d); if ((d->sink && pa_sink_check_suspend(d->sink) <= 0) || (d->source && pa_source_check_suspend(d->source) <= 0)) restart(d); return PA_HOOK_OK; }
static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SINK(o)->userdata; switch (code) { case PA_SINK_MESSAGE_GET_LATENCY: { size_t n = 0; n += u->memchunk.length; *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec); return 0; } } return pa_sink_process_msg(o, code, data, offset, chunk); }
static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct context *context = PA_SINK(o)->userdata; pa_usec_t now; long latency; pa_log_debug("sink_process_msg: code %d", code); switch (code) { case PA_SINK_MESSAGE_SET_VOLUME: /* 3 */ break; case PA_SINK_MESSAGE_SET_MUTE: /* 6 */ break; case PA_SINK_MESSAGE_GET_LATENCY: /* 7 */ now = pa_rtclock_now(); latency = context->timestamp > now ? context->timestamp - now : 0ULL; pa_log_debug("sink_process_msg: latency %ld", latency); *((pa_usec_t*) data) = latency; return 0; case PA_SINK_MESSAGE_GET_REQUESTED_LATENCY: /* 8 */ break; case PA_SINK_MESSAGE_SET_STATE: /* 9 */ if (PA_PTR_TO_UINT(data) == PA_SINK_RUNNING) /* 0 */ { pa_log("sink_process_msg: running"); context->timestamp = pa_rtclock_now(); } else { pa_log("sink_process_msg: not running"); close_send(context); } break; } return pa_sink_process_msg(o, code, data, offset, chunk); }
static void sink_subscribe_cb(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) { struct userdata *u = (struct userdata *)userdata; if (t == PA_SUBSCRIPTION_EVENT_CHANGE) { pa_sink *sink = PA_SINK(pa_idxset_get_by_index(c->sinks, idx)); if (sink) { if (u->master_sink == sink) { const pa_cvolume *vol = pa_sink_get_volume(sink, 0, 0); int aep_step; u->linear_q15_master_volume_L = lrint(pa_sw_volume_to_linear(vol->values[0]) * 32767.0); if (vol->channels == 1) u->linear_q15_master_volume_R = lrint(pa_sw_volume_to_linear(vol->values[0]) * 32767.0); else u->linear_q15_master_volume_R = lrint(pa_sw_volume_to_linear(vol->values[1]) * 32767.0); aep_step = voice_pa_vol_to_aep_step(u, vol->values[0]); if (voice_cmt_ul_is_active_iothread(u) || (u->voip_source && PA_SOURCE_IS_LINKED(u->voip_source->state) && pa_source_used_by(u->voip_source))) { if (aep_step <= 0) { voice_update_aep_volume(0); voice_update_sidetone_gain(0); } else { voice_update_aep_volume(aep_step - 1); voice_update_sidetone_gain(aep_step - 1); } } xprot_change_volume(u->xprot, u->linear_q15_master_volume_L, u->linear_q15_master_volume_R); } } } }
static int process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u; if (pa_sink_isinstance(o)) { u = PA_SINK(o)->userdata; switch (code) { case PA_SINK_MESSAGE_GET_LATENCY: { pa_usec_t r = 0; if (u->hwo) r = sink_get_latency(u); *((pa_usec_t*) data) = r; return 0; } } return pa_sink_process_msg(o, code, data, offset, chunk); } if (pa_source_isinstance(o)) { u = PA_SOURCE(o)->userdata; switch (code) { case PA_SOURCE_MESSAGE_GET_LATENCY: { pa_usec_t r = 0; if (u->hwi) r = source_get_latency(u); *((pa_usec_t*) data) = r; return 0; } } return pa_source_process_msg(o, code, data, offset, chunk); } return -1; }
/* Called from I/O thread context */ static int voip_sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SINK(o)->userdata; switch (code) { case VOICE_SINK_GET_SIDE_INFO_QUEUE_PTR: { /* TODO: Make sure there is only one client (or multiple queues) */ if (!u->dl_sideinfo_queue) { pa_log_warn("Side info queue not set"); } *((pa_queue **) data) = u->dl_sideinfo_queue; pa_log_debug("Side info queue (%p) passed to client", (void *) u->dl_sideinfo_queue); return 0; } case PA_SINK_MESSAGE_GET_LATENCY: { pa_usec_t usec = 0; if (PA_MSGOBJECT(u->raw_sink)->process_msg(PA_MSGOBJECT(u->raw_sink), PA_SINK_MESSAGE_GET_LATENCY, &usec, (int64_t)0, NULL) < 0) usec = 0; *((pa_usec_t*) data) = usec; return 0; } case PA_SINK_MESSAGE_ADD_INPUT: { pa_sink_input *i = PA_SINK_INPUT(data); if (i == u->hw_sink_input) { pa_log_error("Denied loop connection"); // TODO: How to deny connection... return -1; } // Pass trough to pa_sink_process_msg break; } } return pa_sink_process_msg(o, code, data, offset, chunk); }
/* Called from I/O thread context */ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SINK(o)->userdata; switch (code) { case PA_SINK_MESSAGE_GET_LATENCY: /* The sink is _put() before the sink input is, so let's * make sure we don't access it yet */ if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) || !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) { *((int64_t*) data) = 0; return 0; } *((int64_t*) data) = /* Get the latency of the master sink */ pa_sink_get_latency_within_thread(u->sink_input->sink, true) + /* Add the latency internal to our sink input on top */ pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec); return 0; case PA_SINK_MESSAGE_SET_STATE: { pa_sink_state_t new_state = (pa_sink_state_t) PA_PTR_TO_UINT(data); /* When set to running or idle for the first time, request a rewind * of the master sink to make sure we are heard immediately */ if ((new_state == PA_SINK_IDLE || new_state == PA_SINK_RUNNING) && u->sink->thread_info.state == PA_SINK_INIT) { pa_log_debug("Requesting rewind due to state change."); pa_sink_input_request_rewind(u->sink_input, 0, false, true, true); } break; } } return pa_sink_process_msg(o, code, data, offset, chunk); }
static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SINK(o)->userdata; switch (code) { case PA_SINK_MESSAGE_GET_LATENCY: { size_t n = 0; int l; #ifdef FIONREAD if (ioctl(u->fd, FIONREAD, &l) >= 0 && l > 0) n = (size_t) l; #endif n += u->memchunk.length; *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec); return 0; } } return pa_sink_process_msg(o, code, data, offset, chunk); }
/* Called from IO context */ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SINK(o)->userdata; switch (code) { case PA_SINK_MESSAGE_GET_LATENCY: { pa_usec_t r = 0; if (u->fd >= 0) { if (u->use_mmap) r = mmap_sink_get_latency(u); else r = io_sink_get_latency(u); } *((int64_t*) data) = (int64_t)r; return 0; } } return pa_sink_process_msg(o, code, data, offset, chunk); }
/* Called from IO context */ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SINK(o)->userdata; int ret; pa_bool_t do_trigger = FALSE, quick = TRUE; switch (code) { case PA_SINK_MESSAGE_GET_LATENCY: { pa_usec_t r = 0; if (u->fd >= 0) { if (u->use_mmap) r = mmap_sink_get_latency(u); else r = io_sink_get_latency(u); } *((pa_usec_t*) data) = r; return 0; } case PA_SINK_MESSAGE_SET_STATE: switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) { case PA_SINK_SUSPENDED: pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state)); if (!u->source || u->source_suspended) { if (suspend(u) < 0) return -1; } do_trigger = TRUE; u->sink_suspended = TRUE; break; case PA_SINK_IDLE: case PA_SINK_RUNNING: if (u->sink->thread_info.state == PA_SINK_INIT) { do_trigger = TRUE; quick = u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state); } if (u->sink->thread_info.state == PA_SINK_SUSPENDED) { if (!u->source || u->source_suspended) { if (unsuspend(u) < 0) return -1; quick = FALSE; } do_trigger = TRUE; u->out_mmap_current = 0; u->out_mmap_saved_nfrags = 0; u->sink_suspended = FALSE; } break; case PA_SINK_INVALID_STATE: case PA_SINK_UNLINKED: case PA_SINK_INIT: ; } break; } ret = pa_sink_process_msg(o, code, data, offset, chunk); if (ret >= 0 && do_trigger) { if (trigger(u, quick) < 0) return -1; } return ret; }
static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *memchunk) { struct userdata *u = PA_SINK(o)->userdata; switch (code) { case SINK_MESSAGE_RENDER: /* Handle the request from the JACK thread */ if (u->sink->thread_info.state == PA_SINK_RUNNING) { pa_memchunk chunk; size_t nbytes; void *p; pa_assert(offset > 0); nbytes = (size_t) offset * pa_frame_size(&u->sink->sample_spec); pa_sink_render_full(u->sink, nbytes, &chunk); p = (uint8_t*) pa_memblock_acquire(chunk.memblock) + chunk.index; pa_deinterleave(p, u->buffer, u->channels, sizeof(float), (unsigned) offset); pa_memblock_release(chunk.memblock); pa_memblock_unref(chunk.memblock); } else { unsigned c; pa_sample_spec ss; /* Humm, we're not RUNNING, hence let's write some silence */ ss = u->sink->sample_spec; ss.channels = 1; for (c = 0; c < u->channels; c++) pa_silence_memory(u->buffer[c], (size_t) offset * pa_sample_size(&ss), &ss); } u->frames_in_buffer = (jack_nframes_t) offset; u->saved_frame_time = * (jack_nframes_t*) data; u->saved_frame_time_valid = TRUE; return 0; case SINK_MESSAGE_BUFFER_SIZE: pa_sink_set_max_request_within_thread(u->sink, (size_t) offset * pa_frame_size(&u->sink->sample_spec)); return 0; case SINK_MESSAGE_ON_SHUTDOWN: pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL); return 0; case PA_SINK_MESSAGE_GET_LATENCY: { jack_nframes_t l, ft, d; size_t n; /* This is the "worst-case" latency */ l = jack_port_get_total_latency(u->client, u->port[0]) + u->frames_in_buffer; if (u->saved_frame_time_valid) { /* Adjust the worst case latency by the time that * passed since we last handed data to JACK */ ft = jack_frame_time(u->client); d = ft > u->saved_frame_time ? ft - u->saved_frame_time : 0; l = l > d ? l - d : 0; } /* Convert it to usec */ n = l * pa_frame_size(&u->sink->sample_spec); *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec); return 0; } } return pa_sink_process_msg(o, code, data, offset, memchunk); }