Modified video_util_test code and install video_util_test
[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 <Elementary.h>
23 #include <appcore-efl.h>
24 #include <video_util.h>
25
26 #define PACKAGE                 "video_util_test"
27 #define MAX_STRING_LEN  256
28
29 #ifdef LOG_TAG
30 #undef LOG_TAG
31 #endif
32
33 #define LOG_TAG                 "VIDEO_UTIL_TEST"
34
35 enum {
36         CURRENT_STATUS_MAINMENU,
37         CURRENT_STATUS_FILENAME,
38         CURRENT_STATUS_SET_FORMAT,
39         CURRENT_STATUS_SET_VIDEO_CODEC,
40         CURRENT_STATUS_SET_AUDIO_CODEC,
41         CURRENT_STATUS_SET_ACCURATE_MODE,
42         CURRENT_STATUS_SET_RESOLUTION,
43         CURRENT_STATUS_SET_FPS,
44         CURRENT_STATUS_SET_TIME,
45         CURRENT_STATUS_SET_OUTFILENAME,
46         CURRENT_STATUS_MAX
47 };
48
49 static video_util_h video_h = NULL;
50 char g_uri[MAX_STRING_LEN] = { 0, };
51
52 int g_menu_state = CURRENT_STATUS_MAINMENU;
53 int g_handle_num = 1;
54 int format = VIDEO_UTIL_FILE_FORMAT_3GP;
55 int video_codec = VIDEO_UTIL_VIDEO_CODEC_MPEG4;
56 int audio_codec = VIDEO_UTIL_AUDIO_CODEC_AAC;
57 int accurate_mode = 0;
58 int width = 176;
59 int height = 144;
60 int fps = 10;
61 unsigned long start_position = 0;
62 unsigned long duration = 5000;
63 int make_video_cnt = 1;
64 char g_out[MAX_STRING_LEN] = { 0, };
65
66 typedef struct {
67         video_util_h video_h;
68         int idx;
69         unsigned long start_time;
70         unsigned long duration;
71 } test_util_s;
72
73 static void display_sub_basic();
74
75 static int _create_app(void *data)
76 {
77         LOGD("My app is going alive!\n");
78         return 0;
79 }
80
81 static int _terminate_app(void *data)
82 {
83         LOGD("My app is going gone!\n");
84         return 0;
85 }
86
87 struct appcore_ops ops = {
88         .create = _create_app,
89         .terminate = _terminate_app,
90 };
91
92 void _quit_program(void)
93 {
94         elm_exit();
95 }
96
97 bool test_transcode_spec_cb(int value, void *user_data)
98 {
99         if (!user_data) {
100                 LOGE("user_data is NULL");
101                 return FALSE;
102         }
103
104         if (!strcmp(user_data, "format_check")) {
105                 switch (value) {
106                 case 0:
107                         LOGI("[%s] --- [3gp]", user_data);
108                         break;
109                 case 1:
110                         LOGI("[%s] --- [mp4]", user_data);
111                         break;
112                 default:
113                         break;
114                 }
115         } else if (!strcmp(user_data, "video_codec_check")) {
116                 switch (value) {
117                 case 0:
118                         LOGI("[%s] --- [m4v]", user_data);
119                         break;
120                 case 1:
121                         LOGI("[%s] --- [h263]", user_data);
122                         break;
123                 case 2:
124                         LOGI("[%s] --- [h264]", user_data);
125                         break;
126                 default:
127                         break;
128                 }
129         } else if (!strcmp(user_data, "audio_codec_check")) {
130                 switch (value) {
131                 case 0:
132                         LOGI("[%s] --- [aac]", user_data);
133                         break;
134                 case 1:
135                         LOGI("[%s] --- [amrnb]", user_data);
136                         break;
137                 default:
138                         break;
139                 }
140         }
141
142         return true;
143 }
144
145 bool _supported_spec_check(video_util_h handle)
146 {
147         int ret = 0;
148         ret = video_util_foreach_supported_file_format(handle, (video_util_supported_file_format_cb)test_transcode_spec_cb, "format_check");
149         printf("video_util_foreach_supported_file_format [%d]\n", ret);
150         ret = video_util_foreach_supported_video_codec(handle, (video_util_supported_video_encoder_cb)test_transcode_spec_cb, "video_codec_check");
151         printf("video_util_foreach_supported_video_codec [%d]\n", ret);
152         ret = video_util_foreach_supported_audio_codec(handle, (video_util_supported_audio_encoder_cb)test_transcode_spec_cb, "audio_codec_check");
153         printf("video_util_foreach_supported_audio_codec [%d]\n", ret);
154
155         return true;
156 }
157
158 void _transcode_completed_cb(video_util_error_e error, void *user_data)
159 {
160         int idx = 0;
161         unsigned long ntn_start_position = 0;
162
163         test_util_s *_util_s = (test_util_s *)user_data;
164
165         LOGI("transcode_completed_cb============= [%2d / %2d][%d]\n", _util_s->idx, make_video_cnt, error);
166         printf("transcode_completed_cb============= [%2d / %2d][%d]\n", _util_s->idx, make_video_cnt, error);
167
168         if (_util_s->idx == (make_video_cnt - 1)) {
169                 LOGI("End trascoding");
170                 video_util_destroy(_util_s->video_h);
171                 _util_s->video_h = NULL;
172                 video_h = NULL;
173                 return;
174         }
175
176         idx = _util_s->idx + 1;
177         ntn_start_position = _util_s->start_time + duration;
178         _util_s->idx = idx;
179         _util_s->start_time = ntn_start_position;
180
181         _video_util_start_transcoding((test_util_s *)_util_s);
182
183         return;
184 }
185
186 void _transcode_progress_cb(unsigned long current_position, unsigned long duration, void *user_data)
187 {
188         test_util_s *_util_s = (test_util_s *)user_data;
189
190         LOGD("transcode_progress_cb-------------- [%2d][%ld][%ld]\n", _util_s->idx, current_position, duration);
191
192         return;
193 }
194
195 void _video_util_start_transcoding(test_util_s *util_s)
196 {
197         int ret = VIDEO_UTIL_ERROR_NONE;
198         char output_file_path[MAX_STRING_LEN] = { 0, };
199
200         if (!video_h) {
201                 LOGE("video_util handle is NULL, please set format after create");
202                 return;
203         }
204
205         LOGI("video_util set below");
206         LOGI("format: %d, video codec: %d, audio codec: %d, accurate mode: %d", format, video_codec, audio_codec, accurate_mode);
207         LOGI("width: %d, height: %d, fps: %d", width, height, fps);
208         LOGI("start time: %lu, durtation: %lu", start_position, duration);
209
210         ret = video_util_set_file_format(video_h, format);
211         if (ret != VIDEO_UTIL_ERROR_NONE) {
212                 LOGE("video_util_set_file_format is failed (%d)", ret);
213                 return;
214         }
215
216         ret = video_util_set_video_codec(video_h, video_codec);
217         if (ret != VIDEO_UTIL_ERROR_NONE) {
218                 LOGE("video_util_set_video_codec is failed (%d)", ret);
219                 return;
220         }
221
222         ret = video_util_set_audio_codec(video_h, audio_codec);
223         if (ret != VIDEO_UTIL_ERROR_NONE) {
224                 LOGE("video_util_set_audio_codec is failed (%d)", ret);
225                 return;
226         }
227
228         ret = video_util_set_accurate_mode(video_h, accurate_mode);
229         if (ret != VIDEO_UTIL_ERROR_NONE) {
230                 LOGE("video_util_set_accurate_mode is failed (%d)", ret);
231                 return;
232         }
233
234         ret = video_util_set_resolution(video_h, width, height);
235         if (ret != VIDEO_UTIL_ERROR_NONE) {
236                 LOGE("video_util_set_resolution is failed (%d)", ret);
237                 return;
238         }
239
240         ret = video_util_set_fps(video_h, fps);
241         if (ret != VIDEO_UTIL_ERROR_NONE) {
242                 LOGE("video_util_set_fps is failed (%d)", ret);
243                 return;
244         }
245
246         memset(output_file_path, 0x00, MAX_STRING_LEN);
247
248         snprintf(output_file_path, MAX_STRING_LEN, "%s_%d.%s", g_out, util_s->idx, format ? "mp4" : "3gp");
249
250         LOGI("input start_time: %lu, duration: %lu, output_file_path: %s", util_s->start_time, util_s->duration, output_file_path);
251         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);
252
253         if (ret != VIDEO_UTIL_ERROR_NONE) {
254                 LOGE("video_util_start_transcoding is failed (%d)", ret);
255                 return;
256         }
257 }
258
259 void _reset_var()
260 {
261         if (video_h) {
262                 video_util_destroy(video_h);
263                 video_h = NULL;
264         }
265         memset(g_uri, 0x00, MAX_STRING_LEN);
266         memset(g_out, 0x00, MAX_STRING_LEN);
267         g_menu_state = CURRENT_STATUS_MAINMENU;
268         g_handle_num = 1;
269         format = VIDEO_UTIL_FILE_FORMAT_3GP;
270         video_codec = VIDEO_UTIL_VIDEO_CODEC_MPEG4;
271         audio_codec = VIDEO_UTIL_AUDIO_CODEC_AAC;
272         accurate_mode = 0;
273         width = 176;
274         height = 144;
275         fps = 25;
276         start_position = 5000;
277         duration = 5000;
278         make_video_cnt = 3;
279 }
280
281 static void input_filename(char *filename)
282 {
283         int len = strlen(filename);
284         int i = 0;
285         int ret = VIDEO_UTIL_ERROR_NONE;
286
287         if (len < 0 || len > MAX_STRING_LEN) {
288                 LOGE("Input file name is wrong");
289                 return;
290         }
291
292         _reset_var();
293
294         if (video_h) {
295                 ret = video_util_cancel_transcoding(video_h);
296                 ret = video_util_destroy(video_h);
297         }
298
299         video_h = NULL;
300
301         ret = video_util_create(&video_h);
302
303         if (ret != VIDEO_UTIL_ERROR_NONE) {
304                 LOGE("video_util create is failed (%d)", ret);
305                 return;
306         }
307
308         strncpy(g_uri, filename, len);
309
310         ret = video_util_set_file_path(video_h, g_uri);
311
312         if (ret != VIDEO_UTIL_ERROR_NONE) {
313                 LOGE("video_util_set_file_path is failed");
314                 return;
315         }
316
317         _supported_spec_check(video_h);
318 }
319
320 void reset_menu_state()
321 {
322         g_menu_state = CURRENT_STATUS_MAINMENU;
323         return;
324 }
325
326 void _interpret_main_menu(char *cmd)
327 {
328         int len = strlen(cmd);
329
330         if (len == 1) {
331                 if (strncmp(cmd, "a", 1) == 0) {
332                         g_menu_state = CURRENT_STATUS_FILENAME;
333                 } else if (strncmp(cmd, "s", 1) == 0) {
334
335                         test_util_s *_util_s = (test_util_s *)calloc(1, sizeof(test_util_s));
336                         if (!_util_s) {
337                                 g_print("test util calloc failed");
338                                 return;
339                         }
340
341                         _util_s->video_h = video_h;
342                         _util_s->idx = 0;
343                         _util_s->start_time = start_position;
344                         _util_s->duration = duration;
345
346                         _video_util_start_transcoding(_util_s);
347
348                 } else if (strncmp(cmd, "c", 1) == 0) {
349                         int ret = VIDEO_UTIL_ERROR_NONE;
350                         if (!video_h) {
351                                 g_print("video_util handle is NULL, please set format after create");
352                                 return;;
353                         }
354                         ret = video_util_cancel_transcoding(video_h);
355                         if (ret != VIDEO_UTIL_ERROR_NONE) {
356                                 g_print("video_util_cancel_transcoding is failed (%d)", ret);
357                                 return;
358                         }
359                 } else if (strncmp(cmd, "f", 1) == 0) {
360                         g_menu_state = CURRENT_STATUS_SET_FORMAT;
361                 } else if (strncmp(cmd, "m", 1) == 0) {
362                         g_menu_state = CURRENT_STATUS_SET_ACCURATE_MODE;
363                 } else if (strncmp(cmd, "t", 1) == 0) {
364                         g_menu_state = CURRENT_STATUS_SET_TIME;
365                 } else if (strncmp(cmd, "o", 1) == 0) {
366                         g_menu_state = CURRENT_STATUS_SET_OUTFILENAME;
367                 } else if (strncmp(cmd, "q", 1) == 0) {
368                         _quit_program();
369                 } else {
370                         g_print("unknown menu \n");
371                 }
372         } else if (len == 2) {
373                 if (strncmp(cmd, "vc", 2) == 0)
374                         g_menu_state = CURRENT_STATUS_SET_VIDEO_CODEC;
375                 else if (strncmp(cmd, "ac", 2) == 0)
376                         g_menu_state = CURRENT_STATUS_SET_AUDIO_CODEC;
377                 else if (strncmp(cmd, "vr", 2) == 0)
378                         g_menu_state = CURRENT_STATUS_SET_RESOLUTION;
379                 else if (strncmp(cmd, "vf", 2) == 0)
380                         g_menu_state = CURRENT_STATUS_SET_FPS;
381                 else
382                         g_print("unknown menu \n");
383         } else {
384                 g_print("unknown menu \n");
385         }
386         return;
387 }
388
389 static void displaymenu(void)
390 {
391         if (g_menu_state == CURRENT_STATUS_MAINMENU) {
392                 display_sub_basic();
393         } else if (g_menu_state == CURRENT_STATUS_FILENAME) {
394                 g_print("*** input mediapath.\n");
395         } else if (g_menu_state == CURRENT_STATUS_SET_FORMAT) {
396                 g_print("*** input file format.(0:3gp, 1:mp4)\n");
397         } else if (g_menu_state == CURRENT_STATUS_SET_VIDEO_CODEC) {
398                 g_print("*** input video codec.(0:m4v, 1:h263, 2:h264)\n");
399         } else if (g_menu_state == CURRENT_STATUS_SET_AUDIO_CODEC) {
400                 g_print("*** input audio codec.(0:aac, 1:amrnb)\n");
401         } else if (g_menu_state == CURRENT_STATUS_SET_ACCURATE_MODE) {
402                 g_print("*** input accurate mode.(0: OFF, 1: ON)\n");
403         } else if (g_menu_state == CURRENT_STATUS_SET_RESOLUTION) {
404                 g_print("*** input video resolution.(width, height)\n");
405         } else if (g_menu_state == CURRENT_STATUS_SET_FPS) {
406                 g_print("*** input video fps.(5<=fps<=30)\n");
407         } else if (g_menu_state == CURRENT_STATUS_SET_FPS) {
408                 g_print("*** input video fps.(5<=fps<=30)\n");
409         } else if (g_menu_state == CURRENT_STATUS_SET_TIME) {
410                 g_print("*** input transcode start/duration time(ms), run nth.(start time, duration, n)\n");
411         } else if (g_menu_state == CURRENT_STATUS_SET_OUTFILENAME) {
412                 g_print("*** input output filename.(defaunt path /opt/usr/media/)\n");
413         } else {
414                 g_print("*** unknown status.\n");
415                 _quit_program();
416         }
417         g_print(" >>> ");
418 }
419
420 gboolean timeout_menu_display(void *data)
421 {
422         displaymenu();
423         return FALSE;
424 }
425
426 static void interpret(char *cmd)
427 {
428         switch (g_menu_state) {
429
430         case CURRENT_STATUS_MAINMENU:
431                 {
432                         _interpret_main_menu(cmd);
433                         break;
434                 }
435         case CURRENT_STATUS_FILENAME:
436                 {
437                         input_filename(cmd);
438                         reset_menu_state();
439                         break;
440                 }
441         case CURRENT_STATUS_SET_FORMAT:
442                 {
443                         int ret = VIDEO_UTIL_ERROR_NONE;
444                         format = atoi(cmd);
445                         if (format < 0 || format >= VIDEO_UTIL_FILE_FORMAT_MAX) {
446                                 LOGE("input cmd is out of range.");
447                                 reset_menu_state();
448                                 break;
449                         }
450                         if (!video_h) {
451                                 LOGE("video_util handle is NULL, please set format after create");
452                                 reset_menu_state();
453                                 break;
454                         }
455                         ret = video_util_set_file_format(video_h, format);
456                         if (ret != VIDEO_UTIL_ERROR_NONE) {
457                                 LOGE("video_util_set_file_format is failed (%d)", ret);
458                                 reset_menu_state();
459                                 break;
460                         }
461
462                         reset_menu_state();
463                         break;
464                 }
465         case CURRENT_STATUS_SET_VIDEO_CODEC:
466                 {
467                         int ret = VIDEO_UTIL_ERROR_NONE;
468                         video_codec = atoi(cmd);
469                         if (video_codec < 0 || video_codec >= VIDEO_UTIL_VIDEO_CODEC_NONE) {
470                                 LOGE("input cmd is out of range");
471                                 reset_menu_state();
472                                 break;
473                         }
474                         if (!video_h) {
475                                 LOGE("video_util handle is NULL, please set format after create");
476                                 reset_menu_state();
477                                 break;
478                         }
479                         ret = video_util_set_video_codec(video_h, video_codec);
480                         if (ret != VIDEO_UTIL_ERROR_NONE) {
481                                 LOGE("video_util_set_video_codec is failed (%d)", ret);
482                                 reset_menu_state();
483                                 break;
484                         }
485
486                         reset_menu_state();
487                         break;
488                 }
489         case CURRENT_STATUS_SET_AUDIO_CODEC:
490                 {
491                         int ret = VIDEO_UTIL_ERROR_NONE;
492                         audio_codec = atoi(cmd);
493                         if (audio_codec < 0 || audio_codec >= VIDEO_UTIL_VIDEO_CODEC_NONE) {
494                                 LOGE("input cmd is out of range");
495                                 reset_menu_state();
496                                 break;
497                         }
498                         if (!video_h) {
499                                 LOGE("video_util handle is NULL, please set format after create");
500                                 reset_menu_state();
501                                 break;
502                         }
503                         ret = video_util_set_video_codec(video_h, audio_codec);
504                         if (ret != VIDEO_UTIL_ERROR_NONE) {
505                                 LOGE("video_util_set_video_codec is failed (%d)", ret);
506                                 reset_menu_state();
507                                 break;
508                         }
509
510                         reset_menu_state();
511                         break;
512                 }
513         case CURRENT_STATUS_SET_ACCURATE_MODE:
514                 {
515                         int ret = VIDEO_UTIL_ERROR_NONE;
516                         accurate_mode = atoi(cmd);
517
518                         if (!video_h) {
519                                 LOGE("video_util handle is NULL, please set format after create");
520                                 reset_menu_state();
521                                 break;
522                         }
523                         ret = video_util_set_accurate_mode(video_h, (bool)accurate_mode);
524                         if (ret != VIDEO_UTIL_ERROR_NONE) {
525                                 LOGE("video_util_set_video_codec is failed (%d)", ret);
526                                 reset_menu_state();
527                                 break;
528                         }
529
530                         reset_menu_state();
531                         break;
532                 }
533         case CURRENT_STATUS_SET_RESOLUTION:
534                 {
535                         int ret = VIDEO_UTIL_ERROR_NONE;
536                         int value = atoi(cmd);
537                         static int resolution_cnt = 0;
538
539                         if (!video_h) {
540                                 LOGE("video_util handle is NULL, please set format after create");
541                                 reset_menu_state();
542                                 break;
543                         }
544
545                         switch (resolution_cnt) {
546                         case 0:
547                                 width = value;
548                                 resolution_cnt++;
549                                 break;
550                         case 1:
551                                 resolution_cnt = 0;
552                                 height = value;
553
554                                 ret = video_util_set_resolution(video_h, width, height);
555                                 if (ret != VIDEO_UTIL_ERROR_NONE) {
556                                         LOGE("video_util_set_resolution is failed (%d)", ret);
557                                         reset_menu_state();
558                                         break;
559                                 }
560
561                                 reset_menu_state();
562                                 break;
563                         }
564                         break;
565                 }
566         case CURRENT_STATUS_SET_FPS:
567                 {
568                         int ret = VIDEO_UTIL_ERROR_NONE;
569                         fps = atoi(cmd);
570
571                         if (fps < 5 || fps > 30) {
572                                 LOGE("input cmd is out of range");
573                                 reset_menu_state();
574                                 break;
575                         }
576                         if (!video_h) {
577                                 LOGE("video_util handle is NULL, please set format after create");
578                                 reset_menu_state();
579                                 break;
580                         }
581                         ret = video_util_set_fps(video_h, fps);
582                         if (ret != VIDEO_UTIL_ERROR_NONE) {
583                                 LOGE("video_util_set_fps is failed (%d)", ret);
584                                 reset_menu_state();
585                                 break;
586                         }
587
588                         reset_menu_state();
589                         break;
590                 }
591         case CURRENT_STATUS_SET_TIME:
592                 {
593                         int ret = VIDEO_UTIL_ERROR_NONE;
594                         int value = atoi(cmd);
595                         static int set_time_cnt = 0;
596
597                         switch (set_time_cnt) {
598                         case 0:
599                                 start_position = value;
600                                 set_time_cnt++;
601                                 break;
602                         case 1:
603                                 duration = value;
604                                 set_time_cnt++;
605                                 break;
606                         case 2:
607                                 set_time_cnt = 0;
608                                 make_video_cnt = value;
609                                 reset_menu_state();
610                                 break;
611
612                         }
613                         break;
614                 }
615         case CURRENT_STATUS_SET_OUTFILENAME:
616                 {
617                         LOGI("output file is %s", g_out);
618                         snprintf(g_out, MAX_STRING_LEN, "/opt/usr/media/%s", cmd);
619                         g_out[MAX_STRING_LEN - 1] = '\0';
620                         LOGI("output file is %s", g_out);
621                         reset_menu_state();
622                         break;
623                 }
624         default:
625                 break;
626         }
627
628         g_timeout_add(100, timeout_menu_display, 0);
629 }
630
631 static void display_sub_basic()
632 {
633         g_print("\n");
634         g_print("=========================================================================================\n");
635         g_print("                                    video_util_test test\n");
636         g_print("-----------------------------------------------------------------------------------------\n");
637         g_print(" a. Create \t\t\t");
638         g_print(" s. Start transcoding \t");
639         g_print(" c. Cancel transcoding \n");
640         g_print(" f. Set file format\t\t");
641         g_print("vc. Set video codec\t");
642         g_print("ac. Set audio codec\n");
643         g_print(" m. Set accurate mode\t\t");
644         g_print("vr. Set resolution\t");
645         g_print("vf. Set video fps\n");
646         g_print(" t. Set start/duration time \t");
647         g_print(" o. Set output filename\t");
648         g_print(" q. quite test suite\t");
649         g_print("\n");
650         g_print("=========================================================================================\n");
651 }
652
653 gboolean input(GIOChannel *channel)
654 {
655         gchar buf[MAX_STRING_LEN];
656         gsize read;
657         GError *error = NULL;
658
659         g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
660         buf[read] = '\0';
661         g_strstrip(buf);
662         interpret(buf);
663
664         return TRUE;
665 }
666
667 int main(int argc, char *argv[])
668 {
669         GIOChannel *stdin_channel;
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         ops.data = NULL;
677
678         return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
679
680 }