1, module-loopback播放时 ,pulse audio基本的插件连接情况如下图所示
2,module-loopback中latency的计算如下:
a: source中的latency计算:
pa_asyncmsgq_send(u->source_output->source->asyncmsgq, PA_MSGOBJECT(u->source_output), SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT, NULL, 0, NULL);
其中SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT的处理逻辑又如下:
而pa_source_get_latency_within_thread调用process_msg(PA_SOURCE_MESSAGE_GET_LATENCY).
作为例子,module-null-source的计算如下所示:
b: sink中的latency计算(与source基本类似)
pa_asyncmsgq_send(u->sink_input->sink->asyncmsgq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT, NULL, 0, NULL);
其中的pa_sink_get_latency_within_thread调用process_msg(PA_SINK_MESSAGE_GET_LATENCY). 作为例子,module-null-sink的PA_SINK_MESSAGE_GET_LATENCY处理如下所示:
c: loopback插件本身中的latency的计算,其本身又分为3部分:
一个是source output中队列的缓存:
length = pa_memblockq_get_length(u->source_output->thread_info.delay_memblockq);
一个是sink input总队列的缓存(注意:此计算方式包括了loopback中队列的缓存):
length = pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq);
u->latency_snapshot.sink_input_buffer =
pa_memblockq_get_length(u->memblockq) +
(u->sink_input->thread_info.resampler ? pa_resampler_request(u->sink_input->thread_info.resampler, length) : length);
再有一个是计算收发平衡的点:
buffer =
u->latency_snapshot.sink_input_buffer +
u->latency_snapshot.source_output_buffer;
if (u->latency_snapshot.recv_counter <= u->latency_snapshot.send_counter)
buffer += (size_t) (u->latency_snapshot.send_counter - u->latency_snapshot.recv_counter);
else
buffer += PA_CLIP_SUB(buffer, (size_t) (u->latency_snapshot.recv_counter - u->latency_snapshot.send_counter));
buffer_latency = pa_bytes_to_usec(buffer, &u->sink_input->sample_spec);
如上, 总共的latency的计算,就是将a, b , c三部分相加即可。