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