remove build warning
[platform/core/api/mediastreamrecorder.git] / src / streamrecorder_private.c
1 /*
2 * Copyright (c) 2015 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 <mm.h>
20 #include <mm_types.h>
21 #include <mm_streamrecorder.h>
22 #include <streamrecorder_private.h>
23 #include <dlog.h>
24 #include <math.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <fcntl.h>
28
29
30 #define LOWSET_DECIBEL -300.0
31
32 int __convert_streamrecorder_error_code(const char *func, int code)
33 {
34         int ret = STREAMRECORDER_ERROR_INVALID_OPERATION;
35         const char *errorstr = NULL;
36
37         switch (code) {
38         case STREAMRECORDER_ERROR_INVALID_PARAMETER:
39                 ret = STREAMRECORDER_ERROR_INVALID_PARAMETER;
40                 errorstr = "INVALID_PARAMETER";
41                 break;
42         case MM_ERROR_NONE:
43                 ret = STREAMRECORDER_ERROR_NONE;
44                 errorstr = "ERROR_NONE";
45                 break;
46         case MM_ERROR_STREAMRECORDER_INVALID_ARGUMENT:
47         case MM_ERROR_COMMON_INVALID_ATTRTYPE:
48                 ret = STREAMRECORDER_ERROR_INVALID_PARAMETER;
49                 errorstr = "INVALID_PARAMETER";
50                 break;
51         case MM_ERROR_COMMON_INVALID_PERMISSION:
52                 ret = STREAMRECORDER_ERROR_PERMISSION_DENIED;
53                 errorstr = "ERROR_PERMISSION_DENIED";
54                 break;
55         case MM_ERROR_STREAMRECORDER_NOT_INITIALIZED:
56                 ret = STREAMRECORDER_ERROR_INVALID_STATE;
57                 errorstr = "INVALID_STATE";
58                 break;
59         case MM_ERROR_STREAMRECORDER_GST_CORE:
60         case MM_ERROR_STREAMRECORDER_GST_LIBRARY:
61         case MM_ERROR_STREAMRECORDER_GST_RESOURCE:
62         case MM_ERROR_STREAMRECORDER_GST_STREAM:
63         case MM_ERROR_STREAMRECORDER_GST_STATECHANGE:
64         case MM_ERROR_STREAMRECORDER_GST_NEGOTIATION:
65         case MM_ERROR_STREAMRECORDER_GST_LINK:
66         case MM_ERROR_STREAMRECORDER_GST_FLOW_ERROR:
67         case MM_ERROR_STREAMRECORDER_ENCODER:
68         case MM_ERROR_STREAMRECORDER_ENCODER_BUFFER:
69         case MM_ERROR_STREAMRECORDER_ENCODER_WRONG_TYPE:
70         case MM_ERROR_STREAMRECORDER_ENCODER_WORKING:
71         case MM_ERROR_STREAMRECORDER_RESPONSE_TIMEOUT:
72         case MM_ERROR_STREAMRECORDER_CMD_IS_RUNNING:
73                 ret = STREAMRECORDER_ERROR_INVALID_OPERATION;
74                 errorstr = "INVALID_OPERATION";
75                 break;
76         case MM_ERROR_STREAMRECORDER_RESOURCE_CREATION:
77         case MM_ERROR_COMMON_OUT_OF_MEMORY:
78                 ret = STREAMRECORDER_ERROR_OUT_OF_MEMORY;
79                 errorstr = "OUT_OF_MEMORY";
80                 break;
81         case MM_ERROR_OUT_OF_STORAGE:
82                 ret = STREAMRECORDER_ERROR_OUT_OF_STORAGE;
83                 errorstr = "OUT_OF_STORAGE";
84                 break;
85         case MM_ERROR_COMMON_OUT_OF_ARRAY:
86         case MM_ERROR_COMMON_OUT_OF_RANGE:
87         case MM_ERROR_COMMON_ATTR_NOT_EXIST:
88                 ret = STREAMRECORDER_ERROR_NOT_SUPPORTED;
89                 errorstr = "NOT_SUPPORTED";
90                 break;
91         default:
92                 ret = STREAMRECORDER_ERROR_INVALID_OPERATION;
93                 errorstr = "INVALID_OPERATION";
94                 break;
95         }
96
97
98         LOGE("[%s] %s(0x%08x) : core frameworks error code(0x%08x)", func, errorstr, ret, code);
99
100         return ret;
101 }
102
103 static streamrecorder_state_e __streamrecorder_state_convert(void *mm_state)
104 {
105         streamrecorder_state_e state = STREAMRECORDER_STATE_NONE;
106         MMStreamRecorderStateType srstate = (MMStreamRecorderStateType)mm_state;
107
108         switch (srstate) {
109         case MM_STREAMRECORDER_STATE_NONE:
110                 state = STREAMRECORDER_STATE_NONE;
111                 break;
112         case MM_STREAMRECORDER_STATE_CREATED:
113                 state = STREAMRECORDER_STATE_CREATED;
114                 break;
115         case MM_STREAMRECORDER_STATE_PREPARED:
116                 state = STREAMRECORDER_STATE_PREPARED;
117                 break;
118         case MM_STREAMRECORDER_STATE_RECORDING:
119                 state = STREAMRECORDER_STATE_RECORDING;
120                 break;
121         case MM_STREAMRECORDER_STATE_PAUSED:
122                 state = STREAMRECORDER_STATE_PAUSED;
123                 break;
124         default:
125                 state = STREAMRECORDER_STATE_NONE;
126                 break;
127         }
128
129         return state;
130 }
131
132 static int _streamrecorder_check_and_set_attribute(streamrecorder_h recorder, const char *attribute_name, int set_value)
133 {
134         int ret = MM_ERROR_NONE;
135
136         MMStreamRecorderStateType mmstate;
137         streamrecorder_s *handle = (streamrecorder_s *)recorder;
138
139         if (recorder == NULL) {
140                 LOGE("handle is NULL");
141                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
142         }
143
144         mm_streamrecorder_get_state(handle->mm_handle, &mmstate);
145         if (mmstate >= MM_STREAMRECORDER_STATE_RECORDING) {
146                 LOGE("invalid state %d", mmstate);
147                 return STREAMRECORDER_ERROR_INVALID_STATE;
148         }
149
150         ret = mm_streamrecorder_set_attributes(handle->mm_handle, NULL,
151                                                                                                 attribute_name, set_value,
152                                                                                                 NULL);
153
154         if (ret != MM_ERROR_NONE) {
155                 LOGE("set [%s] failed 0x%x", attribute_name, ret);
156         }
157
158
159
160         return __convert_streamrecorder_error_code(attribute_name, ret);
161 }
162
163
164 int _streamrecorder_set_videosource_buffer(streamrecorder_h recorder)
165 {
166         int ret = STREAMRECORDER_ERROR_NONE;
167         if (recorder == NULL)
168                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
169
170         streamrecorder_s *handle = (streamrecorder_s *)recorder;
171
172         mm_streamrecorder_set_attributes(handle->mm_handle, NULL,
173                                                                 MMSTR_RECORDER_MODE, MM_STREAMRECORDER_MODE_MEDIABUFFER,
174                                                                 NULL);
175
176         mm_streamrecorder_set_attributes(handle->mm_handle, NULL,
177                                                                 MMSTR_VIDEO_ENABLE, true,
178                                                                 NULL);
179
180
181         return ret;
182 }
183
184 int _streamrecorder_set_audiosource_buffer(streamrecorder_h recorder)
185 {
186         int ret = STREAMRECORDER_ERROR_NONE;
187         if (recorder == NULL)
188                 return  STREAMRECORDER_ERROR_INVALID_PARAMETER;
189
190         streamrecorder_s *handle;
191         handle = (streamrecorder_s *) recorder;
192         mm_streamrecorder_set_attributes(handle->mm_handle, NULL,
193                                                                         MMSTR_RECORDER_MODE, MM_STREAMRECORDER_MODE_MEDIABUFFER,
194                                                                         NULL);
195
196         mm_streamrecorder_set_attributes(handle->mm_handle, NULL,
197                                                                 MMSTR_AUDIO_ENABLE, true,
198                                                                 NULL);
199
200         return ret;
201 }
202
203 int _streamrecorder_get_state(streamrecorder_h recorder, streamrecorder_state_e *state)
204 {
205         int ret = STREAMRECORDER_ERROR_NONE;
206         MMStreamRecorderStateType srstate;
207
208         if (recorder == NULL) {
209                 LOGE("NULL pointer handle");
210                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
211         }
212         if (state == NULL) {
213                 LOGE("NULL pointer state");
214                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
215         }
216
217         streamrecorder_s *handle = (streamrecorder_s *)recorder;
218
219
220         ret = mm_streamrecorder_get_state(handle->mm_handle, &srstate);
221         if (ret != MM_ERROR_NONE) {
222                 return __convert_streamrecorder_error_code(__func__, ret);
223         }
224         *state = __streamrecorder_state_convert((void *)srstate);
225
226         return STREAMRECORDER_ERROR_NONE;
227 }
228
229 int _streamrecorder_destroy(streamrecorder_h recorder)
230 {
231         streamrecorder_s *handle = NULL;
232         int ret = MM_ERROR_NONE;
233         streamrecorder_state_e state = STREAMRECORDER_STATE_NONE;
234         if (recorder == NULL) {
235                 LOGE("NULL pointer handle");
236                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
237         }
238
239         handle = (streamrecorder_s *)recorder;
240
241         ret = streamrecorder_get_state(recorder, &state);
242         if (ret != MM_ERROR_NONE) {
243                 LOGE("stramrecorder_unrealize fail");
244                 return __convert_streamrecorder_error_code(__func__, ret);
245         }
246
247         if (state != STREAMRECORDER_STATE_CREATED) {
248                 LOGE("STREAMRECORDER_ERROR_INVALID_STATE (state:%d)", state);
249                 return STREAMRECORDER_ERROR_INVALID_STATE;
250         }
251
252         ret = mm_streamrecorder_destroy(handle->mm_handle);
253
254         if (ret == MM_ERROR_NONE) {
255                 free(handle->pkt);
256                 free(handle);
257         }
258
259         return __convert_streamrecorder_error_code(__func__, ret);
260 }
261
262 int _streamrecorder_prepare(streamrecorder_h recorder)
263 {
264         int ret = MM_ERROR_NONE;
265         streamrecorder_s *handle = (streamrecorder_s *)recorder;
266         streamrecorder_state_e state = STREAMRECORDER_STATE_NONE;
267
268         if (recorder == NULL) {
269                 LOGE("NULL pointer handle");
270                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
271         }
272         ret = streamrecorder_get_state(recorder, &state);
273         if (ret != MM_ERROR_NONE) {
274                 return __convert_streamrecorder_error_code(__func__, ret);
275         }
276
277         if (state != STREAMRECORDER_STATE_CREATED) {
278                 LOGE("STREAMRECORDER_ERROR_INVALID_STATE (state:%d)", state);
279                 return STREAMRECORDER_ERROR_INVALID_STATE;
280         }
281
282         ret = mm_streamrecorder_realize(handle->mm_handle);
283         if (ret != MM_ERROR_NONE) {
284                 LOGE("prepare fail");
285                 return __convert_streamrecorder_error_code(__func__, ret);
286         }
287
288         return STREAMRECORDER_ERROR_NONE;
289 }
290
291 int _streamrecorder_unprepare(streamrecorder_h recorder)
292 {
293         int ret = MM_ERROR_NONE;
294         streamrecorder_s *handle = (streamrecorder_s *)recorder;
295         streamrecorder_state_e state = STREAMRECORDER_STATE_NONE;
296
297         if (recorder == NULL) {
298                 LOGE("NULL pointer handle");
299                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
300         }
301
302         ret = streamrecorder_get_state(recorder, &state);
303         if (ret != MM_ERROR_NONE) {
304                 return __convert_streamrecorder_error_code(__func__, ret);
305         }
306
307         if (state != STREAMRECORDER_STATE_CREATED) {
308                 LOGE("STREAMRECORDER_ERROR_INVALID_STATE (state:%d)", state);
309                 return STREAMRECORDER_ERROR_INVALID_STATE;
310         }
311
312         ret = mm_streamrecorder_unrealize(handle->mm_handle);
313         if (ret != MM_ERROR_NONE) {
314                 LOGE("stramrecorder_unrealize fail");
315         }
316         return __convert_streamrecorder_error_code(__func__, ret);
317 }
318
319 int _streamrecorder_start(streamrecorder_h recorder)
320 {
321         int ret = MM_ERROR_NONE;
322         streamrecorder_s *handle = (streamrecorder_s *)recorder;
323         streamrecorder_state_e state = STREAMRECORDER_STATE_NONE;
324
325         if (recorder == NULL) {
326                 LOGE("NULL pointer handle");
327                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
328         }
329
330         ret = streamrecorder_get_state(recorder, &state);
331         if (ret != MM_ERROR_NONE) {
332                 return __convert_streamrecorder_error_code(__func__, ret);
333         }
334
335         if (!(state == STREAMRECORDER_STATE_PREPARED || state == STREAMRECORDER_STATE_PAUSED)) {
336                 LOGE("STREAMRECORDER_ERROR_INVALID_STATE (state:%d)", state);
337                 return STREAMRECORDER_ERROR_INVALID_STATE;
338         }
339
340         return __convert_streamrecorder_error_code(__func__, mm_streamrecorder_record(handle->mm_handle));
341 }
342
343 int _streamrecorder_pause(streamrecorder_h recorder)
344 {
345         int ret = MM_ERROR_NONE;
346         streamrecorder_s *handle = (streamrecorder_s *)recorder;
347         streamrecorder_state_e state = STREAMRECORDER_STATE_NONE;
348
349         if (recorder == NULL) {
350                 LOGE("NULL pointer handle");
351                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
352         }
353
354         ret = streamrecorder_get_state(recorder, &state);
355         if (ret != MM_ERROR_NONE) {
356                 return __convert_streamrecorder_error_code(__func__, ret);
357         }
358
359         if (state != STREAMRECORDER_STATE_RECORDING) {
360                 LOGE("STREAMRECORDER_ERROR_INVALID_STATE (state:%d)", state);
361                 return STREAMRECORDER_ERROR_INVALID_STATE;
362         }
363
364
365         return __convert_streamrecorder_error_code(__func__, mm_streamrecorder_pause(handle->mm_handle));
366 }
367
368 int _streamrecorder_commit(streamrecorder_h recorder)
369 {
370         int ret = MM_ERROR_NONE;
371         streamrecorder_s *handle = (streamrecorder_s *)recorder;
372         streamrecorder_state_e state = STREAMRECORDER_STATE_NONE;
373
374         if (recorder == NULL) {
375                 LOGE("NULL pointer handle");
376                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
377         }
378
379         ret = streamrecorder_get_state(recorder, &state);
380         if (ret != MM_ERROR_NONE) {
381                 return __convert_streamrecorder_error_code(__func__, ret);
382         }
383
384         if (!(state == STREAMRECORDER_STATE_RECORDING || state == STREAMRECORDER_STATE_PAUSED)) {
385                 LOGE("STREAMRECORDER_ERROR_INVALID_STATE (state:%d)", state);
386                 return STREAMRECORDER_ERROR_INVALID_STATE;
387         }
388
389         return __convert_streamrecorder_error_code(__func__, mm_streamrecorder_commit(handle->mm_handle));
390 }
391
392 int _streamrecorder_cancel(streamrecorder_h recorder)
393 {
394         streamrecorder_s *handle = (streamrecorder_s *)recorder;
395
396         if (recorder == NULL) {
397                 LOGE("NULL pointer handle");
398                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
399         }
400
401         return __convert_streamrecorder_error_code(__func__, mm_streamrecorder_cancel(handle->mm_handle));
402 }
403
404 int _streamrecorder_set_video_framerate(streamrecorder_h recorder , int framerate)
405 {
406         int ret = MM_ERROR_NONE;
407         streamrecorder_s *handle = (streamrecorder_s *)recorder;
408
409         if (recorder == NULL) {
410                 LOGE("NULL pointer handle");
411                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
412         }
413
414         ret = mm_streamrecorder_set_attributes(handle->mm_handle, NULL,
415                                                                 MMSTR_VIDEO_FRAMERATE, framerate,
416                                                                 NULL);
417
418         return __convert_streamrecorder_error_code(__func__, ret);
419
420 }
421
422 int _streamrecorder_get_video_framerate(streamrecorder_h recorder, int *framerate)
423 {
424         int ret = MM_ERROR_NONE;
425         streamrecorder_s *handle = (streamrecorder_s *)recorder;
426
427         if (recorder == NULL) {
428                 LOGE("NULL pointer handle");
429                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
430         }
431
432         if (framerate == NULL) {
433                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
434         }
435
436         ret = mm_streamrecorder_get_attributes(handle->mm_handle, NULL,
437                                                                 MMSTR_VIDEO_FRAMERATE, framerate,
438                                                                 NULL);
439
440         return __convert_streamrecorder_error_code(__func__, ret);
441 }
442
443 int _streamrecorder_set_video_source_format(streamrecorder_h recorder , int format)
444 {
445         int ret = MM_ERROR_NONE;
446         streamrecorder_s *handle = (streamrecorder_s *)recorder;
447
448         ret = mm_streamrecorder_set_attributes(handle->mm_handle, NULL,
449                                                                 MMSTR_VIDEO_SOURCE_FORMAT, format,
450                                                                 NULL);
451
452         return __convert_streamrecorder_error_code(__func__, ret);
453 }
454
455 int _streamrecorder_get_video_source_format(streamrecorder_h recorder, int *format)
456 {
457         int ret = MM_ERROR_NONE;
458         streamrecorder_s *handle = (streamrecorder_s *)recorder;
459
460         if (format == NULL) {
461                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
462         }
463
464         ret = mm_streamrecorder_get_attributes(handle->mm_handle, NULL,
465                                                                 MMSTR_VIDEO_SOURCE_FORMAT, format,
466                                                                 NULL);
467
468         return __convert_streamrecorder_error_code(__func__, ret);
469 }
470
471 int _streamrecorder_set_video_resolution(streamrecorder_h recorder, int width, int height)
472 {
473         int ret = MM_ERROR_NONE;
474         streamrecorder_s *handle = (streamrecorder_s *)recorder;
475         streamrecorder_state_e state = STREAMRECORDER_STATE_NONE;
476
477         if (handle == NULL) {
478                 LOGE("NULL pointer handle");
479                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
480         }
481         streamrecorder_get_state(recorder, &state);
482         if (state > STREAMRECORDER_STATE_CREATED) {
483                 LOGE("STREAMRECORDER_ERROR_INVALID_STATE (state:%d)", state);
484                 return STREAMRECORDER_ERROR_INVALID_STATE;
485         }
486
487         if (width == 0 || height == 0) {
488                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
489         }
490
491         ret = mm_streamrecorder_set_attributes(handle->mm_handle, NULL,
492                                                                 MMSTR_VIDEO_RESOLUTION_WIDTH, width,
493                                                                 MMSTR_VIDEO_RESOLUTION_HEIGHT, height,
494                                                                 NULL);
495
496         return __convert_streamrecorder_error_code(__func__, ret);
497 }
498
499 int _streamrecorder_get_video_resolution(streamrecorder_h recorder, int *width, int *height)
500 {
501         int ret = MM_ERROR_NONE;
502         streamrecorder_s *handle = (streamrecorder_s *)recorder;
503
504         if (!handle) {
505                 LOGE("NULL pointer handle");
506                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
507         }
508
509
510         if (!width || !height) {
511                 LOGE("NULL pointer width = [%p], height = [%p]", width, height);
512                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
513         }
514
515         ret = mm_streamrecorder_get_attributes(handle->mm_handle, NULL,
516                                                                 MMSTR_VIDEO_RESOLUTION_WIDTH , width,
517                                                                 MMSTR_VIDEO_RESOLUTION_HEIGHT , height,
518                                                                 NULL);
519         return __convert_streamrecorder_error_code(__func__, ret);
520 }
521
522 int _streamrecorder_foreach_supported_video_resolution(streamrecorder_h recorder, streamrecorder_supported_video_resolution_cb foreach_cb, void *user_data)
523 {
524         int i = 0;
525         int ret = MM_ERROR_NONE;
526         streamrecorder_s * handle = (streamrecorder_s *)recorder;
527
528         if (!handle) {
529                 LOGE("NULL pointer handle");
530                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
531         }
532
533
534         if (!foreach_cb) {
535                 LOGE("NULL pointer callback");
536                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
537         }
538
539         MMStreamRecorderAttrsInfo video_width;
540         MMStreamRecorderAttrsInfo video_height;
541         ret = mm_streamrecorder_get_attribute_info(handle->mm_handle, MMSTR_VIDEO_RESOLUTION_WIDTH, &video_width);
542         ret |= mm_streamrecorder_get_attribute_info(handle->mm_handle, MMSTR_VIDEO_RESOLUTION_HEIGHT, &video_height);
543
544         if (ret != MM_ERROR_NONE) {
545                 return __convert_streamrecorder_error_code(__func__, ret);
546          }
547         for (i = 0 ; i < video_width.int_array.count ; i++) {
548                 if (!foreach_cb(video_width.int_array.array[i], video_height.int_array.array[i], user_data)) {
549                         break;
550                 }
551         }
552
553         return STREAMRECORDER_ERROR_NONE;
554 }
555
556
557 int _streamrecorder_set_filename(streamrecorder_h recorder,  const char *filename)
558 {
559         int ret = MM_ERROR_NONE;
560         streamrecorder_s *handle = (streamrecorder_s *)recorder;
561
562         if (recorder == NULL) {
563                 LOGE("handle is NULL");
564                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
565         }
566
567         if (filename == NULL) {
568                 LOGE("filename is NULL");
569                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
570         }
571
572         MMStreamRecorderStateType mmstate = MM_STREAMRECORDER_STATE_NONE;
573         mm_streamrecorder_get_state(handle->mm_handle, &mmstate);
574         if (mmstate >= MM_STREAMRECORDER_STATE_RECORDING) {
575                 LOGE("invalid state %d", mmstate);
576                 return STREAMRECORDER_ERROR_INVALID_STATE;
577         }
578         ret = mm_streamrecorder_set_attributes(handle->mm_handle, NULL,
579                                                                 MMSTR_FILENAME, filename, strlen(filename),
580                                                                 NULL);
581         return __convert_streamrecorder_error_code(__func__, ret);
582 }
583
584 int _streamrecorder_get_filename(streamrecorder_h recorder,  char **filename)
585 {
586         int ret = MM_ERROR_NONE;
587         char *record_filename = NULL;
588         int record_filename_size;
589         streamrecorder_s *handle = (streamrecorder_s *)recorder;
590
591         if (recorder == NULL) {
592                 LOGE("handle is NULL");
593                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
594         }
595
596         if (filename == NULL) {
597                 LOGE("filename is NULL");
598                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
599         }
600
601         ret = mm_streamrecorder_get_attributes(handle->mm_handle, NULL,
602                                                                 MMSTR_FILENAME, &record_filename, &record_filename_size,
603                                                                 NULL);
604         if (ret == MM_ERROR_NONE && record_filename) {
605                 *filename = strdup(record_filename);
606         } else {
607                 LOGE("internal return (0x%08x), get filename p:%p", ret, record_filename);
608                 *filename = NULL;
609         }
610         return __convert_streamrecorder_error_code(__func__, ret);
611 }
612
613 int _streamrecorder_set_file_format(streamrecorder_h recorder, streamrecorder_file_format_e format)
614 {
615         int format_table[5] = {MM_FILE_FORMAT_3GP, /* STREAMRECORDER_FILE_FORMAT_3GP */
616                                 MM_FILE_FORMAT_MP4, /* STREAMRECORDER_FILE_FORMAT_MP4 */
617                                 MM_FILE_FORMAT_AMR, /* STREAMRECORDER_FILE_FORMAT_AMR */
618                                 MM_FILE_FORMAT_AAC, /* STREAMRECORDER_FILE_FORMAT_ADTS */
619                                 MM_FILE_FORMAT_WAV, /* STREAMRECORDER_FILE_FORMAT_WAV */
620         };
621         if (recorder == NULL) {
622                 LOGE("handle is NULL");
623                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
624         }
625
626         if (format < STREAMRECORDER_FILE_FORMAT_3GP || format > STREAMRECORDER_FILE_FORMAT_WAV) {
627                 LOGE("invalid format %d", format);
628                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
629         }
630         return _streamrecorder_check_and_set_attribute(recorder, MMSTR_FILE_FORMAT, format_table[format]);
631
632 }
633
634 int _streamrecorder_get_file_format(streamrecorder_h recorder, streamrecorder_file_format_e *format)
635 {
636         int ret = MM_ERROR_NONE;
637         streamrecorder_s *handle = (streamrecorder_s *)recorder;
638         int mm_format;
639
640         if (recorder == NULL) {
641                 LOGE("handle is NULL");
642                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
643         }
644
645         if (format == NULL) {
646                 LOGE("format is NULL");
647                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
648         }
649
650         ret = mm_streamrecorder_get_attributes(handle->mm_handle, NULL,
651                                                                 MMSTR_FILE_FORMAT, &mm_format,
652                                                                 NULL);
653         if (ret == MM_ERROR_NONE) {
654                 switch (mm_format) {
655                 case MM_FILE_FORMAT_3GP:
656                         *format = STREAMRECORDER_FILE_FORMAT_3GP;
657                         break;
658                 case MM_FILE_FORMAT_MP4:
659                         *format = STREAMRECORDER_FILE_FORMAT_MP4;
660                         break;
661                 case MM_FILE_FORMAT_AMR:
662                         *format = STREAMRECORDER_FILE_FORMAT_AMR;
663                         break;
664                 case MM_FILE_FORMAT_AAC:
665                         *format = STREAMRECORDER_FILE_FORMAT_ADTS;
666                         break;
667                 case MM_FILE_FORMAT_WAV:
668                         *format = STREAMRECORDER_FILE_FORMAT_WAV;
669                         break;
670                 default:
671                         ret = MM_ERROR_STREAMRECORDER_INTERNAL;
672                         break;
673                 }
674         }
675
676         return __convert_streamrecorder_error_code(__func__, ret);
677 }
678
679 int _streamrecorder_foreach_supported_file_format(streamrecorder_h recorder, streamrecorder_supported_file_format_cb foreach_cb, void *user_data)
680 {
681         int i = 0;
682         int ret = MM_ERROR_NONE;
683         streamrecorder_s *handle = (streamrecorder_s *)recorder;
684         int format;
685
686         if (recorder == NULL) {
687                 LOGE("NULL pointer handle");
688                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
689         }
690         if (foreach_cb == NULL) {
691                 LOGE("NULL pointer foreach_cb");
692                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
693         }
694
695         MMStreamRecorderAttrsInfo info;
696         ret = mm_streamrecorder_get_attribute_info(handle->mm_handle, MMSTR_FILE_FORMAT, &info);
697         if (ret != MM_ERROR_NONE) {
698                 LOGE("mm_streamrecorder_get_attribute_info failed 0x%x", ret);
699                 return __convert_streamrecorder_error_code(__func__, ret);
700         }
701
702         for (i = 0 ; i < info.int_array.count ; i++) {
703                 switch (info.int_array.array[i]) {
704                 case MM_FILE_FORMAT_3GP:
705                         format = STREAMRECORDER_FILE_FORMAT_3GP;
706                         break;
707                 case MM_FILE_FORMAT_MP4:
708                         format = STREAMRECORDER_FILE_FORMAT_MP4;
709                         break;
710                 case MM_FILE_FORMAT_AMR:
711                         format = STREAMRECORDER_FILE_FORMAT_AMR;
712                         break;
713                 case MM_FILE_FORMAT_AAC:
714                         format = STREAMRECORDER_FILE_FORMAT_ADTS;
715                         break;
716                 case MM_FILE_FORMAT_WAV:
717                         format = STREAMRECORDER_FILE_FORMAT_WAV;
718                         break;
719                 default:
720                         format = -1;
721                         break;
722                 }
723
724                 if (format != -1 && !foreach_cb(format, user_data)) {
725                         break;
726                 }
727         }
728
729         return STREAMRECORDER_ERROR_NONE;
730 }
731
732 int _streamrecorder_set_size_limit(streamrecorder_h recorder, int kbyte)
733 {
734         int ret = MM_ERROR_NONE;
735         streamrecorder_s *handle = (streamrecorder_s *)recorder;
736
737         if (recorder == NULL) {
738                 LOGE("NULL pointer handle");
739                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
740         }
741
742         if (kbyte < 0) {
743                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
744         }
745
746         ret = mm_streamrecorder_set_attributes(handle->mm_handle, NULL,
747                                                                 MMSTR_TARGET_MAX_SIZE, kbyte,
748                                                                 NULL);
749         return __convert_streamrecorder_error_code(__func__, ret);
750 }
751
752 int _streamrecorder_set_time_limit(streamrecorder_h recorder, int second)
753 {
754         int ret = MM_ERROR_NONE;
755         streamrecorder_s *handle = (streamrecorder_s *)recorder;
756
757         if (recorder == NULL) {
758                 LOGE("NULL pointer handle");
759                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
760         }
761
762         if (second < 0) {
763                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
764         }
765
766         ret = mm_streamrecorder_set_attributes(handle->mm_handle, NULL,
767                                                                 MMSTR_TARGET_TIME_LIMIT, second,
768                                                                 NULL);
769         return __convert_streamrecorder_error_code(__func__, ret);
770 }
771
772 int _streamrecorder_set_audio_encoder(streamrecorder_h recorder, streamrecorder_audio_codec_e codec)
773 {
774         int audio_table[3] = { MM_AUDIO_CODEC_AMR,      /* STREAMRECORDER_AUDIO_CODEC_AMR */
775                                    MM_AUDIO_CODEC_AAC,      /* STREAMRECORDER_AUDIO_CODEC_AAC */
776                                    MM_AUDIO_CODEC_WAVE      /* STREAMRECORDER_AUDIO_CODEC_PCM */
777         };
778
779         if (recorder == NULL) {
780                 LOGE("handle is NULL");
781                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
782         }
783
784         if ((codec < STREAMRECORDER_AUDIO_CODEC_AMR || codec > STREAMRECORDER_AUDIO_CODEC_PCM)) {
785                 LOGE("invalid parameter : codec %d", codec);
786                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
787         }
788
789         return _streamrecorder_check_and_set_attribute(recorder, MMSTR_AUDIO_ENCODER, audio_table[codec]);
790 }
791
792 int _streamrecorder_get_audio_encoder(streamrecorder_h recorder, streamrecorder_audio_codec_e *codec)
793 {
794         int ret = MM_ERROR_NONE;
795         int mm_codec = 0;
796         int audio_enable = 0;
797         streamrecorder_s *handle = (streamrecorder_s *)recorder;
798
799         if (recorder == NULL) {
800                 LOGE("handle is NULL");
801                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
802         }
803
804         if (codec == NULL) {
805                 LOGE("codec is NULL");
806                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
807         }
808
809         ret = mm_streamrecorder_get_attributes(handle->mm_handle, NULL,
810                                                                 MMSTR_AUDIO_ENCODER, &mm_codec,
811                                                                 MMSTR_AUDIO_ENABLE, &audio_enable,
812                                                                 NULL);
813
814         if (ret == MM_ERROR_NONE && audio_enable != 0) {
815                 switch (mm_codec) {
816                 case MM_AUDIO_CODEC_AMR:
817                         *codec = STREAMRECORDER_AUDIO_CODEC_AMR;
818                         break;
819                 case MM_AUDIO_CODEC_AAC:
820                         *codec = STREAMRECORDER_AUDIO_CODEC_AAC;
821                         break;
822                 case MM_AUDIO_CODEC_WAVE:
823                         *codec = STREAMRECORDER_AUDIO_CODEC_PCM;
824                         break;
825                 default:
826                         ret = MM_ERROR_STREAMRECORDER_INTERNAL;
827                         break;
828                 }
829         } else {
830                 ret = MM_ERROR_STREAMRECORDER_INTERNAL;
831         }
832
833         return __convert_streamrecorder_error_code(__func__, ret);
834 }
835
836 int _streamrecorder_set_video_encoder(streamrecorder_h recorder, streamrecorder_video_codec_e codec)
837 {
838         int ret = MM_ERROR_NONE;
839         int video_table[2] = {MM_VIDEO_CODEC_H263,     /* STREAMRECORDER_VIDEO_CODEC_H263 */
840                                 MM_VIDEO_CODEC_MPEG4,    /* STREAMRECORDER_VIDEO_CODEC_MPEG4 */
841                                                         };
842         streamrecorder_s *handle = (streamrecorder_s *)recorder;
843
844         if (handle == NULL) {
845                 LOGE("handle is NULL");
846                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
847         }
848
849
850         if (codec < STREAMRECORDER_VIDEO_CODEC_H263 || codec > STREAMRECORDER_VIDEO_CODEC_MPEG4) {
851                 LOGE("invalid codec %d", codec);
852                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
853         }
854
855         ret = mm_streamrecorder_set_attributes(handle->mm_handle, NULL,
856                                                                 MMSTR_VIDEO_ENCODER, video_table[codec],
857                                                                 NULL);
858         return __convert_streamrecorder_error_code(__func__, ret);
859 }
860
861 int _streamrecorder_get_video_encoder(streamrecorder_h recorder, streamrecorder_video_codec_e *codec)
862 {
863         int ret = MM_ERROR_NONE;
864         int mm_codec = 0;
865         int video_enable = 0;
866         streamrecorder_s *handle = (streamrecorder_s *)recorder;
867
868         if (handle == NULL) {
869                 LOGE("handle is NULL");
870                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
871         }
872         if (codec == NULL) {
873                 LOGE("codec is NULL");
874                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
875         }
876
877         ret = mm_streamrecorder_get_attributes(handle->mm_handle, NULL,
878                                                                 MMSTR_VIDEO_ENABLE, &video_enable,
879                                                                 MMSTR_VIDEO_ENCODER, &mm_codec,
880                                                                 NULL);
881         if (ret == MM_ERROR_NONE && video_enable != 0) {
882                 switch (mm_codec) {
883                 case MM_VIDEO_CODEC_H263:
884                         *codec = STREAMRECORDER_VIDEO_CODEC_H263;
885                         break;
886                 case MM_VIDEO_CODEC_MPEG4:
887                         *codec = STREAMRECORDER_VIDEO_CODEC_MPEG4;
888                         break;
889                 default:
890                         ret = MM_ERROR_STREAMRECORDER_INTERNAL;
891                         break;
892                 }
893         } else {
894                 ret = MM_ERROR_STREAMRECORDER_INTERNAL;
895         }
896
897         return __convert_streamrecorder_error_code(__func__, ret);
898 }
899
900
901 int _streamrecorder_set_audio_samplerate(streamrecorder_h recorder, int samplerate)
902 {
903         if (samplerate < 1) {
904                 LOGE("invalid samplerate %d", samplerate);
905                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
906         }
907
908         return _streamrecorder_check_and_set_attribute(recorder, MMSTR_AUDIO_SAMPLERATE, samplerate);
909
910 }
911
912 int _streamrecorder_set_audio_encoder_bitrate(streamrecorder_h recorder, int bitrate)
913 {
914         if (bitrate <= 0) {
915                 LOGE("invalid bitrate %d", bitrate);
916                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
917         }
918
919         return _streamrecorder_check_and_set_attribute(recorder, MMSTR_AUDIO_BITRATE, bitrate);
920
921 }
922
923 int _streamrecorder_set_video_encoder_bitrate(streamrecorder_h recorder, int bitrate)
924 {
925         int ret = MM_ERROR_NONE;
926         streamrecorder_s *handle = (streamrecorder_s *)recorder;
927
928         if (handle == NULL) {
929                 LOGE("handle is NULL");
930                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
931         }
932
933         if (bitrate < 0) {
934                 LOGE("Invalid bitrate %d", bitrate);
935                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
936         }
937
938         ret = mm_streamrecorder_set_attributes(handle->mm_handle, NULL, MMSTR_VIDEO_BITRATE, bitrate, NULL);
939
940         return __convert_streamrecorder_error_code(__func__, ret);
941 }
942
943 int _streamrecorder_get_size_limit(streamrecorder_h recorder, int *kbyte)
944 {
945         int ret = MM_ERROR_NONE;
946         streamrecorder_s *handle = (streamrecorder_s *)recorder;
947
948         if (recorder == NULL) {
949                 LOGE("handle is NULL");
950                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
951         }
952
953         if (kbyte == NULL) {
954                 LOGE("Size limit is NULL");
955                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
956         }
957
958         ret = mm_streamrecorder_get_attributes(handle->mm_handle, NULL, MMSTR_TARGET_MAX_SIZE, kbyte, NULL);
959
960         return __convert_streamrecorder_error_code(__func__, ret);
961 }
962
963 int _streamrecorder_get_time_limit(streamrecorder_h recorder, int *second)
964 {
965         int ret = MM_ERROR_NONE;
966         streamrecorder_s *handle = (streamrecorder_s *)recorder;
967
968         if (recorder == NULL) {
969                 LOGE("handle is NULL");
970                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
971         }
972
973         if (second == NULL) {
974                 LOGE("Time limit is NULL");
975                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
976         }
977
978         ret = mm_streamrecorder_get_attributes(handle->mm_handle, NULL, MMSTR_TARGET_TIME_LIMIT, second, NULL);
979
980         return __convert_streamrecorder_error_code(__func__, ret);
981 }
982
983
984 int _streamrecorder_get_audio_samplerate(streamrecorder_h recorder, int *samplerate)
985 {
986         int ret = MM_ERROR_NONE;
987         streamrecorder_s *handle = (streamrecorder_s *)recorder;
988
989         if (recorder == NULL) {
990                 LOGE("handle is NULL");
991                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
992         }
993
994         if (samplerate == NULL) {
995                 LOGE("samplerate is NULL");
996                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
997         }
998
999         ret = mm_streamrecorder_get_attributes(handle->mm_handle, NULL,
1000                                                                 MMSTR_AUDIO_SAMPLERATE, samplerate,
1001                                                                 NULL);
1002
1003         return __convert_streamrecorder_error_code(__func__, ret);
1004 }
1005
1006 int _streamrecorder_get_audio_encoder_bitrate(streamrecorder_h recorder, int *bitrate)
1007 {
1008         int ret = MM_ERROR_NONE;
1009         streamrecorder_s *handle = (streamrecorder_s *)recorder;
1010
1011         if (recorder == NULL) {
1012                 LOGE("handle is NULL");
1013                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
1014         }
1015
1016         if (bitrate == NULL) {
1017                 LOGE("bitrate is NULL");
1018                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
1019         }
1020
1021         ret = mm_streamrecorder_get_attributes(handle->mm_handle, NULL,
1022                                                                 MMSTR_AUDIO_BITRATE, bitrate,
1023                                                                 NULL);
1024
1025         return __convert_streamrecorder_error_code(__func__, ret);
1026 }
1027
1028 int _streamrecorder_get_video_encoder_bitrate(streamrecorder_h recorder, int *bitrate)
1029 {
1030         int ret = MM_ERROR_NONE;
1031         streamrecorder_s *handle = (streamrecorder_s *)recorder;
1032
1033         if (handle == NULL) {
1034                 LOGE("handle is NULL");
1035                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
1036         }
1037
1038         if (bitrate == NULL) {
1039                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
1040         }
1041         ret = mm_streamrecorder_get_attributes(handle->mm_handle, NULL,
1042                                                                 MMSTR_VIDEO_BITRATE, bitrate,
1043                                                                 NULL);
1044         return __convert_streamrecorder_error_code(__func__, ret);
1045 }
1046
1047 int _streamrecorder_foreach_supported_audio_encoder(streamrecorder_h recorder, streamrecorder_supported_audio_encoder_cb foreach_cb, void *user_data)
1048 {
1049         int i = 0;
1050         int ret = MM_ERROR_NONE;
1051         int codec;
1052         streamrecorder_s *handle = (streamrecorder_s *)recorder;
1053
1054         if (recorder == NULL) {
1055                 LOGE("handle is NULL");
1056                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
1057         }
1058         if (foreach_cb == NULL) {
1059                 LOGE("foreach_cb is NULL");
1060                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
1061         }
1062
1063         MMStreamRecorderAttrsInfo info;
1064         ret = mm_streamrecorder_get_attribute_info(handle->mm_handle, MMSTR_AUDIO_ENCODER, &info);
1065         if (ret != MM_ERROR_NONE) {
1066                 return __convert_streamrecorder_error_code(__func__, ret);
1067         }
1068
1069         for (i = 0 ; i < info.int_array.count ; i++) {
1070                 switch (info.int_array.array[i]) {
1071                 case MM_AUDIO_CODEC_AMR:
1072                         codec = STREAMRECORDER_AUDIO_CODEC_AMR;
1073                         break;
1074                 case MM_AUDIO_CODEC_AAC:
1075                         codec = STREAMRECORDER_AUDIO_CODEC_AAC;
1076                         break;
1077                 case MM_AUDIO_CODEC_WAVE:
1078                         codec = STREAMRECORDER_AUDIO_CODEC_PCM;
1079                         break;
1080                 default:
1081                         codec = -1;
1082                         break;
1083                 }
1084                 if (codec != -1 && !foreach_cb(codec, user_data)) {
1085                         break;
1086                 }
1087         }
1088
1089         return STREAMRECORDER_ERROR_NONE;
1090 }
1091
1092
1093 int _streamrecorder_foreach_supported_video_encoder(streamrecorder_h recorder, streamrecorder_supported_video_encoder_cb foreach_cb, void *user_data)
1094 {
1095         int i = 0;
1096         int ret = MM_ERROR_NONE;
1097         int codec;
1098         streamrecorder_s *handle = (streamrecorder_s *)recorder;
1099
1100         if (handle == NULL) {
1101                 LOGE("handle is NULL");
1102                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
1103         }
1104         if (foreach_cb == NULL) {
1105                 LOGE("foreach_cb is NULL");
1106                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
1107         }
1108
1109         MMStreamRecorderAttrsInfo info;
1110         ret = mm_streamrecorder_get_attribute_info(handle->mm_handle, MMSTR_VIDEO_ENCODER, &info);
1111         if (ret != MM_ERROR_NONE) {
1112                 return __convert_streamrecorder_error_code(__func__, ret);
1113         }
1114
1115         for (i = 0 ; i < info.int_array.count ; i++) {
1116                 switch (info.int_array.array[i]) {
1117                 case MM_VIDEO_CODEC_H263:
1118                         codec = STREAMRECORDER_VIDEO_CODEC_H263;
1119                         break;
1120                 case MM_VIDEO_CODEC_MPEG4:
1121                         codec = STREAMRECORDER_VIDEO_CODEC_MPEG4;
1122                         break;
1123                 default:
1124                         codec = -1;
1125                         break;
1126                 }
1127
1128                 if (codec != -1 && !foreach_cb(codec, user_data)) {
1129                         break;
1130                 }
1131         }
1132
1133         return STREAMRECORDER_ERROR_NONE;
1134 }
1135
1136
1137
1138 int _streamrecorder_set_audio_channel(streamrecorder_h recorder, int channel_count)
1139 {
1140         if (channel_count < 1) {
1141                 LOGE("invalid channel %d", channel_count);
1142                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
1143         }
1144
1145         return _streamrecorder_check_and_set_attribute(recorder, MMSTR_AUDIO_CHANNEL, channel_count);
1146 }
1147
1148 int _streamrecorder_get_audio_channel(streamrecorder_h recorder, int *channel_count)
1149 {
1150         int ret = MM_ERROR_NONE;
1151         streamrecorder_s *handle = (streamrecorder_s *)recorder;
1152
1153         if (recorder == NULL) {
1154                 LOGE("handle is NULL");
1155                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
1156         }
1157         if (channel_count == NULL) {
1158                 LOGE("channel_count is NULL");
1159                 return STREAMRECORDER_ERROR_INVALID_PARAMETER;
1160         }
1161
1162         ret = mm_streamrecorder_get_attributes(handle->mm_handle, NULL,
1163                                                                 MMSTR_AUDIO_CHANNEL, channel_count,
1164                                                                 NULL);
1165
1166         return  __convert_streamrecorder_error_code(__func__, ret);
1167 }
1168