Line | Count | Source |
1 | | /* |
2 | | * This file is part of mpv. |
3 | | * |
4 | | * mpv is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU Lesser General Public |
6 | | * License as published by the Free Software Foundation; either |
7 | | * version 2.1 of the License, or (at your option) any later version. |
8 | | * |
9 | | * mpv is distributed in the hope that it will be useful, |
10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | * GNU Lesser General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU Lesser General Public |
15 | | * License along with mpv. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | |
18 | | #include <stddef.h> |
19 | | #include <stdbool.h> |
20 | | #include <inttypes.h> |
21 | | #include <limits.h> |
22 | | #include <math.h> |
23 | | #include <assert.h> |
24 | | |
25 | | #include "mpv_talloc.h" |
26 | | |
27 | | #include "common/msg.h" |
28 | | #include "common/encode.h" |
29 | | #include "options/options.h" |
30 | | #include "common/common.h" |
31 | | #include "osdep/timer.h" |
32 | | |
33 | | #include "audio/format.h" |
34 | | #include "audio/out/ao.h" |
35 | | #include "demux/demux.h" |
36 | | #include "filters/f_async_queue.h" |
37 | | #include "filters/f_decoder_wrapper.h" |
38 | | #include "filters/filter_internal.h" |
39 | | |
40 | | #include "core.h" |
41 | | #include "command.h" |
42 | | |
43 | | enum { |
44 | | AD_OK = 0, |
45 | | AD_EOF = -2, |
46 | | AD_WAIT = -4, |
47 | | }; |
48 | | |
49 | | static void ao_process(struct mp_filter *f); |
50 | | |
51 | | static void update_speed_filters(struct MPContext *mpctx) |
52 | 32.2k | { |
53 | 32.2k | struct ao_chain *ao_c = mpctx->ao_chain; |
54 | 32.2k | if (!ao_c) |
55 | 22.4k | return; |
56 | | |
57 | 9.84k | double pitch = mpctx->opts->playback_pitch; |
58 | 9.84k | double speed = mpctx->opts->playback_speed; |
59 | 9.84k | double resample = mpctx->speed_factor_a; |
60 | 9.84k | double drop = 1.0; |
61 | | |
62 | 9.84k | if (!mpctx->opts->pitch_correction) { |
63 | 0 | resample *= speed; |
64 | 0 | speed = 1.0; |
65 | 0 | } |
66 | | |
67 | 9.84k | int video_sync = mpctx->display_sync_active ? |
68 | 9.84k | mpctx->video_out->opts->video_sync : VS_NONE; |
69 | 9.84k | switch (video_sync) { |
70 | 0 | case VS_DISP_ADROP: |
71 | 0 | drop *= speed * resample / pitch; |
72 | 0 | resample = pitch; |
73 | 0 | speed = 1.0; |
74 | 0 | break; |
75 | 0 | case VS_DISP_TEMPO: |
76 | 0 | speed = mpctx->audio_speed / pitch; |
77 | 0 | resample = pitch; |
78 | 0 | break; |
79 | 9.84k | default: |
80 | 9.84k | resample *= pitch; |
81 | 9.84k | speed /= pitch; |
82 | 9.84k | break; |
83 | 9.84k | } |
84 | | |
85 | 9.84k | mp_output_chain_set_audio_speed(ao_c->filter, speed, resample, drop); |
86 | 9.84k | } |
87 | | |
88 | | static int recreate_audio_filters(struct MPContext *mpctx) |
89 | 9.84k | { |
90 | 9.84k | struct ao_chain *ao_c = mpctx->ao_chain; |
91 | 9.84k | mp_assert(ao_c); |
92 | | |
93 | 9.84k | if (!mp_output_chain_update_filters(ao_c->filter, mpctx->opts->af_settings)) |
94 | 0 | goto fail; |
95 | | |
96 | 9.84k | update_speed_filters(mpctx); |
97 | | |
98 | 9.84k | mp_notify(mpctx, MPV_EVENT_AUDIO_RECONFIG, NULL); |
99 | | |
100 | 9.84k | return 0; |
101 | | |
102 | 0 | fail: |
103 | 0 | MP_ERR(mpctx, "Audio filter initialized failed!\n"); |
104 | 0 | return -1; |
105 | 9.84k | } |
106 | | |
107 | | int reinit_audio_filters(struct MPContext *mpctx) |
108 | 0 | { |
109 | 0 | struct ao_chain *ao_c = mpctx->ao_chain; |
110 | 0 | if (!ao_c) |
111 | 0 | return 0; |
112 | | |
113 | 0 | double delay = mp_output_get_measured_total_delay(ao_c->filter); |
114 | |
|
115 | 0 | if (recreate_audio_filters(mpctx) < 0) |
116 | 0 | return -1; |
117 | | |
118 | 0 | double ndelay = mp_output_get_measured_total_delay(ao_c->filter); |
119 | | |
120 | | // Only force refresh if the amount of dropped buffered data is going to |
121 | | // cause "issues" for the A/V sync logic. |
122 | 0 | if (mpctx->audio_status == STATUS_PLAYING && delay - ndelay >= 0.2) |
123 | 0 | issue_refresh_seek(mpctx, MPSEEK_EXACT); |
124 | 0 | return 1; |
125 | 0 | } |
126 | | |
127 | | static double db_gain(double db) |
128 | 12.0k | { |
129 | 12.0k | return pow(10.0, db/20.0); |
130 | 12.0k | } |
131 | | |
132 | | static float compute_replaygain(struct MPContext *mpctx) |
133 | 12.0k | { |
134 | 12.0k | struct MPOpts *opts = mpctx->opts; |
135 | | |
136 | 12.0k | float rgain = 1.0; |
137 | | |
138 | 12.0k | struct replaygain_data *rg = NULL; |
139 | 12.0k | struct track *track = mpctx->current_track[0][STREAM_AUDIO]; |
140 | 12.0k | if (track) |
141 | 12.0k | rg = track->stream->codec->replaygain_data; |
142 | 12.0k | if (opts->rgain_mode && rg) { |
143 | 0 | MP_VERBOSE(mpctx, "Replaygain: Track=%f/%f Album=%f/%f\n", |
144 | 0 | rg->track_gain, rg->track_peak, |
145 | 0 | rg->album_gain, rg->album_peak); |
146 | |
|
147 | 0 | float gain, peak; |
148 | 0 | if (opts->rgain_mode == 1) { |
149 | 0 | gain = rg->track_gain; |
150 | 0 | peak = rg->track_peak; |
151 | 0 | } else { |
152 | 0 | gain = rg->album_gain; |
153 | 0 | peak = rg->album_peak; |
154 | 0 | } |
155 | |
|
156 | 0 | gain += opts->rgain_preamp; |
157 | 0 | rgain = db_gain(gain); |
158 | |
|
159 | 0 | MP_VERBOSE(mpctx, "Applying replay-gain: %f\n", rgain); |
160 | |
|
161 | 0 | if (!opts->rgain_clip) { // clipping prevention |
162 | 0 | rgain = MPMIN(rgain, 1.0 / peak); |
163 | 0 | MP_VERBOSE(mpctx, "...with clipping prevention: %f\n", rgain); |
164 | 0 | } |
165 | 12.0k | } else if (opts->rgain_fallback) { |
166 | 0 | rgain = db_gain(opts->rgain_fallback); |
167 | 0 | MP_VERBOSE(mpctx, "Applying fallback gain: %f\n", rgain); |
168 | 0 | } |
169 | | |
170 | 12.0k | return rgain; |
171 | 12.0k | } |
172 | | |
173 | | float audio_get_gain(struct MPContext *mpctx) |
174 | 12.0k | { |
175 | 12.0k | struct MPOpts *opts = mpctx->opts; |
176 | 12.0k | float gain = MPMAX(opts->softvol_volume / 100.0, 0); |
177 | 12.0k | gain = pow(gain, 3); |
178 | 12.0k | gain *= compute_replaygain(mpctx); |
179 | 12.0k | gain *= db_gain(opts->softvol_gain); |
180 | 12.0k | if (opts->softvol_mute) |
181 | 0 | gain = 0.0; |
182 | 12.0k | return gain; |
183 | 12.0k | } |
184 | | |
185 | | // Called when opts->softvol_volume or opts->softvol_mute were changed. |
186 | | void audio_update_volume(struct MPContext *mpctx) |
187 | 39.8k | { |
188 | 39.8k | struct ao_chain *ao_c = mpctx->ao_chain; |
189 | 39.8k | if (!ao_c || !ao_c->ao) |
190 | 27.8k | return; |
191 | | |
192 | 12.0k | float gain = audio_get_gain(mpctx); |
193 | 12.0k | ao_set_gain(ao_c->ao, gain); |
194 | 12.0k | } |
195 | | |
196 | | // Call this if opts->playback_speed or mpctx->speed_factor_* change. |
197 | | void update_playback_speed(struct MPContext *mpctx) |
198 | 22.4k | { |
199 | 22.4k | mpctx->audio_speed = mpctx->opts->playback_speed * mpctx->speed_factor_a; |
200 | 22.4k | mpctx->video_speed = mpctx->opts->playback_speed * mpctx->speed_factor_v; |
201 | | |
202 | 22.4k | update_speed_filters(mpctx); |
203 | 22.4k | } |
204 | | |
205 | | static bool has_video_track(struct MPContext *mpctx) |
206 | 6.05k | { |
207 | 6.05k | if (mpctx->vo_chain && mpctx->vo_chain->is_coverart) |
208 | 0 | return false; |
209 | | |
210 | 11.8k | for (int n = 0; n < mpctx->num_tracks; n++) { |
211 | 6.06k | struct track *track = mpctx->tracks[n]; |
212 | 6.06k | if (track->type == STREAM_VIDEO && !track->attached_picture && !track->image) |
213 | 301 | return true; |
214 | 6.06k | } |
215 | | |
216 | 5.75k | return false; |
217 | 6.05k | } |
218 | | |
219 | | static void ao_chain_reset_state(struct ao_chain *ao_c) |
220 | 9.84k | { |
221 | 9.84k | ao_c->last_out_pts = MP_NOPTS_VALUE; |
222 | 9.84k | ao_c->out_eof = false; |
223 | 9.84k | ao_c->start_pts_known = false; |
224 | 9.84k | ao_c->start_pts = MP_NOPTS_VALUE; |
225 | 9.84k | ao_c->untimed_throttle = false; |
226 | 9.84k | ao_c->underrun = false; |
227 | 9.84k | ao_c->delaying_audio_start = false; |
228 | 9.84k | } |
229 | | |
230 | | void reset_audio_state(struct MPContext *mpctx) |
231 | 111k | { |
232 | 111k | if (mpctx->ao_chain) { |
233 | 9.84k | ao_chain_reset_state(mpctx->ao_chain); |
234 | 9.84k | struct track *t = mpctx->ao_chain->track; |
235 | 9.84k | if (t && t->dec) |
236 | 9.84k | mp_decoder_wrapper_set_play_dir(t->dec, mpctx->play_dir); |
237 | 9.84k | } |
238 | 111k | mpctx->audio_status = mpctx->ao_chain ? STATUS_SYNCING : STATUS_EOF; |
239 | 111k | mpctx->delay = 0; |
240 | 111k | } |
241 | | |
242 | | void uninit_audio_out(struct MPContext *mpctx) |
243 | 102k | { |
244 | 102k | struct ao_chain *ao_c = mpctx->ao_chain; |
245 | 102k | if (ao_c) { |
246 | 6.05k | ao_c->ao_queue = NULL; |
247 | 6.05k | TA_FREEP(&ao_c->queue_filter); |
248 | 6.05k | ao_c->ao = NULL; |
249 | 6.05k | } |
250 | 102k | if (mpctx->ao) { |
251 | | // Note: with gapless_audio, stop_play is not correctly set |
252 | 6.02k | if ((mpctx->opts->gapless_audio || mpctx->stop_play == AT_END_OF_FILE) && |
253 | 6.02k | ao_is_playing(mpctx->ao) && !get_internal_paused(mpctx)) |
254 | 1.27k | { |
255 | 1.27k | MP_VERBOSE(mpctx, "draining left over audio\n"); |
256 | 1.27k | ao_drain(mpctx->ao); |
257 | 1.27k | } |
258 | 6.02k | ao_uninit(mpctx->ao); |
259 | | |
260 | 6.02k | mp_notify(mpctx, MPV_EVENT_AUDIO_RECONFIG, NULL); |
261 | 6.02k | } |
262 | 102k | mpctx->ao = NULL; |
263 | 102k | TA_FREEP(&mpctx->ao_filter_fmt); |
264 | 102k | } |
265 | | |
266 | | static void ao_chain_uninit(struct ao_chain *ao_c) |
267 | 10.1k | { |
268 | 10.1k | struct track *track = ao_c->track; |
269 | 10.1k | if (track) { |
270 | 10.1k | mp_assert(track->ao_c == ao_c); |
271 | 10.1k | track->ao_c = NULL; |
272 | 10.1k | if (ao_c->dec_src) |
273 | 10.1k | mp_assert(track->dec->f->pins[0] == ao_c->dec_src); |
274 | 10.1k | if (track->dec) { |
275 | 10.1k | talloc_free(track->dec->f); |
276 | 10.1k | track->dec = NULL; |
277 | 10.1k | } |
278 | 10.1k | } |
279 | | |
280 | 10.1k | if (ao_c->filter_src) |
281 | 0 | mp_pin_disconnect(ao_c->filter_src); |
282 | | |
283 | 10.1k | talloc_free(ao_c->filter->f); |
284 | 10.1k | talloc_free(ao_c->ao_filter); |
285 | 10.1k | talloc_free(ao_c); |
286 | 10.1k | } |
287 | | |
288 | | void uninit_audio_chain(struct MPContext *mpctx) |
289 | 102k | { |
290 | 102k | if (mpctx->ao_chain) { |
291 | 10.1k | ao_chain_uninit(mpctx->ao_chain); |
292 | 10.1k | mpctx->ao_chain = NULL; |
293 | | |
294 | 10.1k | mpctx->audio_status = STATUS_EOF; |
295 | | |
296 | 10.1k | mp_notify(mpctx, MPV_EVENT_AUDIO_RECONFIG, NULL); |
297 | 10.1k | } |
298 | 102k | } |
299 | | |
300 | | static char *audio_config_to_str_buf(char *buf, size_t buf_sz, int rate, |
301 | | int format, struct mp_chmap channels) |
302 | 6.02k | { |
303 | 6.02k | char ch[128]; |
304 | 6.02k | mp_chmap_to_str_buf(ch, sizeof(ch), &channels); |
305 | 6.02k | char *hr_ch = mp_chmap_to_str_hr(&channels); |
306 | 6.02k | if (strcmp(hr_ch, ch) != 0) |
307 | 222 | mp_snprintf_cat(ch, sizeof(ch), " (%s)", hr_ch); |
308 | 6.02k | snprintf(buf, buf_sz, "%dHz %s %dch %s", rate, |
309 | 6.02k | ch, channels.num, af_fmt_to_str(format)); |
310 | 6.02k | return buf; |
311 | 6.02k | } |
312 | | |
313 | | // Decide whether on a format change, we should reinit the AO. |
314 | | static bool keep_weak_gapless_format(struct mp_aframe *old, struct mp_aframe* new) |
315 | 1.73k | { |
316 | 1.73k | bool res = false; |
317 | 1.73k | struct mp_aframe *new_mod = mp_aframe_new_ref(new); |
318 | 1.73k | MP_HANDLE_OOM(new_mod); |
319 | | |
320 | | // If the sample formats are compatible (== libswresample generally can |
321 | | // convert them), keep the AO. On other changes, recreate it. |
322 | | |
323 | 1.73k | int old_fmt = mp_aframe_get_format(old); |
324 | 1.73k | int new_fmt = mp_aframe_get_format(new); |
325 | | |
326 | 1.73k | if (af_format_conversion_score(old_fmt, new_fmt) == INT_MIN) |
327 | 0 | goto done; // completely incompatible formats |
328 | | |
329 | 1.73k | if (!mp_aframe_set_format(new_mod, old_fmt)) |
330 | 0 | goto done; |
331 | | |
332 | 1.73k | res = mp_aframe_config_equals(old, new_mod); |
333 | | |
334 | 1.73k | done: |
335 | 1.73k | talloc_free(new_mod); |
336 | 1.73k | return res; |
337 | 1.73k | } |
338 | | |
339 | | static void ao_chain_set_ao(struct ao_chain *ao_c, struct ao *ao) |
340 | 6.04k | { |
341 | 6.04k | if (ao_c->ao != ao) { |
342 | 6.02k | mp_assert(!ao_c->ao); |
343 | 6.02k | ao_c->ao = ao; |
344 | 6.02k | ao_c->ao_queue = ao_get_queue(ao_c->ao); |
345 | 6.02k | ao_c->queue_filter = mp_async_queue_create_filter(ao_c->ao_filter, |
346 | 6.02k | MP_PIN_IN, ao_c->ao_queue); |
347 | 6.02k | mp_async_queue_set_notifier(ao_c->queue_filter, ao_c->ao_filter); |
348 | | // Make sure filtering never stops with frames stuck in access filter. |
349 | 6.02k | mp_filter_set_high_priority(ao_c->queue_filter, true); |
350 | 6.02k | audio_update_volume(ao_c->mpctx); |
351 | 6.02k | } |
352 | | |
353 | 6.04k | if (ao_c->filter->ao_needs_update) |
354 | 6.04k | mp_output_chain_set_ao(ao_c->filter, ao_c->ao); |
355 | | |
356 | 6.04k | mp_filter_wakeup(ao_c->ao_filter); |
357 | 6.04k | } |
358 | | |
359 | | static int reinit_audio_filters_and_output(struct MPContext *mpctx) |
360 | 6.64k | { |
361 | 6.64k | struct MPOpts *opts = mpctx->opts; |
362 | 6.64k | struct ao_chain *ao_c = mpctx->ao_chain; |
363 | 6.64k | mp_assert(ao_c); |
364 | 6.64k | struct track *track = ao_c->track; |
365 | | |
366 | 6.64k | mp_assert(ao_c->filter->ao_needs_update); |
367 | | |
368 | | // The "ideal" filter output format |
369 | 6.64k | struct mp_aframe *out_fmt = mp_aframe_new_ref(ao_c->filter->output_aformat); |
370 | 6.64k | MP_HANDLE_OOM(out_fmt); |
371 | | |
372 | 6.64k | if (!mp_aframe_config_is_valid(out_fmt)) { |
373 | 0 | talloc_free(out_fmt); |
374 | 0 | goto init_error; |
375 | 0 | } |
376 | | |
377 | 6.64k | if (af_fmt_is_pcm(mp_aframe_get_format(out_fmt))) { |
378 | 6.64k | if (opts->force_srate) |
379 | 0 | mp_aframe_set_rate(out_fmt, opts->force_srate); |
380 | 6.64k | if (opts->audio_output_format) |
381 | 0 | mp_aframe_set_format(out_fmt, opts->audio_output_format); |
382 | 6.64k | if (opts->audio_output_channels.num_chmaps == 1) |
383 | 0 | mp_aframe_set_chmap(out_fmt, &opts->audio_output_channels.chmaps[0]); |
384 | 6.64k | } |
385 | | |
386 | | // Weak gapless audio: if the filter output format is the same as the |
387 | | // previous one, keep the AO and don't reinit anything. |
388 | | // Strong gapless: always keep the AO |
389 | 6.64k | if ((mpctx->ao_filter_fmt && mpctx->ao && opts->gapless_audio < 0 && |
390 | 1.73k | keep_weak_gapless_format(mpctx->ao_filter_fmt, out_fmt)) || |
391 | 6.62k | (mpctx->ao && opts->gapless_audio > 0)) |
392 | 21 | { |
393 | 21 | ao_chain_set_ao(ao_c, mpctx->ao); |
394 | 21 | talloc_free(out_fmt); |
395 | 21 | return 0; |
396 | 21 | } |
397 | | |
398 | | // Format change during syncing. Force playback start early, then wait. |
399 | 6.62k | if (ao_c->ao_queue && mp_async_queue_get_frames(ao_c->ao_queue) && |
400 | 1.13k | mpctx->audio_status == STATUS_SYNCING) |
401 | 571 | { |
402 | 571 | mpctx->audio_status = STATUS_READY; |
403 | 571 | mp_wakeup_core(mpctx); |
404 | 571 | talloc_free(out_fmt); |
405 | 571 | return 0; |
406 | 571 | } |
407 | 6.05k | if (mpctx->audio_status == STATUS_READY) { |
408 | 0 | talloc_free(out_fmt); |
409 | 0 | return 0; |
410 | 0 | } |
411 | | |
412 | 6.05k | uninit_audio_out(mpctx); |
413 | | |
414 | 6.05k | int out_rate = mp_aframe_get_rate(out_fmt); |
415 | 6.05k | int out_format = mp_aframe_get_format(out_fmt); |
416 | 6.05k | struct mp_chmap out_channels = {0}; |
417 | 6.05k | mp_aframe_get_chmap(out_fmt, &out_channels); |
418 | | |
419 | 6.05k | int ao_flags = 0; |
420 | 6.05k | bool spdif_fallback = !af_fmt_is_pcm(out_format) && |
421 | 0 | ao_c->spdif_passthrough; |
422 | | |
423 | 6.05k | if (opts->ao_null_fallback && !spdif_fallback) |
424 | 0 | ao_flags |= AO_INIT_NULL_FALLBACK; |
425 | | |
426 | 6.05k | if (opts->audio_stream_silence) |
427 | 0 | ao_flags |= AO_INIT_STREAM_SILENCE; |
428 | | |
429 | 6.05k | if (opts->audio_exclusive) |
430 | 0 | ao_flags |= AO_INIT_EXCLUSIVE; |
431 | | |
432 | 6.05k | if (af_fmt_is_pcm(out_format)) { |
433 | 6.05k | if (!opts->audio_output_channels.set || |
434 | 6.05k | opts->audio_output_channels.auto_safe) |
435 | 6.05k | ao_flags |= AO_INIT_SAFE_MULTICHANNEL_ONLY; |
436 | | |
437 | 6.05k | mp_chmap_sel_list(&out_channels, |
438 | 6.05k | opts->audio_output_channels.chmaps, |
439 | 6.05k | opts->audio_output_channels.num_chmaps); |
440 | 6.05k | } |
441 | | |
442 | 6.05k | if (!has_video_track(mpctx)) |
443 | 5.75k | ao_flags |= AO_INIT_MEDIA_ROLE_MUSIC; |
444 | | |
445 | 6.05k | mpctx->ao_filter_fmt = out_fmt; |
446 | | |
447 | 6.05k | mpctx->ao = ao_init_best(mpctx->global, ao_flags, mp_wakeup_core_cb, |
448 | 6.05k | mpctx, mpctx->encode_lavc_ctx, out_rate, |
449 | 6.05k | out_format, out_channels); |
450 | | |
451 | 6.05k | int ao_rate = 0; |
452 | 6.05k | int ao_format = 0; |
453 | 6.05k | struct mp_chmap ao_channels = {0}; |
454 | 6.05k | if (mpctx->ao) |
455 | 6.02k | ao_get_format(mpctx->ao, &ao_rate, &ao_format, &ao_channels); |
456 | | |
457 | | // Verify passthrough format was not changed. |
458 | 6.05k | if (mpctx->ao && !af_fmt_is_pcm(out_format)) { |
459 | 0 | if (out_rate != ao_rate || out_format != ao_format || |
460 | 0 | !mp_chmap_equals(&out_channels, &ao_channels)) |
461 | 0 | { |
462 | 0 | MP_ERR(mpctx, "Passthrough format unsupported.\n"); |
463 | 0 | ao_uninit(mpctx->ao); |
464 | 0 | mpctx->ao = NULL; |
465 | 0 | } |
466 | 0 | } |
467 | | |
468 | 6.05k | if (!mpctx->ao) { |
469 | | // If spdif was used, try to fallback to PCM. |
470 | 30 | if (spdif_fallback && ao_c->track && ao_c->track->dec) { |
471 | 0 | MP_VERBOSE(mpctx, "Falling back to PCM output.\n"); |
472 | 0 | ao_c->spdif_passthrough = false; |
473 | 0 | ao_c->spdif_failed = true; |
474 | 0 | mp_decoder_wrapper_set_spdif_flag(ao_c->track->dec, false); |
475 | 0 | if (!mp_decoder_wrapper_reinit(ao_c->track->dec)) |
476 | 0 | goto init_error; |
477 | 0 | reset_audio_state(mpctx); |
478 | 0 | mp_output_chain_reset_harder(ao_c->filter); |
479 | 0 | mp_wakeup_core(mpctx); // reinit with new format next time |
480 | 0 | return 0; |
481 | 0 | } |
482 | | |
483 | 30 | MP_ERR(mpctx, "Could not open/initialize audio device -> no sound.\n"); |
484 | 30 | mpctx->error_playing = MPV_ERROR_AO_INIT_FAILED; |
485 | 30 | goto init_error; |
486 | 30 | } |
487 | | |
488 | 6.02k | char tmp[192]; |
489 | 6.02k | MP_INFO(mpctx, "AO: [%s] %s\n", ao_get_name(mpctx->ao), |
490 | 6.02k | audio_config_to_str_buf(tmp, sizeof(tmp), ao_rate, ao_format, |
491 | 6.02k | ao_channels)); |
492 | 6.02k | MP_VERBOSE(mpctx, "AO: Description: %s\n", ao_get_description(mpctx->ao)); |
493 | 6.02k | update_window_title(mpctx, true); |
494 | | |
495 | 6.02k | ao_c->ao_resume_time = |
496 | 6.02k | opts->audio_wait_open > 0 ? mp_time_sec() + opts->audio_wait_open : 0; |
497 | | |
498 | 6.02k | bool eof = mpctx->audio_status == STATUS_EOF; |
499 | 6.02k | ao_set_paused(mpctx->ao, get_internal_paused(mpctx), eof); |
500 | | |
501 | 6.02k | ao_chain_set_ao(ao_c, mpctx->ao); |
502 | | |
503 | 6.02k | audio_update_volume(mpctx); |
504 | | |
505 | | // Almost nonsensical hack to deal with certain format change scenarios. |
506 | 6.02k | if (mpctx->audio_status == STATUS_PLAYING) |
507 | 1.14k | ao_start(mpctx->ao); |
508 | | |
509 | 6.02k | mp_wakeup_core(mpctx); |
510 | 6.02k | mp_notify(mpctx, MPV_EVENT_AUDIO_RECONFIG, NULL); |
511 | | |
512 | 6.02k | return 0; |
513 | | |
514 | 30 | init_error: |
515 | 30 | uninit_audio_chain(mpctx); |
516 | 30 | uninit_audio_out(mpctx); |
517 | 30 | error_on_track(mpctx, track); |
518 | 30 | return -1; |
519 | 6.05k | } |
520 | | |
521 | | int init_audio_decoder(struct MPContext *mpctx, struct track *track) |
522 | 10.1k | { |
523 | 10.1k | mp_assert(!track->dec); |
524 | 10.1k | if (!track->stream) |
525 | 0 | goto init_error; |
526 | | |
527 | 10.1k | track->dec = mp_decoder_wrapper_create(mpctx->filter_root, track->stream); |
528 | 10.1k | if (!track->dec) |
529 | 0 | goto init_error; |
530 | | |
531 | 10.1k | if (track->ao_c) |
532 | 10.1k | mp_decoder_wrapper_set_spdif_flag(track->dec, true); |
533 | | |
534 | 10.1k | if (!mp_decoder_wrapper_reinit(track->dec)) |
535 | 321 | goto init_error; |
536 | | |
537 | 9.84k | return 1; |
538 | | |
539 | 321 | init_error: |
540 | 321 | if (track->sink) |
541 | 0 | mp_pin_disconnect(track->sink); |
542 | 321 | track->sink = NULL; |
543 | 321 | track->dec_failed = true; |
544 | 321 | return 0; |
545 | 10.1k | } |
546 | | |
547 | | void reinit_audio_chain(struct MPContext *mpctx) |
548 | 22.7k | { |
549 | 22.7k | struct track *track = NULL; |
550 | 22.7k | track = mpctx->current_track[0][STREAM_AUDIO]; |
551 | 22.7k | if (!track || !track->stream) { |
552 | 12.6k | if (!mpctx->encode_lavc_ctx) |
553 | 12.6k | uninit_audio_out(mpctx); |
554 | 12.6k | error_on_track(mpctx, track); |
555 | 12.6k | return; |
556 | 12.6k | } |
557 | 10.1k | reinit_audio_chain_src(mpctx, track); |
558 | 10.1k | } |
559 | | |
560 | | static const struct mp_filter_info ao_filter = { |
561 | | .name = "ao", |
562 | | .process = ao_process, |
563 | | }; |
564 | | |
565 | | // Create the track's decoder and connect it to the filter chain. |
566 | | static bool ao_chain_init_decoder(struct MPContext *mpctx, struct ao_chain *ao_c) |
567 | 10.1k | { |
568 | 10.1k | struct track *track = ao_c->track; |
569 | 10.1k | if (!init_audio_decoder(mpctx, track)) |
570 | 321 | return false; |
571 | 9.84k | ao_c->dec_src = track->dec->f->pins[0]; |
572 | 9.84k | mp_pin_connect(ao_c->filter->f->pins[0], ao_c->dec_src); |
573 | 9.84k | return true; |
574 | 10.1k | } |
575 | | |
576 | | // Destroy the track's decoder, keeping the filter chain and the AO. The chain |
577 | | // stays at EOF until reinit_audio_decoder(). |
578 | | void uninit_audio_decoder(struct MPContext *mpctx) |
579 | 0 | { |
580 | 0 | struct ao_chain *ao_c = mpctx->ao_chain; |
581 | 0 | struct track *track = ao_c->track; |
582 | 0 | mp_assert(track && track->dec); |
583 | 0 | talloc_free(track->dec->f); |
584 | 0 | track->dec = NULL; |
585 | 0 | ao_c->dec_src = NULL; |
586 | 0 | } |
587 | | |
588 | | // Create a new decoder for the track and restart the chain with it. |
589 | | void reinit_audio_decoder(struct MPContext *mpctx) |
590 | 0 | { |
591 | 0 | struct ao_chain *ao_c = mpctx->ao_chain; |
592 | 0 | mp_assert(ao_c->track && !ao_c->track->dec); |
593 | | // init_audio_decoder() deselects the track and destroys the chain on failure. |
594 | 0 | if (!ao_chain_init_decoder(mpctx, ao_c)) |
595 | 0 | return; |
596 | | // The filter chain still holds the old decoder's EOF. |
597 | 0 | mp_filter_reset(ao_c->filter->f); |
598 | 0 | reset_audio_state(mpctx); |
599 | 0 | } |
600 | | |
601 | | // (track=NULL creates a blank chain, used for lavfi-complex) |
602 | | void reinit_audio_chain_src(struct MPContext *mpctx, struct track *track) |
603 | 10.1k | { |
604 | 10.1k | mp_assert(!mpctx->ao_chain); |
605 | | |
606 | 10.1k | mp_notify(mpctx, MPV_EVENT_AUDIO_RECONFIG, NULL); |
607 | | |
608 | 10.1k | struct ao_chain *ao_c = talloc_zero(NULL, struct ao_chain); |
609 | 10.1k | mpctx->ao_chain = ao_c; |
610 | 10.1k | ao_c->mpctx = mpctx; |
611 | 10.1k | ao_c->log = mpctx->log; |
612 | 10.1k | ao_c->filter = |
613 | 10.1k | mp_output_chain_create(mpctx->filter_root, MP_OUTPUT_CHAIN_AUDIO); |
614 | 10.1k | ao_c->spdif_passthrough = true; |
615 | 10.1k | ao_c->last_out_pts = MP_NOPTS_VALUE; |
616 | 10.1k | ao_c->delay = mpctx->opts->audio_delay; |
617 | | |
618 | 10.1k | ao_c->ao_filter = mp_filter_create(mpctx->filter_root, &ao_filter); |
619 | 10.1k | if (!ao_c->filter || !ao_c->ao_filter) |
620 | 0 | goto init_error; |
621 | 10.1k | ao_c->ao_filter->priv = ao_c; |
622 | | |
623 | 10.1k | mp_filter_add_pin(ao_c->ao_filter, MP_PIN_IN, "in"); |
624 | 10.1k | mp_pin_connect(ao_c->ao_filter->pins[0], ao_c->filter->f->pins[1]); |
625 | | |
626 | 10.1k | if (track) { |
627 | 10.1k | ao_c->track = track; |
628 | 10.1k | track->ao_c = ao_c; |
629 | 10.1k | if (!ao_chain_init_decoder(mpctx, ao_c)) |
630 | 321 | goto init_error; |
631 | 10.1k | } |
632 | | |
633 | 9.84k | reset_audio_state(mpctx); |
634 | | |
635 | 9.84k | if (recreate_audio_filters(mpctx) < 0) |
636 | 0 | goto init_error; |
637 | | |
638 | 9.84k | if (mpctx->ao) |
639 | 0 | audio_update_volume(mpctx); |
640 | | |
641 | 9.84k | mp_wakeup_core(mpctx); |
642 | 9.84k | return; |
643 | | |
644 | 321 | init_error: |
645 | 321 | uninit_audio_chain(mpctx); |
646 | 321 | uninit_audio_out(mpctx); |
647 | 321 | error_on_track(mpctx, track); |
648 | 321 | } |
649 | | |
650 | | // Return pts value corresponding to the start point of audio written to the |
651 | | // ao queue so far. |
652 | | double written_audio_pts(struct MPContext *mpctx) |
653 | 48.9k | { |
654 | 48.9k | return mpctx->ao_chain ? mpctx->ao_chain->last_out_pts : MP_NOPTS_VALUE; |
655 | 48.9k | } |
656 | | |
657 | | // Return pts value corresponding to currently playing audio adjusted for AO delay |
658 | | // and playback speed. |
659 | | double playing_audio_pts(struct MPContext *mpctx) |
660 | 48.9k | { |
661 | 48.9k | double pts = written_audio_pts(mpctx); |
662 | 48.9k | if (pts == MP_NOPTS_VALUE || !mpctx->ao) |
663 | 23.5k | return pts; |
664 | 25.4k | return pts - mpctx->audio_speed * ao_get_delay(mpctx->ao); |
665 | 48.9k | } |
666 | | |
667 | | // This garbage is needed for untimed AOs. These consume audio infinitely fast, |
668 | | // so try keeping approximate A/V sync by blocking audio transfer as needed. |
669 | | static void update_throttle(struct MPContext *mpctx) |
670 | 1.16M | { |
671 | 1.16M | struct ao_chain *ao_c = mpctx->ao_chain; |
672 | 1.16M | bool new_throttle = mpctx->audio_status == STATUS_PLAYING && |
673 | 976k | mpctx->delay > 0 && ao_c && ao_c->ao && |
674 | 0 | ao_untimed(ao_c->ao) && |
675 | 0 | mpctx->video_status != STATUS_EOF; |
676 | 1.16M | if (ao_c && new_throttle != ao_c->untimed_throttle) { |
677 | 0 | ao_c->untimed_throttle = new_throttle; |
678 | 0 | mp_wakeup_core(mpctx); |
679 | 0 | mp_filter_wakeup(ao_c->ao_filter); |
680 | 0 | } |
681 | 1.16M | } |
682 | | |
683 | | static void ao_process(struct mp_filter *f) |
684 | 2.02M | { |
685 | 2.02M | struct ao_chain *ao_c = f->priv; |
686 | 2.02M | struct MPContext *mpctx = ao_c->mpctx; |
687 | | |
688 | | // Once the chain reached EOF nothing is read from it again. |
689 | 2.02M | if (ao_c->out_eof) |
690 | 10.1k | return; |
691 | | |
692 | 2.01M | if (!ao_c->queue_filter) { |
693 | | // This will eventually lead to the creation of the AO + queue, due |
694 | | // to how f_output_chain and AO management works. |
695 | 14.7k | mp_pin_out_request_data(f->ppins[0]); |
696 | | // Check for EOF with no data case, which is a mess because everything |
697 | | // hates us. |
698 | 14.7k | struct mp_frame frame = mp_pin_out_read(f->ppins[0]); |
699 | 14.7k | if (frame.type == MP_FRAME_EOF) { |
700 | 4.93k | MP_VERBOSE(mpctx, "got EOF with no data before it\n"); |
701 | 4.93k | ao_c->out_eof = true; |
702 | 4.93k | mpctx->audio_status = STATUS_DRAINING; |
703 | 4.93k | mp_wakeup_core(mpctx); |
704 | 9.84k | } else if (frame.type) { |
705 | 0 | mp_pin_out_unread(f->ppins[0], frame); |
706 | 0 | } |
707 | 14.7k | return; |
708 | 14.7k | } |
709 | | |
710 | | // Due to mp_async_queue_set_notifier() this function is called when the |
711 | | // queue becomes full. This affects state changes in the normal playloop, |
712 | | // so wake it up. But avoid redundant wakeups during normal playback. |
713 | 2.00M | if (mpctx->audio_status != STATUS_PLAYING && |
714 | 62.0k | mp_async_queue_is_full(ao_c->ao_queue)) |
715 | 2.29k | mp_wakeup_core(mpctx); |
716 | | |
717 | 2.00M | if (mpctx->audio_status == STATUS_SYNCING && !ao_c->start_pts_known) |
718 | 200 | return; |
719 | | |
720 | 2.00M | if (ao_c->untimed_throttle) |
721 | 0 | return; |
722 | | |
723 | 2.00M | if (!mp_pin_can_transfer_data(ao_c->queue_filter->pins[0], f->ppins[0])) |
724 | 1.00M | return; |
725 | | |
726 | 1.00M | struct mp_frame frame = mp_pin_out_read(f->ppins[0]); |
727 | 1.00M | if (frame.type == MP_FRAME_AUDIO) { |
728 | 997k | struct mp_aframe *af = frame.data; |
729 | | |
730 | 997k | double endpts = get_play_end_pts(mpctx); |
731 | 997k | if (endpts != MP_NOPTS_VALUE) { |
732 | 0 | endpts *= mpctx->play_dir; |
733 | | // Avoid decoding and discarding the entire rest of the file. |
734 | 0 | if (mp_aframe_get_pts(af) >= endpts) { |
735 | 0 | mp_pin_out_unread(f->ppins[0], frame); |
736 | 0 | if (!ao_c->out_eof) { |
737 | 0 | ao_c->out_eof = true; |
738 | 0 | mp_pin_in_write(ao_c->queue_filter->pins[0], MP_EOF_FRAME); |
739 | 0 | } |
740 | 0 | return; |
741 | 0 | } |
742 | 0 | } |
743 | 997k | double startpts = mpctx->audio_status == STATUS_SYNCING ? |
744 | 967k | ao_c->start_pts : MP_NOPTS_VALUE; |
745 | 997k | mp_aframe_clip_timestamps(af, startpts, endpts); |
746 | | |
747 | 997k | int samples = mp_aframe_get_size(af); |
748 | 997k | if (!samples) { |
749 | 1 | mp_filter_internal_mark_progress(f); |
750 | 1 | mp_frame_unref(&frame); |
751 | 1 | return; |
752 | 1 | } |
753 | | |
754 | 997k | mpctx->shown_aframes += samples; |
755 | 997k | double real_samplerate = mp_aframe_get_rate(af) / mpctx->audio_speed; |
756 | 997k | if (mpctx->video_status != STATUS_EOF) |
757 | 111 | mpctx->delay += samples / real_samplerate; |
758 | 997k | ao_c->last_out_pts = mp_aframe_end_pts(af); |
759 | 997k | update_throttle(mpctx); |
760 | | |
761 | | // Gapless case: the AO is still playing from previous file. It makes |
762 | | // no sense to wait, and in fact the "full queue" event we're waiting |
763 | | // for may never happen, so start immediately. |
764 | | // If the new audio starts "later" (big video sync offset), transfer |
765 | | // of data is stopped somewhere else. |
766 | 997k | if (mpctx->audio_status == STATUS_SYNCING && ao_is_playing(ao_c->ao)) { |
767 | 0 | mpctx->audio_status = STATUS_READY; |
768 | 0 | mp_wakeup_core(mpctx); |
769 | 0 | MP_VERBOSE(mpctx, "previous audio still playing; continuing\n"); |
770 | 0 | } |
771 | | |
772 | 997k | mp_pin_in_write(ao_c->queue_filter->pins[0], frame); |
773 | 997k | } else if (frame.type == MP_FRAME_EOF) { |
774 | 4.86k | MP_VERBOSE(mpctx, "audio filter EOF\n"); |
775 | | |
776 | 4.86k | ao_c->out_eof = true; |
777 | 4.86k | mp_wakeup_core(mpctx); |
778 | | |
779 | 4.86k | mp_pin_in_write(ao_c->queue_filter->pins[0], frame); |
780 | 4.86k | mp_filter_internal_mark_progress(f); |
781 | 4.86k | } else { |
782 | 0 | mp_frame_unref(&frame); |
783 | 0 | } |
784 | 1.00M | } |
785 | | |
786 | | void reload_audio_output(struct MPContext *mpctx) |
787 | 27.8k | { |
788 | 27.8k | if (!mpctx->ao) |
789 | 27.8k | return; |
790 | | |
791 | 0 | ao_reset(mpctx->ao); |
792 | 0 | uninit_audio_out(mpctx); |
793 | 0 | reinit_audio_filters(mpctx); // mostly to issue refresh seek |
794 | |
|
795 | 0 | struct ao_chain *ao_c = mpctx->ao_chain; |
796 | |
|
797 | 0 | if (ao_c) { |
798 | 0 | reset_audio_state(mpctx); |
799 | 0 | mp_output_chain_reset_harder(ao_c->filter); |
800 | 0 | } |
801 | | |
802 | | // Whether we can use spdif might have changed. If we failed to use spdif |
803 | | // in the previous initialization, try it with spdif again (we'll fallback |
804 | | // to PCM again if necessary). |
805 | 0 | if (ao_c && ao_c->track) { |
806 | 0 | struct mp_decoder_wrapper *dec = ao_c->track->dec; |
807 | 0 | if (dec && ao_c->spdif_failed) { |
808 | 0 | ao_c->spdif_passthrough = true; |
809 | 0 | ao_c->spdif_failed = false; |
810 | 0 | mp_decoder_wrapper_set_spdif_flag(ao_c->track->dec, true); |
811 | 0 | if (!mp_decoder_wrapper_reinit(dec)) { |
812 | 0 | MP_ERR(mpctx, "Error reinitializing audio.\n"); |
813 | 0 | error_on_track(mpctx, ao_c->track); |
814 | 0 | } |
815 | 0 | } |
816 | 0 | } |
817 | |
|
818 | 0 | mp_wakeup_core(mpctx); |
819 | 0 | } |
820 | | |
821 | | // Returns audio start pts for seeking or video sync. |
822 | | // Returns false if PTS is not known yet. |
823 | | static bool get_sync_pts(struct MPContext *mpctx, double *pts) |
824 | 78.6k | { |
825 | 78.6k | struct MPOpts *opts = mpctx->opts; |
826 | | |
827 | 78.6k | *pts = MP_NOPTS_VALUE; |
828 | | |
829 | 78.6k | if (!opts->initial_audio_sync) |
830 | 0 | return true; |
831 | | |
832 | 78.6k | bool sync_to_video = mpctx->vo_chain && mpctx->video_status != STATUS_EOF && |
833 | 8.90k | !mpctx->vo_chain->is_sparse; |
834 | | |
835 | 78.6k | if (sync_to_video) { |
836 | 8.90k | if (mpctx->video_status < STATUS_READY) |
837 | 8.62k | return false; // wait until we know a video PTS |
838 | 283 | if (mpctx->video_pts != MP_NOPTS_VALUE) |
839 | 283 | *pts = mpctx->video_pts - opts->audio_delay; |
840 | 69.7k | } else if (mpctx->hrseek_active) { |
841 | 0 | *pts = mpctx->hrseek_pts; |
842 | 69.7k | } else { |
843 | | // If audio-only is enabled mid-stream during playback, sync accordingly. |
844 | 69.7k | *pts = mpctx->playback_pts; |
845 | 69.7k | } |
846 | | |
847 | 69.9k | return true; |
848 | 78.6k | } |
849 | | |
850 | | // Look whether audio can be started yet - if audio has to start some time |
851 | | // after video. |
852 | | // Caller needs to ensure mpctx->restart_complete is OK |
853 | | void audio_start_ao(struct MPContext *mpctx) |
854 | 18.0k | { |
855 | 18.0k | struct ao_chain *ao_c = mpctx->ao_chain; |
856 | 18.0k | if (!ao_c || !ao_c->ao || mpctx->audio_status != STATUS_READY) |
857 | 13.2k | return; |
858 | 4.88k | double pts = MP_NOPTS_VALUE; |
859 | 4.88k | if (!get_sync_pts(mpctx, &pts)) |
860 | 0 | return; |
861 | 4.88k | double apts = playing_audio_pts(mpctx); |
862 | 4.88k | if (mpctx->video_status != STATUS_EOF) { |
863 | 101 | double speed = mpctx->opts->playback_speed; |
864 | 101 | if (!ao_c->delaying_audio_start && pts != MP_NOPTS_VALUE && |
865 | 91 | apts != MP_NOPTS_VALUE && pts < apts) |
866 | 10 | { |
867 | 10 | MP_VERBOSE(mpctx, "delaying audio start %f vs. %f, diff=%f\n", |
868 | 10 | apts, pts, (apts - pts) / speed); |
869 | 10 | ao_c->delaying_audio_start = true; |
870 | 10 | ao_c->delayed_start_gap = apts - pts; |
871 | 10 | mpctx->delay += apts - pts; |
872 | 10 | ao_c->delayed_start_speed = 0; |
873 | 10 | } |
874 | 101 | if (ao_c->delaying_audio_start) { |
875 | | // Start audio once the video has played the gap to it. The video |
876 | | // pts moves in frame steps and ahead of the display, so the gap |
877 | | // is counted down in playback time from the first check on, every |
878 | | // interval at the speed it was played with. |
879 | 20 | int64_t now = mp_time_ns(); |
880 | 20 | double passed = MP_TIME_NS_TO_S(now - ao_c->delayed_start_last); |
881 | 20 | ao_c->delayed_start_gap -= passed * ao_c->delayed_start_speed; |
882 | 20 | ao_c->delayed_start_last = now; |
883 | 20 | ao_c->delayed_start_speed = get_internal_paused(mpctx) ? 0 : speed; |
884 | 20 | if (ao_c->delayed_start_gap > 0) { |
885 | 10 | if (ao_c->delayed_start_speed > 0) |
886 | 10 | mp_set_timeout(mpctx, ao_c->delayed_start_gap / speed); |
887 | 10 | return; |
888 | 10 | } |
889 | 20 | } |
890 | 101 | } |
891 | | |
892 | 4.87k | MP_VERBOSE(mpctx, "starting audio playback\n"); |
893 | 4.87k | ao_c->delaying_audio_start = false; |
894 | 4.87k | ao_start(ao_c->ao); |
895 | 4.87k | mpctx->audio_status = STATUS_PLAYING; |
896 | 4.87k | if (ao_c->out_eof) { |
897 | 2.01k | mpctx->audio_status = STATUS_DRAINING; |
898 | 2.01k | MP_VERBOSE(mpctx, "audio draining\n"); |
899 | 2.01k | } |
900 | 4.87k | ao_c->underrun = false; |
901 | 4.87k | mp_wakeup_core(mpctx); |
902 | 4.87k | } |
903 | | |
904 | | void fill_audio_out_buffers(struct MPContext *mpctx) |
905 | 170k | { |
906 | 170k | struct MPOpts *opts = mpctx->opts; |
907 | | |
908 | 170k | if (mpctx->ao && ao_query_and_reset_events(mpctx->ao, AO_EVENT_RELOAD)) |
909 | 0 | reload_audio_output(mpctx); |
910 | | |
911 | 170k | update_throttle(mpctx); |
912 | | |
913 | 170k | struct ao_chain *ao_c = mpctx->ao_chain; |
914 | 170k | if (!ao_c) |
915 | 79.8k | return; |
916 | | |
917 | 90.5k | if (ao_c->filter->failed_output_conversion) { |
918 | 0 | error_on_track(mpctx, ao_c->track); |
919 | 0 | return; |
920 | 0 | } |
921 | | |
922 | 90.5k | if (ao_c->filter->ao_needs_update) { |
923 | 6.64k | if (reinit_audio_filters_and_output(mpctx) < 0) |
924 | 30 | return; |
925 | 6.64k | } |
926 | | |
927 | 90.5k | if (mpctx->vo_chain && ao_c->track && ao_c->track->dec && |
928 | 12.0k | mp_decoder_wrapper_get_pts_reset(ao_c->track->dec)) |
929 | 0 | { |
930 | 0 | MP_WARN(mpctx, "Reset playback due to audio timestamp reset.\n"); |
931 | 0 | reset_playback_state(mpctx); |
932 | 0 | mp_wakeup_core(mpctx); |
933 | 0 | } |
934 | | |
935 | 90.5k | if (mpctx->audio_status == STATUS_SYNCING) { |
936 | 73.7k | double pts; |
937 | 73.7k | bool ok = get_sync_pts(mpctx, &pts); |
938 | | |
939 | | // If the AO is still playing from the previous file (due to gapless), |
940 | | // but if video is active, this may not work if audio starts later than |
941 | | // video, and gapless has no advantages anyway. So block doing anything |
942 | | // until the old audio is fully played. |
943 | | // (Buggy if AO underruns.) |
944 | 73.7k | if (mpctx->ao && ao_is_playing(mpctx->ao) && |
945 | 0 | mpctx->video_status != STATUS_EOF) { |
946 | 0 | MP_VERBOSE(mpctx, "blocked, waiting for old audio to play\n"); |
947 | 0 | ok = false; |
948 | 0 | } |
949 | | |
950 | 73.7k | if (ao_c->start_pts_known != ok || ao_c->start_pts != pts) { |
951 | 7.38k | ao_c->start_pts_known = ok; |
952 | 7.38k | ao_c->start_pts = pts; |
953 | 7.38k | mp_filter_wakeup(ao_c->ao_filter); |
954 | 7.38k | } |
955 | | |
956 | 73.7k | if (ao_c->ao && mp_async_queue_is_full(ao_c->ao_queue)) { |
957 | 2.29k | mpctx->audio_status = STATUS_READY; |
958 | 2.29k | mp_wakeup_core(mpctx); |
959 | 2.29k | MP_VERBOSE(mpctx, "audio ready\n"); |
960 | 71.4k | } else if (ao_c->out_eof) { |
961 | | // Force playback start early. |
962 | 2.01k | mpctx->audio_status = STATUS_READY; |
963 | 2.01k | mp_wakeup_core(mpctx); |
964 | 2.01k | MP_VERBOSE(mpctx, "audio ready (and EOF)\n"); |
965 | 2.01k | } |
966 | 73.7k | } |
967 | | |
968 | 90.5k | if (ao_c->ao && !ao_is_playing(ao_c->ao) && !ao_c->underrun && |
969 | 12.0k | (mpctx->audio_status == STATUS_PLAYING || |
970 | 10.3k | mpctx->audio_status == STATUS_DRAINING)) |
971 | 2.14k | { |
972 | | // Should be playing, but somehow isn't. |
973 | | |
974 | 2.14k | if (ao_c->out_eof && !mp_async_queue_get_frames(ao_c->ao_queue)) { |
975 | 2.14k | MP_VERBOSE(mpctx, "AO signaled EOF (while in state %s)\n", |
976 | 2.14k | mp_status_str(mpctx->audio_status)); |
977 | 2.14k | mpctx->audio_status = STATUS_EOF; |
978 | 2.14k | mp_wakeup_core(mpctx); |
979 | | // stops untimed AOs, stops pull AOs from streaming silence |
980 | 2.14k | ao_reset(ao_c->ao); |
981 | 2.14k | } else { |
982 | 0 | if (!ao_c->ao_underrun) { |
983 | 0 | MP_WARN(mpctx, "Audio device underrun detected.\n"); |
984 | 0 | ao_c->ao_underrun = true; |
985 | 0 | mp_wakeup_core(mpctx); |
986 | 0 | ao_c->underrun = true; |
987 | 0 | } |
988 | | |
989 | | // Wait until buffers are filled before recovering underrun. |
990 | 0 | if (ao_c->out_eof || mp_async_queue_is_full(ao_c->ao_queue)) { |
991 | 0 | MP_VERBOSE(mpctx, "restarting audio after underrun\n"); |
992 | 0 | ao_start(mpctx->ao_chain->ao); |
993 | 0 | ao_c->ao_underrun = false; |
994 | 0 | ao_c->underrun = false; |
995 | 0 | mp_wakeup_core(mpctx); |
996 | 0 | } |
997 | 0 | } |
998 | 2.14k | } |
999 | | |
1000 | 90.5k | if (mpctx->audio_status == STATUS_PLAYING && ao_c->out_eof) { |
1001 | 1.04k | mpctx->audio_status = STATUS_DRAINING; |
1002 | 1.04k | MP_VERBOSE(mpctx, "audio draining\n"); |
1003 | 1.04k | mp_wakeup_core(mpctx); |
1004 | 1.04k | } |
1005 | | |
1006 | 90.5k | if (mpctx->audio_status == STATUS_DRAINING) { |
1007 | | // Wait until the AO has played all queued data. In the gapless case, |
1008 | | // we trigger EOF immediately, and let it play asynchronously. |
1009 | 7.63k | if (!ao_c->ao || (!ao_is_playing(ao_c->ao) || opts->gapless_audio)) |
1010 | 7.63k | { |
1011 | 7.63k | MP_VERBOSE(mpctx, "audio EOF reached\n"); |
1012 | 7.63k | mpctx->audio_status = STATUS_EOF; |
1013 | 7.63k | mp_wakeup_core(mpctx); |
1014 | 7.63k | } |
1015 | 7.63k | } |
1016 | | |
1017 | 90.5k | if (mpctx->restart_complete) |
1018 | 13.2k | audio_start_ao(mpctx); // in case it got delayed |
1019 | 90.5k | } |
1020 | | |
1021 | | // Drop data queued for output, or which the AO is currently outputting. |
1022 | | void clear_audio_output_buffers(struct MPContext *mpctx) |
1023 | 85.7k | { |
1024 | 85.7k | if (mpctx->ao) |
1025 | 44 | ao_reset(mpctx->ao); |
1026 | 85.7k | } |