2012-04-27 09:26:04 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mplayer2.
|
|
|
|
*
|
|
|
|
* mplayer2 is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mplayer2 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with mplayer2. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2012-08-15 20:23:02 +00:00
|
|
|
#include <libavutil/common.h>
|
2012-04-27 09:26:04 +00:00
|
|
|
#include <portaudio.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/m_option.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "audio/format.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2014-03-08 23:17:18 +00:00
|
|
|
#include "osdep/timer.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "ao.h"
|
2014-03-07 14:24:32 +00:00
|
|
|
#include "internal.h"
|
2012-04-27 09:26:04 +00:00
|
|
|
|
|
|
|
struct priv {
|
|
|
|
PaStream *stream;
|
2013-07-21 19:35:29 +00:00
|
|
|
|
|
|
|
// Options
|
|
|
|
char *cfg_device;
|
2012-04-27 09:26:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct format_map {
|
|
|
|
int mp_format;
|
|
|
|
PaSampleFormat pa_format;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct format_map format_maps[] = {
|
|
|
|
// first entry is the default format
|
2013-11-15 20:25:05 +00:00
|
|
|
{AF_FORMAT_S16, paInt16},
|
|
|
|
{AF_FORMAT_S24, paInt24},
|
|
|
|
{AF_FORMAT_S32, paInt32},
|
|
|
|
{AF_FORMAT_S8, paInt8},
|
|
|
|
{AF_FORMAT_U8, paUInt8},
|
|
|
|
{AF_FORMAT_FLOAT, paFloat32},
|
2012-04-27 09:26:04 +00:00
|
|
|
{AF_FORMAT_UNKNOWN, 0}
|
|
|
|
};
|
|
|
|
|
2013-12-21 19:03:36 +00:00
|
|
|
static bool check_pa_ret(struct mp_log *log, int ret)
|
2012-04-27 09:26:04 +00:00
|
|
|
{
|
|
|
|
if (ret < 0) {
|
2013-12-21 19:03:36 +00:00
|
|
|
mp_err(log, "%s\n", Pa_GetErrorText(ret));
|
2012-04-27 09:26:04 +00:00
|
|
|
if (ret == paUnanticipatedHostError) {
|
|
|
|
const PaHostErrorInfo* hosterr = Pa_GetLastHostErrorInfo();
|
2013-12-21 19:03:36 +00:00
|
|
|
mp_err(log, "Host error: %s\n", hosterr->errorText);
|
2012-04-27 09:26:04 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-21 19:03:36 +00:00
|
|
|
#define CHECK_PA_RET(ret) check_pa_ret(ao->log, (ret))
|
|
|
|
|
2012-04-27 09:26:04 +00:00
|
|
|
static int to_int(const char *s, int return_on_error)
|
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
int res = strtol(s, &endptr, 10);
|
|
|
|
return (s[0] && !endptr[0]) ? res : return_on_error;
|
|
|
|
}
|
|
|
|
|
2013-12-21 19:03:36 +00:00
|
|
|
static int find_device(struct mp_log *log, const char *name)
|
2012-04-27 09:26:04 +00:00
|
|
|
{
|
2013-07-21 19:35:29 +00:00
|
|
|
int found = paNoDevice;
|
|
|
|
if (!name)
|
|
|
|
return found;
|
2012-04-27 09:26:04 +00:00
|
|
|
int help = strcmp(name, "help") == 0;
|
|
|
|
int count = Pa_GetDeviceCount();
|
2013-12-21 19:03:36 +00:00
|
|
|
check_pa_ret(log, count);
|
2012-04-27 09:26:04 +00:00
|
|
|
int index = to_int(name, -1);
|
|
|
|
if (help)
|
2013-12-21 19:03:36 +00:00
|
|
|
mp_info(log, "PortAudio devices:\n");
|
2012-04-27 09:26:04 +00:00
|
|
|
for (int n = 0; n < count; n++) {
|
|
|
|
const PaDeviceInfo* info = Pa_GetDeviceInfo(n);
|
|
|
|
if (help) {
|
|
|
|
if (info->maxOutputChannels < 1)
|
|
|
|
continue;
|
2013-12-21 19:03:36 +00:00
|
|
|
mp_info(log, " %d '%s', %d channels, latency: %.2f "
|
2012-04-27 09:26:04 +00:00
|
|
|
"ms, sample rate: %.0f\n", n, info->name,
|
|
|
|
info->maxOutputChannels,
|
|
|
|
info->defaultHighOutputLatency * 1000,
|
|
|
|
info->defaultSampleRate);
|
|
|
|
}
|
|
|
|
if (strcmp(name, info->name) == 0 || n == index) {
|
|
|
|
found = n;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found == paNoDevice && !help)
|
2013-12-21 19:03:36 +00:00
|
|
|
mp_warn(log, "Device '%s' not found!\n", name);
|
2012-04-27 09:26:04 +00:00
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2013-12-21 19:03:36 +00:00
|
|
|
static int validate_device_opt(struct mp_log *log, const m_option_t *opt,
|
|
|
|
struct bstr name, struct bstr param)
|
2013-07-21 19:35:29 +00:00
|
|
|
{
|
|
|
|
// Note: we do not check whether the device actually exist, because this
|
|
|
|
// might break elaborate configs with several AOs trying several
|
|
|
|
// devices. We do it merely for making "help" special.
|
|
|
|
if (bstr_equals0(param, "help")) {
|
2013-12-21 19:03:36 +00:00
|
|
|
if (!check_pa_ret(log, Pa_Initialize()))
|
2013-07-21 19:35:29 +00:00
|
|
|
return M_OPT_EXIT;
|
2013-12-21 19:03:36 +00:00
|
|
|
find_device(log, "help");
|
2013-07-21 19:35:29 +00:00
|
|
|
Pa_Terminate();
|
|
|
|
return M_OPT_EXIT - 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-27 09:26:04 +00:00
|
|
|
static int stream_callback(const void *input,
|
|
|
|
void *output_v,
|
|
|
|
unsigned long frameCount,
|
|
|
|
const PaStreamCallbackTimeInfo *timeInfo,
|
|
|
|
PaStreamCallbackFlags statusFlags,
|
|
|
|
void *userData)
|
|
|
|
{
|
|
|
|
struct ao *ao = userData;
|
|
|
|
|
|
|
|
// NOTE: PA + ALSA in dmix mode seems to pretend that there is no latency
|
|
|
|
// (outputBufferDacTime == currentTime)
|
2014-03-08 23:17:18 +00:00
|
|
|
double play_time = timeInfo->outputBufferDacTime
|
|
|
|
+ frameCount / (float)ao->samplerate;
|
|
|
|
double latency = play_time - timeInfo->currentTime;
|
|
|
|
int64_t end = mp_time_us() + MPMAX(0, latency * 1000000.0);
|
2012-04-27 09:26:04 +00:00
|
|
|
|
2014-03-08 23:17:18 +00:00
|
|
|
ao_read_data(ao, &output_v, frameCount, end);
|
2012-04-27 09:26:04 +00:00
|
|
|
|
2014-03-08 23:17:18 +00:00
|
|
|
return paContinue;
|
2012-04-27 09:26:04 +00:00
|
|
|
}
|
|
|
|
|
2014-03-08 23:49:39 +00:00
|
|
|
static void uninit(struct ao *ao)
|
2012-04-27 09:26:04 +00:00
|
|
|
{
|
|
|
|
struct priv *priv = ao->priv;
|
|
|
|
|
|
|
|
if (priv->stream) {
|
2014-03-08 23:49:39 +00:00
|
|
|
if (Pa_IsStreamActive(priv->stream) == 1)
|
2013-12-21 19:03:36 +00:00
|
|
|
CHECK_PA_RET(Pa_StopStream(priv->stream));
|
|
|
|
CHECK_PA_RET(Pa_CloseStream(priv->stream));
|
2012-04-27 09:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Pa_Terminate();
|
|
|
|
}
|
|
|
|
|
2013-07-22 20:57:51 +00:00
|
|
|
static int init(struct ao *ao)
|
2012-04-27 09:26:04 +00:00
|
|
|
{
|
2013-07-21 19:35:29 +00:00
|
|
|
struct priv *priv = ao->priv;
|
2012-04-27 09:26:04 +00:00
|
|
|
|
2013-12-21 19:03:36 +00:00
|
|
|
if (!CHECK_PA_RET(Pa_Initialize()))
|
2012-04-27 09:26:04 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
int pa_device = Pa_GetDefaultOutputDevice();
|
2013-07-21 19:35:29 +00:00
|
|
|
if (priv->cfg_device && priv->cfg_device[0])
|
2013-12-21 19:03:36 +00:00
|
|
|
pa_device = find_device(ao->log, priv->cfg_device);
|
2012-04-27 09:26:04 +00:00
|
|
|
if (pa_device == paNoDevice)
|
|
|
|
goto error_exit;
|
|
|
|
|
2013-05-09 16:06:26 +00:00
|
|
|
// The actual channel order probably depends on the platform.
|
|
|
|
struct mp_chmap_sel sel = {0};
|
|
|
|
mp_chmap_sel_add_waveext_def(&sel);
|
|
|
|
if (!ao_chmap_sel_adjust(ao, &sel, &ao->channels))
|
|
|
|
goto error_exit;
|
2013-04-05 21:06:22 +00:00
|
|
|
|
2012-04-27 09:26:04 +00:00
|
|
|
PaStreamParameters sp = {
|
|
|
|
.device = pa_device,
|
2013-04-05 21:06:22 +00:00
|
|
|
.channelCount = ao->channels.num,
|
2012-04-27 09:26:04 +00:00
|
|
|
.suggestedLatency
|
|
|
|
= Pa_GetDeviceInfo(pa_device)->defaultHighOutputLatency,
|
|
|
|
};
|
|
|
|
|
2013-11-10 22:15:02 +00:00
|
|
|
ao->format = af_fmt_from_planar(ao->format);
|
|
|
|
|
2012-04-27 09:26:04 +00:00
|
|
|
const struct format_map *fmt = format_maps;
|
|
|
|
while (fmt->pa_format) {
|
|
|
|
if (fmt->mp_format == ao->format) {
|
|
|
|
PaStreamParameters test = sp;
|
|
|
|
test.sampleFormat = fmt->pa_format;
|
|
|
|
if (Pa_IsFormatSupported(NULL, &test, ao->samplerate) == paNoError)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fmt++;
|
|
|
|
}
|
|
|
|
if (!fmt->pa_format) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_VERBOSE(ao, "Unsupported format, using default.\n");
|
2012-04-27 09:26:04 +00:00
|
|
|
fmt = format_maps;
|
|
|
|
}
|
|
|
|
|
|
|
|
ao->format = fmt->mp_format;
|
|
|
|
sp.sampleFormat = fmt->pa_format;
|
2014-05-27 06:21:18 +00:00
|
|
|
int framelen = ao->channels.num * af_fmt2bps(ao->format);
|
2014-03-08 23:17:18 +00:00
|
|
|
ao->bps = ao->samplerate * framelen;
|
2012-04-27 09:26:04 +00:00
|
|
|
|
2013-12-21 19:03:36 +00:00
|
|
|
if (!CHECK_PA_RET(Pa_IsFormatSupported(NULL, &sp, ao->samplerate)))
|
2012-04-27 09:26:04 +00:00
|
|
|
goto error_exit;
|
2013-12-21 19:03:36 +00:00
|
|
|
if (!CHECK_PA_RET(Pa_OpenStream(&priv->stream, NULL, &sp, ao->samplerate,
|
2012-04-27 09:26:04 +00:00
|
|
|
paFramesPerBufferUnspecified, paNoFlag,
|
|
|
|
stream_callback, ao)))
|
|
|
|
goto error_exit;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error_exit:
|
2014-03-08 23:49:39 +00:00
|
|
|
uninit(ao);
|
2012-04-27 09:26:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reset(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct priv *priv = ao->priv;
|
|
|
|
|
|
|
|
if (Pa_IsStreamStopped(priv->stream) != 1)
|
2013-12-21 19:03:36 +00:00
|
|
|
CHECK_PA_RET(Pa_AbortStream(priv->stream));
|
2012-04-27 09:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void resume(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct priv *priv = ao->priv;
|
|
|
|
|
2014-03-08 23:17:18 +00:00
|
|
|
if (Pa_IsStreamStopped(priv->stream) == 1)
|
|
|
|
CHECK_PA_RET(Pa_StartStream(priv->stream));
|
2012-04-27 09:26:04 +00:00
|
|
|
}
|
|
|
|
|
2013-07-21 19:35:29 +00:00
|
|
|
#define OPT_BASE_STRUCT struct priv
|
|
|
|
|
2012-04-27 09:26:04 +00:00
|
|
|
const struct ao_driver audio_out_portaudio = {
|
2013-10-23 17:07:27 +00:00
|
|
|
.description = "PortAudio",
|
|
|
|
.name = "portaudio",
|
2012-04-27 09:26:04 +00:00
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.reset = reset,
|
|
|
|
.resume = resume,
|
2013-07-21 19:35:29 +00:00
|
|
|
.priv_size = sizeof(struct priv),
|
|
|
|
.options = (const struct m_option[]) {
|
|
|
|
OPT_STRING_VALIDATE("device", cfg_device, 0, validate_device_opt),
|
|
|
|
{0}
|
|
|
|
},
|
2012-04-27 09:26:04 +00:00
|
|
|
};
|