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;
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;
66 unsigned long start_time;
67 unsigned long duration;
73 static void display_sub_basic();
74 static void _video_util_start_transcoding(test_util_s *util_s);
75 static void _reset_var();
77 void _quit_program(void)
83 bool test_transcode_spec_cb(int value, void *user_data)
86 LOGE("user_data is NULL");
90 if (!strcmp(user_data, "format_check")) {
93 LOGI("[%s] --- [3gp]", (const char *)user_data);
96 LOGI("[%s] --- [mp4]", (const char *)user_data);
101 } else if (!strcmp(user_data, "video_codec_check")) {
104 LOGI("[%s] --- [m4v]", (const char *)user_data);
107 LOGI("[%s] --- [h263]", (const char *)user_data);
110 LOGI("[%s] --- [h264]", (const char *)user_data);
115 } else if (!strcmp(user_data, "audio_codec_check")) {
118 LOGI("[%s] --- [aac]", (const char *)user_data);
121 LOGI("[%s] --- [amrnb]", (const char *)user_data);
131 bool _supported_spec_check(video_util_h handle)
134 ret = video_util_foreach_supported_file_format(handle, (video_util_supported_file_format_cb)test_transcode_spec_cb, "format_check");
135 printf("video_util_foreach_supported_file_format [%d]\n", ret);
136 ret = video_util_foreach_supported_video_codec(handle, (video_util_supported_video_encoder_cb)test_transcode_spec_cb, "video_codec_check");
137 printf("video_util_foreach_supported_video_codec [%d]\n", ret);
138 ret = video_util_foreach_supported_audio_codec(handle, (video_util_supported_audio_encoder_cb)test_transcode_spec_cb, "audio_codec_check");
139 printf("video_util_foreach_supported_audio_codec [%d]\n", ret);
144 void _transcode_completed_cb(video_util_error_e error, void *user_data)
147 unsigned long ntn_start_position = 0;
149 test_util_s *_util_s = (test_util_s *)user_data;
152 LOGE("completed_cb user data is NULL");
156 LOGI("transcode_completed_cb============= [%2d / %2d][%d]\n", _util_s->idx, make_video_cnt, error);
157 printf("transcode_completed_cb============= [%2d / %2d][%d]\n", _util_s->idx, make_video_cnt, error);
159 if (_util_s->idx == (make_video_cnt - 1)) {
160 LOGI("End trascoding");
164 idx = _util_s->idx + 1;
165 ntn_start_position = _util_s->start_time + duration;
167 _util_s->start_time = ntn_start_position;
169 _video_util_start_transcoding(_util_s);
174 void _transcode_progress_cb(unsigned long current_position, unsigned long duration, void *user_data)
176 test_util_s *_util_s = (test_util_s *)user_data;
178 LOGD("transcode_progress_cb-------------- [%2d][%ld][%ld]\n", _util_s->idx, current_position, duration);
183 static void _video_util_start_transcoding(test_util_s *util_s)
185 int ret = VIDEO_UTIL_ERROR_NONE;
186 gchar *output_file_path = NULL;
189 LOGE("video_util handle is NULL, please set format after create");
193 LOGI("video_util set below");
194 LOGI("format: %d, video codec: %d, audio codec: %d, accurate mode: %d", format, video_codec, audio_codec, accurate_mode);
195 LOGI("width: %d, height: %d, fps: %d", width, height, fps);
196 LOGI("start time: %lu, durtation: %lu", start_position, duration);
198 ret = video_util_set_file_format(video_h, format);
199 if (ret != VIDEO_UTIL_ERROR_NONE) {
200 LOGE("video_util_set_file_format is failed (%d)", ret);
204 ret = video_util_set_video_codec(video_h, video_codec);
205 if (ret != VIDEO_UTIL_ERROR_NONE) {
206 LOGE("video_util_set_video_codec is failed (%d)", ret);
210 ret = video_util_set_audio_codec(video_h, audio_codec);
211 if (ret != VIDEO_UTIL_ERROR_NONE) {
212 LOGE("video_util_set_audio_codec is failed (%d)", ret);
216 ret = video_util_set_accurate_mode(video_h, accurate_mode);
217 if (ret != VIDEO_UTIL_ERROR_NONE) {
218 LOGE("video_util_set_accurate_mode is failed (%d)", ret);
222 ret = video_util_set_resolution(video_h, width, height);
223 if (ret != VIDEO_UTIL_ERROR_NONE) {
224 LOGE("video_util_set_resolution is failed (%d)", ret);
228 ret = video_util_set_fps(video_h, fps);
229 if (ret != VIDEO_UTIL_ERROR_NONE) {
230 LOGE("video_util_set_fps is failed (%d)", ret);
234 output_file_path = g_strdup_printf("%s_%d.%s", g_out, util_s->idx, format ? "mp4" : "3gp");
238 LOGI("input start_time: %lu, duration: %lu, output_file_path: %s", util_s->start_time, util_s->duration, output_file_path);
239 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);
240 if (ret != VIDEO_UTIL_ERROR_NONE) {
241 LOGE("video_util_start_transcoding is failed (%d)", ret);
242 g_free(output_file_path);
246 g_free(output_file_path);
250 static void _reset_var()
253 video_util_destroy(video_h);
260 g_menu_state = CURRENT_STATUS_MAINMENU;
262 format = VIDEO_UTIL_FILE_FORMAT_3GP;
263 video_codec = VIDEO_UTIL_VIDEO_CODEC_MPEG4;
264 audio_codec = VIDEO_UTIL_AUDIO_CODEC_AAC;
269 start_position = 5000;
274 static void input_filename(char *filename)
276 int len = strlen(filename);
277 int ret = VIDEO_UTIL_ERROR_NONE;
280 if (len < 0 || len > MAX_STRING_LEN - 1) {
281 LOGE("Input file name is wrong");
288 ret = video_util_cancel_transcoding(video_h);
289 ret = video_util_destroy(video_h);
294 ret = video_util_create(&video_h);
296 if (ret != VIDEO_UTIL_ERROR_NONE) {
297 LOGE("video_util create is failed (%d)", ret);
301 g_uri = g_strndup(filename, len);
303 ret = video_util_set_file_path(video_h, g_uri);
304 if (ret != VIDEO_UTIL_ERROR_NONE) {
305 LOGE("video_util_set_file_path is failed");
312 _supported_spec_check(video_h);
315 void reset_menu_state()
317 g_menu_state = CURRENT_STATUS_MAINMENU;
321 void _interpret_main_menu(char *cmd)
323 int len = strlen(cmd);
326 if (strncmp(cmd, "a", 1) == 0) {
327 g_menu_state = CURRENT_STATUS_FILENAME;
328 } else if (strncmp(cmd, "s", 1) == 0) {
330 _util_s = (test_util_s *)calloc(1, sizeof(test_util_s));
331 LOGE("_util_s malloc");
335 LOGE("test util calloc failed");
339 _util_s->video_h = video_h;
341 _util_s->start_time = start_position;
342 _util_s->duration = duration;
344 _video_util_start_transcoding(_util_s);
346 } else if (strncmp(cmd, "c", 1) == 0) {
347 int ret = VIDEO_UTIL_ERROR_NONE;
349 g_print("video_util handle is NULL, please set format after create");
352 ret = video_util_cancel_transcoding(video_h);
353 if (ret != VIDEO_UTIL_ERROR_NONE) {
354 g_print("video_util_cancel_transcoding is failed (%d)", ret);
357 } else if (strncmp(cmd, "f", 1) == 0) {
358 g_menu_state = CURRENT_STATUS_SET_FORMAT;
359 } else if (strncmp(cmd, "m", 1) == 0) {
360 g_menu_state = CURRENT_STATUS_SET_ACCURATE_MODE;
361 } else if (strncmp(cmd, "t", 1) == 0) {
362 g_menu_state = CURRENT_STATUS_SET_TIME;
363 } else if (strncmp(cmd, "o", 1) == 0) {
364 g_menu_state = CURRENT_STATUS_SET_OUTFILENAME;
365 } else if (strncmp(cmd, "q", 1) == 0) {
367 } else if (strncmp(cmd, "d", 1) == 0) {
368 int ret = VIDEO_UTIL_ERROR_NONE;
370 g_print("video_util handle is NULL, please set format after create");
373 ret = video_util_destroy(video_h);
374 if (ret != VIDEO_UTIL_ERROR_NONE) {
375 g_print("video_util_cancel_transcoding is failed (%d)", ret);
381 g_print("unknown menu \n");
383 } else if (len == 2) {
384 if (strncmp(cmd, "vc", 2) == 0)
385 g_menu_state = CURRENT_STATUS_SET_VIDEO_CODEC;
386 else if (strncmp(cmd, "ac", 2) == 0)
387 g_menu_state = CURRENT_STATUS_SET_AUDIO_CODEC;
388 else if (strncmp(cmd, "vr", 2) == 0)
389 g_menu_state = CURRENT_STATUS_SET_RESOLUTION;
390 else if (strncmp(cmd, "vf", 2) == 0)
391 g_menu_state = CURRENT_STATUS_SET_FPS;
393 g_print("unknown menu \n");
395 g_print("unknown menu \n");
400 static void displaymenu(void)
402 if (g_menu_state == CURRENT_STATUS_MAINMENU) {
404 } else if (g_menu_state == CURRENT_STATUS_FILENAME) {
405 g_print("*** input mediapath.\n");
406 } else if (g_menu_state == CURRENT_STATUS_SET_FORMAT) {
407 g_print("*** input file format.(0:3gp, 1:mp4)\n");
408 } else if (g_menu_state == CURRENT_STATUS_SET_VIDEO_CODEC) {
409 g_print("*** input video codec.(0:m4v, 1:h263, 2:h264)\n");
410 } else if (g_menu_state == CURRENT_STATUS_SET_AUDIO_CODEC) {
411 g_print("*** input audio codec.(0:aac, 1:amrnb)\n");
412 } else if (g_menu_state == CURRENT_STATUS_SET_ACCURATE_MODE) {
413 g_print("*** input accurate mode.(0: OFF, 1: ON)\n");
414 } else if (g_menu_state == CURRENT_STATUS_SET_RESOLUTION) {
415 g_print("*** input video resolution.(width, height)\n");
416 } else if (g_menu_state == CURRENT_STATUS_SET_FPS) {
417 g_print("*** input video fps.(5<=fps<=30)\n");
418 } else if (g_menu_state == CURRENT_STATUS_SET_TIME) {
419 g_print("*** input transcode start/duration time(ms), run nth.(start time, duration, n)\n");
420 } else if (g_menu_state == CURRENT_STATUS_SET_OUTFILENAME) {
421 g_print("*** input output filename.(defaunt path /opt/usr/media/)\n");
423 g_print("*** unknown status.\n");
429 gboolean timeout_menu_display(void *data)
435 static void interpret(char *cmd)
437 switch (g_menu_state) {
439 case CURRENT_STATUS_MAINMENU:
441 _interpret_main_menu(cmd);
444 case CURRENT_STATUS_FILENAME:
450 case CURRENT_STATUS_SET_FORMAT:
452 int ret = VIDEO_UTIL_ERROR_NONE;
454 if (format < 0 || format >= VIDEO_UTIL_FILE_FORMAT_MAX) {
455 LOGE("input cmd is out of range.");
460 LOGE("video_util handle is NULL, please set format after create");
464 ret = video_util_set_file_format(video_h, format);
465 if (ret != VIDEO_UTIL_ERROR_NONE) {
466 LOGE("video_util_set_file_format is failed (%d)", ret);
474 case CURRENT_STATUS_SET_VIDEO_CODEC:
476 int ret = VIDEO_UTIL_ERROR_NONE;
477 video_codec = atoi(cmd);
478 if (video_codec < 0 || video_codec >= VIDEO_UTIL_VIDEO_CODEC_NONE) {
479 LOGE("input cmd is out of range");
484 LOGE("video_util handle is NULL, please set format after create");
488 ret = video_util_set_video_codec(video_h, video_codec);
489 if (ret != VIDEO_UTIL_ERROR_NONE) {
490 LOGE("video_util_set_video_codec is failed (%d)", ret);
498 case CURRENT_STATUS_SET_AUDIO_CODEC:
500 int ret = VIDEO_UTIL_ERROR_NONE;
501 audio_codec = atoi(cmd);
502 if (audio_codec < 0 || audio_codec >= VIDEO_UTIL_VIDEO_CODEC_NONE) {
503 LOGE("input cmd is out of range");
508 LOGE("video_util handle is NULL, please set format after create");
512 ret = video_util_set_video_codec(video_h, audio_codec);
513 if (ret != VIDEO_UTIL_ERROR_NONE) {
514 LOGE("video_util_set_video_codec is failed (%d)", ret);
522 case CURRENT_STATUS_SET_ACCURATE_MODE:
524 int ret = VIDEO_UTIL_ERROR_NONE;
525 accurate_mode = atoi(cmd);
528 LOGE("video_util handle is NULL, please set format after create");
532 ret = video_util_set_accurate_mode(video_h, (bool)accurate_mode);
533 if (ret != VIDEO_UTIL_ERROR_NONE) {
534 LOGE("video_util_set_video_codec is failed (%d)", ret);
542 case CURRENT_STATUS_SET_RESOLUTION:
544 int ret = VIDEO_UTIL_ERROR_NONE;
545 int value = atoi(cmd);
546 static int resolution_cnt = 0;
549 LOGE("video_util handle is NULL, please set format after create");
554 switch (resolution_cnt) {
563 ret = video_util_set_resolution(video_h, width, height);
564 if (ret != VIDEO_UTIL_ERROR_NONE) {
565 LOGE("video_util_set_resolution is failed (%d)", ret);
575 case CURRENT_STATUS_SET_FPS:
577 int ret = VIDEO_UTIL_ERROR_NONE;
580 if (fps < 5 || fps > 30) {
581 LOGE("input cmd is out of range");
586 LOGE("video_util handle is NULL, please set format after create");
590 ret = video_util_set_fps(video_h, fps);
591 if (ret != VIDEO_UTIL_ERROR_NONE) {
592 LOGE("video_util_set_fps is failed (%d)", ret);
600 case CURRENT_STATUS_SET_TIME:
602 int value = atoi(cmd);
603 static int set_time_cnt = 0;
605 switch (set_time_cnt) {
607 start_position = value;
616 make_video_cnt = value;
623 case CURRENT_STATUS_SET_OUTFILENAME:
625 LOGI("output file is %s", g_out);
627 g_out = g_strdup_printf("/opt/usr/media/%s", cmd);
628 LOGI("output file is %s", g_out);
636 g_timeout_add(100, timeout_menu_display, 0);
639 static void display_sub_basic()
642 g_print("=========================================================================================\n");
643 g_print(" video_util_test test\n");
644 g_print("-----------------------------------------------------------------------------------------\n");
645 g_print(" a. Create \t\t\t");
646 g_print(" s. Start transcoding \t");
647 g_print(" c. Cancel transcoding \n");
648 g_print(" f. Set file format\t\t");
649 g_print("vc. Set video codec\t");
650 g_print("ac. Set audio codec\n");
651 g_print(" m. Set accurate mode\t\t");
652 g_print("vr. Set resolution\t");
653 g_print("vf. Set video fps\n");
654 g_print(" t. Set start/duration time \t");
655 g_print(" o. Set output filename\t");
656 g_print(" q. quite test suite\t\n");
657 g_print(" d. Destroy\t");
659 g_print("=========================================================================================\n");
662 gboolean input(GIOChannel *channel)
664 gchar buf[MAX_STRING_LEN];
666 GError *error = NULL;
668 g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
676 int main(int argc, char *argv[])
678 GIOChannel *stdin_channel;
679 GMainLoop *loop = g_main_loop_new(NULL, 0);
680 stdin_channel = g_io_channel_unix_new(0);
681 g_io_channel_set_flags(stdin_channel, G_IO_FLAG_NONBLOCK, NULL);
682 g_io_add_watch(stdin_channel, G_IO_IN, (GIOFunc)input, NULL);
686 g_main_loop_run(loop);
687 g_print("STOP main loop\n");
689 g_main_loop_unref(loop);