add NOTICE file and change file permission
[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         memset( (mm_player_t*)player, 0x00, sizeof(mm_player_t) );
140
141         /* free player */
142         g_free( (void*)player );
143
144         MMTA_ACUM_ITEM_SHOW_RESULT_TO(MMTA_SHOW_FILE);
145
146         MMTA_RELEASE();
147
148         return result;
149 }
150
151
152 int mm_player_realize(MMHandleType player)
153 {
154         int result = MM_ERROR_NONE;
155
156         debug_log("\n");
157
158         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
159
160         MMPLAYER_CMD_LOCK( player );
161
162         __ta__("[KPI] initialize media player service",
163         result = _mmplayer_realize(player);
164         )
165
166         MMPLAYER_CMD_UNLOCK( player );
167
168         return result;
169 }
170
171
172 int mm_player_unrealize(MMHandleType player)
173 {
174         int result = MM_ERROR_NONE;
175
176         debug_log("\n");
177
178         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
179
180         MMPLAYER_CMD_LOCK( player );
181
182         __ta__("[KPI] cleanup media player service",
183         result = _mmplayer_unrealize(player);
184         )
185
186         MMPLAYER_CMD_UNLOCK( player );
187
188         return result;
189 }
190
191
192 int mm_player_set_message_callback(MMHandleType player, MMMessageCallback callback, void *user_param)
193 {
194         int result = MM_ERROR_NONE;
195
196         debug_log("\n");
197
198         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
199
200         MMPLAYER_CMD_LOCK( player );
201
202         result = _mmplayer_set_message_callback(player, callback, user_param);
203
204         MMPLAYER_CMD_UNLOCK( player );
205
206         return result;
207 }
208
209 int mm_player_set_pd_message_callback(MMHandleType player, MMMessageCallback callback, void *user_param)
210 {
211         int result = MM_ERROR_NONE;
212
213         debug_log("\n");
214
215         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
216
217         result = _mm_player_set_pd_downloader_message_cb(player, callback, user_param);
218
219         return result;
220 }
221
222 int mm_player_set_audio_stream_callback(MMHandleType player, mm_player_audio_stream_callback callback, void *user_param)
223 {
224         int result = MM_ERROR_NONE;
225
226         debug_log("\n");
227
228         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
229
230         MMPLAYER_CMD_LOCK( player );
231
232         result = _mmplayer_set_audiostream_cb(player, callback, user_param);
233
234         MMPLAYER_CMD_UNLOCK( player );
235
236         return result;
237 }
238
239
240 int mm_player_set_audio_buffer_callback(MMHandleType player, mm_player_audio_stream_callback callback, void *user_param)
241 {
242         int result = MM_ERROR_NONE;
243
244         debug_log("\n");
245
246         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
247
248         MMPLAYER_CMD_LOCK( player );
249
250         result = _mmplayer_set_audiobuffer_cb(player, callback, user_param);
251
252         MMPLAYER_CMD_UNLOCK( player );
253
254         return result;
255 }
256
257 int mm_player_set_video_stream_callback(MMHandleType player, mm_player_video_stream_callback callback, void *user_param)
258 {
259         int result = MM_ERROR_NONE;
260
261         debug_log("\n");
262
263         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
264
265         MMPLAYER_CMD_LOCK( player );
266
267     result = _mmplayer_set_videostream_cb(player, callback, user_param);
268
269         MMPLAYER_CMD_UNLOCK( player );
270
271         return result;
272 }
273
274 int mm_player_do_video_capture(MMHandleType player)
275 {
276         int result = MM_ERROR_NONE;
277
278         debug_log("\n");
279
280         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
281
282         MMPLAYER_CMD_LOCK( player );
283
284         result = _mmplayer_do_video_capture(player);
285
286         MMPLAYER_CMD_UNLOCK( player );
287
288         return result;
289 }
290
291 int mm_player_set_buffer_need_data_callback(MMHandleType player, mm_player_buffer_need_data_callback callback, void * user_param)
292 {
293         int result = MM_ERROR_NONE;
294
295         debug_log("\n");
296
297         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
298
299         MMPLAYER_CMD_LOCK( player );
300
301     result = _mmplayer_set_buffer_need_data_cb(player, callback, user_param);
302
303         MMPLAYER_CMD_UNLOCK( player );
304
305         return result;
306 }
307
308
309 int mm_player_set_buffer_enough_data_callback(MMHandleType player, mm_player_buffer_enough_data_callback callback, void * user_param)
310 {
311         int result = MM_ERROR_NONE;
312
313         debug_log("\n");
314
315         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
316
317         MMPLAYER_CMD_LOCK( player );
318
319     result = _mmplayer_set_buffer_enough_data_cb(player, callback, user_param);
320
321         MMPLAYER_CMD_UNLOCK( player );
322
323         return result;
324 }
325
326
327 int mm_player_set_buffer_seek_data_callback(MMHandleType player, mm_player_buffer_seek_data_callback callback, void * user_param)
328 {
329         int result = MM_ERROR_NONE;
330
331         debug_log("\n");
332
333         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
334
335         MMPLAYER_CMD_LOCK( player );
336
337     result = _mmplayer_set_buffer_seek_data_cb(player, callback, user_param);
338
339         MMPLAYER_CMD_UNLOCK( player );
340
341         return result;
342 }
343
344
345 int mm_player_set_volume(MMHandleType player, MMPlayerVolumeType *volume)
346 {
347         int result = MM_ERROR_NONE;
348
349         debug_log("\n");
350
351         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
352         return_val_if_fail(volume, MM_ERROR_INVALID_ARGUMENT);
353
354         MMPLAYER_CMD_LOCK( player );
355
356         result = _mmplayer_set_volume(player, *volume);
357
358         MMPLAYER_CMD_UNLOCK( player );
359
360         return result;
361 }
362
363
364 int mm_player_get_volume(MMHandleType player, MMPlayerVolumeType *volume)
365 {
366         int result = MM_ERROR_NONE;
367
368         debug_log("\n");
369
370         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
371         return_val_if_fail(volume, MM_ERROR_INVALID_ARGUMENT);
372
373         MMPLAYER_CMD_LOCK( player );
374
375         result = _mmplayer_get_volume(player, volume);
376
377         MMPLAYER_CMD_UNLOCK( player );
378
379         return result;
380 }
381
382
383 int mm_player_set_mute(MMHandleType player, int mute)
384 {
385         int result = MM_ERROR_NONE;
386
387         debug_log("\n");
388
389         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
390
391         MMPLAYER_CMD_LOCK( player );
392
393         result = _mmplayer_set_mute(player, mute);
394
395         MMPLAYER_CMD_UNLOCK( player );
396
397         return result;
398 }
399
400
401 int mm_player_get_mute(MMHandleType player, int *mute)
402 {
403         int result = MM_ERROR_NONE;
404
405         debug_log("\n");
406
407         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
408         return_val_if_fail(mute, MM_ERROR_INVALID_ARGUMENT);
409
410         MMPLAYER_CMD_LOCK( player );
411
412         result = _mmplayer_get_mute(player, mute);
413
414         MMPLAYER_CMD_UNLOCK( player );
415
416         return result;
417 }
418
419
420 int mm_player_get_state(MMHandleType player, MMPlayerStateType *state)
421 {
422         int result = MM_ERROR_NONE;
423
424         debug_log("\n");
425
426         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
427         return_val_if_fail(state, MM_ERROR_COMMON_INVALID_ARGUMENT);
428
429         *state = MM_PLAYER_STATE_NULL;
430
431         result = _mmplayer_get_state(player, (int*)state);
432
433         return result;
434 }
435
436 /* NOTE : Not supported */
437 int mm_player_change_videosink(MMHandleType player, MMDisplaySurfaceType display_surface_type, void *display_overlay)
438 {
439         int result = MM_ERROR_NONE;
440
441         debug_log("\n");
442
443         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
444
445         return MM_ERROR_NOT_SUPPORT_API;
446 }
447
448 int mm_player_push_buffer(MMHandleType player, unsigned char *buf, int size)
449 {
450         int result = MM_ERROR_NONE;
451
452         debug_log("\n");
453
454         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
455
456         //MMPLAYER_CMD_LOCK( player );
457
458         //MMTA_ACUM_ITEM_BEGIN("[KPI] start media player service", false);
459         result = _mmplayer_push_buffer(player, buf, size);
460
461         //MMPLAYER_CMD_UNLOCK( player );
462
463         return result;
464 }
465
466
467 int mm_player_start(MMHandleType player)
468 {
469         int result = MM_ERROR_NONE;
470
471         debug_log("\n");
472
473         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
474
475         MMPLAYER_CMD_LOCK( player );
476
477         MMTA_ACUM_ITEM_BEGIN("[KPI] start media player service", false);
478         result = _mmplayer_start(player);
479
480         MMPLAYER_CMD_UNLOCK( player );
481
482         return result;
483 }
484
485
486 int  mm_player_stop(MMHandleType player)
487 {
488         int result = MM_ERROR_NONE;
489
490         debug_log("\n");
491
492         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
493
494         MMPLAYER_CMD_LOCK( player );
495
496         __ta__("[KPI] stop media player service",
497         result = _mmplayer_stop(player);
498         )
499
500         MMPLAYER_CMD_UNLOCK( player );
501
502         return result;
503 }
504
505
506 int mm_player_pause(MMHandleType player)
507 {
508         int result = MM_ERROR_NONE;
509
510         debug_log("\n");
511
512         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
513
514         MMPLAYER_CMD_LOCK( player );
515
516         __ta__("[KPI] pause media player service",
517         result = _mmplayer_pause(player);
518         )
519
520         MMPLAYER_CMD_UNLOCK( player );
521
522         return result;
523 }
524
525
526 int mm_player_resume(MMHandleType player)
527 {
528         int result = MM_ERROR_NONE;
529
530         debug_log("\n");
531
532         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
533
534         MMPLAYER_CMD_LOCK( player );
535
536         __ta__("[KPI] resume media player service",
537         result = _mmplayer_resume(player);
538         )
539
540         MMPLAYER_CMD_UNLOCK( player );
541
542         return result;
543 }
544
545
546 int mm_player_activate_section_repeat(MMHandleType player, int start_pos, int end_pos)
547 {
548         int result = MM_ERROR_NONE;
549
550         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
551
552         MMPLAYER_CMD_LOCK( player );
553
554         result = _mmplayer_activate_section_repeat(player, start_pos, end_pos);
555
556         MMPLAYER_CMD_UNLOCK( player );
557
558         return result;
559 }
560
561
562 int mm_player_deactivate_section_repeat(MMHandleType player)
563 {
564         int result = MM_ERROR_NONE;
565
566         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
567
568         MMPLAYER_CMD_LOCK( player );
569
570         result = _mmplayer_deactivate_section_repeat(player);
571
572         MMPLAYER_CMD_UNLOCK( player );
573
574         return result;
575 }
576
577
578 int mm_player_set_play_speed(MMHandleType player, float rate)
579 {
580         int result = MM_ERROR_NONE;
581
582         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
583
584         MMPLAYER_CMD_LOCK( player );
585
586         result = _mmplayer_set_playspeed(player, rate);
587
588         MMPLAYER_CMD_UNLOCK( player );
589
590         return result;
591 }
592
593
594 int mm_player_set_position(MMHandleType player, MMPlayerPosFormatType format, int pos)
595 {
596         int result = MM_ERROR_NONE;
597
598         debug_log("\n");
599
600         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
601
602         if (format >= MM_PLAYER_POS_FORMAT_NUM)
603         {
604                 debug_error("wrong format\n");
605                 return MM_ERROR_COMMON_INVALID_ARGUMENT;
606         }
607
608         MMPLAYER_CMD_LOCK( player );
609
610         result = _mmplayer_set_position(player, format, pos);
611
612         MMPLAYER_CMD_UNLOCK( player );
613
614         return result;
615 }
616
617
618 int mm_player_get_position(MMHandleType player, MMPlayerPosFormatType format, int *pos)
619 {
620         int result = MM_ERROR_NONE;
621
622         debug_log("\n");
623
624         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
625         return_val_if_fail(pos, MM_ERROR_COMMON_INVALID_ARGUMENT);
626
627         if (format >= MM_PLAYER_POS_FORMAT_NUM)
628         {
629                 debug_error("wrong format\n");
630                 return MM_ERROR_COMMON_INVALID_ARGUMENT;
631         }
632
633         MMPLAYER_CMD_LOCK( player );
634
635         result = _mmplayer_get_position(player, (int)format, (unsigned long*)pos);
636
637         MMPLAYER_CMD_UNLOCK( player );
638
639         return result;
640 }
641
642 int mm_player_get_buffer_position(MMHandleType player, MMPlayerPosFormatType format, int  *start_pos, int  *stop_pos)
643 {
644         int result = MM_ERROR_NONE;
645
646         debug_log("\n");
647
648         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
649         return_val_if_fail(start_pos && stop_pos, MM_ERROR_COMMON_INVALID_ARGUMENT);
650
651         MMPLAYER_CMD_LOCK( player );
652
653         result = _mmplayer_get_buffer_position(player, (int)format, (unsigned long*)start_pos, (unsigned long*)stop_pos );
654
655         MMPLAYER_CMD_UNLOCK( player );
656
657         return result;
658 }
659
660 int mm_player_adjust_subtitle_position(MMHandleType player, MMPlayerPosFormatType format, int pos)
661 {
662         int result = MM_ERROR_NONE;
663
664         debug_log("\n");
665
666         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
667
668         if (format >= MM_PLAYER_POS_FORMAT_NUM)
669         {
670                 debug_error("wrong format\n");
671                 return MM_ERROR_COMMON_INVALID_ARGUMENT;
672         }
673
674         MMPLAYER_CMD_LOCK( player );
675
676         result = _mmplayer_adjust_subtitle_postion(player, format, pos);
677
678         MMPLAYER_CMD_UNLOCK( player );
679
680         return result;
681 }
682
683
684 int mm_player_set_subtitle_silent(MMHandleType player, int silent)
685 {
686         int result = MM_ERROR_NONE;
687
688         debug_log("\n");
689
690         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
691
692         MMPLAYER_CMD_LOCK( player );
693
694         result = _mmplayer_set_subtitle_silent(player, silent);
695
696         MMPLAYER_CMD_UNLOCK( player );
697
698         return result;
699 }
700
701
702 int mm_player_get_subtitle_silent(MMHandleType player, int* silent)
703 {
704         int result = MM_ERROR_NONE;
705
706         debug_log("\n");
707
708         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
709
710         MMPLAYER_CMD_LOCK( player );
711
712         result = _mmplayer_get_subtitle_silent(player, silent);
713
714         MMPLAYER_CMD_UNLOCK( player );
715
716         return result;
717 }
718
719
720 int mm_player_set_attribute(MMHandleType player,  char **err_attr_name, const char *first_attribute_name, ...)
721 {
722         int result = MM_ERROR_NONE;
723         va_list var_args;
724
725         debug_log("\n");
726
727         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
728         return_val_if_fail(first_attribute_name, MM_ERROR_COMMON_INVALID_ARGUMENT);
729
730         va_start (var_args, first_attribute_name);
731         result = _mmplayer_set_attribute(player, err_attr_name, first_attribute_name, var_args);
732         va_end (var_args);
733
734         return result;
735 }
736
737
738 int mm_player_get_attribute(MMHandleType player,  char **err_attr_name, const char *first_attribute_name, ...)
739 {
740         int result = MM_ERROR_NONE;
741         va_list var_args;
742
743         debug_log("\n");
744
745         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
746         return_val_if_fail(first_attribute_name, MM_ERROR_COMMON_INVALID_ARGUMENT);
747
748         va_start (var_args, first_attribute_name);
749         result = _mmplayer_get_attribute(player, err_attr_name, first_attribute_name, var_args);
750         va_end (var_args);
751
752         return result;
753 }
754
755
756 int mm_player_get_attribute_info(MMHandleType player,  const char *attribute_name, MMPlayerAttrsInfo *info)
757 {
758         int result = MM_ERROR_NONE;
759         debug_log("\n");
760
761         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
762         return_val_if_fail(attribute_name, MM_ERROR_COMMON_INVALID_ARGUMENT);
763         return_val_if_fail(info, MM_ERROR_COMMON_INVALID_ARGUMENT);
764
765         result = _mmplayer_get_attributes_info((MMHandleType)player, attribute_name, info);
766
767         return result;
768 }
769
770 int mm_player_get_pd_status(MMHandleType player, guint64 *current_pos, guint64 *total_size)
771 {
772         int result = MM_ERROR_NONE;
773
774         debug_log("\n");
775
776         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
777         return_val_if_fail(current_pos, MM_ERROR_COMMON_INVALID_ARGUMENT);
778         return_val_if_fail(total_size, MM_ERROR_COMMON_INVALID_ARGUMENT);
779
780         result = _mmplayer_get_pd_downloader_status(player, current_pos, total_size);
781
782         return result;
783 }
784
785 int mm_player_get_track_count(MMHandleType player,  MMPlayerTrackType track_type, int *count)
786 {
787         int result = MM_ERROR_NONE;
788
789         debug_log("\n");
790
791         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
792         return_val_if_fail(count, MM_ERROR_COMMON_INVALID_ARGUMENT);
793
794         result = _mmplayer_get_track_count(player, track_type, count);
795
796         return result;
797
798 }