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(void);
74 static void _video_util_start_transcoding(test_util_s *util_s);
75 static void _reset_var(void);
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);
243 g_free(output_file_path);
247 static void _reset_var(void)
250 video_util_destroy(video_h);
257 g_menu_state = CURRENT_STATUS_MAINMENU;
259 format = VIDEO_UTIL_FILE_FORMAT_3GP;
260 video_codec = VIDEO_UTIL_VIDEO_CODEC_MPEG4;
261 audio_codec = VIDEO_UTIL_AUDIO_CODEC_AAC;
266 start_position = 5000;
271 static void input_filename(char *filename)
273 int len = strlen(filename);
274 int ret = VIDEO_UTIL_ERROR_NONE;
276 if (len < 0 || len > MAX_STRING_LEN - 1) {
277 LOGE("Input file name is wrong");
284 ret = video_util_cancel_transcoding(video_h);
285 ret = video_util_destroy(video_h);
290 ret = video_util_create(&video_h);
291 if (ret != VIDEO_UTIL_ERROR_NONE) {
292 LOGE("video_util create is failed (%d)", ret);
296 ret = video_util_set_file_path(video_h, filename);
297 if (ret != VIDEO_UTIL_ERROR_NONE) {
298 LOGE("video_util_set_file_path is failed");
302 _supported_spec_check(video_h);
305 void reset_menu_state()
307 g_menu_state = CURRENT_STATUS_MAINMENU;
311 void _interpret_main_menu(char *cmd)
313 int len = strlen(cmd);
316 if (strncmp(cmd, "a", 1) == 0) {
317 g_menu_state = CURRENT_STATUS_FILENAME;
318 } else if (strncmp(cmd, "s", 1) == 0) {
320 _util_s = (test_util_s *)calloc(1, sizeof(test_util_s));
321 LOGE("_util_s malloc");
325 LOGE("test util calloc failed");
329 _util_s->video_h = video_h;
331 _util_s->start_time = start_position;
332 _util_s->duration = duration;
334 _video_util_start_transcoding(_util_s);
336 } else if (strncmp(cmd, "c", 1) == 0) {
337 int ret = VIDEO_UTIL_ERROR_NONE;
339 g_print("video_util handle is NULL, please set format after create");
342 ret = video_util_cancel_transcoding(video_h);
343 if (ret != VIDEO_UTIL_ERROR_NONE) {
344 g_print("video_util_cancel_transcoding is failed (%d)", ret);
347 } else if (strncmp(cmd, "f", 1) == 0) {
348 g_menu_state = CURRENT_STATUS_SET_FORMAT;
349 } else if (strncmp(cmd, "m", 1) == 0) {
350 g_menu_state = CURRENT_STATUS_SET_ACCURATE_MODE;
351 } else if (strncmp(cmd, "t", 1) == 0) {
352 g_menu_state = CURRENT_STATUS_SET_TIME;
353 } else if (strncmp(cmd, "o", 1) == 0) {
354 g_menu_state = CURRENT_STATUS_SET_OUTFILENAME;
355 } else if (strncmp(cmd, "q", 1) == 0) {
357 } else if (strncmp(cmd, "d", 1) == 0) {
358 int ret = VIDEO_UTIL_ERROR_NONE;
360 g_print("video_util handle is NULL, please set format after create");
363 ret = video_util_destroy(video_h);
364 if (ret != VIDEO_UTIL_ERROR_NONE) {
365 g_print("video_util_cancel_transcoding is failed (%d)", ret);
371 g_print("unknown menu \n");
373 } else if (len == 2) {
374 if (strncmp(cmd, "vc", 2) == 0)
375 g_menu_state = CURRENT_STATUS_SET_VIDEO_CODEC;
376 else if (strncmp(cmd, "ac", 2) == 0)
377 g_menu_state = CURRENT_STATUS_SET_AUDIO_CODEC;
378 else if (strncmp(cmd, "vr", 2) == 0)
379 g_menu_state = CURRENT_STATUS_SET_RESOLUTION;
380 else if (strncmp(cmd, "vf", 2) == 0)
381 g_menu_state = CURRENT_STATUS_SET_FPS;
383 g_print("unknown menu \n");
385 g_print("unknown menu \n");
390 static void displaymenu(void)
392 if (g_menu_state == CURRENT_STATUS_MAINMENU) {
393 _display_sub_basic();
394 } else if (g_menu_state == CURRENT_STATUS_FILENAME) {
395 g_print("*** input mediapath.\n");
396 } else if (g_menu_state == CURRENT_STATUS_SET_FORMAT) {
397 g_print("*** input file format.(0:3gp, 1:mp4)\n");
398 } else if (g_menu_state == CURRENT_STATUS_SET_VIDEO_CODEC) {
399 g_print("*** input video codec.(0:m4v, 1:h263, 2:h264)\n");
400 } else if (g_menu_state == CURRENT_STATUS_SET_AUDIO_CODEC) {
401 g_print("*** input audio codec.(0:aac, 1:amrnb)\n");
402 } else if (g_menu_state == CURRENT_STATUS_SET_ACCURATE_MODE) {
403 g_print("*** input accurate mode.(0: OFF, 1: ON)\n");
404 } else if (g_menu_state == CURRENT_STATUS_SET_RESOLUTION) {
405 g_print("*** input video resolution.(width, height)\n");
406 } else if (g_menu_state == CURRENT_STATUS_SET_FPS) {
407 g_print("*** input video fps.(5<=fps<=30)\n");
408 } else if (g_menu_state == CURRENT_STATUS_SET_TIME) {
409 g_print("*** input transcode start/duration time(ms), run nth.(start time, duration, n)\n");
410 } else if (g_menu_state == CURRENT_STATUS_SET_OUTFILENAME) {
411 g_print("*** input output filename.(defaunt path /opt/usr/media/)\n");
413 g_print("*** unknown status.\n");
419 gboolean timeout_menu_display(void *data)
425 static void interpret(char *cmd)
427 switch (g_menu_state) {
429 case CURRENT_STATUS_MAINMENU:
431 _interpret_main_menu(cmd);
434 case CURRENT_STATUS_FILENAME:
440 case CURRENT_STATUS_SET_FORMAT:
442 int ret = VIDEO_UTIL_ERROR_NONE;
444 if (format < 0 || format >= VIDEO_UTIL_FILE_FORMAT_MAX) {
445 LOGE("input cmd is out of range.");
450 LOGE("video_util handle is NULL, please set format after create");
454 ret = video_util_set_file_format(video_h, format);
455 if (ret != VIDEO_UTIL_ERROR_NONE) {
456 LOGE("video_util_set_file_format is failed (%d)", ret);
464 case CURRENT_STATUS_SET_VIDEO_CODEC:
466 int ret = VIDEO_UTIL_ERROR_NONE;
467 video_codec = atoi(cmd);
468 if (video_codec < 0 || video_codec >= VIDEO_UTIL_VIDEO_CODEC_NONE) {
469 LOGE("input cmd is out of range");
474 LOGE("video_util handle is NULL, please set format after create");
478 ret = video_util_set_video_codec(video_h, video_codec);
479 if (ret != VIDEO_UTIL_ERROR_NONE) {
480 LOGE("video_util_set_video_codec is failed (%d)", ret);
488 case CURRENT_STATUS_SET_AUDIO_CODEC:
490 int ret = VIDEO_UTIL_ERROR_NONE;
491 audio_codec = atoi(cmd);
492 if (audio_codec < 0 || audio_codec >= VIDEO_UTIL_VIDEO_CODEC_NONE) {
493 LOGE("input cmd is out of range");
498 LOGE("video_util handle is NULL, please set format after create");
502 ret = video_util_set_video_codec(video_h, audio_codec);
503 if (ret != VIDEO_UTIL_ERROR_NONE) {
504 LOGE("video_util_set_video_codec is failed (%d)", ret);
512 case CURRENT_STATUS_SET_ACCURATE_MODE:
514 int ret = VIDEO_UTIL_ERROR_NONE;
515 accurate_mode = atoi(cmd);
518 LOGE("video_util handle is NULL, please set format after create");
522 ret = video_util_set_accurate_mode(video_h, (bool)accurate_mode);
523 if (ret != VIDEO_UTIL_ERROR_NONE) {
524 LOGE("video_util_set_video_codec is failed (%d)", ret);
532 case CURRENT_STATUS_SET_RESOLUTION:
534 int ret = VIDEO_UTIL_ERROR_NONE;
535 int value = atoi(cmd);
536 static int resolution_cnt = 0;
539 LOGE("video_util handle is NULL, please set format after create");
544 switch (resolution_cnt) {
553 ret = video_util_set_resolution(video_h, width, height);
554 if (ret != VIDEO_UTIL_ERROR_NONE) {
555 LOGE("video_util_set_resolution is failed (%d)", ret);
565 case CURRENT_STATUS_SET_FPS:
567 int ret = VIDEO_UTIL_ERROR_NONE;
570 if (fps < 5 || fps > 30) {
571 LOGE("input cmd is out of range");
576 LOGE("video_util handle is NULL, please set format after create");
580 ret = video_util_set_fps(video_h, fps);
581 if (ret != VIDEO_UTIL_ERROR_NONE) {
582 LOGE("video_util_set_fps is failed (%d)", ret);
590 case CURRENT_STATUS_SET_TIME:
592 int value = atoi(cmd);
593 static int set_time_cnt = 0;
595 switch (set_time_cnt) {
597 start_position = value;
606 make_video_cnt = value;
613 case CURRENT_STATUS_SET_OUTFILENAME:
615 LOGI("output file is %s", g_out);
617 g_out = g_strdup_printf("/opt/usr/media/%s", cmd);
618 LOGI("output file is %s", g_out);
626 g_timeout_add(100, timeout_menu_display, 0);
629 static void _display_sub_basic(void)
632 g_print("=========================================================================================\n");
633 g_print(" video_util_test test\n");
634 g_print("-----------------------------------------------------------------------------------------\n");
635 g_print(" a. Create \t\t\t");
636 g_print(" s. Start transcoding \t");
637 g_print(" c. Cancel transcoding \n");
638 g_print(" f. Set file format\t\t");
639 g_print("vc. Set video codec\t");
640 g_print("ac. Set audio codec\n");
641 g_print(" m. Set accurate mode\t\t");
642 g_print("vr. Set resolution\t");
643 g_print("vf. Set video fps\n");
644 g_print(" t. Set start/duration time \t");
645 g_print(" o. Set output filename\t");
646 g_print(" q. quite test suite\t\n");
647 g_print(" d. Destroy\t");
649 g_print("=========================================================================================\n");
652 gboolean input(GIOChannel *channel)
654 gchar buf[MAX_STRING_LEN];
656 GError *error = NULL;
658 g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
666 int main(int argc, char *argv[])
668 GIOChannel *stdin_channel;
669 GMainLoop *loop = g_main_loop_new(NULL, 0);
670 stdin_channel = g_io_channel_unix_new(0);
671 g_io_channel_set_flags(stdin_channel, G_IO_FLAG_NONBLOCK, NULL);
672 g_io_add_watch(stdin_channel, G_IO_IN, (GIOFunc)input, NULL);
676 g_main_loop_run(loop);
677 g_print("STOP main loop\n");
679 g_main_loop_unref(loop);