Skip to content

Commit

Permalink
test: add default track selection testing
Browse files Browse the repository at this point in the history
The amount of options we have that are related track selection is insane
and touching any of this stuff is scary. Add some track selection
testing to the CI to hopefully make it slightly less scary to touch any
of that. Since trying to work in media files from some outside source
would be a pain and need to live in some repo somewhere, it's nicer to
just generate dummy files on the fly with ffmpeg during the ci runs and
use that. Obviously, the actual tests themselves could be even more
thorough (external tracks were not even considered), but this is more
than a good enough start for now.
  • Loading branch information
Dudemanguy committed Feb 12, 2025
1 parent 25067e8 commit f7a681b
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/libmpv_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ static inline void check_api_error(int status)
static inline void initialize(void)
{
check_api_error(mpv_set_option_string(ctx, "vo", "null"));
check_api_error(mpv_set_option_string(ctx, "ao", "null"));
check_api_error(mpv_request_log_messages(ctx, "debug"));
check_api_error(mpv_initialize(ctx));
}
194 changes: 194 additions & 0 deletions test/libmpv_test_track_selection.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*
* This file is part of mpv.
*
* mpv is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* mpv 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
*/

#include <unistd.h>
#include "libmpv_common.h"

static void check_string(const char *property, const char *expect)
{
char *result_string;
check_api_error(mpv_get_property(ctx, property, MPV_FORMAT_STRING, &result_string));
if (strcmp(expect, result_string) != 0)
fail("String: expected '%s' but got '%s'!\n", expect, result_string);
mpv_free(result_string);
}

static void wait_for_file_load(void)
{
bool loaded = false;
while (!loaded) {
mpv_event *event = wrap_wait_event();
switch (event->event_id) {
case MPV_EVENT_FILE_LOADED:
loaded = true;
break;
}
}
}

