Async API allows to use full capabilites of PulseAudio.
Signed-off-by: Lukasz Marek <lukasz.m.luki2@gmail.com>
... | ... |
@@ -18,13 +18,14 @@ |
18 | 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | 19 |
*/ |
20 | 20 |
|
21 |
-#include <pulse/simple.h> |
|
21 |
+#include <pulse/pulseaudio.h> |
|
22 | 22 |
#include <pulse/error.h> |
23 | 23 |
#include "libavformat/avformat.h" |
24 | 24 |
#include "libavformat/internal.h" |
25 | 25 |
#include "libavutil/opt.h" |
26 | 26 |
#include "libavutil/time.h" |
27 | 27 |
#include "libavutil/log.h" |
28 |
+#include "libavutil/attributes.h" |
|
28 | 29 |
#include "pulse_audio_common.h" |
29 | 30 |
|
30 | 31 |
typedef struct PulseData { |
... | ... |
@@ -33,12 +34,203 @@ typedef struct PulseData { |
33 | 33 |
const char *name; |
34 | 34 |
const char *stream_name; |
35 | 35 |
const char *device; |
36 |
- pa_simple *pa; |
|
37 | 36 |
int64_t timestamp; |
38 | 37 |
int buffer_size; /**< Buffer size in bytes */ |
39 | 38 |
int buffer_duration; /**< Buffer size in ms, recalculated to buffer_size */ |
39 |
+ int last_result; |
|
40 |
+ pa_threaded_mainloop *mainloop; |
|
41 |
+ pa_context *ctx; |
|
42 |
+ pa_stream *stream; |
|
40 | 43 |
} PulseData; |
41 | 44 |
|
45 |
+static void pulse_stream_writable(pa_stream *stream, size_t nbytes, void *userdata) |
|
46 |
+{ |
|
47 |
+ AVFormatContext *h = userdata; |
|
48 |
+ PulseData *s = h->priv_data; |
|
49 |
+ |
|
50 |
+ if (stream != s->stream) |
|
51 |
+ return; |
|
52 |
+ |
|
53 |
+ pa_threaded_mainloop_signal(s->mainloop, 0); |
|
54 |
+} |
|
55 |
+ |
|
56 |
+static void pulse_stream_state(pa_stream *stream, void *userdata) |
|
57 |
+{ |
|
58 |
+ PulseData *s = userdata; |
|
59 |
+ |
|
60 |
+ if (stream != s->stream) |
|
61 |
+ return; |
|
62 |
+ |
|
63 |
+ switch (pa_stream_get_state(s->stream)) { |
|
64 |
+ case PA_STREAM_READY: |
|
65 |
+ case PA_STREAM_FAILED: |
|
66 |
+ case PA_STREAM_TERMINATED: |
|
67 |
+ pa_threaded_mainloop_signal(s->mainloop, 0); |
|
68 |
+ default: |
|
69 |
+ break; |
|
70 |
+ } |
|
71 |
+} |
|
72 |
+ |
|
73 |
+static int pulse_stream_wait(PulseData *s) |
|
74 |
+{ |
|
75 |
+ pa_stream_state_t state; |
|
76 |
+ |
|
77 |
+ while ((state = pa_stream_get_state(s->stream)) != PA_STREAM_READY) { |
|
78 |
+ if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED) |
|
79 |
+ return AVERROR_EXTERNAL; |
|
80 |
+ pa_threaded_mainloop_wait(s->mainloop); |
|
81 |
+ } |
|
82 |
+ return 0; |
|
83 |
+} |
|
84 |
+ |
|
85 |
+static void pulse_context_state(pa_context *ctx, void *userdata) |
|
86 |
+{ |
|
87 |
+ PulseData *s = userdata; |
|
88 |
+ |
|
89 |
+ if (s->ctx != ctx) |
|
90 |
+ return; |
|
91 |
+ |
|
92 |
+ switch (pa_context_get_state(ctx)) { |
|
93 |
+ case PA_CONTEXT_READY: |
|
94 |
+ case PA_CONTEXT_FAILED: |
|
95 |
+ case PA_CONTEXT_TERMINATED: |
|
96 |
+ pa_threaded_mainloop_signal(s->mainloop, 0); |
|
97 |
+ default: |
|
98 |
+ break; |
|
99 |
+ } |
|
100 |
+} |
|
101 |
+ |
|
102 |
+static int pulse_context_wait(PulseData *s) |
|
103 |
+{ |
|
104 |
+ pa_context_state_t state; |
|
105 |
+ |
|
106 |
+ while ((state = pa_context_get_state(s->ctx)) != PA_CONTEXT_READY) { |
|
107 |
+ if (state == PA_CONTEXT_FAILED || state == PA_CONTEXT_TERMINATED) |
|
108 |
+ return AVERROR_EXTERNAL; |
|
109 |
+ pa_threaded_mainloop_wait(s->mainloop); |
|
110 |
+ } |
|
111 |
+ return 0; |
|
112 |
+} |
|
113 |
+ |
|
114 |
+static void pulse_stream_result(pa_stream *stream, int success, void *userdata) |
|
115 |
+{ |
|
116 |
+ PulseData *s = userdata; |
|
117 |
+ |
|
118 |
+ if (stream != s->stream) |
|
119 |
+ return; |
|
120 |
+ |
|
121 |
+ s->last_result = success ? 0 : AVERROR_EXTERNAL; |
|
122 |
+ pa_threaded_mainloop_signal(s->mainloop, 0); |
|
123 |
+} |
|
124 |
+ |
|
125 |
+static int pulse_finish_stream_operation(PulseData *s, pa_operation *op, const char *name) |
|
126 |
+{ |
|
127 |
+ if (!op) { |
|
128 |
+ pa_threaded_mainloop_unlock(s->mainloop); |
|
129 |
+ av_log(s, AV_LOG_ERROR, "%s failed.\n", name); |
|
130 |
+ return AVERROR_EXTERNAL; |
|
131 |
+ } |
|
132 |
+ s->last_result = 2; |
|
133 |
+ while (s->last_result == 2) |
|
134 |
+ pa_threaded_mainloop_wait(s->mainloop); |
|
135 |
+ pa_operation_unref(op); |
|
136 |
+ pa_threaded_mainloop_unlock(s->mainloop); |
|
137 |
+ if (s->last_result != 0) |
|
138 |
+ av_log(s, AV_LOG_ERROR, "%s failed.\n", name); |
|
139 |
+ return s->last_result; |
|
140 |
+} |
|
141 |
+ |
|
142 |
+static int pulse_flash_stream(PulseData *s) |
|
143 |
+{ |
|
144 |
+ pa_operation *op; |
|
145 |
+ pa_threaded_mainloop_lock(s->mainloop); |
|
146 |
+ op = pa_stream_flush(s->stream, pulse_stream_result, s); |
|
147 |
+ return pulse_finish_stream_operation(s, op, "pa_stream_flush"); |
|
148 |
+} |
|
149 |
+ |
|
150 |
+static void pulse_map_channels_to_pulse(int64_t channel_layout, pa_channel_map *channel_map) |
|
151 |
+{ |
|
152 |
+ channel_map->channels = 0; |
|
153 |
+ if (channel_layout & AV_CH_FRONT_LEFT) |
|
154 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT; |
|
155 |
+ if (channel_layout & AV_CH_FRONT_RIGHT) |
|
156 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT; |
|
157 |
+ if (channel_layout & AV_CH_FRONT_CENTER) |
|
158 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_CENTER; |
|
159 |
+ if (channel_layout & AV_CH_LOW_FREQUENCY) |
|
160 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_LFE; |
|
161 |
+ if (channel_layout & AV_CH_BACK_LEFT) |
|
162 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_LEFT; |
|
163 |
+ if (channel_layout & AV_CH_BACK_RIGHT) |
|
164 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_RIGHT; |
|
165 |
+ if (channel_layout & AV_CH_FRONT_LEFT_OF_CENTER) |
|
166 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER; |
|
167 |
+ if (channel_layout & AV_CH_FRONT_RIGHT_OF_CENTER) |
|
168 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER; |
|
169 |
+ if (channel_layout & AV_CH_BACK_CENTER) |
|
170 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_CENTER; |
|
171 |
+ if (channel_layout & AV_CH_SIDE_LEFT) |
|
172 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_SIDE_LEFT; |
|
173 |
+ if (channel_layout & AV_CH_SIDE_RIGHT) |
|
174 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT; |
|
175 |
+ if (channel_layout & AV_CH_TOP_CENTER) |
|
176 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_CENTER; |
|
177 |
+ if (channel_layout & AV_CH_TOP_FRONT_LEFT) |
|
178 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_LEFT; |
|
179 |
+ if (channel_layout & AV_CH_TOP_FRONT_CENTER) |
|
180 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_CENTER; |
|
181 |
+ if (channel_layout & AV_CH_TOP_FRONT_RIGHT) |
|
182 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_RIGHT; |
|
183 |
+ if (channel_layout & AV_CH_TOP_BACK_LEFT) |
|
184 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_LEFT; |
|
185 |
+ if (channel_layout & AV_CH_TOP_BACK_CENTER) |
|
186 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_CENTER; |
|
187 |
+ if (channel_layout & AV_CH_TOP_BACK_RIGHT) |
|
188 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_RIGHT; |
|
189 |
+ if (channel_layout & AV_CH_STEREO_LEFT) |
|
190 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT; |
|
191 |
+ if (channel_layout & AV_CH_STEREO_RIGHT) |
|
192 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT; |
|
193 |
+ if (channel_layout & AV_CH_WIDE_LEFT) |
|
194 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX0; |
|
195 |
+ if (channel_layout & AV_CH_WIDE_RIGHT) |
|
196 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX1; |
|
197 |
+ if (channel_layout & AV_CH_SURROUND_DIRECT_LEFT) |
|
198 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX2; |
|
199 |
+ if (channel_layout & AV_CH_SURROUND_DIRECT_RIGHT) |
|
200 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX3; |
|
201 |
+ if (channel_layout & AV_CH_LOW_FREQUENCY_2) |
|
202 |
+ channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_LFE; |
|
203 |
+} |
|
204 |
+ |
|
205 |
+static av_cold int pulse_write_trailer(AVFormatContext *h) |
|
206 |
+{ |
|
207 |
+ PulseData *s = h->priv_data; |
|
208 |
+ |
|
209 |
+ if (s->mainloop) { |
|
210 |
+ pa_threaded_mainloop_lock(s->mainloop); |
|
211 |
+ if (s->stream) { |
|
212 |
+ pa_stream_disconnect(s->stream); |
|
213 |
+ pa_stream_set_state_callback(s->stream, NULL, NULL); |
|
214 |
+ pa_stream_set_write_callback(s->stream, NULL, NULL); |
|
215 |
+ pa_stream_unref(s->stream); |
|
216 |
+ s->stream = NULL; |
|
217 |
+ } |
|
218 |
+ if (s->ctx) { |
|
219 |
+ pa_context_disconnect(s->ctx); |
|
220 |
+ pa_context_set_state_callback(s->ctx, NULL, NULL); |
|
221 |
+ pa_context_unref(s->ctx); |
|
222 |
+ s->ctx = NULL; |
|
223 |
+ } |
|
224 |
+ pa_threaded_mainloop_unlock(s->mainloop); |
|
225 |
+ pa_threaded_mainloop_stop(s->mainloop); |
|
226 |
+ pa_threaded_mainloop_free(s->mainloop); |
|
227 |
+ s->mainloop = NULL; |
|
228 |
+ } |
|
229 |
+ |
|
230 |
+ return 0; |
|
231 |
+} |
|
232 |
+ |
|
42 | 233 |
static av_cold int pulse_write_header(AVFormatContext *h) |
43 | 234 |
{ |
44 | 235 |
PulseData *s = h->priv_data; |
... | ... |
@@ -46,7 +238,12 @@ static av_cold int pulse_write_header(AVFormatContext *h) |
46 | 46 |
int ret; |
47 | 47 |
pa_sample_spec sample_spec; |
48 | 48 |
pa_buffer_attr buffer_attributes = { -1, -1, -1, -1, -1 }; |
49 |
+ pa_channel_map channel_map; |
|
50 |
+ pa_mainloop_api *mainloop_api; |
|
49 | 51 |
const char *stream_name = s->stream_name; |
52 |
+ static const pa_stream_flags_t stream_flags = PA_STREAM_INTERPOLATE_TIMING | |
|
53 |
+ PA_STREAM_AUTO_TIMING_UPDATE | |
|
54 |
+ PA_STREAM_NOT_MONOTONIC; |
|
50 | 55 |
|
51 | 56 |
if (h->nb_streams != 1 || h->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO) { |
52 | 57 |
av_log(s, AV_LOG_ERROR, "Only a single audio stream is supported.\n"); |
... | ... |
@@ -82,47 +279,109 @@ static av_cold int pulse_write_header(AVFormatContext *h) |
82 | 82 |
return AVERROR(EINVAL); |
83 | 83 |
} |
84 | 84 |
|
85 |
- s->pa = pa_simple_new(s->server, // Server |
|
86 |
- s->name, // Application name |
|
87 |
- PA_STREAM_PLAYBACK, |
|
88 |
- s->device, // Device |
|
89 |
- stream_name, // Description of a stream |
|
90 |
- &sample_spec, // Sample format |
|
91 |
- NULL, // Use default channel map |
|
92 |
- &buffer_attributes, // Buffering attributes |
|
93 |
- &ret); // Result |
|
85 |
+ if (sample_spec.channels == 1) { |
|
86 |
+ channel_map.channels = 1; |
|
87 |
+ channel_map.map[0] = PA_CHANNEL_POSITION_MONO; |
|
88 |
+ } else if (st->codec->channel_layout) { |
|
89 |
+ if (av_get_channel_layout_nb_channels(st->codec->channel_layout) != st->codec->channels) |
|
90 |
+ return AVERROR(EINVAL); |
|
91 |
+ pulse_map_channels_to_pulse(st->codec->channel_layout, &channel_map); |
|
92 |
+ /* Unknown channel is present in channel_layout, let PulseAudio use its default. */ |
|
93 |
+ if (channel_map.channels != sample_spec.channels) { |
|
94 |
+ av_log(s, AV_LOG_WARNING, "Unknown channel. Using defaul channel map.\n"); |
|
95 |
+ channel_map.channels = 0; |
|
96 |
+ } |
|
97 |
+ } else |
|
98 |
+ channel_map.channels = 0; |
|
94 | 99 |
|
95 |
- if (!s->pa) { |
|
96 |
- av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n", pa_strerror(ret)); |
|
97 |
- return AVERROR(EIO); |
|
100 |
+ if (!channel_map.channels) |
|
101 |
+ av_log(s, AV_LOG_WARNING, "Using PulseAudio's default channel map.\n"); |
|
102 |
+ else if (!pa_channel_map_valid(&channel_map)) { |
|
103 |
+ av_log(s, AV_LOG_ERROR, "Invalid channel map.\n"); |
|
104 |
+ return AVERROR(EINVAL); |
|
98 | 105 |
} |
99 | 106 |
|
100 |
- avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */ |
|
107 |
+ /* start main loop */ |
|
108 |
+ s->mainloop = pa_threaded_mainloop_new(); |
|
109 |
+ if (!s->mainloop) { |
|
110 |
+ av_log(s, AV_LOG_ERROR, "Cannot create threaded mainloop.\n"); |
|
111 |
+ return AVERROR(ENOMEM); |
|
112 |
+ } |
|
113 |
+ if ((ret = pa_threaded_mainloop_start(s->mainloop)) < 0) { |
|
114 |
+ av_log(s, AV_LOG_ERROR, "Cannot start threaded mainloop: %s.\n", pa_strerror(ret)); |
|
115 |
+ pa_threaded_mainloop_free(s->mainloop); |
|
116 |
+ s->mainloop = NULL; |
|
117 |
+ return AVERROR_EXTERNAL; |
|
118 |
+ } |
|
101 | 119 |
|
102 |
- return 0; |
|
103 |
-} |
|
120 |
+ pa_threaded_mainloop_lock(s->mainloop); |
|
121 |
+ |
|
122 |
+ mainloop_api = pa_threaded_mainloop_get_api(s->mainloop); |
|
123 |
+ if (!mainloop_api) { |
|
124 |
+ av_log(s, AV_LOG_ERROR, "Cannot get mainloop API.\n"); |
|
125 |
+ ret = AVERROR_EXTERNAL; |
|
126 |
+ goto fail; |
|
127 |
+ } |
|
128 |
+ |
|
129 |
+ s->ctx = pa_context_new(mainloop_api, s->name); |
|
130 |
+ if (!s->ctx) { |
|
131 |
+ av_log(s, AV_LOG_ERROR, "Cannot create context.\n"); |
|
132 |
+ ret = AVERROR(ENOMEM); |
|
133 |
+ goto fail; |
|
134 |
+ } |
|
135 |
+ pa_context_set_state_callback(s->ctx, pulse_context_state, s); |
|
136 |
+ |
|
137 |
+ if ((ret = pa_context_connect(s->ctx, s->server, 0, NULL)) < 0) { |
|
138 |
+ av_log(s, AV_LOG_ERROR, "Cannot connect context: %s.\n", pa_strerror(ret)); |
|
139 |
+ ret = AVERROR_EXTERNAL; |
|
140 |
+ goto fail; |
|
141 |
+ } |
|
142 |
+ |
|
143 |
+ if ((ret = pulse_context_wait(s)) < 0) { |
|
144 |
+ av_log(s, AV_LOG_ERROR, "Context failed.\n"); |
|
145 |
+ goto fail; |
|
146 |
+ } |
|
147 |
+ |
|
148 |
+ s->stream = pa_stream_new(s->ctx, stream_name, &sample_spec, |
|
149 |
+ channel_map.channels ? &channel_map : NULL); |
|
150 |
+ if (!s->stream) { |
|
151 |
+ av_log(s, AV_LOG_ERROR, "Cannot create stream.\n"); |
|
152 |
+ ret = AVERROR(ENOMEM); |
|
153 |
+ goto fail; |
|
154 |
+ } |
|
155 |
+ pa_stream_set_state_callback(s->stream, pulse_stream_state, s); |
|
156 |
+ pa_stream_set_write_callback(s->stream, pulse_stream_writable, h); |
|
157 |
+ |
|
158 |
+ if ((ret = pa_stream_connect_playback(s->stream, s->device, &buffer_attributes, |
|
159 |
+ stream_flags, NULL, NULL)) < 0) { |
|
160 |
+ av_log(s, AV_LOG_ERROR, "pa_stream_connect_playback failed: %s.\n", pa_strerror(ret)); |
|
161 |
+ ret = AVERROR_EXTERNAL; |
|
162 |
+ goto fail; |
|
163 |
+ } |
|
164 |
+ |
|
165 |
+ if ((ret = pulse_stream_wait(s)) < 0) { |
|
166 |
+ av_log(s, AV_LOG_ERROR, "Stream failed.\n"); |
|
167 |
+ goto fail; |
|
168 |
+ } |
|
169 |
+ |
|
170 |
+ pa_threaded_mainloop_unlock(s->mainloop); |
|
171 |
+ |
|
172 |
+ avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */ |
|
104 | 173 |
|
105 |
-static av_cold int pulse_write_trailer(AVFormatContext *h) |
|
106 |
-{ |
|
107 |
- PulseData *s = h->priv_data; |
|
108 |
- pa_simple_flush(s->pa, NULL); |
|
109 |
- pa_simple_free(s->pa); |
|
110 |
- s->pa = NULL; |
|
111 | 174 |
return 0; |
175 |
+ fail: |
|
176 |
+ pa_threaded_mainloop_unlock(s->mainloop); |
|
177 |
+ pulse_write_trailer(h); |
|
178 |
+ return ret; |
|
112 | 179 |
} |
113 | 180 |
|
114 | 181 |
static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt) |
115 | 182 |
{ |
116 | 183 |
PulseData *s = h->priv_data; |
117 |
- int error; |
|
184 |
+ int ret; |
|
118 | 185 |
|
119 |
- if (!pkt) { |
|
120 |
- if (pa_simple_flush(s->pa, &error) < 0) { |
|
121 |
- av_log(s, AV_LOG_ERROR, "pa_simple_flush failed: %s\n", pa_strerror(error)); |
|
122 |
- return AVERROR(EIO); |
|
123 |
- } |
|
124 |
- return 1; |
|
125 |
- } |
|
186 |
+ if (!pkt) |
|
187 |
+ return pulse_flash_stream(s); |
|
126 | 188 |
|
127 | 189 |
if (pkt->dts != AV_NOPTS_VALUE) |
128 | 190 |
s->timestamp = pkt->dts; |
... | ... |
@@ -137,12 +396,24 @@ static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt) |
137 | 137 |
s->timestamp += av_rescale_q(samples, r, st->time_base); |
138 | 138 |
} |
139 | 139 |
|
140 |
- if (pa_simple_write(s->pa, pkt->data, pkt->size, &error) < 0) { |
|
141 |
- av_log(s, AV_LOG_ERROR, "pa_simple_write failed: %s\n", pa_strerror(error)); |
|
142 |
- return AVERROR(EIO); |
|
140 |
+ pa_threaded_mainloop_lock(s->mainloop); |
|
141 |
+ if (!PA_STREAM_IS_GOOD(pa_stream_get_state(s->stream))) { |
|
142 |
+ av_log(s, AV_LOG_ERROR, "PulseAudio stream is in invalid state.\n"); |
|
143 |
+ goto fail; |
|
144 |
+ } |
|
145 |
+ while (!pa_stream_writable_size(s->stream)) |
|
146 |
+ pa_threaded_mainloop_wait(s->mainloop); |
|
147 |
+ |
|
148 |
+ if ((ret = pa_stream_write(s->stream, pkt->data, pkt->size, NULL, 0, PA_SEEK_RELATIVE)) < 0) { |
|
149 |
+ av_log(s, AV_LOG_ERROR, "pa_stream_write failed: %s\n", pa_strerror(ret)); |
|
150 |
+ goto fail; |
|
143 | 151 |
} |
152 |
+ pa_threaded_mainloop_unlock(s->mainloop); |
|
144 | 153 |
|
145 | 154 |
return 0; |
155 |
+ fail: |
|
156 |
+ pa_threaded_mainloop_unlock(s->mainloop); |
|
157 |
+ return AVERROR_EXTERNAL; |
|
146 | 158 |
} |
147 | 159 |
|
148 | 160 |
static int pulse_write_frame(AVFormatContext *h, int stream_index, |
... | ... |
@@ -166,9 +437,13 @@ static int pulse_write_frame(AVFormatContext *h, int stream_index, |
166 | 166 |
static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall) |
167 | 167 |
{ |
168 | 168 |
PulseData *s = h->priv_data; |
169 |
- pa_usec_t latency = pa_simple_get_latency(s->pa, NULL); |
|
169 |
+ pa_usec_t latency; |
|
170 |
+ int neg; |
|
171 |
+ pa_threaded_mainloop_lock(s->mainloop); |
|
172 |
+ pa_stream_get_latency(s->stream, &latency, &neg); |
|
173 |
+ pa_threaded_mainloop_unlock(s->mainloop); |
|
170 | 174 |
*wall = av_gettime(); |
171 |
- *dts = s->timestamp - latency; |
|
175 |
+ *dts = s->timestamp - (neg ? -latency : latency); |
|
172 | 176 |
} |
173 | 177 |
|
174 | 178 |
static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list) |