59
57
"format=<sample format> "
60
58
"channels=<number of channels> "
61
59
"rate=<sample rate> "
62
"sink_name=<name of sink>"
63
"channel_map=<channel map>"
60
"sink_name=<name of sink> "
61
"channel_map=<channel map> "
64
62
"description=<description for the sink>");
66
64
#define DEFAULT_SINK_NAME "null"
65
#define MAX_LATENCY_USEC (PA_USEC_PER_SEC * 2)
96
96
case PA_SINK_MESSAGE_SET_STATE:
98
98
if (PA_PTR_TO_UINT(data) == PA_SINK_RUNNING)
99
pa_rtclock_get(&u->timestamp);
99
u->timestamp = pa_rtclock_usec();
103
103
case PA_SINK_MESSAGE_GET_LATENCY: {
106
pa_rtclock_get(&now);
108
if (pa_timeval_cmp(&u->timestamp, &now) > 0)
109
*((pa_usec_t*) data) = 0;
111
*((pa_usec_t*) data) = pa_timeval_diff(&u->timestamp, &now);
106
now = pa_rtclock_usec();
107
*((pa_usec_t*) data) = u->timestamp > now ? u->timestamp - now : 0;
116
113
return pa_sink_process_msg(o, code, data, offset, chunk);
116
static void sink_update_requested_latency_cb(pa_sink *s) {
119
pa_sink_assert_ref(s);
123
u->block_usec = pa_sink_get_requested_latency_within_thread(s);
126
static void process_rewind(struct userdata *u, pa_usec_t now) {
127
size_t rewind_nbytes, in_buffer;
132
/* Figure out how much we shall rewind and reset the counter */
133
rewind_nbytes = u->sink->thread_info.rewind_nbytes;
134
u->sink->thread_info.rewind_nbytes = 0;
136
pa_assert(rewind_nbytes > 0);
137
pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) rewind_nbytes);
139
if (u->timestamp <= now)
142
delay = u->timestamp - now;
143
in_buffer = pa_usec_to_bytes(delay, &u->sink->sample_spec);
148
if (rewind_nbytes > in_buffer)
149
rewind_nbytes = in_buffer;
151
pa_sink_process_rewind(u->sink, rewind_nbytes);
152
u->timestamp -= pa_bytes_to_usec(rewind_nbytes, &u->sink->sample_spec);
154
pa_log_debug("Rewound %lu bytes.", (unsigned long) rewind_nbytes);
159
pa_sink_process_rewind(u->sink, 0);
162
static void process_render(struct userdata *u, pa_usec_t now) {
167
/* This is the configured latency. Sink inputs connected to us
168
might not have a single frame more than the maxrequest value
169
queed. Hence: at maximum read this many bytes from the sink
172
/* Fill the buffer up the the latency size */
173
while (u->timestamp < now + u->block_usec) {
176
pa_sink_render(u->sink, u->sink->thread_info.max_request, &chunk);
177
pa_memblock_unref(chunk.memblock);
179
/* pa_log_debug("Ate %lu bytes.", (unsigned long) chunk.length); */
180
u->timestamp += pa_bytes_to_usec(chunk.length, &u->sink->sample_spec);
184
if (ate >= u->sink->thread_info.max_request)
188
/* pa_log_debug("Ate in sum %lu bytes (of %lu)", (unsigned long) ate, (unsigned long) nbytes); */
119
191
static void thread_func(void *userdata) {
120
192
struct userdata *u = userdata;
126
198
pa_thread_mq_install(&u->thread_mq);
127
199
pa_rtpoll_install(u->rtpoll);
129
pa_rtclock_get(&u->timestamp);
201
u->timestamp = pa_rtclock_usec();
134
206
/* Render some data and drop it immediately */
135
if (u->sink->thread_info.state == PA_SINK_RUNNING) {
138
pa_rtclock_get(&now);
140
if (pa_timeval_cmp(&u->timestamp, &now) <= 0) {
141
pa_sink_skip(u->sink, u->block_size);
142
pa_timeval_add(&u->timestamp, pa_bytes_to_usec(u->block_size, &u->sink->sample_spec));
207
if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
210
now = pa_rtclock_usec();
212
if (u->sink->thread_info.rewind_requested) {
213
if (u->sink->thread_info.rewind_nbytes > 0)
214
process_rewind(u, now);
216
pa_sink_process_rewind(u->sink, 0);
145
pa_rtpoll_set_timer_absolute(u->rtpoll, &u->timestamp);
219
if (u->timestamp <= now)
220
process_render(u, now);
222
pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp);
147
224
pa_rtpoll_set_timer_disabled(u->rtpoll);
149
226
/* Hmm, nothing to do. Let's sleep */
150
if ((ret = pa_rtpoll_run(u->rtpoll, 1)) < 0)
227
if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
186
u = pa_xnew0(struct userdata, 1);
264
m->userdata = u = pa_xnew0(struct userdata, 1);
187
265
u->core = m->core;
190
pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
191
267
u->rtpoll = pa_rtpoll_new();
192
pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
194
if (!(u->sink = pa_sink_new(m->core, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
195
pa_log("Failed to create sink.");
268
pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
270
pa_sink_new_data_init(&data);
271
data.driver = __FILE__;
273
pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
274
pa_sink_new_data_set_sample_spec(&data, &ss);
275
pa_sink_new_data_set_channel_map(&data, &map);
276
pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, pa_modargs_get_value(ma, "description", "Null Output"));
277
pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
279
u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
280
pa_sink_new_data_done(&data);
283
pa_log("Failed to create sink object.");
199
287
u->sink->parent.process_msg = sink_process_msg;
288
u->sink->update_requested_latency = sink_update_requested_latency_cb;
200
289
u->sink->userdata = u;
201
u->sink->flags = PA_SINK_LATENCY;
203
pa_sink_set_module(u->sink, m);
204
291
pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
205
292
pa_sink_set_rtpoll(u->sink, u->rtpoll);
206
pa_sink_set_description(u->sink, pa_modargs_get_value(ma, "description", "NULL sink"));
208
u->block_size = pa_bytes_per_second(&ss) / 20; /* 50 ms */
209
if (u->block_size <= 0)
210
u->block_size = pa_frame_size(&ss);
294
pa_sink_set_latency_range(u->sink, (pa_usec_t) -1, MAX_LATENCY_USEC);
295
u->block_usec = u->sink->thread_info.max_latency;
297
u->sink->thread_info.max_rewind =
298
u->sink->thread_info.max_request =
299
pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec);
212
301
if (!(u->thread = pa_thread_new(thread_func, u))) {
213
302
pa_log("Failed to create thread.");