static void test_track_selection(char *file, char *path)
{
int ret = access(path, F_OK);
if (ret)
fail("Test file, '%s', was not found!\n", path);

const char *cmd[] = {"loadfile", path, NULL};
if (strcmp(file, "eng_default.mkv") == 0) {
// --no-config
check_api_error(mpv_command(ctx, cmd));
wait_for_file_load();
check_string("current-tracks/sub/selected", "yes");

// --subs-falback=no
check_api_error(mpv_set_property_string(ctx, "subs-fallback", "no"));
check_api_error(mpv_command(ctx, cmd));
wait_for_file_load();
check_string("track-list/2/selected", "no");

// reset options to defaults
check_api_error(mpv_set_property_string(ctx, "subs-fallback", "default"));
} else if (strcmp(file, "eng_default_forced.mkv") == 0) {
// --subs-fallback-forced=no
check_api_error(mpv_set_property_string(ctx, "subs-fallback-forced", "no"));
check_api_error(mpv_command(ctx, cmd));
wait_for_file_load();
check_string("current-tracks/sub/selected", "yes");

// reset options to defaults
check_api_error(mpv_set_property_string(ctx, "subs-fallback-forced", "yes"));
} else if (strcmp(file, "eng_forced_matching_audio.mkv") == 0) {
// select forced track
check_api_error(mpv_command(ctx, cmd));
wait_for_file_load();
check_string("current-tracks/sub/selected", "yes");
} else if (strcmp(file, "eng_forced_no_matching_audio.mkv") == 0) {
// forced track should not be selected
check_api_error(mpv_command(ctx, cmd));
wait_for_file_load();
check_string("track-list/2/selected", "no");
} else if (strcmp(file, "eng_forced_always_audio.mkv") == 0) {
// forced track should be selected anyway despite no matching audio
check_api_error(mpv_set_property_string(ctx, "subs-fallback-forced", "always"));
check_api_error(mpv_command(ctx, cmd));
wait_for_file_load();
check_string("current-tracks/sub/selected", "yes");
} else if (strcmp(file, "eng_no_default.mkv") == 0) {
// track should not be selected
check_api_error(mpv_command(ctx, cmd));
wait_for_file_load();
check_string("track-list/2/selected", "no");

// --subs-fallback=yes
check_api_error(mpv_set_property_string(ctx, "subs-fallback", "yes"));
check_api_error(mpv_command(ctx, cmd));
wait_for_file_load();
check_string("current-tracks/sub/selected", "yes");

// reset options to defaults
check_api_error(mpv_set_property_string(ctx, "subs-fallback", "default"));
} else if (strcmp(file, "multilang.mkv") == 0) {
// --alang=jpn should select forced jpn subs
check_api_error(mpv_set_property_string(ctx, "alang", "jpn"));
check_api_error(mpv_command(ctx, cmd));
wait_for_file_load();
check_string("current-tracks/audio/lang", "jpn");
check_string("current-tracks/sub/lang", "jpn");

// --alang=pol should select default, non-forced ger subs
check_api_error(mpv_set_property_string(ctx, "alang", "pol"));
check_api_error(mpv_command(ctx, cmd));
wait_for_file_load();
check_string("current-tracks/audio/lang", "pol");
check_string("current-tracks/sub/lang", "ger");

// --slang=eng and --subs-with-matching-audio should not pick any subs
check_api_error(mpv_set_property_string(ctx, "alang", "eng"));
check_api_error(mpv_set_property_string(ctx, "slang", "eng"));
check_api_error(mpv_set_property_string(ctx, "subs-with-matching-audio", "no"));
check_api_error(mpv_command(ctx, cmd));
wait_for_file_load();
check_string("current-tracks/audio/lang", "eng");
check_string("track-list/5/selected", "no");
check_string("track-list/6/selected", "no");
check_string("track-list/7/selected", "no");
check_string("track-list/8/selected", "no");

// --subs-with-matching-audio=forced checks
check_api_error(mpv_set_property_string(ctx, "subs-with-matching-audio", "forced"));
check_api_error(mpv_command(ctx, cmd));
wait_for_file_load();
check_string("current-tracks/audio/lang", "eng");
check_string("current-tracks/sub/lang", "eng");

// forced jpn subs should be selected
check_api_error(mpv_set_property_string(ctx, "alang", "jpn"));
check_api_error(mpv_set_property_string(ctx, "slang", "jpn"));
check_api_error(mpv_command(ctx, cmd));
wait_for_file_load();
check_string("current-tracks/audio/lang", "jpn");
check_string("current-tracks/sub/lang", "jpn");

// default+forced eng subs should be selected
check_api_error(mpv_set_property_string(ctx, "alang", "ger"));
check_api_error(mpv_set_property_string(ctx, "slang", "ger"));
check_api_error(mpv_command(ctx, cmd));
wait_for_file_load();
check_string("current-tracks/audio/lang", "ger");
check_string("current-tracks/sub/lang", "eng");

// eng audio and pol subs should be selected
check_api_error(mpv_set_property_string(ctx, "alang", "it"));
check_api_error(mpv_set_property_string(ctx, "slang", "pt,it,pol,ger"));
check_api_error(mpv_command(ctx, cmd));
wait_for_file_load();
check_string("current-tracks/audio/lang", "eng");
check_string("current-tracks/sub/lang", "pol");

// forced jpn subs should be selected
check_api_error(mpv_set_property_string(ctx, "alang", "ger"));
check_api_error(mpv_set_property_string(ctx, "slang", "jpn,pol"));
check_api_error(mpv_set_property_string(ctx, "subs-with-matching-audio", "yes"));
check_api_error(mpv_set_property_string(ctx, "subs-fallback-forced", "always"));
check_api_error(mpv_command(ctx, cmd));
wait_for_file_load();
check_string("current-tracks/audio/lang", "ger");
check_string("current-tracks/sub/lang", "jpn");
}
}

int main(int argc, char *argv[])
{
if (argc < 3)
return 1;
atexit(exit_cleanup);

ctx = mpv_create();
if (!ctx)
return 1;

initialize();

const char *fmt = "================ TEST: %s %s ================\n";
printf(fmt, "test_track_selection", argv[1]);
test_track_selection(argv[1], argv[2]);
printf("================ SHUTDOWN ================\n");

mpv_command_string(ctx, "quit");
while (wrap_wait_event()->event_id != MPV_EVENT_SHUTDOWN) {}

return 0;
}
9 changes: 9 additions & 0 deletions test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ if get_option('libmpv')
include_directories: incdir, link_with: libmpv)
test('libmpv-test-options', exe, suite: 'libmpv')

