ab6a87781537b3722faa35a58c41676f74818f1e
[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 char g_uri[MAX_STRING_LEN] = { 0, };
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 char g_out[MAX_STRING_LEN] = { 0, };
63
64 typedef struct {
65         video_util_h video_h;
66         int idx;
67         unsigned long start_time;
68         unsigned long duration;
69 } test_util_s;
70
71 static void display_sub_basic();
72 void _video_util_start_transcoding(test_util_s *util_s);
73
74 void _quit_program(void)
75 {
76         exit(0);
77 }
78
79 bool test_transcode_spec_cb(int value, void *user_data)
80 {
81         if (!user_data) {
82                 LOGE("user_data is NULL");
83                 return FALSE;
84         }
85
86         if (!strcmp(user_data, "format_check")) {
87                 switch (value) {
88                 case 0:
89                         LOGI("[%s] --- [3gp]", user_data);
90                         break;
91                 case 1:
92                         LOGI("[%s] --- [mp4]", user_data);
93                         break;
94                 default:
95                         break;
96                 }
97         } else if (!strcmp(user_data, "video_codec_check")) {
98                 switch (value) {
99                 case 0:
100                         LOGI("[%s] --- [m4v]", user_data);
101                         break;
102                 case 1:
103                         LOGI("[%s] --- [h263]", user_data);
104                         break;
105                 case 2:
106                         LOGI("[%s] --- [h264]", user_data);
107                         break;
108                 default:
109                         break;
110                 }
111         } else if (!strcmp(user_data, "audio_codec_check")) {
112                 switch (value) {
113                 case 0:
114                         LOGI("[%s] --- [aac]", user_data);
115                         break;
116                 case 1:
117                         LOGI("[%s] --- [amrnb]", user_data);
118                         break;
119                 default:
120                         break;
121                 }
122         }
123
124         return true;
125 }
126
127 bool _supported_spec_check(video_util_h handle)
128 {
129         int ret = 0;
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);
136
137         return true;
138 }
139
140 void _transcode_completed_cb(video_util_error_e error, void *user_data)
141 {
142         int idx = 0;
143         unsigned long ntn_start_position = 0;
144
145         test_util_s *_util_s = (test_util_s *)user_data;
146
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);
149
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;
154                 video_h = NULL;
155                 return;
156         }
157
158         idx = _util_s->idx + 1;
159         ntn_start_position = _util_s->start_time + duration;
160         _util_s->idx = idx;
161         _util_s->start_time = ntn_start_position;
162
163         _video_util_start_transcoding(_util_s);
164
165         return;
166 }
167
168 void _transcode_progress_cb(unsigned long current_position, unsigned long duration, void *user_data)
169 {
170         test_util_s *_util_s = (test_util_s *)user_data;
171
172         LOGD("transcode_progress_cb-------------- [%2d][%ld][%ld]\n", _util_s->idx, current_position, duration);
173
174         return;
175 }
176
177 void _video_util_start_transcoding(test_util_s *util_s)
178 {
179         int ret = VIDEO_UTIL_ERROR_NONE;
180         char output_file_path[MAX_STRING_LEN] = { 0, };
181
182         if (!video_h) {
183                 LOGE("video_util handle is NULL, please set format after create");
184                 return;
185         }
186
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);
191
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);
195                 return;
196         }
197
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);
201                 return;
202         }
203
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);
207                 return;
208         }
209
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);
213                 return;
214         }
215
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);
219                 return;
220         }
221
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);
225                 return;
226         }
227
228         memset(output_file_path, 0x00, MAX_STRING_LEN);
229
230         snprintf(output_file_path, MAX_STRING_LEN, "%s_%d.%s", g_out, util_s->idx, format ? "mp4" : "3gp");
231
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);
234
235         if (ret != VIDEO_UTIL_ERROR_NONE) {
236                 LOGE("video_util_start_transcoding is failed (%d)", ret);
237                 return;
238         }
239
240         return;
241 }
242
243 void _reset_var()
244 {
245         if (video_h) {
246                 video_util_destroy(video_h);
247                 video_h = NULL;
248         }
249         memset(g_uri, 0x00, MAX_STRING_LEN);
250         memset(g_out, 0x00, MAX_STRING_LEN);
251         g_menu_state = CURRENT_STATUS_MAINMENU;
252         g_handle_num = 1;
253         format = VIDEO_UTIL_FILE_FORMAT_3GP;
254         video_codec = VIDEO_UTIL_VIDEO_CODEC_MPEG4;
255         audio_codec = VIDEO_UTIL_AUDIO_CODEC_AAC;
256         accurate_mode = 0;
257         width = 176;
258         height = 144;
259         fps = 25;
260         start_position = 5000;
261         duration = 5000;
262         make_video_cnt = 3;
263 }
264
265 static void input_filename(char *filename)
266 {
267         int len = strlen(filename);
268         int ret = VIDEO_UTIL_ERROR_NONE;
269
270         if (len < 0 || len > MAX_STRING_LEN) {
271                 LOGE("Input file name is wrong");
272                 return;
273         }
274
275         _reset_var();
276
277         if (video_h) {
278                 ret = video_util_cancel_transcoding(video_h);
279                 ret = video_util_destroy(video_h);
280         }
281
282         video_h = NULL;
283
284         ret = video_util_create(&video_h);
285
286         if (ret != VIDEO_UTIL_ERROR_NONE) {
287                 LOGE("video_util create is failed (%d)", ret);
288                 return;
289         }
290
291         strncpy(g_uri, filename, len);
292
293         ret = video_util_set_file_path(video_h, g_uri);
294
295         if (ret != VIDEO_UTIL_ERROR_NONE) {
296                 LOGE("video_util_set_file_path is failed");
297                 return;
298         }
299
300         _supported_spec_check(video_h);
301 }
302
303 void reset_menu_state()
304 {
305         g_menu_state = CURRENT_STATUS_MAINMENU;
306         return;
307 }
308
309 void _interpret_main_menu(char *cmd)
310 {
311         int len = strlen(cmd);
312
313         if (len == 1) {
314                 if (strncmp(cmd, "a", 1) == 0) {
315                         g_menu_state = CURRENT_STATUS_FILENAME;
316                 } else if (strncmp(cmd, "s", 1) == 0) {
317
318                         test_util_s *_util_s = (test_util_s *)calloc(1, sizeof(test_util_s));
319                         if (!_util_s) {
320                                 g_print("test util calloc failed");
321                                 return;
322                         }
323
324                         _util_s->video_h = video_h;
325                         _util_s->idx = 0;
326                         _util_s->start_time = start_position;
327                         _util_s->duration = duration;
328
329                         _video_util_start_transcoding(_util_s);
330
331                 } else if (strncmp(cmd, "c", 1) == 0) {
332                         int ret = VIDEO_UTIL_ERROR_NONE;
333                         if (!video_h) {
334                                 g_print("video_util handle is NULL, please set format after create");
335                                 return;;
336                         }
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);
340                                 return;
341                         }
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) {
351                         _quit_program();
352                 } else {
353                         g_print("unknown menu \n");
354                 }
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;
364                 else
365                         g_print("unknown menu \n");
366         } else {
367                 g_print("unknown menu \n");
368         }
369         return;
370 }
371
372 static void displaymenu(void)
373 {
374         if (g_menu_state == CURRENT_STATUS_MAINMENU) {
375                 display_sub_basic();
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");
396         } else {
397                 g_print("*** unknown status.\n");
398                 _quit_program();
399         }
400         g_print(" >>> ");
401 }
402
403 gboolean timeout_menu_display(void *data)
404 {
405         displaymenu();
406         return FALSE;
407 }
408
409 static void interpret(char *cmd)
410 {
411         switch (g_menu_state) {
412
413         case CURRENT_STATUS_MAINMENU:
414                 {
415                         _interpret_main_menu(cmd);
416                         break;
417                 }
418         case CURRENT_STATUS_FILENAME:
419                 {
420                         input_filename(cmd);
421                         reset_menu_state();
422                         break;
423                 }
424         case CURRENT_STATUS_SET_FORMAT:
425                 {
426                         int ret = VIDEO_UTIL_ERROR_NONE;
427                         format = atoi(cmd);
428                         if (format < 0 || format >= VIDEO_UTIL_FILE_FORMAT_MAX) {
429                                 LOGE("input cmd is out of range.");
430                                 reset_menu_state();
431                                 break;
432                         }
433                         if (!video_h) {
434                                 LOGE("video_util handle is NULL, please set format after create");
435                                 reset_menu_state();
436                                 break;
437                         }
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);
441                                 reset_menu_state();
442                                 break;
443                         }
444
445                         reset_menu_state();
446                         break;
447                 }
448         case CURRENT_STATUS_SET_VIDEO_CODEC:
449                 {
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");
454                                 reset_menu_state();
455                                 break;
456                         }
457                         if (!video_h) {
458                                 LOGE("video_util handle is NULL, please set format after create");
459                                 reset_menu_state();
460                                 break;
461                         }
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);
465                                 reset_menu_state();
466                                 break;
467                         }
468
469                         reset_menu_state();
470                         break;
471                 }
472         case CURRENT_STATUS_SET_AUDIO_CODEC:
473                 {
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");
478                                 reset_menu_state();
479                                 break;
480                         }
481                         if (!video_h) {
482                                 LOGE("video_util handle is NULL, please set format after create");
483                                 reset_menu_state();
484                                 break;
485                         }
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);
489                                 reset_menu_state();
490                                 break;
491                         }
492
493                         reset_menu_state();
494                         break;
495                 }
496         case CURRENT_STATUS_SET_ACCURATE_MODE:
497                 {
498                         int ret = VIDEO_UTIL_ERROR_NONE;
499                         accurate_mode = atoi(cmd);
500
501                         if (!video_h) {
502                                 LOGE("video_util handle is NULL, please set format after create");
503                                 reset_menu_state();
504                                 break;
505                         }
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);
509                                 reset_menu_state();
510                                 break;
511                         }
512
513                         reset_menu_state();
514                         break;
515                 }
516         case CURRENT_STATUS_SET_RESOLUTION:
517                 {
518                         int ret = VIDEO_UTIL_ERROR_NONE;
519                         int value = atoi(cmd);
520                         static int resolution_cnt = 0;
521
522                         if (!video_h) {
523                                 LOGE("video_util handle is NULL, please set format after create");
524                                 reset_menu_state();
525                                 break;
526                         }
527
528                         switch (resolution_cnt) {
529                         case 0:
530                                 width = value;
531                                 resolution_cnt++;
532                                 break;
533                         case 1:
534                                 resolution_cnt = 0;
535                                 height = value;
536
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);
540                                         reset_menu_state();
541                                         break;
542                                 }
543
544                                 reset_menu_state();
545                                 break;
546                         }
547                         break;
548                 }
549         case CURRENT_STATUS_SET_FPS:
550                 {
551                         int ret = VIDEO_UTIL_ERROR_NONE;
552                         fps = atoi(cmd);
553
554                         if (fps < 5 || fps > 30) {
555                                 LOGE("input cmd is out of range");
556                                 reset_menu_state();
557                                 break;
558                         }
559                         if (!video_h) {
560                                 LOGE("video_util handle is NULL, please set format after create");
561                                 reset_menu_state();
562                                 break;
563                         }
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);
567                                 reset_menu_state();
568                                 break;
569                         }
570
571                         reset_menu_state();
572                         break;
573                 }
574         case CURRENT_STATUS_SET_TIME:
575                 {
576                         int value = atoi(cmd);
577                         static int set_time_cnt = 0;
578
579                         switch (set_time_cnt) {
580                         case 0:
581                                 start_position = value;
582                                 set_time_cnt++;
583                                 break;
584                         case 1:
585                                 duration = value;
586                                 set_time_cnt++;
587                                 break;
588                         case 2:
589                                 set_time_cnt = 0;
590                                 make_video_cnt = value;
591                                 reset_menu_state();
592                                 break;
593
594                         }
595                         break;
596                 }
597         case CURRENT_STATUS_SET_OUTFILENAME:
598                 {
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);
603                         reset_menu_state();
604                         break;
605                 }
606         default:
607                 break;
608         }
609
610         g_timeout_add(100, timeout_menu_display, 0);
611 }
612
613 static void display_sub_basic()
614 {
615         g_print("\n");
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");
631         g_print("\n");
632         g_print("=========================================================================================\n");
633 }
634
635 gboolean input(GIOChannel *channel)
636 {
637         gchar buf[MAX_STRING_LEN];
638         gsize read;
639         GError *error = NULL;
640
641         g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
642         buf[read] = '\0';
643         g_strstrip(buf);
644         interpret(buf);
645
646         return TRUE;
647 }
648
649 int main(int argc, char *argv[])
650 {
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);
656
657         displaymenu();
658
659         g_main_loop_run(loop);
660         g_print("STOP main loop\n");
661
662         g_main_loop_unref(loop);
663         return 0;
664
665 }