Revise some testsuite code
[platform/core/api/video-util.git] / test / video_util_test.c
1 /*
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
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <glib.h>
21 #include <dlog.h>
22 #include <video_util.h>
23
24 #define PACKAGE                 "video_util_test"
25 #define MAX_STRING_LEN  256
26
27 #ifdef LOG_TAG
28 #undef LOG_TAG
29 #endif
30
31 #define LOG_TAG                 "VIDEO_UTIL_TEST"
32
33 enum {
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,
44         CURRENT_STATUS_MAX
45 };
46
47 static video_util_h video_h = NULL;
48 gchar *g_out = NULL;
49
50 int g_menu_state = CURRENT_STATUS_MAINMENU;
51 int g_handle_num = 1;
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;
56 int width = 176;
57 int height = 144;
58 int fps = 10;
59 unsigned long start_position = 0;
60 unsigned long duration = 5000;
61 int make_video_cnt = 1;
62
63 typedef struct {
64         video_util_h video_h;
65         int idx;
66         unsigned long start_time;
67         unsigned long duration;
68 } test_util_s;
69
70 test_util_s *_util_s;
71
72
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);
76
77 void _quit_program(void)
78 {
79         _reset_var();
80         exit(0);
81 }
82
83 bool test_transcode_spec_cb(int value, void *user_data)
84 {
85         if (!user_data) {
86                 LOGE("user_data is NULL");
87                 return FALSE;
88         }
89
90         if (!strcmp(user_data, "format_check")) {
91                 switch (value) {
92                 case 0:
93                         LOGI("[%s] --- [3gp]", (const char *)user_data);
94                         break;
95                 case 1:
96                         LOGI("[%s] --- [mp4]", (const char *)user_data);
97                         break;
98                 default:
99                         break;
100                 }
101         } else if (!strcmp(user_data, "video_codec_check")) {
102                 switch (value) {
103                 case 0:
104                         LOGI("[%s] --- [m4v]", (const char *)user_data);
105                         break;
106                 case 1:
107                         LOGI("[%s] --- [h263]", (const char *)user_data);
108                         break;
109                 case 2:
110                         LOGI("[%s] --- [h264]", (const char *)user_data);
111                         break;
112                 default:
113                         break;
114                 }
115         } else if (!strcmp(user_data, "audio_codec_check")) {
116                 switch (value) {
117                 case 0:
118                         LOGI("[%s] --- [aac]", (const char *)user_data);
119                         break;
120                 case 1:
121                         LOGI("[%s] --- [amrnb]", (const char *)user_data);
122                         break;
123                 default:
124                         break;
125                 }
126         }
127
128         return true;
129 }
130
131 bool _supported_spec_check(video_util_h handle)
132 {
133         int ret = 0;
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);
140
141         return true;
142 }
143
144 void _transcode_completed_cb(video_util_error_e error, void *user_data)
145 {
146         int idx = 0;
147         unsigned long ntn_start_position = 0;
148
149         test_util_s *_util_s = (test_util_s *)user_data;
150
151         if (!_util_s) {
152                 LOGE("completed_cb user data is NULL");
153                 return;
154         }
155
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);
158
159         if (_util_s->idx == (make_video_cnt - 1)) {
160                 LOGI("End trascoding");
161                 return;
162         }
163
164         idx = _util_s->idx + 1;
165         ntn_start_position = _util_s->start_time + duration;
166         _util_s->idx = idx;
167         _util_s->start_time = ntn_start_position;
168
169         _video_util_start_transcoding(_util_s);
170
171         return;
172 }
173
174 void _transcode_progress_cb(unsigned long current_position, unsigned long duration, void *user_data)
175 {
176         test_util_s *_util_s = (test_util_s *)user_data;
177
178         LOGD("transcode_progress_cb-------------- [%2d][%ld][%ld]\n", _util_s->idx, current_position, duration);
179
180         return;
181 }
182
183 static void _video_util_start_transcoding(test_util_s *util_s)
184 {
185         int ret = VIDEO_UTIL_ERROR_NONE;
186         gchar *output_file_path = NULL;
187
188         if (!video_h) {
189                 LOGE("video_util handle is NULL, please set format after create");
190                 return;
191         }
192
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);
197
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);
201                 return;
202         }
203
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);
207                 return;
208         }
209
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);
213                 return;
214         }
215
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);
219                 return;
220         }
221
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);
225                 return;
226         }
227
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);
231                 return;
232         }
233
234         output_file_path = g_strdup_printf("%s_%d.%s", g_out, util_s->idx, format ? "mp4" : "3gp");
235         g_free(g_out);
236         g_out = NULL;
237
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
243         g_free(output_file_path);
244         return;
245 }
246
247 static void _reset_var(void)
248 {
249         if (video_h) {
250                 video_util_destroy(video_h);
251                 video_h = NULL;
252         }
253
254         g_free(g_out);
255         g_out = NULL;
256
257         g_menu_state = CURRENT_STATUS_MAINMENU;
258         g_handle_num = 1;
259         format = VIDEO_UTIL_FILE_FORMAT_3GP;
260         video_codec = VIDEO_UTIL_VIDEO_CODEC_MPEG4;
261         audio_codec = VIDEO_UTIL_AUDIO_CODEC_AAC;
262         accurate_mode = 0;
263         width = 176;
264         height = 144;
265         fps = 25;
266         start_position = 5000;
267         duration = 5000;
268         make_video_cnt = 3;
269 }
270
271 static void input_filename(char *filename)
272 {
273         int len = strlen(filename);
274         int ret = VIDEO_UTIL_ERROR_NONE;
275
276         if (len < 0 || len > MAX_STRING_LEN - 1) {
277                 LOGE("Input file name is wrong");
278                 return;
279         }
280
281         _reset_var();
282
283         if (video_h) {
284                 ret = video_util_cancel_transcoding(video_h);
285                 ret = video_util_destroy(video_h);
286         }
287
288         video_h = NULL;
289
290         ret = video_util_create(&video_h);
291         if (ret != VIDEO_UTIL_ERROR_NONE) {
292                 LOGE("video_util create is failed (%d)", ret);
293                 return;
294         }
295
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");
299                 return;
300         }
301
302         _supported_spec_check(video_h);
303 }
304
305 void reset_menu_state()
306 {
307         g_menu_state = CURRENT_STATUS_MAINMENU;
308         return;
309 }
310
311 void _interpret_main_menu(char *cmd)
312 {
313         int len = strlen(cmd);
314
315         if (len == 1) {
316                 if (strncmp(cmd, "a", 1) == 0) {
317                         g_menu_state = CURRENT_STATUS_FILENAME;
318                 } else if (strncmp(cmd, "s", 1) == 0) {
319                         if (!_util_s) {
320                                 _util_s = (test_util_s *)calloc(1, sizeof(test_util_s));
321                                 LOGE("_util_s malloc");
322                         }
323
324                         if (!_util_s) {
325                                 LOGE("test util calloc failed");
326                                 return;
327                         }
328
329                         _util_s->video_h = video_h;
330                         _util_s->idx = 0;
331                         _util_s->start_time = start_position;
332                         _util_s->duration = duration;
333
334                         _video_util_start_transcoding(_util_s);
335
336                 } else if (strncmp(cmd, "c", 1) == 0) {
337                         int ret = VIDEO_UTIL_ERROR_NONE;
338                         if (!video_h) {
339                                 g_print("video_util handle is NULL, please set format after create");
340                                 return;;
341                         }
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);
345                                 return;
346                         }
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) {
356                         _quit_program();
357                 } else if (strncmp(cmd, "d", 1) == 0) {
358                         int ret = VIDEO_UTIL_ERROR_NONE;
359                         if (!video_h) {
360                                 g_print("video_util handle is NULL, please set format after create");
361                                 return;;
362                         }
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);
366                                 return;
367                         }
368                         free(_util_s);
369                         _util_s = NULL;
370                 } else {
371                         g_print("unknown menu \n");
372                 }
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;
382                 else
383                         g_print("unknown menu \n");
384         } else {
385                 g_print("unknown menu \n");
386         }
387         return;
388 }
389
390 static void displaymenu(void)
391 {
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");
412         } else {
413                 g_print("*** unknown status.\n");
414                 _quit_program();
415         }
416         g_print(" >>> ");
417 }
418
419 gboolean timeout_menu_display(void *data)
420 {
421         displaymenu();
422         return FALSE;
423 }
424
425 static void interpret(char *cmd)
426 {
427         switch (g_menu_state) {
428
429         case CURRENT_STATUS_MAINMENU:
430                 {
431                         _interpret_main_menu(cmd);
432                         break;
433                 }
434         case CURRENT_STATUS_FILENAME:
435                 {
436                         input_filename(cmd);
437                         reset_menu_state();
438                         break;
439                 }
440         case CURRENT_STATUS_SET_FORMAT:
441                 {
442                         int ret = VIDEO_UTIL_ERROR_NONE;
443                         format = atoi(cmd);
444                         if (format < 0 || format >= VIDEO_UTIL_FILE_FORMAT_MAX) {
445                                 LOGE("input cmd is out of range.");
446                                 reset_menu_state();
447                                 break;
448                         }
449                         if (!video_h) {
450                                 LOGE("video_util handle is NULL, please set format after create");
451                                 reset_menu_state();
452                                 break;
453                         }
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);
457                                 reset_menu_state();
458                                 break;
459                         }
460
461                         reset_menu_state();
462                         break;
463                 }
464         case CURRENT_STATUS_SET_VIDEO_CODEC:
465                 {
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");
470                                 reset_menu_state();
471                                 break;
472                         }
473                         if (!video_h) {
474                                 LOGE("video_util handle is NULL, please set format after create");
475                                 reset_menu_state();
476                                 break;
477                         }
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);
481                                 reset_menu_state();
482                                 break;
483                         }
484
485                         reset_menu_state();
486                         break;
487                 }
488         case CURRENT_STATUS_SET_AUDIO_CODEC:
489                 {
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");
494                                 reset_menu_state();
495                                 break;
496                         }
497                         if (!video_h) {
498                                 LOGE("video_util handle is NULL, please set format after create");
499                                 reset_menu_state();
500                                 break;
501                         }
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);
505                                 reset_menu_state();
506                                 break;
507                         }
508
509                         reset_menu_state();
510                         break;
511                 }
512         case CURRENT_STATUS_SET_ACCURATE_MODE:
513                 {
514                         int ret = VIDEO_UTIL_ERROR_NONE;
515                         accurate_mode = atoi(cmd);
516
517                         if (!video_h) {
518                                 LOGE("video_util handle is NULL, please set format after create");
519                                 reset_menu_state();
520                                 break;
521                         }
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);
525                                 reset_menu_state();
526                                 break;
527                         }
528
529                         reset_menu_state();
530                         break;
531                 }
532         case CURRENT_STATUS_SET_RESOLUTION:
533                 {
534                         int ret = VIDEO_UTIL_ERROR_NONE;
535                         int value = atoi(cmd);
536                         static int resolution_cnt = 0;
537
538                         if (!video_h) {
539                                 LOGE("video_util handle is NULL, please set format after create");
540                                 reset_menu_state();
541                                 break;
542                         }
543
544                         switch (resolution_cnt) {
545                         case 0:
546                                 width = value;
547                                 resolution_cnt++;
548                                 break;
549                         case 1:
550                                 resolution_cnt = 0;
551                                 height = value;
552
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);
556                                         reset_menu_state();
557                                         break;
558                                 }
559
560                                 reset_menu_state();
561                                 break;
562                         }
563                         break;
564                 }
565         case CURRENT_STATUS_SET_FPS:
566                 {
567                         int ret = VIDEO_UTIL_ERROR_NONE;
568                         fps = atoi(cmd);
569
570                         if (fps < 5 || fps > 30) {
571                                 LOGE("input cmd is out of range");
572                                 reset_menu_state();
573                                 break;
574                         }
575                         if (!video_h) {
576                                 LOGE("video_util handle is NULL, please set format after create");
577                                 reset_menu_state();
578                                 break;
579                         }
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);
583                                 reset_menu_state();
584                                 break;
585                         }
586
587                         reset_menu_state();
588                         break;
589                 }
590         case CURRENT_STATUS_SET_TIME:
591                 {
592                         int value = atoi(cmd);
593                         static int set_time_cnt = 0;
594
595                         switch (set_time_cnt) {
596                         case 0:
597                                 start_position = value;
598                                 set_time_cnt++;
599                                 break;
600                         case 1:
601                                 duration = value;
602                                 set_time_cnt++;
603                                 break;
604                         case 2:
605                                 set_time_cnt = 0;
606                                 make_video_cnt = value;
607                                 reset_menu_state();
608                                 break;
609
610                         }
611                         break;
612                 }
613         case CURRENT_STATUS_SET_OUTFILENAME:
614                 {
615                         LOGI("output file is %s", g_out);
616                         g_free(g_out);
617                         g_out = g_strdup_printf("/opt/usr/media/%s", cmd);
618                         LOGI("output file is %s", g_out);
619                         reset_menu_state();
620                         break;
621                 }
622         default:
623                 break;
624         }
625
626         g_timeout_add(100, timeout_menu_display, 0);
627 }
628
629 static void _display_sub_basic(void)
630 {
631         g_print("\n");
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");
648         g_print("\n");
649         g_print("=========================================================================================\n");
650 }
651
652 gboolean input(GIOChannel *channel)
653 {
654         gchar buf[MAX_STRING_LEN];
655         gsize read;
656         GError *error = NULL;
657
658         g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
659         buf[read] = '\0';
660         g_strstrip(buf);
661         interpret(buf);
662
663         return TRUE;
664 }
665
666 int main(int argc, char *argv[])
667 {
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);
673
674         displaymenu();
675
676         g_main_loop_run(loop);
677         g_print("STOP main loop\n");
678
679         g_main_loop_unref(loop);
680         return 0;
681
682 }