mediastreamer2_player_tester.c 6.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006-2013 Belledonne Communications, Grenoble

This program 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.

This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

François Grisez's avatar
François Grisez committed
20
#include "mediastreamer2_tester.h"
21
#include "mediastreamer2/msmediaplayer.h"
22
#include "mediastreamer2/mediastream.h"
François Grisez's avatar
François Grisez committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

static int tester_init() {
	ms_init();
	return 0;
}

static int tester_cleanup() {
	ms_exit();
	return 0;
}

typedef struct _Eof {
	bool_t eof;
	int time_ms;
	ms_mutex_t mutex;
} Eof;

static void eof_init(Eof *obj) {
	obj->eof = FALSE;
	obj->time_ms = 0;
	ms_mutex_init(&obj->mutex, NULL);
}

static void eof_callback(void *user_data) {
	Eof *obj = (Eof *)user_data;
	ms_mutex_lock(&obj->mutex);
	obj->eof = TRUE;
	ms_mutex_unlock(&obj->mutex);
}

static void wait_for_eof(Eof *obj, int refresh_time_ms, int timeout_ms) {
	ms_mutex_lock(&obj->mutex);
	while(obj->time_ms < timeout_ms && !obj->eof) {
		ms_mutex_unlock(&obj->mutex);
57
		ms_usleep(refresh_time_ms * 1000);
François Grisez's avatar
François Grisez committed
58 59 60 61 62 63
		obj->time_ms += refresh_time_ms;
		ms_mutex_lock(&obj->mutex);
	}
	ms_mutex_unlock(&obj->mutex);
}

64
static void play_file(const char *filepath, bool_t unsupported_format, bool_t seeking_test, bool_t play_twice) {
François Grisez's avatar
François Grisez committed
65 66
	bool_t succeed;
	Eof eof;
67
	MSMediaPlayer *file_player = NULL;
68
	MSSndCard *snd_card = ms_snd_card_manager_get_default_playback_card(ms_snd_card_manager_get());
François Grisez's avatar
François Grisez committed
69
	const char *display_name = video_stream_get_default_video_renderer();
70 71 72
	int duration, timeout;
	const int seek_time = 6100;
	const double timeout_prec = 0.05;
François Grisez's avatar
François Grisez committed
73 74 75

	eof_init(&eof);

76
	BC_ASSERT_PTR_NOT_NULL(snd_card);
77
	file_player = ms_media_player_new(snd_card, display_name, 0);
78
	BC_ASSERT_PTR_NOT_NULL(file_player);
François Grisez's avatar
François Grisez committed
79 80
	if(file_player == NULL) return;

81
	BC_ASSERT_EQUAL(ms_media_player_get_state(file_player), MSPlayerClosed, int, "%d");
82
	ms_media_player_set_eof_callback(file_player, eof_callback, &eof);
François Grisez's avatar
François Grisez committed
83

84
	succeed = ms_media_player_open(file_player, filepath);
François Grisez's avatar
François Grisez committed
85
	if(unsupported_format) {
86 87
		BC_ASSERT_FALSE(succeed);
		BC_ASSERT_EQUAL(ms_media_player_get_state(file_player), MSPlayerClosed, int, "%d");
François Grisez's avatar
François Grisez committed
88
	} else {
89 90
		BC_ASSERT_TRUE(succeed);
		BC_ASSERT_EQUAL(ms_media_player_get_state(file_player), MSPlayerPaused, int, "%d");
François Grisez's avatar
François Grisez committed
91 92
	}
	if(!succeed) {
93
		ms_media_player_free(file_player);
François Grisez's avatar
François Grisez committed
94 95 96
		return;
	}

97 98
	duration = ms_media_player_get_duration(file_player);
	if(ms_media_player_get_file_format(file_player) == MS_FILE_FORMAT_WAVE) {
99
		BC_ASSERT_TRUE(duration == -1);
100 101
		duration = 20000;
	} else {
102
		BC_ASSERT_TRUE(duration >= 0);
103 104 105 106 107 108 109
	}

	if(seeking_test) {
		timeout = (duration - seek_time) * (1 + timeout_prec);
	} else {
		timeout = duration * (1 + timeout_prec);
	}
110

111
	succeed = ms_media_player_start(file_player);
112 113
	BC_ASSERT_TRUE(succeed);
	BC_ASSERT_EQUAL(ms_media_player_get_state(file_player), MSPlayerPlaying, int, "%d");
François Grisez's avatar
François Grisez committed
114

115
	if(seeking_test) {
116
		BC_ASSERT_TRUE(ms_media_player_seek(file_player, seek_time));
117 118
	}

François Grisez's avatar
François Grisez committed
119
	if(succeed) {
120
		wait_for_eof(&eof, 100, timeout);
121
		BC_ASSERT_TRUE(eof.eof);
François Grisez's avatar
François Grisez committed
122
	}
123
	ms_media_player_close(file_player);
124
	BC_ASSERT_EQUAL(ms_media_player_get_state(file_player), MSPlayerClosed, int, "%d");
125 126 127

	if(play_twice) {
		eof_init(&eof);
128 129
		BC_ASSERT_TRUE(ms_media_player_open(file_player, filepath));
		BC_ASSERT_TRUE(ms_media_player_start(file_player));
130 131
		wait_for_eof(&eof, 100, timeout);
		ms_media_player_close(file_player);
132
		BC_ASSERT_TRUE(eof.eof);
133
	}
134
	ms_media_player_free(file_player);
François Grisez's avatar
François Grisez committed
135 136
}

