Tizen 2.1 base
[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                 g_main_loop_quit(g_loop);
51                 return;
52         }
53
54         idx = _util_s->idx + 1;
55         start_position = _util_s->start_time + g_make_video_interval;
56         _util_s->idx = idx;
57         _util_s->start_time = start_position;
58
59         test_transcode_do(_util_s);
60
61 #ifdef CANCEL_TEST
62         int ret = VIDEO_UTIL_ERROR_NONE;
63         if(_util_s->idx == 3)
64         {
65                 printf("Try cancel============= [%2d]]\n", _util_s->idx);
66                 ret = video_util_cancel_transcoding(_util_s->video_h);
67                 if(ret != VIDEO_UTIL_ERROR_NONE)
68                 {
69                         printf("[%d]error video_util_cancel_transcoding [%d]\n", __LINE__, ret);
70                         g_main_loop_quit(g_loop);
71                         return;
72                 }
73
74                 _util_s->idx = 4;
75                 test_transcode_do(_util_s);
76                 return;
77         }
78 #endif
79         return;
80 }
81
82 void test_transcode_progress_cb(unsigned long current_position,  unsigned long duration, void *user_data)
83 {
84         test_util_s *_util_s = (test_util_s *)user_data;
85
86         printf("transcode_progress_cb-------------- [%2d][%ld][%ld]\n", _util_s->idx, current_position, duration);
87
88 #if 0
89         unsigned long pos = 0;
90         unsigned long dur = 0;
91         video_util_get_progress_transcoding(_util_s->video_h, &pos, &dur);
92         printf("transcode_progress_cb-------------- [%2d][%ld][%ld]\n", _util_s->idx, pos, dur);
93 #endif
94         return;
95 }
96
97 bool test_transcode_spec_cb(int value, void *user_data)
98 {
99         if(user_data != NULL)
100                 printf("[%s]-----------", (char*)user_data);
101         printf("[%d] \n", value);
102
103         return true;
104 }
105
106 bool supported_spec_check(video_util_h handle)
107 {
108         int ret = 0;
109         ret = video_util_foreach_supported_file_format(handle, (video_util_supported_file_format_cb)test_transcode_spec_cb, "format_check");
110         printf("[%d] video_util_foreach_supported_file_format [%d]\n", __LINE__, ret);
111         ret = video_util_foreach_supported_video_codec(handle, (video_util_supported_video_encoder_cb)test_transcode_spec_cb, "video_codec_check");
112         printf("[%d] video_util_foreach_supported_video_codec [%d]\n", __LINE__, ret);
113         ret = video_util_foreach_supported_audio_codec(handle, (video_util_supported_audio_encoder_cb)test_transcode_spec_cb, "audio_codec_check");
114         printf("[%d] video_util_foreach_supported_audio_codec [%d]\n", __LINE__, ret);
115
116         return true;
117 }
118
119 static int test_transcode_do(test_util_s *util_s)
120 {
121         int ret = 0;
122         char test_output_file_path[128] = {0, };
123
124         memset(test_output_file_path, 0x00, sizeof(test_output_file_path));
125         snprintf(test_output_file_path, sizeof(test_output_file_path), "/opt/media/Videos/transcode_test_%d.mp4", util_s->idx);
126
127         printf("g_start_time[%d] duration[%d] [%s]\n", util_s->start_time, util_s->duration, test_output_file_path);
128
129         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);
130         if(ret != VIDEO_UTIL_ERROR_NONE)
131         {
132                 printf("[%d]error video_util_start_transcoding [%d]\n", __LINE__, ret);
133                 g_main_loop_quit(g_loop);
134                 return ret;
135         }
136
137         return ret;
138 }
139
140 int main(int argc, char *argv[])
141 {
142         int ret = VIDEO_UTIL_ERROR_NONE;
143         video_util_h video_h = NULL;
144         test_util_s *_util_s = NULL;
145         char * test_video_file_path = "/opt/usr/media/Videos/Color.mp4";
146
147         ret = video_util_create(&video_h);
148         if(ret != VIDEO_UTIL_ERROR_NONE)
149         {
150                 printf("[%d]error video_util_create [%d]\n", __LINE__, ret);
151         }
152
153 #if 0
154         supported_spec_check(video_h);
155 #endif
156
157         ret = video_util_set_file_path(video_h, test_video_file_path);
158         ret = video_util_set_file_format(video_h, VIDEO_UTIL_FILE_FORMAT_3GP);
159         ret = video_util_set_video_codec(video_h, VIDEO_UTIL_VIDEO_CODEC_MPEG4);
160         ret = video_util_set_audio_codec(video_h, VIDEO_UTIL_AUDIO_CODEC_AAC);
161         ret = video_util_set_accurate_mode(video_h, 0);
162         ret = video_util_set_resolution(video_h, 176, 144);
163         ret = video_util_set_fps(video_h, 10);
164
165         if(ret != VIDEO_UTIL_ERROR_NONE)
166         {
167                 printf("[%d]error video_util_set condition [%d]\n", __LINE__, ret);
168                 return 0;
169         }
170
171         _util_s = (test_util_s*)calloc(1,sizeof(test_util_s));
172         if(_util_s == NULL)
173         {
174                 printf("[%d]error calloc\n", __LINE__);
175                 return 0;
176         }
177
178         _util_s->video_h = video_h;
179         _util_s->idx = 0;
180         _util_s->start_time = 0;
181         _util_s->duration = g_duration;
182
183         test_transcode_do(_util_s);
184
185         g_loop = g_main_loop_new(NULL, FALSE);
186
187         g_main_loop_run(g_loop);
188         g_main_loop_unref(g_loop);
189
190         ret = video_util_destroy(video_h);      //destory handle in cb
191         if(ret != VIDEO_UTIL_ERROR_NONE)
192         {
193                 printf("[%d]error video_util_destroy [%d]\n", __LINE__, ret);
194         }
195         else
196         {
197                 printf("[%d]Success video_util_destroy [%d]\n", __LINE__, ret);
198         }
199
200         if(_util_s != NULL)
201                 free(_util_s);
202
203         return 0;
204 }