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;
74 static void display_sub_basic();
75 void _video_util_start_transcoding(test_util_s *util_s);
77 void _quit_program(void)
82 bool test_transcode_spec_cb(int value, void *user_data)
85 LOGE("user_data is NULL");
89 if (!strcmp(user_data, "format_check")) {
92 LOGI("[%s] --- [3gp]", (const char *)user_data);
95 LOGI("[%s] --- [mp4]", (const char *)user_data);
100 } else if (!strcmp(user_data, "video_codec_check")) {
103 LOGI("[%s] --- [m4v]", (const char *)user_data);
106 LOGI("[%s] --- [h263]", (const char *)user_data);
109 LOGI("[%s] --- [h264]", (const char *)user_data);
114 } else if (!strcmp(user_data, "audio_codec_check")) {
117 LOGI("[%s] --- [aac]", (const char *)user_data);
120 LOGI("[%s] --- [amrnb]", (const char *)user_data);
130 bool _supported_spec_check(video_util_h handle)
133 ret = video_util_foreach_supported_file_format(handle, (video_util_supported_file_format_cb)test_transcode_spec_cb, "format_check");
134 printf("video_util_foreach_supported_file_format [%d]\n", ret);
135 ret = video_util_foreach_supported_video_codec(handle, (video_util_supported_video_encoder_cb)test_transcode_spec_cb, "video_codec_check");
136 printf("video_util_foreach_supported_video_codec [%d]\n", ret);
137 ret = video_util_foreach_supported_audio_codec(handle, (video_util_supported_audio_encoder_cb)test_transcode_spec_cb, "audio_codec_check");
138 printf("video_util_foreach_supported_audio_codec [%d]\n", ret);
143 void _transcode_completed_cb(video_util_error_e error, void *user_data)
146 unsigned long ntn_start_position = 0;
148 test_util_s *_util_s = (test_util_s *)user_data;
151 LOGE("completed_cb user data is NULL");
155 LOGI("transcode_completed_cb============= [%2d / %2d][%d]\n", _util_s->idx, make_video_cnt, error);
156 printf("transcode_completed_cb============= [%2d / %2d][%d]\n", _util_s->idx, make_video_cnt, error);
158 if (_util_s->idx == (make_video_cnt - 1)) {
159 LOGI("End trascoding");
163 idx = _util_s->idx + 1;
164 ntn_start_position = _util_s->start_time + duration;
166 _util_s->start_time = ntn_start_position;
168 _video_util_start_transcoding(_util_s);
173 void _transcode_progress_cb(unsigned long current_position, unsigned long duration, void *user_data)
175 test_util_s *_util_s = (test_util_s *)user_data;
177 LOGD("transcode_progress_cb-------------- [%2d][%ld][%ld]\n", _util_s->idx, current_position, duration);
182 void _video_util_start_transcoding(test_util_s *util_s)
184 int ret = VIDEO_UTIL_ERROR_NONE;
185 char output_file_path[MAX_STRING_LEN] = { 0, };
188 LOGE("video_util handle is NULL, please set format after create");
192 LOGI("video_util set below");
193 LOGI("format: %d, video codec: %d, audio codec: %d, accurate mode: %d", format, video_codec, audio_codec, accurate_mode);
194 LOGI("width: %d, height: %d, fps: %d", width, height, fps);
195 LOGI("start time: %lu, durtation: %lu", start_position, duration);
197 ret = video_util_set_file_format(video_h, format);
198 if (ret != VIDEO_UTIL_ERROR_NONE) {
199 LOGE("video_util_set_file_format is failed (%d)", ret);
203 ret = video_util_set_video_codec(video_h, video_codec);
204 if (ret != VIDEO_UTIL_ERROR_NONE) {
205 LOGE("video_util_set_video_codec is failed (%d)", ret);
209 ret = video_util_set_audio_codec(video_h, audio_codec);
210 if (ret != VIDEO_UTIL_ERROR_NONE) {
211 LOGE("video_util_set_audio_codec is failed (%d)", ret);
215 ret = video_util_set_accurate_mode(video_h, accurate_mode);
216 if (ret != VIDEO_UTIL_ERROR_NONE) {
217 LOGE("video_util_set_accurate_mode is failed (%d)", ret);
221 ret = video_util_set_resolution(video_h, width, height);
222 if (ret != VIDEO_UTIL_ERROR_NONE) {
223 LOGE("video_util_set_resolution is failed (%d)", ret);
227 ret = video_util_set_fps(video_h, fps);
228 if (ret != VIDEO_UTIL_ERROR_NONE) {
229 LOGE("video_util_set_fps is failed (%d)", ret);
233 memset(output_file_path, 0x00, MAX_STRING_LEN);
235 snprintf(output_file_path, MAX_STRING_LEN, "%s_%d.%s", g_out, util_s->idx, format ? "mp4" : "3gp");
237 LOGI("input start_time: %lu, duration: %lu, output_file_path: %s", util_s->start_time, util_s->duration, output_file_path);
238 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);
251 video_util_destroy(video_h);
254 memset(g_uri, 0x00, MAX_STRING_LEN);
255 memset(g_out, 0x00, MAX_STRING_LEN);
256 g_menu_state = CURRENT_STATUS_MAINMENU;
258 format = VIDEO_UTIL_FILE_FORMAT_3GP;
259 video_codec = VIDEO_UTIL_VIDEO_CODEC_MPEG4;
260 audio_codec = VIDEO_UTIL_AUDIO_CODEC_AAC;
265 start_position = 5000;
270 static void input_filename(char *filename)
272 int len = strlen(filename);
273 int ret = VIDEO_UTIL_ERROR_NONE;
275 if (len < 0 || len > MAX_STRING_LEN - 1) {
276 LOGE("Input file name is wrong");
283 ret = video_util_cancel_transcoding(video_h);
284 ret = video_util_destroy(video_h);
289 ret = video_util_create(&video_h);
291 if (ret != VIDEO_UTIL_ERROR_NONE) {
292 LOGE("video_util create is failed (%d)", ret);
296 strncpy(g_uri, filename, len);
299 ret = video_util_set_file_path(video_h, g_uri);
301 if (ret != VIDEO_UTIL_ERROR_NONE) {
302 LOGE("video_util_set_file_path is failed");
306 _supported_spec_check(video_h);
309 void reset_menu_state()
311 g_menu_state = CURRENT_STATUS_MAINMENU;
315 void _interpret_main_menu(char *cmd)
317 int len = strlen(cmd);
320 if (strncmp(cmd, "a", 1) == 0) {
321 g_menu_state = CURRENT_STATUS_FILENAME;
322 } else if (strncmp(cmd, "s", 1) == 0) {
324 _util_s = (test_util_s *)calloc(1, sizeof(test_util_s));
325 LOGE("_util_s malloc");
329 LOGE("test util calloc failed");
333 _util_s->video_h = video_h;
335 _util_s->start_time = start_position;
336 _util_s->duration = duration;
338 _video_util_start_transcoding(_util_s);
340 } else if (strncmp(cmd, "c", 1) == 0) {
341 int ret = VIDEO_UTIL_ERROR_NONE;
343 g_print("video_util handle is NULL, please set format after create");
346 ret = video_util_cancel_transcoding(video_h);
347 if (ret != VIDEO_UTIL_ERROR_NONE) {
348 g_print("video_util_cancel_transcoding is failed (%d)", ret);
351 } else if (strncmp(cmd, "f", 1) == 0) {
352 g_menu_state = CURRENT_STATUS_SET_FORMAT;
353 } else if (strncmp(cmd, "m", 1) == 0) {
354 g_menu_state = CURRENT_STATUS_SET_ACCURATE_MODE;
355 } else if (strncmp(cmd, "t", 1) == 0) {
356 g_menu_state = CURRENT_STATUS_SET_TIME;
357 } else if (strncmp(cmd, "o", 1) == 0) {
358 g_menu_state = CURRENT_STATUS_SET_OUTFILENAME;
359 } else if (strncmp(cmd, "q", 1) == 0) {
361 } else if (strncmp(cmd, "d", 1) == 0) {
362 int ret = VIDEO_UTIL_ERROR_NONE;
364 g_print("video_util handle is NULL, please set format after create");
367 ret = video_util_destroy(video_h);
368 if (ret != VIDEO_UTIL_ERROR_NONE) {
369 g_print("video_util_cancel_transcoding is failed (%d)", ret);
375 g_print("unknown menu \n");
377 } else if (len == 2) {
378 if (strncmp(cmd, "vc", 2) == 0)
379 g_menu_state = CURRENT_STATUS_SET_VIDEO_CODEC;
380 else if (strncmp(cmd, "ac", 2) == 0)
381 g_menu_state = CURRENT_STATUS_SET_AUDIO_CODEC;
382 else if (strncmp(cmd, "vr", 2) == 0)
383 g_menu_state = CURRENT_STATUS_SET_RESOLUTION;
384 else if (strncmp(cmd, "vf", 2) == 0)
385 g_menu_state = CURRENT_STATUS_SET_FPS;
387 g_print("unknown menu \n");
389 g_print("unknown menu \n");
394 static void displaymenu(void)
396 if (g_menu_state == CURRENT_STATUS_MAINMENU) {
398 } else if (g_menu_state == CURRENT_STATUS_FILENAME) {
399 g_print("*** input mediapath.\n");
400 } else if (g_menu_state == CURRENT_STATUS_SET_FORMAT) {
401 g_print("*** input file format.(0:3gp, 1:mp4)\n");
402 } else if (g_menu_state == CURRENT_STATUS_SET_VIDEO_CODEC) {
403 g_print("*** input video codec.(0:m4v, 1:h263, 2:h264)\n");
404 } else if (g_menu_state == CURRENT_STATUS_SET_AUDIO_CODEC) {
405 g_print("*** input audio codec.(0:aac, 1:amrnb)\n");
406 } else if (g_menu_state == CURRENT_STATUS_SET_ACCURATE_MODE) {
407 g_print("*** input accurate mode.(0: OFF, 1: ON)\n");
408 } else if (g_menu_state == CURRENT_STATUS_SET_RESOLUTION) {
409 g_print("*** input video resolution.(width, height)\n");
410 } else if (g_menu_state == CURRENT_STATUS_SET_FPS) {
411 g_print("*** input video fps.(5<=fps<=30)\n");
412 } else if (g_menu_state == CURRENT_STATUS_SET_FPS) {
413 g_print("*** input video fps.(5<=fps<=30)\n");
414 } else if (g_menu_state == CURRENT_STATUS_SET_TIME) {
415 g_print("*** input transcode start/duration time(ms), run nth.(start time, duration, n)\n");
416 } else if (g_menu_state == CURRENT_STATUS_SET_OUTFILENAME) {
417 g_print("*** input output filename.(defaunt path /opt/usr/media/)\n");
419 g_print("*** unknown status.\n");
425 gboolean timeout_menu_display(void *data)
431 static void interpret(char *cmd)
433 switch (g_menu_state) {
435 case CURRENT_STATUS_MAINMENU:
437 _interpret_main_menu(cmd);
440 case CURRENT_STATUS_FILENAME:
446 case CURRENT_STATUS_SET_FORMAT:
448 int ret = VIDEO_UTIL_ERROR_NONE;
450 if (format < 0 || format >= VIDEO_UTIL_FILE_FORMAT_MAX) {
451 LOGE("input cmd is out of range.");
456 LOGE("video_util handle is NULL, please set format after create");
460 ret = video_util_set_file_format(video_h, format);
461 if (ret != VIDEO_UTIL_ERROR_NONE) {
462 LOGE("video_util_set_file_format is failed (%d)", ret);
470 case CURRENT_STATUS_SET_VIDEO_CODEC:
472 int ret = VIDEO_UTIL_ERROR_NONE;
473 video_codec = atoi(cmd);
474 if (video_codec < 0 || video_codec >= VIDEO_UTIL_VIDEO_CODEC_NONE) {
475 LOGE("input cmd is out of range");
480 LOGE("video_util handle is NULL, please set format after create");
484 ret = video_util_set_video_codec(video_h, video_codec);
485 if (ret != VIDEO_UTIL_ERROR_NONE) {
486 LOGE("video_util_set_video_codec is failed (%d)", ret);
494 case CURRENT_STATUS_SET_AUDIO_CODEC:
496 int ret = VIDEO_UTIL_ERROR_NONE;
497 audio_codec = atoi(cmd);
498 if (audio_codec < 0 || audio_codec >= VIDEO_UTIL_VIDEO_CODEC_NONE) {
499 LOGE("input cmd is out of range");
504 LOGE("video_util handle is NULL, please set format after create");
508 ret = video_util_set_video_codec(video_h, audio_codec);
509 if (ret != VIDEO_UTIL_ERROR_NONE) {
510 LOGE("video_util_set_video_codec is failed (%d)", ret);
518 case CURRENT_STATUS_SET_ACCURATE_MODE:
520 int ret = VIDEO_UTIL_ERROR_NONE;
521 accurate_mode = atoi(cmd);
524 LOGE("video_util handle is NULL, please set format after create");
528 ret = video_util_set_accurate_mode(video_h, (bool)accurate_mode);
529 if (ret != VIDEO_UTIL_ERROR_NONE) {
530 LOGE("video_util_set_video_codec is failed (%d)", ret);
538 case CURRENT_STATUS_SET_RESOLUTION:
540 int ret = VIDEO_UTIL_ERROR_NONE;
541 int value = atoi(cmd);
542 static int resolution_cnt = 0;
545 LOGE("video_util handle is NULL, please set format after create");
550 switch (resolution_cnt) {
559 ret = video_util_set_resolution(video_h, width, height);
560 if (ret != VIDEO_UTIL_ERROR_NONE) {
561 LOGE("video_util_set_resolution is failed (%d)", ret);
571 case CURRENT_STATUS_SET_FPS:
573 int ret = VIDEO_UTIL_ERROR_NONE;
576 if (fps < 5 || fps > 30) {
577 LOGE("input cmd is out of range");
582 LOGE("video_util handle is NULL, please set format after create");
586 ret = video_util_set_fps(video_h, fps);
587 if (ret != VIDEO_UTIL_ERROR_NONE) {
588 LOGE("video_util_set_fps is failed (%d)", ret);
596 case CURRENT_STATUS_SET_TIME:
598 int value = atoi(cmd);
599 static int set_time_cnt = 0;
601 switch (set_time_cnt) {
603 start_position = value;
612 make_video_cnt = value;
619 case CURRENT_STATUS_SET_OUTFILENAME:
621 LOGI("output file is %s", g_out);
622 snprintf(g_out, MAX_STRING_LEN, "/opt/usr/media/%s", cmd);
623 g_out[MAX_STRING_LEN - 1] = '\0';
624 LOGI("output file is %s", g_out);
632 g_timeout_add(100, timeout_menu_display, 0);
635 static void display_sub_basic()
638 g_print("=========================================================================================\n");
639 g_print(" video_util_test test\n");
640 g_print("-----------------------------------------------------------------------------------------\n");
641 g_print(" a. Create \t\t\t");
642 g_print(" s. Start transcoding \t");
643 g_print(" c. Cancel transcoding \n");
644 g_print(" f. Set file format\t\t");
645 g_print("vc. Set video codec\t");
646 g_print("ac. Set audio codec\n");
647 g_print(" m. Set accurate mode\t\t");
648 g_print("vr. Set resolution\t");
649 g_print("vf. Set video fps\n");
650 g_print(" t. Set start/duration time \t");
651 g_print(" o. Set output filename\t");
652 g_print(" q. quite test suite\t\n");
653 g_print(" d. Destroy\t");
655 g_print("=========================================================================================\n");
658 gboolean input(GIOChannel *channel)
660 gchar buf[MAX_STRING_LEN];
662 GError *error = NULL;
664 g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
672 int main(int argc, char *argv[])
674 GIOChannel *stdin_channel;
675 GMainLoop *loop = g_main_loop_new(NULL, 0);
676 stdin_channel = g_io_channel_unix_new(0);
677 g_io_channel_set_flags(stdin_channel, G_IO_FLAG_NONBLOCK, NULL);
678 g_io_add_watch(stdin_channel, G_IO_IN, (GIOFunc)input, NULL);
682 g_main_loop_run(loop);
683 g_print("STOP main loop\n");
685 g_main_loop_unref(loop);