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)
54 #define CHECK_SUCCESS_GOTO(rerror, expression, label) \
56 if (!(expression)) { \
57 rerror = AVERROR_EXTERNAL; \
62 #define CHECK_DEAD_GOTO(p, rerror, label) \
64 if (!(p)->context || !PA_CONTEXT_IS_GOOD(pa_context_get_state((p)->context)) || \
65 !(p)->stream || !PA_STREAM_IS_GOOD(pa_stream_get_state((p)->stream))) { \
66 rerror = AVERROR_EXTERNAL; \
74 switch (pa_context_get_state(c)) {
75 case PA_CONTEXT_READY:
76 case PA_CONTEXT_TERMINATED:
77 case PA_CONTEXT_FAILED:
78 pa_threaded_mainloop_signal(p->
mainloop, 0);
86 switch (pa_stream_get_state(s)) {
88 case PA_STREAM_FAILED:
89 case PA_STREAM_TERMINATED:
90 pa_threaded_mainloop_signal(p->
mainloop, 0);
98 pa_threaded_mainloop_signal(p->
mainloop, 0);
104 pa_threaded_mainloop_signal(p->
mainloop, 0);
112 pa_threaded_mainloop_stop(pd->
mainloop);
115 pa_stream_unref(pd->
stream);
119 pa_context_disconnect(pd->
context);
125 pa_threaded_mainloop_free(pd->
mainloop);
146 pa_buffer_attr attr = { -1 };
160 if (!(pd->
mainloop = pa_threaded_mainloop_new())) {
165 if (!(pd->
context = pa_context_new(pa_threaded_mainloop_get_api(pd->
mainloop), pd->
name))) {
172 if (pa_context_connect(pd->
context, pd->
server, 0, NULL) < 0) {
177 pa_threaded_mainloop_lock(pd->
mainloop);
179 if (pa_threaded_mainloop_start(pd->
mainloop) < 0) {
181 goto unlock_and_fail;
185 pa_context_state_t
state;
187 state = pa_context_get_state(pd->
context);
189 if (state == PA_CONTEXT_READY)
192 if (!PA_CONTEXT_IS_GOOD(state)) {
194 goto unlock_and_fail;
198 pa_threaded_mainloop_wait(pd->
mainloop);
203 goto unlock_and_fail;
211 ret = pa_stream_connect_record(pd->
stream, device, &attr,
212 PA_STREAM_INTERPOLATE_TIMING
213 |PA_STREAM_ADJUST_LATENCY
214 |PA_STREAM_AUTO_TIMING_UPDATE);
218 goto unlock_and_fail;
222 pa_stream_state_t
state;
224 state = pa_stream_get_state(pd->
stream);
226 if (state == PA_STREAM_READY)
229 if (!PA_STREAM_IS_GOOD(state)) {
231 goto unlock_and_fail;
235 pa_threaded_mainloop_wait(pd->
mainloop);
238 pa_threaded_mainloop_unlock(pd->
mainloop);
258 pa_threaded_mainloop_unlock(pd->
mainloop);
274 pa_threaded_mainloop_lock(pd->
mainloop);
281 r = pa_stream_peek(pd->
stream, &read_data, &read_length);
284 if (read_length <= 0) {
285 pa_threaded_mainloop_wait(pd->
mainloop);
287 }
else if (!read_data) {
290 r = pa_stream_drop(pd->
stream);
297 goto unlock_and_fail;
301 pa_operation_unref(pa_stream_update_timing_info(pd->
stream, NULL, NULL));
303 if (pa_stream_get_latency(pd->
stream, &latency, &negative) >= 0) {
307 int frame_duration = read_length /
frame_size;
321 memcpy(pkt->
data, read_data, read_length);
322 pa_stream_drop(pd->
stream);
324 pa_threaded_mainloop_unlock(pd->
mainloop);
328 pa_threaded_mainloop_unlock(pd->
mainloop);
338 #define OFFSET(a) offsetof(PulseData, a)
339 #define D AV_OPT_FLAG_DECODING_PARAM
346 {
"channels",
"set number of audio channels",
OFFSET(channels),
AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX,
D },
348 {
"fragment_size",
"set buffering size, affects latency and cpu usage",
OFFSET(fragment_size),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
D },
369 .priv_class = &pulse_demuxer_class,