# Old versions of ffmpeg are bugged when setting forced tracks and older
# versions of meson don't support the custom version checking argument.
if meson.version().version_compare('>= 1.5.0')
ffmpeg = find_program('ffmpeg', required: false, version: '>= 6.1', version_argument: '-version')
if ffmpeg.found()
subdir('samples')
endif
endif

exe = executable('libmpv-encode', 'libmpv_encode.c',
include_directories: incdir, link_with: libmpv)
test('libmpv-encode', exe, suite: 'libmpv')
Expand Down
57 changes: 57 additions & 0 deletions test/samples/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Create core video, audio, and subtitle streams to construct files out of.
video = custom_target('video.mkv',
output: 'video.mkv',
command: [ffmpeg, '-v', 'error', '-y', '-f', 'lavfi', '-i',
'testsrc=duration=2:size=1280x720', '@OUTPUT@'],
)

audio = custom_target('audio.flac',
output: 'audio.flac',
command: [ffmpeg, '-v', 'error', '-y', '-f', 'lavfi', '-i',
'sine=frequency=1000:duration=2', '@OUTPUT@'],
)

sub = join_paths(source_root, 'test', 'samples', 'sub.srt')

common_args = ['-v', 'error', '-y', '-i', video, '-i', audio,
'-f', 'srt', '-i', sub, '-c', 'copy',
'-metadata:s:s:0', 'language=eng']

samples = {
'eng_default.mkv':
[ffmpeg, common_args, '-disposition:s:0', 'default', '@OUTPUT@'],
'eng_default_forced.mkv':
[ffmpeg, common_args, '-disposition:s:0', 'default+forced', '@OUTPUT@'],
'eng_forced_matching_audio.mkv':
[ffmpeg, common_args, '-metadata:s:a:0', 'language=eng',
'-disposition:s:0', 'forced', '@OUTPUT@'],
'eng_forced_no_matching_audio.mkv':
[ffmpeg, common_args, '-disposition:s:0', 'forced', '@OUTPUT@'],
'eng_forced_always_audio.mkv':
[ffmpeg, common_args, '-metadata:s:a:0', 'language=ger',
'-disposition:s:0', 'forced', '@OUTPUT@'],
'eng_no_default.mkv':
[ffmpeg, common_args, '@OUTPUT@'],
'multilang.mkv':
[ffmpeg, '-v', 'error', '-y', '-i', video, '-i', audio, '-i', audio, '-i', audio, '-i', audio,
'-f', 'srt', '-i', sub, '-i', sub, '-i', sub, '-i', sub, '-c', 'copy', '-map', '0:0',
'-map', '1:0', '-map', '2:0', '-map', '3:0', '-map', '4:0', '-map', '5:0',
'-map', '6:0', '-map', '7:0', '-map', '8:0', '-metadata:s:a:0', 'language=eng',
'-metadata:s:a:1', 'language=jpn', '-metadata:s:a:2', 'language=ger', '-metadata:s:a:3',
'language=pol', '-metadata:s:s:0', 'language=eng', '-metadata:s:s:1', 'language=jpn',
'-metadata:s:s:2', 'language=ger', '-metadata:s:s:3', 'language=pol', '-disposition:s:0',
'default+forced', '-disposition:s:1', 'forced', '-disposition:s:2', 'default', '@OUTPUT@'],
}

foreach name, cmd: samples
target = custom_target(name,
output: name,
depends: [video, audio],
command: cmd,
)
testname = 'libmpv-test-' + name
exe = executable(testname, '../libmpv_test_track_selection.c',
include_directories: incdir, link_with: libmpv)
test(testname, exe, args: [name, target.full_path()],
depends: target, suite: 'libmpv')
endforeach
6 changes: 6 additions & 0 deletions test/samples/sub.srt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1
00:00:00,000 --> 00:00:01,000
foo
2
00:00:01,000 --> 00:00:02,000
bar

0 comments on commit f7a681b

Please sign in to comment.