921d0a374e3df5772ffcb0bf91cd2c26fb47a1a8
[platform/core/api/video-util.git] / test / video_util_test.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <glib.h>
21 #include <video_util.h>
22
23 //#define CANCEL_TEST
24
25 typedef struct
26 {
27         video_util_h video_h;
28         int     idx;
29         int start_time;
30         int duration;
31 }test_util_s;
32
33 GMainLoop *g_loop = NULL;
34 int g_make_video_cnt = 5;
35 int g_make_video_interval = 10000;
36 int g_duration = 5000;
37
38 static int test_transcode_do(test_util_s *_util_s);
39
40 void test_transcode_completed_cb(video_util_error_e error, void *user_data)
41 {
42         int idx = 0;
43         int start_position = 0;
44
45         test_util_s *_util_s = (test_util_s *)user_data;
46         printf("transcode_completed_cb============= [%2d][%d]\n", _util_s->idx, error);
47
48         if(_util_s->idx == (g_make_video_cnt-1))
49         {
50                 if(g_loop)
51                         g_main_loop_quit(g_loop);
52                 return;
53         }
54
55         idx = _util_s->idx + 1;
56         start_position = _util_s->start_time + g_make_video_interval;
57         _util_s->idx = idx;
58         _util_s->start_time = start_position;
59
60         test_transcode_do(_util_s);
61
62 #ifdef CANCEL_TEST
63         int ret = VIDEO_UTIL_ERROR_NONE;
64         if(_util_s->idx == 3)
65         {
66                 printf("Try cancel============= [%2d]]\n", _util_s->idx);
67                 ret = video_util_cancel_transcoding(_util_s->video_h);
68                 if(ret != VIDEO_UTIL_ERROR_NONE)
69                 {
70                         printf("[%d]error video_util_cancel_transcoding [%d]\n", __LINE__, ret);
71                         if(g_loop)
72                                 g_main_loop_quit(g_loop);
73                         return;
74                 }
75
76                 _util_s->idx = 4;
77                 test_transcode_do(_util_s);
78                 return;
79         }
80 #endif
81         return;
82 }
83
84 void test_transcode_progress_cb(unsigned long current_position,  unsigned long duration, void *user_data)
85 {
86         test_util_s *_util_s = (test_util_s *)user_data;
87
88         printf("transcode_progress_cb-------------- [%2d][%ld][%ld]\n", _util_s->idx, current_position, duration);
89
90 #if 0
91         unsigned long pos = 0;
92         unsigned long dur = 0;
93         video_util_get_progress_transcoding(_util_s->video_h, &pos, &dur);
94         printf("transcode_progress_cb-------------- [%2d][%ld][%ld]\n", _util_s->idx, pos, dur);
95 #endif
96         return;
97 }
98
99 bool test_transcode_spec_cb(int value, void *user_data)
100 {
101         if(user_data != NULL)
102                 printf("[%s]-----------", (char*)user_data);
103         printf("[%d] \n", value);
104
105         return true;
106 }
107
108 bool supported_spec_check(video_util_h handle)
109 {
110         int ret = 0;
111         ret = video_util_foreach_supported_file_format(handle, (video_util_supported_file_format_cb)test_transcode_spec_cb, "format_check");
112         printf("video_util_foreach_supported_file_format [%d]\n", ret);
113         ret = video_util_foreach_supported_video_codec(handle, (video_util_supported_video_encoder_cb)test_transcode_spec_cb, "video_codec_check");
114         printf("video_util_foreach_supported_video_codec [%d]\n", ret);
115         ret = video_util_foreach_supported_audio_codec(handle, (video_util_supported_audio_encoder_cb)test_transcode_spec_cb, "audio_codec_check");
116         printf("video_util_foreach_supported_audio_codec [%d]\n", ret);
117
118         return true;
119 }
120
121 static int test_transcode_do(test_util_s *util_s)
122 {
123         int ret = VIDEO_UTIL_ERROR_NONE;
124         char test_output_file_path[128] = {0, };
125
126         memset(test_output_file_path, 0x00, sizeof(test_output_file_path));
127         snprintf(test_output_file_path, sizeof(test_output_file_path), "/opt/usr/media/Videos/transcode_test_%d.mp4", util_s->idx);
128
129         printf("g_start_time[%d] duration[%d] [%s]\n", util_s->start_time, util_s->duration, test_output_file_path);
130
131         ret = video_util_start_transcoding(util_s->video_h, util_s->start_time, util_s->duration, test_output_file_path, test_transcode_progress_cb, test_transcode_completed_cb, util_s);
132         if(ret != VIDEO_UTIL_ERROR_NONE)
133         {
134                 printf("[%d]error video_util_start_transcoding [%d]\n", __LINE__, ret);
135                 if(g_loop)
136                         g_main_loop_quit(g_loop);
137                 return ret;
138         }
139
140         return ret;
141 }
142
143 int main(int argc, char *argv[])
144 {
145         int ret = VIDEO_UTIL_ERROR_NONE;
146         video_util_h video_h = NULL;
147         test_util_s *_util_s = NULL;
148         int cnt = argc -1;
149
150         if(cnt < 1)
151         {
152                 printf("type file path plz. [%d]\n", cnt);
153                 return 0;
154         }
155
156         ret = video_util_create(&video_h);
157         if(ret != VIDEO_UTIL_ERROR_NONE)
158         {
159                 printf("[%d]error video_util_create [%d]\n", __LINE__, ret);
160         }
161
162 #if 0
163         supported_spec_check(video_h);
164 #endif
165
166         ret = video_util_set_file_path(video_h,  argv[1]);
167         ret = video_util_set_file_format(video_h, VIDEO_UTIL_FILE_FORMAT_3GP);
168         ret = video_util_set_video_codec(video_h, VIDEO_UTIL_VIDEO_CODEC_MPEG4);
169         ret = video_util_set_audio_codec(video_h, VIDEO_UTIL_AUDIO_CODEC_AAC);
170         ret = video_util_set_accurate_mode(video_h, 0);
171         ret = video_util_set_resolution(video_h, 176, 144);
172         ret = video_util_set_fps(video_h, 10);
173
174         if(ret != VIDEO_UTIL_ERROR_NONE)
175         {
176                 printf("[%d]error video_util_set condition [%d]\n", __LINE__, ret);
177                 return 0;
178         }
179
180         _util_s = (test_util_s*)calloc(1,sizeof(test_util_s));
181         if(_util_s == NULL)
182         {
183                 printf("[%d]error calloc\n", __LINE__);
184                 return 0;
185         }
186
187         _util_s->video_h = video_h;
188         _util_s->idx = 0;
189         _util_s->start_time = 0;
190         _util_s->duration = g_duration;
191
192         ret = test_transcode_do(_util_s);
193         if(ret != VIDEO_UTIL_ERROR_NONE)
194                 goto Exit;
195
196         g_loop = g_main_loop_new(NULL, FALSE);
197
198         g_main_loop_run(g_loop);
199         g_main_loop_unref(g_loop);
200
201 Exit:
202         ret = video_util_destroy(video_h);      //destory handle in cb
203         if(ret != VIDEO_UTIL_ERROR_NONE)
204         {
205                 printf("[%d]error video_util_destroy [%d]\n", __LINE__, ret);
206         }
207         else
208         {
209                 printf("[%d]Success video_util_destroy [%d]\n", __LINE__, ret);
210         }
211
212         if(_util_s != NULL)
213                 free(_util_s);
214
215         return 0;
216 }