|
| 1 | +/* |
| 2 | + * libmpv audio output driver |
| 3 | + * |
| 4 | + * This file is part of mpv. |
| 5 | + * |
| 6 | + * mpv is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 2.1 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * mpv is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public |
| 17 | + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "ao.h" |
| 21 | +#include "audio/format.h" |
| 22 | +#include "ao_libmpv.h" |
| 23 | +#include "internal.h" |
| 24 | +#include "common/msg.h" |
| 25 | + |
| 26 | +struct priv { |
| 27 | + void (*write_cb)(void *userdata, const void *data, int bytes); |
| 28 | + void *userdata; |
| 29 | +}; |
| 30 | + |
| 31 | +void ao_libmpv_set_cb(struct ao *ao, void (*cb)(void *userdata, const void *data, int bytes), void *userdata) |
| 32 | +{ |
| 33 | + struct priv *p = ao->priv; |
| 34 | + p->write_cb = cb; |
| 35 | + p->userdata = userdata; |
| 36 | +} |
| 37 | + |
| 38 | +static int init(struct ao *ao) |
| 39 | +{ |
| 40 | + ao->format = af_fmt_from_planar(ao->format); |
| 41 | + |
| 42 | + struct mp_chmap_sel sel = {0}; |
| 43 | + mp_chmap_sel_add_waveext(&sel); |
| 44 | + if (!ao_chmap_sel_adjust(ao, &sel, &ao->channels)) |
| 45 | + return -1; |
| 46 | + |
| 47 | + ao->bps = ao->channels.num * (int64_t)ao->samplerate * af_fmt_to_bytes(ao->format); |
| 48 | + |
| 49 | + MP_INFO(ao, "libmpv: Samplerate: %d Hz Channels: %d Format: %s\n", |
| 50 | + ao->samplerate, ao->channels.num, af_fmt_to_str(ao->format)); |
| 51 | + |
| 52 | + ao->untimed = true; |
| 53 | + ao->device_buffer = 1 << 16; |
| 54 | + |
| 55 | + return 0; |
| 56 | +} |
| 57 | + |
| 58 | +static void uninit(struct ao *ao) |
| 59 | +{ |
| 60 | +} |
| 61 | + |
| 62 | +static bool audio_write(struct ao *ao, void **data, int samples) |
| 63 | +{ |
| 64 | + struct priv *priv = ao->priv; |
| 65 | + const int len = samples * ao->sstride; |
| 66 | + |
| 67 | + if (priv->write_cb) |
| 68 | + priv->write_cb(priv->userdata, data[0], len); |
| 69 | + |
| 70 | + return true; |
| 71 | +} |
| 72 | + |
| 73 | +static void get_state(struct ao *ao, struct mp_pcm_state *state) |
| 74 | +{ |
| 75 | + state->free_samples = ao->device_buffer; |
| 76 | + state->queued_samples = 0; |
| 77 | + state->delay = 0; |
| 78 | +} |
| 79 | + |
| 80 | +static bool set_pause(struct ao *ao, bool paused) |
| 81 | +{ |
| 82 | + return true; // signal support so common code doesn't write silence |
| 83 | +} |
| 84 | + |
| 85 | +static void start(struct ao *ao) |
| 86 | +{ |
| 87 | + // we use data immediately |
| 88 | +} |
| 89 | + |
| 90 | +static void reset(struct ao *ao) |
| 91 | +{ |
| 92 | +} |
| 93 | + |
| 94 | +#define OPT_BASE_STRUCT struct priv |
| 95 | + |
| 96 | +const struct ao_driver audio_out_libmpv = { |
| 97 | + .description = "libmpv audio output with a callback", |
| 98 | + .name = "libmpv", |
| 99 | + .init = init, |
| 100 | + .uninit = uninit, |
| 101 | + .get_state = get_state, |
| 102 | + .set_pause = set_pause, |
| 103 | + .write = audio_write, |
| 104 | + .start = start, |
| 105 | + .reset = reset, |
| 106 | + .priv_size = sizeof(struct priv), |
| 107 | +}; |
0 commit comments