2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
22 #include <video_util.h>
24 #define PACKAGE "video_util_test"
25 #define MAX_STRING_LEN 256
31 #define LOG_TAG "VIDEO_UTIL_TEST"
34 CURRENT_STATUS_MAINMENU,
35 CURRENT_STATUS_FILENAME,
36 CURRENT_STATUS_SET_FORMAT,
37 CURRENT_STATUS_SET_VIDEO_CODEC,
38 CURRENT_STATUS_SET_AUDIO_CODEC,
39 CURRENT_STATUS_SET_ACCURATE_MODE,
40 CURRENT_STATUS_SET_RESOLUTION,
41 CURRENT_STATUS_SET_FPS,
42 CURRENT_STATUS_SET_TIME,
43 CURRENT_STATUS_SET_OUTFILENAME,
47 static video_util_h video_h = NULL;
48 char g_uri[MAX_STRING_LEN] = { 0, };
50 int g_menu_state = CURRENT_STATUS_MAINMENU;
52 int format = VIDEO_UTIL_FILE_FORMAT_3GP;
53 int video_codec = VIDEO_UTIL_VIDEO_CODEC_MPEG4;
54 int audio_codec = VIDEO_UTIL_AUDIO_CODEC_AAC;
55 int accurate_mode = 0;
59 unsigned long start_position = 0;
60 unsigned long duration = 5000;
61 int make_video_cnt = 1;
62 char g_out[MAX_STRING_LEN] = { 0, };
67 unsigned long start_time;
68 unsigned long duration;
71 static void display_sub_basic();
72 void _video_util_start_transcoding(test_util_s *util_s);
74 void _quit_program(void)
79 bool test_transcode_spec_cb(int value, void *user_data)
82 LOGE("user_data is NULL");
86 if (!strcmp(user_data, "format_check")) {
89 LOGI("[%s] --- [3gp]", user_data);
92 LOGI("[%s] --- [mp4]", user_data);
97 } else if (!strcmp(user_data, "video_codec_check")) {
100 LOGI("[%s] --- [m4v]", user_data);
103 LOGI("[%s] --- [h263]", user_data);
106 LOGI("[%s] --- [h264]", user_data);
111 } else if (!strcmp(user_data, "audio_codec_check")) {
114 LOGI("[%s] --- [aac]", user_data);
117 LOGI("[%s] --- [amrnb]", user_data);
127 bool _supported_spec_check(video_util_h handle)
130 ret = video_util_foreach_supported_file_format(handle, (video_util_supported_file_format_cb)test_transcode_spec_cb, "format_check");
131 printf("video_util_foreach_supported_file_format [%d]\n", ret);
132 ret = video_util_foreach_supported_video_codec(handle, (video_util_supported_video_encoder_cb)test_transcode_spec_cb, "video_codec_check");
133 printf("video_util_foreach_supported_video_codec [%d]\n", ret);
134 ret = video_util_foreach_supported_audio_codec(handle, (video_util_supported_audio_encoder_cb)test_transcode_spec_cb, "audio_codec_check");
135 printf("video_util_foreach_supported_audio_codec [%d]\n", ret);
140 void _transcode_completed_cb(video_util_error_e error, void *user_data)
143 unsigned long ntn_start_position = 0;
145 test_util_s *_util_s = (test_util_s *)user_data;
147 LOGI("transcode_completed_cb============= [%2d / %2d][%d]\n", _util_s->idx, make_video_cnt, error);
148 printf("transcode_completed_cb============= [%2d / %2d][%d]\n", _util_s->idx, make_video_cnt, error);
150 if (_util_s->idx == (make_video_cnt - 1)) {
151 LOGI("End trascoding");
152 video_util_destroy(_util_s->video_h);
153 _util_s->video_h = NULL;
158 idx = _util_s->idx + 1;
159 ntn_start_position = _util_s->start_time + duration;
161 _util_s->start_time = ntn_start_position;
163 _video_util_start_transcoding(_util_s);
168 void _transcode_progress_cb(unsigned long current_position, unsigned long duration, void *user_data)
170 test_util_s *_util_s = (test_util_s *)user_data;
172 LOGD("transcode_progress_cb-------------- [%2d][%ld][%ld]\n", _util_s->idx, current_position, duration);
177 void _video_util_start_transcoding(test_util_s *util_s)
179 int ret = VIDEO_UTIL_ERROR_NONE;
180 char output_file_path[MAX_STRING_LEN] = { 0, };
183 LOGE("video_util handle is NULL, please set format after create");
187 LOGI("video_util set below");
188 LOGI("format: %d, video codec: %d, audio codec: %d, accurate mode: %d", format, video_codec, audio_codec, accurate_mode);
189 LOGI("width: %d, height: %d, fps: %d", width, height, fps);
190 LOGI("start time: %lu, durtation: %lu", start_position, duration);
192 ret = video_util_set_file_format(video_h, format);
193 if (ret != VIDEO_UTIL_ERROR_NONE) {
194 LOGE("video_util_set_file_format is failed (%d)", ret);
198 ret = video_util_set_video_codec(video_h, video_codec);
199 if (ret != VIDEO_UTIL_ERROR_NONE) {
200 LOGE("video_util_set_video_codec is failed (%d)", ret);
204 ret = video_util_set_audio_codec(video_h, audio_codec);
205 if (ret != VIDEO_UTIL_ERROR_NONE) {
206 LOGE("video_util_set_audio_codec is failed (%d)", ret);
210 ret = video_util_set_accurate_mode(video_h, accurate_mode);
211 if (ret != VIDEO_UTIL_ERROR_NONE) {
212 LOGE("video_util_set_accurate_mode is failed (%d)", ret);
216 ret = video_util_set_resolution(video_h, width, height);
217 if (ret != VIDEO_UTIL_ERROR_NONE) {
218 LOGE("video_util_set_resolution is failed (%d)", ret);
222 ret = video_util_set_fps(video_h, fps);
223 if (ret != VIDEO_UTIL_ERROR_NONE) {
224 LOGE("video_util_set_fps is failed (%d)", ret);
228 memset(output_file_path, 0x00, MAX_STRING_LEN);
230 snprintf(output_file_path, MAX_STRING_LEN, "%s_%d.%s", g_out, util_s->idx, format ? "mp4" : "3gp");
232 LOGI("input start_time: %lu, duration: %lu, output_file_path: %s", util_s->start_time, util_s->duration, output_file_path);
233 ret = video_util_start_transcoding(util_s->video_h, util_s->start_time, util_s->duration, output_file_path, _transcode_progress_cb, _transcode_completed_cb, util_s);
235 if (ret != VIDEO_UTIL_ERROR_NONE) {
236 LOGE("video_util_start_transcoding is failed (%d)", ret);
246 video_util_destroy(video_h);
249 memset(g_uri, 0x00, MAX_STRING_LEN);
250 memset(g_out, 0x00, MAX_STRING_LEN);
251 g_menu_state = CURRENT_STATUS_MAINMENU;
253 format = VIDEO_UTIL_FILE_FORMAT_3GP;
254 video_codec = VIDEO_UTIL_VIDEO_CODEC_MPEG4;
255 audio_codec = VIDEO_UTIL_AUDIO_CODEC_AAC;
260 start_position = 5000;
265 static void input_filename(char *filename)
267 int len = strlen(filename);
268 int ret = VIDEO_UTIL_ERROR_NONE;
270 if (len < 0 || len > MAX_STRING_LEN) {
271 LOGE("Input file name is wrong");
278 ret = video_util_cancel_transcoding(video_h);
279 ret = video_util_destroy(video_h);
284 ret = video_util_create(&video_h);
286 if (ret != VIDEO_UTIL_ERROR_NONE) {
287 LOGE("video_util create is failed (%d)", ret);
291 strncpy(g_uri, filename, len);
293 ret = video_util_set_file_path(video_h, g_uri);
295 if (ret != VIDEO_UTIL_ERROR_NONE) {
296 LOGE("video_util_set_file_path is failed");
300 _supported_spec_check(video_h);
303 void reset_menu_state()
305 g_menu_state = CURRENT_STATUS_MAINMENU;
309 void _interpret_main_menu(char *cmd)
311 int len = strlen(cmd);
314 if (strncmp(cmd, "a", 1) == 0) {
315 g_menu_state = CURRENT_STATUS_FILENAME;
316 } else if (strncmp(cmd, "s", 1) == 0) {
318 test_util_s *_util_s = (test_util_s *)calloc(1, sizeof(test_util_s));
320 g_print("test util calloc failed");
324 _util_s->video_h = video_h;
326 _util_s->start_time = start_position;
327 _util_s->duration = duration;
329 _video_util_start_transcoding(_util_s);
331 } else if (strncmp(cmd, "c", 1) == 0) {
332 int ret = VIDEO_UTIL_ERROR_NONE;
334 g_print("video_util handle is NULL, please set format after create");
337 ret = video_util_cancel_transcoding(video_h);
338 if (ret != VIDEO_UTIL_ERROR_NONE) {
339 g_print("video_util_cancel_transcoding is failed (%d)", ret);
342 } else if (strncmp(cmd, "f", 1) == 0) {
343 g_menu_state = CURRENT_STATUS_SET_FORMAT;
344 } else if (strncmp(cmd, "m", 1) == 0) {
345 g_menu_state = CURRENT_STATUS_SET_ACCURATE_MODE;
346 } else if (strncmp(cmd, "t", 1) == 0) {
347 g_menu_state = CURRENT_STATUS_SET_TIME;
348 } else if (strncmp(cmd, "o", 1) == 0) {
349 g_menu_state = CURRENT_STATUS_SET_OUTFILENAME;
350 } else if (strncmp(cmd, "q", 1) == 0) {
353 g_print("unknown menu \n");
355 } else if (len == 2) {
356 if (strncmp(cmd, "vc", 2) == 0)
357 g_menu_state = CURRENT_STATUS_SET_VIDEO_CODEC;
358 else if (strncmp(cmd, "ac", 2) == 0)
359 g_menu_state = CURRENT_STATUS_SET_AUDIO_CODEC;
360 else if (strncmp(cmd, "vr", 2) == 0)
361 g_menu_state = CURRENT_STATUS_SET_RESOLUTION;
362 else if (strncmp(cmd, "vf", 2) == 0)
363 g_menu_state = CURRENT_STATUS_SET_FPS;
365 g_print("unknown menu \n");
367 g_print("unknown menu \n");
372 static void displaymenu(void)
374 if (g_menu_state == CURRENT_STATUS_MAINMENU) {
376 } else if (g_menu_state == CURRENT_STATUS_FILENAME) {
377 g_print("*** input mediapath.\n");
378 } else if (g_menu_state == CURRENT_STATUS_SET_FORMAT) {
379 g_print("*** input file format.(0:3gp, 1:mp4)\n");
380 } else if (g_menu_state == CURRENT_STATUS_SET_VIDEO_CODEC) {
381 g_print("*** input video codec.(0:m4v, 1:h263, 2:h264)\n");
382 } else if (g_menu_state == CURRENT_STATUS_SET_AUDIO_CODEC) {
383 g_print("*** input audio codec.(0:aac, 1:amrnb)\n");
384 } else if (g_menu_state == CURRENT_STATUS_SET_ACCURATE_MODE) {
385 g_print("*** input accurate mode.(0: OFF, 1: ON)\n");
386 } else if (g_menu_state == CURRENT_STATUS_SET_RESOLUTION) {
387 g_print("*** input video resolution.(width, height)\n");
388 } else if (g_menu_state == CURRENT_STATUS_SET_FPS) {
389 g_print("*** input video fps.(5<=fps<=30)\n");
390 } else if (g_menu_state == CURRENT_STATUS_SET_FPS) {
391 g_print("*** input video fps.(5<=fps<=30)\n");
392 } else if (g_menu_state == CURRENT_STATUS_SET_TIME) {
393 g_print("*** input transcode start/duration time(ms), run nth.(start time, duration, n)\n");
394 } else if (g_menu_state == CURRENT_STATUS_SET_OUTFILENAME) {
395 g_print("*** input output filename.(defaunt path /opt/usr/media/)\n");
397 g_print("*** unknown status.\n");
403 gboolean timeout_menu_display(void *data)
409 static void interpret(char *cmd)
411 switch (g_menu_state) {
413 case CURRENT_STATUS_MAINMENU:
415 _interpret_main_menu(cmd);
418 case CURRENT_STATUS_FILENAME:
424 case CURRENT_STATUS_SET_FORMAT:
426 int ret = VIDEO_UTIL_ERROR_NONE;
428 if (format < 0 || format >= VIDEO_UTIL_FILE_FORMAT_MAX) {
429 LOGE("input cmd is out of range.");
434 LOGE("video_util handle is NULL, please set format after create");
438 ret = video_util_set_file_format(video_h, format);
439 if (ret != VIDEO_UTIL_ERROR_NONE) {
440 LOGE("video_util_set_file_format is failed (%d)", ret);
448 case CURRENT_STATUS_SET_VIDEO_CODEC:
450 int ret = VIDEO_UTIL_ERROR_NONE;
451 video_codec = atoi(cmd);
452 if (video_codec < 0 || video_codec >= VIDEO_UTIL_VIDEO_CODEC_NONE) {
453 LOGE("input cmd is out of range");
458 LOGE("video_util handle is NULL, please set format after create");
462 ret = video_util_set_video_codec(video_h, video_codec);
463 if (ret != VIDEO_UTIL_ERROR_NONE) {
464 LOGE("video_util_set_video_codec is failed (%d)", ret);
472 case CURRENT_STATUS_SET_AUDIO_CODEC:
474 int ret = VIDEO_UTIL_ERROR_NONE;
475 audio_codec = atoi(cmd);
476 if (audio_codec < 0 || audio_codec >= VIDEO_UTIL_VIDEO_CODEC_NONE) {
477 LOGE("input cmd is out of range");
482 LOGE("video_util handle is NULL, please set format after create");
486 ret = video_util_set_video_codec(video_h, audio_codec);
487 if (ret != VIDEO_UTIL_ERROR_NONE) {
488 LOGE("video_util_set_video_codec is failed (%d)", ret);
496 case CURRENT_STATUS_SET_ACCURATE_MODE:
498 int ret = VIDEO_UTIL_ERROR_NONE;
499 accurate_mode = atoi(cmd);
502 LOGE("video_util handle is NULL, please set format after create");
506 ret = video_util_set_accurate_mode(video_h, (bool)accurate_mode);
507 if (ret != VIDEO_UTIL_ERROR_NONE) {
508 LOGE("video_util_set_video_codec is failed (%d)", ret);
516 case CURRENT_STATUS_SET_RESOLUTION:
518 int ret = VIDEO_UTIL_ERROR_NONE;
519 int value = atoi(cmd);
520 static int resolution_cnt = 0;
523 LOGE("video_util handle is NULL, please set format after create");
528 switch (resolution_cnt) {
537 ret = video_util_set_resolution(video_h, width, height);
538 if (ret != VIDEO_UTIL_ERROR_NONE) {
539 LOGE("video_util_set_resolution is failed (%d)", ret);
549 case CURRENT_STATUS_SET_FPS:
551 int ret = VIDEO_UTIL_ERROR_NONE;
554 if (fps < 5 || fps > 30) {
555 LOGE("input cmd is out of range");
560 LOGE("video_util handle is NULL, please set format after create");
564 ret = video_util_set_fps(video_h, fps);
565 if (ret != VIDEO_UTIL_ERROR_NONE) {
566 LOGE("video_util_set_fps is failed (%d)", ret);
574 case CURRENT_STATUS_SET_TIME:
576 int value = atoi(cmd);
577 static int set_time_cnt = 0;
579 switch (set_time_cnt) {
581 start_position = value;
590 make_video_cnt = value;
597 case CURRENT_STATUS_SET_OUTFILENAME:
599 LOGI("output file is %s", g_out);
600 snprintf(g_out, MAX_STRING_LEN, "/opt/usr/media/%s", cmd);
601 g_out[MAX_STRING_LEN - 1] = '\0';
602 LOGI("output file is %s", g_out);
610 g_timeout_add(100, timeout_menu_display, 0);
613 static void display_sub_basic()
616 g_print("=========================================================================================\n");
617 g_print(" video_util_test test\n");
618 g_print("-----------------------------------------------------------------------------------------\n");
619 g_print(" a. Create \t\t\t");
620 g_print(" s. Start transcoding \t");
621 g_print(" c. Cancel transcoding \n");
622 g_print(" f. Set file format\t\t");
623 g_print("vc. Set video codec\t");
624 g_print("ac. Set audio codec\n");
625 g_print(" m. Set accurate mode\t\t");
626 g_print("vr. Set resolution\t");
627 g_print("vf. Set video fps\n");
628 g_print(" t. Set start/duration time \t");
629 g_print(" o. Set output filename\t");
630 g_print(" q. quite test suite\t");
632 g_print("=========================================================================================\n");
635 gboolean input(GIOChannel *channel)
637 gchar buf[MAX_STRING_LEN];
639 GError *error = NULL;
641 g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
649 int main(int argc, char *argv[])
651 GIOChannel *stdin_channel;
652 GMainLoop *loop = g_main_loop_new(NULL, 0);
653 stdin_channel = g_io_channel_unix_new(0);
654 g_io_channel_set_flags(stdin_channel, G_IO_FLAG_NONBLOCK, NULL);
655 g_io_add_watch(stdin_channel, G_IO_IN, (GIOFunc)input, NULL);
659 g_main_loop_run(loop);
660 g_print("STOP main loop\n");
662 g_main_loop_unref(loop);