update audio effect (change file name, API naming, ini parsing. add new preset type...
[platform/core/multimedia/libmm-player.git] / src / mm_player.c
1 /*
2  * libmm-player
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: JongHyuk Choi <jhchoi.choi@samsung.com>, YeJin Cho <cho.yejin@samsung.com>, YoungHwan An <younghwan_.an@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <gst/gst.h>
23 #include <string.h>
24
25 #include <mm_types.h>
26 #include <mm_message.h>
27
28 #include "mm_player.h"
29 #include "mm_player_priv.h"
30 #include "mm_player_attrs.h"
31 #include "mm_player_utils.h"
32 #include "mm_player_ini.h"
33 #include "mm_debug.h"
34 #include "mm_player_capture.h"
35
36 int mm_player_create(MMHandleType *player)
37 {
38         int result = MM_ERROR_NONE;
39         mm_player_t* new_player = NULL;
40
41         debug_log("\n");
42
43         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
44
45         if (!g_thread_supported ())
46         g_thread_init (NULL);
47
48         MMTA_INIT();
49
50         __ta__("mm_player_ini_load",
51         result = mm_player_ini_load();
52         )
53         if(result != MM_ERROR_NONE)
54                 return result;
55
56         __ta__("mm_player_audio_effect_ini_load",
57         result = mm_player_audio_effect_ini_load();
58         )
59         if(result != MM_ERROR_NONE)
60                 return result;
61
62         /* alloc player structure */
63         new_player = g_malloc(sizeof(mm_player_t));
64         if ( ! new_player )
65         {
66                 debug_critical("Cannot allocate memory for player\n");
67                 goto ERROR;
68         }
69         memset(new_player, 0, sizeof(mm_player_t));
70
71         /* create player lock */
72         new_player->cmd_lock = g_mutex_new();
73
74         if ( ! new_player->cmd_lock )
75         {
76                 debug_critical("failed to create player lock\n");
77                 goto ERROR;
78         }
79
80         /* create msg callback lock */
81         new_player->msg_cb_lock = g_mutex_new();
82
83         if ( ! new_player->msg_cb_lock )
84         {
85                 debug_critical("failed to create msg cb lock\n");
86                 goto ERROR;
87         }
88         __ta__("[KPI] create media player service",
89         result = _mmplayer_create_player((MMHandleType)new_player);
90         )
91
92         if(result != MM_ERROR_NONE)
93                 goto ERROR;
94
95         *player = (MMHandleType)new_player;
96
97         return result;
98
99 ERROR:
100
101         if ( new_player )
102         {
103                 if (new_player->cmd_lock)
104                 {
105                         g_mutex_free(new_player->cmd_lock);
106                         new_player->cmd_lock = NULL;
107                 }
108
109                 _mmplayer_destroy( (MMHandleType)new_player );
110                 MMPLAYER_FREEIF( new_player );
111         }
112
113         *player = (MMHandleType)0;
114         return MM_ERROR_PLAYER_NO_FREE_SPACE; // are you sure?
115 }
116
117 int  mm_player_destroy(MMHandleType player)
118 {
119         int result = MM_ERROR_NONE;
120
121         debug_log("\n");
122
123         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
124
125         MMPLAYER_CMD_LOCK( player );
126
127         __ta__("[KPI] destroy media player service",
128         result = _mmplayer_destroy(player);
129         )
130
131         MMPLAYER_CMD_UNLOCK( player );
132
133         if (((mm_player_t*)player)->cmd_lock)
134         {
135                 g_mutex_free(((mm_player_t*)player)->cmd_lock);
136                 ((mm_player_t*)player)->cmd_lock = NULL;
137         }
138
139         /* free player */
140         g_free( (void*)player );
141
142         MMTA_ACUM_ITEM_SHOW_RESULT_TO(MMTA_SHOW_FILE);
143
144         MMTA_RELEASE();
145
146         return result;
147 }
148
149
150 int mm_player_realize(MMHandleType player)
151 {
152         int result = MM_ERROR_NONE;
153
154         debug_log("\n");
155
156         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
157
158         MMPLAYER_CMD_LOCK( player );
159
160         __ta__("[KPI] initialize media player service",
161         result = _mmplayer_realize(player);
162         )
163
164         MMPLAYER_CMD_UNLOCK( player );
165
166         return result;
167 }
168
169
170 int mm_player_unrealize(MMHandleType player)
171 {
172         int result = MM_ERROR_NONE;
173
174         debug_log("\n");
175
176         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
177
178         MMPLAYER_CMD_LOCK( player );
179
180         __ta__("[KPI] cleanup media player service",
181         result = _mmplayer_unrealize(player);
182         )
183
184         MMPLAYER_CMD_UNLOCK( player );
185
186         return result;
187 }
188
189
190 int mm_player_set_message_callback(MMHandleType player, MMMessageCallback callback, void *user_param)
191 {
192         int result = MM_ERROR_NONE;
193
194         debug_log("\n");
195
196         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
197
198         MMPLAYER_CMD_LOCK( player );
199
200         result = _mmplayer_set_message_callback(player, callback, user_param);
201
202         MMPLAYER_CMD_UNLOCK( player );
203
204         return result;
205 }
206
207 int mm_player_set_pd_message_callback(MMHandleType player, MMMessageCallback callback, void *user_param)
208 {
209         int result = MM_ERROR_NONE;
210
211         debug_log("\n");
212
213         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
214
215         result = _mm_player_set_pd_downloader_message_cb(player, callback, user_param);
216
217         return result;
218 }
219
220 int mm_player_set_audio_stream_callback(MMHandleType player, mm_player_audio_stream_callback callback, void *user_param)
221 {
222         int result = MM_ERROR_NONE;
223
224         debug_log("\n");
225
226         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
227
228         MMPLAYER_CMD_LOCK( player );
229
230         result = _mmplayer_set_audiostream_cb(player, callback, user_param);
231
232         MMPLAYER_CMD_UNLOCK( player );
233
234         return result;
235 }
236
237
238 int mm_player_set_audio_buffer_callback(MMHandleType player, mm_player_audio_stream_callback callback, void *user_param)
239 {
240         int result = MM_ERROR_NONE;
241
242         debug_log("\n");
243
244         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
245
246         MMPLAYER_CMD_LOCK( player );
247
248         result = _mmplayer_set_audiobuffer_cb(player, callback, user_param);
249
250         MMPLAYER_CMD_UNLOCK( player );
251
252         return result;
253 }
254
255 int mm_player_set_video_stream_callback(MMHandleType player, mm_player_video_stream_callback callback, void *user_param)
256 {
257         int result = MM_ERROR_NONE;
258
259         debug_log("\n");
260
261         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
262
263         MMPLAYER_CMD_LOCK( player );
264
265     result = _mmplayer_set_videostream_cb(player, callback, user_param);
266
267         MMPLAYER_CMD_UNLOCK( player );
268
269         return result;
270 }
271
272 int mm_player_do_video_capture(MMHandleType player)
273 {
274         int result = MM_ERROR_NONE;
275
276         debug_log("\n");
277
278         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
279
280         MMPLAYER_CMD_LOCK( player );
281
282         result = _mmplayer_do_video_capture(player);
283
284         MMPLAYER_CMD_UNLOCK( player );
285
286         return result;
287 }
288
289 int mm_player_set_buffer_need_data_callback(MMHandleType player, mm_player_buffer_need_data_callback callback, void * user_param)
290 {
291         int result = MM_ERROR_NONE;
292
293         debug_log("\n");
294
295         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
296
297         MMPLAYER_CMD_LOCK( player );
298
299     result = _mmplayer_set_buffer_need_data_cb(player, callback, user_param);
300
301         MMPLAYER_CMD_UNLOCK( player );
302
303         return result;
304 }
305
306
307 int mm_player_set_buffer_enough_data_callback(MMHandleType player, mm_player_buffer_enough_data_callback callback, void * user_param)
308 {
309         int result = MM_ERROR_NONE;
310
311         debug_log("\n");
312
313         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
314
315         MMPLAYER_CMD_LOCK( player );
316
317     result = _mmplayer_set_buffer_enough_data_cb(player, callback, user_param);
318
319         MMPLAYER_CMD_UNLOCK( player );
320
321         return result;
322 }
323
324
325 int mm_player_set_buffer_seek_data_callback(MMHandleType player, mm_player_buffer_seek_data_callback callback, void * user_param)
326 {
327         int result = MM_ERROR_NONE;
328
329         debug_log("\n");
330
331         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
332
333         MMPLAYER_CMD_LOCK( player );
334
335     result = _mmplayer_set_buffer_seek_data_cb(player, callback, user_param);
336
337         MMPLAYER_CMD_UNLOCK( player );
338
339         return result;
340 }
341
342
343 int mm_player_set_volume(MMHandleType player, MMPlayerVolumeType *volume)
344 {
345         int result = MM_ERROR_NONE;
346
347         debug_log("\n");
348
349         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
350         return_val_if_fail(volume, MM_ERROR_INVALID_ARGUMENT);
351
352         MMPLAYER_CMD_LOCK( player );
353
354         result = _mmplayer_set_volume(player, *volume);
355
356         MMPLAYER_CMD_UNLOCK( player );
357
358         return result;
359 }
360
361
362 int mm_player_get_volume(MMHandleType player, MMPlayerVolumeType *volume)
363 {
364         int result = MM_ERROR_NONE;
365
366         debug_log("\n");
367
368         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
369         return_val_if_fail(volume, MM_ERROR_INVALID_ARGUMENT);
370
371         MMPLAYER_CMD_LOCK( player );
372
373         result = _mmplayer_get_volume(player, volume);
374
375         MMPLAYER_CMD_UNLOCK( player );
376
377         return result;
378 }
379
380
381 int mm_player_set_mute(MMHandleType player, int mute)
382 {
383         int result = MM_ERROR_NONE;
384
385         debug_log("\n");
386
387         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
388
389         MMPLAYER_CMD_LOCK( player );
390
391         result = _mmplayer_set_mute(player, mute);
392
393         MMPLAYER_CMD_UNLOCK( player );
394
395         return result;
396 }
397
398
399 int mm_player_get_mute(MMHandleType player, int *mute)
400 {
401         int result = MM_ERROR_NONE;
402
403         debug_log("\n");
404
405         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
406         return_val_if_fail(mute, MM_ERROR_INVALID_ARGUMENT);
407
408         MMPLAYER_CMD_LOCK( player );
409
410         result = _mmplayer_get_mute(player, mute);
411
412         MMPLAYER_CMD_UNLOCK( player );
413
414         return result;
415 }
416
417
418 int mm_player_get_state(MMHandleType player, MMPlayerStateType *state)
419 {
420         int result = MM_ERROR_NONE;
421
422         debug_log("\n");
423
424         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
425         return_val_if_fail(state, MM_ERROR_COMMON_INVALID_ARGUMENT);
426
427         *state = MM_PLAYER_STATE_NULL;
428
429         MMPLAYER_CMD_LOCK( player );
430
431         result = _mmplayer_get_state(player, (int*)state); /* FIXIT : why int* ? */
432
433         MMPLAYER_CMD_UNLOCK( player );
434
435         return result;
436 }
437
438 /* NOTE : Not supported */
439 int mm_player_change_videosink(MMHandleType player, MMDisplaySurfaceType display_surface_type, void *display_overlay)
440 {
441         int result = MM_ERROR_NONE;
442
443         debug_log("\n");
444
445         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
446
447         return MM_ERROR_NOT_SUPPORT_API;
448 }
449
450 int mm_player_push_buffer(MMHandleType player, unsigned char *buf, int size)
451 {
452         int result = MM_ERROR_NONE;
453
454         debug_log("\n");
455
456         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
457
458         //MMPLAYER_CMD_LOCK( player );
459
460         //MMTA_ACUM_ITEM_BEGIN("[KPI] start media player service", false);
461         result = _mmplayer_push_buffer(player, buf, size);
462
463         //MMPLAYER_CMD_UNLOCK( player );
464
465         return result;
466 }
467
468
469 int mm_player_start(MMHandleType player)
470 {
471         int result = MM_ERROR_NONE;
472
473         debug_log("\n");
474
475         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
476
477         MMPLAYER_CMD_LOCK( player );
478
479         MMTA_ACUM_ITEM_BEGIN("[KPI] start media player service", false);
480         result = _mmplayer_start(player);
481
482         MMPLAYER_CMD_UNLOCK( player );
483
484         return result;
485 }
486
487
488 int  mm_player_stop(MMHandleType player)
489 {
490         int result = MM_ERROR_NONE;
491
492         debug_log("\n");
493
494         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
495
496         MMPLAYER_CMD_LOCK( player );
497
498         __ta__("[KPI] stop media player service",
499         result = _mmplayer_stop(player);
500         )
501
502         MMPLAYER_CMD_UNLOCK( player );
503
504         return result;
505 }
506
507
508 int mm_player_pause(MMHandleType player)
509 {
510         int result = MM_ERROR_NONE;
511
512         debug_log("\n");
513
514         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
515
516         MMPLAYER_CMD_LOCK( player );
517
518         __ta__("[KPI] pause media player service",
519         result = _mmplayer_pause(player);
520         )
521
522         MMPLAYER_CMD_UNLOCK( player );
523
524         return result;
525 }
526
527
528 int mm_player_resume(MMHandleType player)
529 {
530         int result = MM_ERROR_NONE;
531
532         debug_log("\n");
533
534         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
535
536         MMPLAYER_CMD_LOCK( player );
537
538         __ta__("[KPI] resume media player service",
539         result = _mmplayer_resume(player);
540         )
541
542         MMPLAYER_CMD_UNLOCK( player );
543
544         return result;
545 }
546
547
548 int mm_player_activate_section_repeat(MMHandleType player, int start_pos, int end_pos)
549 {
550         int result = MM_ERROR_NONE;
551
552         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
553
554         MMPLAYER_CMD_LOCK( player );
555
556         result = _mmplayer_activate_section_repeat(player, start_pos, end_pos);
557
558         MMPLAYER_CMD_UNLOCK( player );
559
560         return result;
561 }
562
563
564 int mm_player_deactivate_section_repeat(MMHandleType player)
565 {
566         int result = MM_ERROR_NONE;
567
568         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
569
570         MMPLAYER_CMD_LOCK( player );
571
572         result = _mmplayer_deactivate_section_repeat(player);
573
574         MMPLAYER_CMD_UNLOCK( player );
575
576         return result;
577 }
578
579
580 int mm_player_set_play_speed(MMHandleType player, float rate)
581 {
582         int result = MM_ERROR_NONE;
583
584         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
585
586         MMPLAYER_CMD_LOCK( player );
587
588         result = _mmplayer_set_playspeed(player, rate);
589
590         MMPLAYER_CMD_UNLOCK( player );
591
592         return result;
593 }
594
595
596 int mm_player_set_position(MMHandleType player, MMPlayerPosFormatType format, int pos)
597 {
598         int result = MM_ERROR_NONE;
599
600         debug_log("\n");
601
602         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
603
604         if (format >= MM_PLAYER_POS_FORMAT_NUM)
605         {
606                 debug_error("wrong format\n");
607                 return MM_ERROR_COMMON_INVALID_ARGUMENT;
608         }
609
610         MMPLAYER_CMD_LOCK( player );
611
612         result = _mmplayer_set_position(player, format, pos);
613
614         MMPLAYER_CMD_UNLOCK( player );
615
616         return result;
617 }
618
619
620 int mm_player_get_position(MMHandleType player, MMPlayerPosFormatType format, int *pos)
621 {
622         int result = MM_ERROR_NONE;
623
624         debug_log("\n");
625
626         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
627         return_val_if_fail(pos, MM_ERROR_COMMON_INVALID_ARGUMENT);
628
629         if (format >= MM_PLAYER_POS_FORMAT_NUM)
630         {
631                 debug_error("wrong format\n");
632                 return MM_ERROR_COMMON_INVALID_ARGUMENT;
633         }
634
635         MMPLAYER_CMD_LOCK( player );
636
637         result = _mmplayer_get_position(player, (int)format, (unsigned long*)pos);
638
639         MMPLAYER_CMD_UNLOCK( player );
640
641         return result;
642 }
643
644 int mm_player_get_buffer_position(MMHandleType player, MMPlayerPosFormatType format, int  *start_pos, int  *stop_pos)
645 {
646         int result = MM_ERROR_NONE;
647
648         debug_log("\n");
649
650         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
651         return_val_if_fail(start_pos && stop_pos, MM_ERROR_COMMON_INVALID_ARGUMENT);
652
653         MMPLAYER_CMD_LOCK( player );
654
655         result = _mmplayer_get_buffer_position(player, (int)format, (unsigned long*)start_pos, (unsigned long*)stop_pos );
656
657         MMPLAYER_CMD_UNLOCK( player );
658
659         return result;
660 }
661
662 int mm_player_adjust_subtitle_position(MMHandleType player, MMPlayerPosFormatType format, int pos)
663 {
664         int result = MM_ERROR_NONE;
665
666         debug_log("\n");
667
668         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
669
670         if (format >= MM_PLAYER_POS_FORMAT_NUM)
671         {
672                 debug_error("wrong format\n");
673                 return MM_ERROR_COMMON_INVALID_ARGUMENT;
674         }
675
676         MMPLAYER_CMD_LOCK( player );
677
678         result = _mmplayer_adjust_subtitle_postion(player, format, pos);
679
680         MMPLAYER_CMD_UNLOCK( player );
681
682         return result;
683 }
684
685
686 int mm_player_set_subtitle_silent(MMHandleType player, int silent)
687 {
688         int result = MM_ERROR_NONE;
689
690         debug_log("\n");
691
692         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
693
694         MMPLAYER_CMD_LOCK( player );
695
696         result = _mmplayer_set_subtitle_silent(player, silent);
697
698         MMPLAYER_CMD_UNLOCK( player );
699
700         return result;
701 }
702
703
704 int mm_player_get_subtitle_silent(MMHandleType player, int* silent)
705 {
706         int result = MM_ERROR_NONE;
707
708         debug_log("\n");
709
710         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
711
712         MMPLAYER_CMD_LOCK( player );
713
714         result = _mmplayer_get_subtitle_silent(player, silent);
715
716         MMPLAYER_CMD_UNLOCK( player );
717
718         return result;
719 }
720
721
722 int mm_player_set_attribute(MMHandleType player,  char **err_attr_name, const char *first_attribute_name, ...)
723 {
724         int result = MM_ERROR_NONE;
725         va_list var_args;
726
727         debug_log("\n");
728
729         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
730         return_val_if_fail(first_attribute_name, MM_ERROR_COMMON_INVALID_ARGUMENT);
731
732         va_start (var_args, first_attribute_name);
733         result = _mmplayer_set_attribute(player, err_attr_name, first_attribute_name, var_args);
734         va_end (var_args);
735
736         return result;
737 }
738
739
740 int mm_player_get_attribute(MMHandleType player,  char **err_attr_name, const char *first_attribute_name, ...)
741 {
742         int result = MM_ERROR_NONE;
743         va_list var_args;
744
745         debug_log("\n");
746
747         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
748         return_val_if_fail(first_attribute_name, MM_ERROR_COMMON_INVALID_ARGUMENT);
749
750         va_start (var_args, first_attribute_name);
751         result = _mmplayer_get_attribute(player, err_attr_name, first_attribute_name, var_args);
752         va_end (var_args);
753
754         return result;
755 }
756
757
758 int mm_player_get_attribute_info(MMHandleType player,  const char *attribute_name, MMPlayerAttrsInfo *info)
759 {
760         int result = MM_ERROR_NONE;
761         debug_log("\n");
762
763         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
764         return_val_if_fail(attribute_name, MM_ERROR_COMMON_INVALID_ARGUMENT);
765         return_val_if_fail(info, MM_ERROR_COMMON_INVALID_ARGUMENT);
766
767         result = _mmplayer_get_attributes_info((MMHandleType)player, attribute_name, info);
768
769         return result;
770 }
771
772 int mm_player_get_pd_status(MMHandleType player, guint64 *current_pos, guint64 *total_size)
773 {
774         int result = MM_ERROR_NONE;
775
776         debug_log("\n");
777
778         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
779         return_val_if_fail(current_pos, MM_ERROR_COMMON_INVALID_ARGUMENT);
780         return_val_if_fail(total_size, MM_ERROR_COMMON_INVALID_ARGUMENT);
781
782         result = _mmplayer_get_pd_downloader_status(player, current_pos, total_size);
783
784         return result;
785 }
786
787 int mm_player_get_track_count(MMHandleType player,  MMPlayerTrackType track_type, int *count)
788 {
789         int result = MM_ERROR_NONE;
790
791         debug_log("\n");
792
793         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
794         return_val_if_fail(count, MM_ERROR_COMMON_INVALID_ARGUMENT);
795
796         result = _mmplayer_get_track_count(player, track_type, count);
797
798         return result;
799
800 }