mediastreamer2_player_tester.c 5.58 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"
François Grisez's avatar
François Grisez committed
22 23

static int tester_init() {
24
	ortp_set_log_level_mask(ORTP_MESSAGE | ORTP_WARNING | ORTP_ERROR | ORTP_FATAL);
François Grisez's avatar
François Grisez committed
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
	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((useconds_t)(refresh_time_ms) * 1000U);
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, int timeout) {
François Grisez's avatar
François Grisez committed
65 66
	bool_t succeed;
	Eof eof;
67
	MSMediaPlayer *file_player = NULL;
François Grisez's avatar
François Grisez committed
68
	MSSndCard *snd_card = ms_snd_card_manager_get_default_card(ms_snd_card_manager_get());
François Grisez's avatar
François Grisez committed
69 70 71 72
	const char *display_name = video_stream_get_default_video_renderer();

	eof_init(&eof);

73
	file_player = ms_media_player_new(snd_card, display_name, 0);
François Grisez's avatar
François Grisez committed
74 75 76
	CU_ASSERT_PTR_NOT_NULL(file_player);
	if(file_player == NULL) return;

77
	CU_ASSERT_EQUAL(ms_media_player_get_state(file_player), MSPlayerClosed);
78
	ms_media_player_set_eof_callback(file_player, eof_callback, &eof);
François Grisez's avatar
François Grisez committed
79

80
	succeed = ms_media_player_open(file_player, filepath);
François Grisez's avatar
François Grisez committed
81 82
	if(unsupported_format) {
		CU_ASSERT_FALSE(succeed);
83
		CU_ASSERT_EQUAL(ms_media_player_get_state(file_player), MSPlayerClosed);
François Grisez's avatar
François Grisez committed
84 85
	} else {
		CU_ASSERT_TRUE(succeed);
86
		CU_ASSERT_EQUAL(ms_media_player_get_state(file_player), MSPlayerPaused);
François Grisez's avatar
François Grisez committed
87 88
	}
	if(!succeed) {
89
		ms_media_player_free(file_player);
François Grisez's avatar
François Grisez committed
90 91 92
		return;
	}

93

94
	succeed = ms_media_player_start(file_player);
François Grisez's avatar
François Grisez committed
95
	CU_ASSERT_TRUE(succeed);
96
	CU_ASSERT_EQUAL(ms_media_player_get_state(file_player), MSPlayerPlaying);
François Grisez's avatar
François Grisez committed
97

98
	if(seeking_test) {
99
		CU_ASSERT_TRUE(ms_media_player_seek(file_player, 5000));
100 101
	}

François Grisez's avatar
François Grisez committed
102
	if(succeed) {
103
		wait_for_eof(&eof, 100, timeout);
104
		CU_ASSERT_TRUE(eof.eof);
François Grisez's avatar
François Grisez committed
105
	}
106
	ms_media_player_close(file_player);
107
	CU_ASSERT_EQUAL(ms_media_player_get_state(file_player), MSPlayerClosed);
108 109 110 111 112 113 114 115 116

	if(play_twice) {
		eof_init(&eof);
		CU_ASSERT_TRUE(ms_media_player_open(file_player, filepath));
		CU_ASSERT_TRUE(ms_media_player_start(file_player));
		wait_for_eof(&eof, 100, timeout);
		ms_media_player_close(file_player);
		CU_ASSERT_TRUE(eof.eof);
	}
117
	ms_media_player_free(file_player);
François Grisez's avatar
François Grisez committed
118 119
}

120 121
static void play_root_file(const char *filepath, bool_t unsupported_format, bool_t seeking_test, bool_t play_twice, int timeout){
    char* file = ms_strdup_printf("%s/%s", mediastreamer2_tester_get_file_root(), filepath);
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
122
    play_file(file, unsupported_format, seeking_test, play_twice, timeout);
123 124 125
    ms_free(file);
}

François Grisez's avatar
François Grisez committed
126
static void play_hello_8000_wav(void) {
François Grisez's avatar
François Grisez committed
127
	play_root_file("sounds/hello8000.wav", FALSE, FALSE, FALSE, 20000);
François Grisez's avatar
François Grisez committed
128 129 130
}

static void play_hello_16000_wav(void) {
François Grisez's avatar
François Grisez committed
131
	play_root_file("sounds/hello16000.wav", FALSE, FALSE, FALSE, 20000);
François Grisez's avatar
François Grisez committed
132 133 134
}

static void play_hello_pcmu_mka(void) {
François Grisez's avatar
François Grisez committed
135
	play_root_file("sounds/hello_pcmu.mka", !ms_media_player_matroska_supported(), FALSE, FALSE, 20000);
François Grisez's avatar
François Grisez committed
136 137 138
}

static void play_hello_opus_mka(void) {
François Grisez's avatar
François Grisez committed
139
	play_root_file("sounds/hello_opus.mka", !ms_media_player_matroska_supported(), FALSE, FALSE, 20000);
François Grisez's avatar
François Grisez committed
140 141 142
}

static void play_hello_pcmu_h264_mkv(void) {
François Grisez's avatar
François Grisez committed
143
	play_root_file("sounds/hello_pcmu_h264.mkv", !ms_media_player_matroska_supported(), FALSE, FALSE, 20000);
François Grisez's avatar
François Grisez committed
144 145 146
}

static void play_hello_opus_h264_mkv(void) {
François Grisez's avatar
François Grisez committed
147
	play_root_file("sounds/hello_opus_h264.mkv", !ms_media_player_matroska_supported(), FALSE, FALSE, 20000);
148 149
}

150
static void seeking_test(void) {
François Grisez's avatar
François Grisez committed
151
	play_root_file("sounds/hello_opus_h264.mkv", !ms_media_player_matroska_supported(), TRUE, FALSE, 15000);
152 153 154
}

static void playing_twice_test(void) {
155
	play_root_file("./sounds/hello_opus_h264.mkv", !ms_media_player_matroska_supported(), FALSE, TRUE, 15000);
François Grisez's avatar
François Grisez committed
156 157 158
}

static test_t tests[] = {
159 160 161 162 163 164
	{	"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 hello_pcmu_h264.mkv"          ,	play_hello_pcmu_h264_mkv          },
	{	"Play hello_opus_h264.mkv"          ,	play_hello_opus_h264_mkv          },
165 166
	{	"Seeking"                           ,	seeking_test                      },
	{	"Playing twice"                     ,	playing_twice_test                }
François Grisez's avatar
François Grisez committed
167 168 169 170 171 172 173 174 175
};

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