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