137
static void play_root_file(const char *filepath, bool_t unsupported_format, bool_t seeking_test, bool_t play_twice){
138
	char* file = ms_strdup_printf("%s/%s", bc_tester_read_dir_prefix, filepath);
139
	play_file(file, unsupported_format, seeking_test, play_twice);
140
	ms_free(file);
141 142
}

François Grisez's avatar
François Grisez committed
143
static void play_hello_8000_wav(void) {
144
	play_root_file("sounds/hello8000.wav", FALSE, FALSE, FALSE);
François Grisez's avatar
François Grisez committed
145 146 147
}

static void play_hello_16000_wav(void) {
148
	play_root_file("sounds/hello16000.wav", FALSE, FALSE, FALSE);
François Grisez's avatar
François Grisez committed
149 150 151
}

static void play_hello_pcmu_mka(void) {
152
	play_root_file("sounds/hello_pcmu.mka", !ms_media_player_matroska_supported(), FALSE, FALSE);
François Grisez's avatar
François Grisez committed
153 154 155
}

static void play_hello_opus_mka(void) {
156
	play_root_file("sounds/hello_opus.mka", !ms_media_player_matroska_supported(), FALSE, FALSE);
François Grisez's avatar
François Grisez committed
157 158
}

159 160
static void play_sintel_trailer_pcmu_h264_mkv(void) {
	play_root_file("sounds/sintel_trailer_pcmu_h264.mkv", !ms_media_player_matroska_supported(), FALSE, FALSE);
François Grisez's avatar
François Grisez committed
161 162
}

163 164
static void play_sintel_trailer_opus_h264_mkv(void) {
	play_root_file("sounds/sintel_trailer_opus_h264.mkv", !ms_media_player_matroska_supported(), FALSE, FALSE);
165 166
}

167
static void seeking_test(void) {
168
	play_root_file("sounds/sintel_trailer_opus_h264.mkv", !ms_media_player_matroska_supported(), TRUE, FALSE);
169 170 171
}

static void playing_twice_test(void) {
172
	play_root_file("./sounds/sintel_trailer_opus_h264.mkv", !ms_media_player_matroska_supported(), FALSE, TRUE);
François Grisez's avatar
François Grisez committed
173 174 175
}

static test_t tests[] = {
176 177 178 179 180 181 182 183
	{	"Play hello8000.wav"                 ,	play_hello_8000_wav                },
	{	"Play hello16000.wav"                ,	play_hello_16000_wav               },
	{	"Play hello_pcmu.mka"                ,	play_hello_pcmu_mka                },
	{	"Play hello_opus.mka"                ,	play_hello_opus_mka                },
	{	"Play sintel_trailer_pcmu_h264.mkv"  ,	play_sintel_trailer_pcmu_h264_mkv  },
	{	"Play sintel_trailer_opus_h264.mkv"  ,	play_sintel_trailer_opus_h264_mkv  },
	{	"Seeking"                            ,	seeking_test                       },
	{	"Playing twice"                      ,	playing_twice_test                 }
François Grisez's avatar
François Grisez committed
184 185 186 187 188 189 190 191 192
};

test_suite_t player_test_suite = {
	"Player",
	tester_init,
	tester_cleanup,
	sizeof(tests)/sizeof(test_t),
	tests
};