Use fps to set appropriate audio buffer size

If we get a sane container-fps value from mpv, we use it to set an
appropriate buffer size length.

Added some further comments.

Signed-off-by: Mahyar Koshkouei <mahyar.koshkouei@gmail.com>
This commit is contained in:
Mahyar Koshkouei 2018-02-23 12:27:43 +00:00
parent 6e0e2b949c
commit 6bb5c358f3
1 changed files with 25 additions and 6 deletions

View File

@ -16,6 +16,7 @@
*/ */
#include <dlfcn.h> #include <dlfcn.h>
#include <math.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -305,6 +306,9 @@ static void context_reset(void)
mpv_set_option_string(mpv, "ao", "audio-cb"); mpv_set_option_string(mpv, "ao", "audio-cb");
/* Attempt to enable hardware acceleration. MPV will fallback to software
* decoding on failure.
*/
if(mpv_set_option_string(mpv, "hwdec", "auto") < 0) if(mpv_set_option_string(mpv, "hwdec", "auto") < 0)
log_cb(RETRO_LOG_ERROR, "failed to set hwdec option\n"); log_cb(RETRO_LOG_ERROR, "failed to set hwdec option\n");
@ -330,8 +334,14 @@ static void context_reset(void)
/* TODO #2: Check for the highest samplerate in audio stream, and use that. /* TODO #2: Check for the highest samplerate in audio stream, and use that.
* Fall back to 48kHz otherwise. * Fall back to 48kHz otherwise.
* We currently force the audio to be sampled at 48KHz.
*/ */
mpv_set_option_string(mpv, "audio-samplerate", "48000"); mpv_set_option_string(mpv, "audio-samplerate", "48000");
mpv_set_option_string(mpv, "opengl-swapinterval", "0");
/* The following works best when vsync is switched off in Retroarch. */
//mpv_set_option_string(mpv, "video-sync", "display-resample");
//mpv_set_option_string(mpv, "display-fps", "60");
log_cb(RETRO_LOG_INFO, "Context reset.\n"); log_cb(RETRO_LOG_INFO, "Context reset.\n");
@ -397,10 +407,10 @@ void retro_reset(void)
return; return;
} }
static void audio_callback(void) static void audio_callback(double fps)
{ {
/* Obtain len samples to reduce lag. */ /* Obtain len samples to reduce lag. */
int len = 2*1024; int len = 48000 / (int)floor(fps);
static int16_t frames[512]; static int16_t frames[512];
while(len > 0) while(len > 0)
@ -493,11 +503,13 @@ void retro_run(void)
*/ */
static bool updated_video_dimensions = false; static bool updated_video_dimensions = false;
static int64_t width = 0, height = 0; static int64_t width = 0, height = 0;
static double container_fps = 30.0f;
if(updated_video_dimensions == false) if(updated_video_dimensions == false)
{ {
mpv_get_property(mpv, "dwidth", MPV_FORMAT_INT64, &width); mpv_get_property(mpv, "dwidth", MPV_FORMAT_INT64, &width);
mpv_get_property(mpv, "dheight", MPV_FORMAT_INT64, &height); mpv_get_property(mpv, "dheight", MPV_FORMAT_INT64, &height);
mpv_get_property(mpv, "container-fps", MPV_FORMAT_DOUBLE, &container_fps);
/* We don't know the dimensions of the video when /* We don't know the dimensions of the video when
* retro_get_system_av_info is called, so we have to set it here for * retro_get_system_av_info is called, so we have to set it here for
@ -514,8 +526,10 @@ void retro_run(void)
.aspect_ratio = -1, .aspect_ratio = -1,
}; };
log_cb(RETRO_LOG_INFO, "Setting fps to %f\n", container_fps);
struct retro_system_timing timing = { struct retro_system_timing timing = {
.fps = 60.0f, .fps = container_fps,
.sample_rate = 48000.0f, .sample_rate = 48000.0f,
}; };
@ -534,6 +548,10 @@ void retro_run(void)
retropad_update_input(); retropad_update_input();
/* TODO #2: Implement an audio callback feature in to libmpv */
audio_callback(container_fps);
#if 1
if(frame_queue > 0) if(frame_queue > 0)
{ {
mpv_opengl_cb_draw(mpv_gl, hw_render.get_current_framebuffer(), width, height); mpv_opengl_cb_draw(mpv_gl, hw_render.get_current_framebuffer(), width, height);
@ -542,9 +560,10 @@ void retro_run(void)
} }
else else
video_cb(NULL, width, height, 0); video_cb(NULL, width, height, 0);
#else
/* TODO #2: Implement an audio callback feature in to libmpv */ mpv_opengl_cb_draw(mpv_gl, hw_render.get_current_framebuffer(), width, height);
audio_callback(); video_cb(RETRO_HW_FRAME_BUFFER_VALID, width, height, 0);
#endif
return; return;
} }