24 #include <pulse/rtclock.h>
25 #include <pulse/error.h>
33 #define DEFAULT_CODEC_ID AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE)
55 #define CHECK_SUCCESS_GOTO(rerror, expression, label) \
57 if (!(expression)) { \
58 rerror = AVERROR_EXTERNAL; \
63 #define CHECK_DEAD_GOTO(p, rerror, label) \
65 if (!(p)->context || !PA_CONTEXT_IS_GOOD(pa_context_get_state((p)->context)) || \
66 !(p)->stream || !PA_STREAM_IS_GOOD(pa_stream_get_state((p)->stream))) { \
67 rerror = AVERROR_EXTERNAL; \
75 switch (pa_context_get_state(c)) {
76 case PA_CONTEXT_READY:
77 case PA_CONTEXT_TERMINATED:
78 case PA_CONTEXT_FAILED:
79 pa_threaded_mainloop_signal(p->
mainloop, 0);
87 switch (pa_stream_get_state(s)) {
89 case PA_STREAM_FAILED:
90 case PA_STREAM_TERMINATED:
91 pa_threaded_mainloop_signal(p->
mainloop, 0);
99 pa_threaded_mainloop_signal(p->
mainloop, 0);
105 pa_threaded_mainloop_signal(p->
mainloop, 0);
113 pa_threaded_mainloop_stop(pd->
mainloop);
116 pa_stream_unref(pd->
stream);
120 pa_context_disconnect(pd->
context);
126 pa_threaded_mainloop_free(pd->
mainloop);
147 pa_buffer_attr attr = { -1 };
161 if (!(pd->
mainloop = pa_threaded_mainloop_new())) {
166 if (!(pd->
context = pa_context_new(pa_threaded_mainloop_get_api(pd->
mainloop), pd->
name))) {
173 if (pa_context_connect(pd->
context, pd->
server, 0, NULL) < 0) {
178 pa_threaded_mainloop_lock(pd->
mainloop);
180 if (pa_threaded_mainloop_start(pd->
mainloop) < 0) {
182 goto unlock_and_fail;
186 pa_context_state_t
state;
188 state = pa_context_get_state(pd->
context);
190 if (state == PA_CONTEXT_READY)
193 if (!PA_CONTEXT_IS_GOOD(state)) {
195 goto unlock_and_fail;
199 pa_threaded_mainloop_wait(pd->
mainloop);
204 goto unlock_and_fail;
212 ret = pa_stream_connect_record(pd->
stream, device, &attr,
213 PA_STREAM_INTERPOLATE_TIMING
214 |PA_STREAM_ADJUST_LATENCY
215 |PA_STREAM_AUTO_TIMING_UPDATE);
219 goto unlock_and_fail;
223 pa_stream_state_t
state;
225 state = pa_stream_get_state(pd->
stream);
227 if (state == PA_STREAM_READY)
230 if (!PA_STREAM_IS_GOOD(state)) {
232 goto unlock_and_fail;
236 pa_threaded_mainloop_wait(pd->
mainloop);
239 pa_threaded_mainloop_unlock(pd->
mainloop);
259 pa_threaded_mainloop_unlock(pd->
mainloop);
275 pa_threaded_mainloop_lock(pd->
mainloop);
282 r = pa_stream_peek(pd->
stream, &read_data, &read_length);
285 if (read_length <= 0) {
286 pa_threaded_mainloop_wait(pd->
mainloop);
288 }
else if (!read_data) {
291 r = pa_stream_drop(pd->
stream);
298 goto unlock_and_fail;
302 pa_operation_unref(pa_stream_update_timing_info(pd->
stream, NULL, NULL));
304 if (pa_stream_get_latency(pd->
stream, &latency, &negative) >= 0) {
308 int frame_duration = read_length /
frame_size;
323 memcpy(pkt->
data, read_data, read_length);
324 pa_stream_drop(pd->
stream);
326 pa_threaded_mainloop_unlock(pd->
mainloop);
330 pa_threaded_mainloop_unlock(pd->
mainloop);
340 #define OFFSET(a) offsetof(PulseData, a)
341 #define D AV_OPT_FLAG_DECODING_PARAM
348 {
"channels",
"set number of audio channels",
OFFSET(channels),
AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX,
D },
350 {
"fragment_size",
"set buffering size, affects latency and cpu usage",
OFFSET(fragment_size),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
D },
351 {
"wallclock",
"set the initial pts using the current time",
OFFSET(wallclock),
AV_OPT_TYPE_INT, {.i64 = 1}, -1, 1,
D },
372 .priv_class = &pulse_demuxer_class,