Git 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
77         __ta__("[KPI] create media player service",
78         result = _mmplayer_create_player((MMHandleType)new_player);
79         )
80
81         if(result != MM_ERROR_NONE)
82                 goto ERROR;
83
84         *player = (MMHandleType)new_player;
85
86         return result;
87
88 ERROR:
89
90         if ( new_player )
91         {
92                 _mmplayer_destroy( (MMHandleType)new_player );
93                 MMPLAYER_FREEIF( new_player );
94         }
95
96         *player = (MMHandleType)0;
97         return MM_ERROR_PLAYER_NO_FREE_SPACE; // are you sure?
98 }
99
100 int  mm_player_destroy(MMHandleType player)
101 {
102         int result = MM_ERROR_NONE;
103
104         debug_log("\n");
105
106         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
107
108         MMPLAYER_CMD_LOCK( player );
109
110         __ta__("[KPI] destroy media player service",
111         result = _mmplayer_destroy(player);
112         )
113
114         MMPLAYER_CMD_UNLOCK( player );
115
116         /* free player */
117         g_free( (void*)player );
118
119         MMTA_ACUM_ITEM_SHOW_RESULT_TO(MMTA_SHOW_FILE);
120
121         MMTA_RELEASE();
122
123         return result;
124 }
125
126
127 int mm_player_realize(MMHandleType player)
128 {
129         int result = MM_ERROR_NONE;
130
131         debug_log("\n");
132
133         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
134
135         MMPLAYER_CMD_LOCK( player );
136
137         __ta__("[KPI] initialize media player service",
138         result = _mmplayer_realize(player);
139         )
140
141         MMPLAYER_CMD_UNLOCK( player );
142
143         return result;
144 }
145
146
147 int mm_player_unrealize(MMHandleType player)
148 {
149         int result = MM_ERROR_NONE;
150
151         debug_log("\n");
152
153         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
154
155         MMPLAYER_CMD_LOCK( player );
156
157         __ta__("[KPI] cleanup media player service",
158         result = _mmplayer_unrealize(player);
159         )
160
161         MMPLAYER_CMD_UNLOCK( player );
162
163         return result;
164 }
165
166
167 int mm_player_set_message_callback(MMHandleType player, MMMessageCallback callback, void *user_param)
168 {
169         int result = MM_ERROR_NONE;
170
171         debug_log("\n");
172
173         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
174
175         MMPLAYER_CMD_LOCK( player );
176
177         result = _mmplayer_set_message_callback(player, callback, user_param);
178
179         MMPLAYER_CMD_UNLOCK( player );
180
181         return result;
182 }
183
184
185 int mm_player_set_audio_stream_callback(MMHandleType player, mm_player_audio_stream_callback callback, void *user_param)
186 {
187         int result = MM_ERROR_NONE;
188
189         debug_log("\n");
190
191         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
192
193         MMPLAYER_CMD_LOCK( player );
194
195         result = _mmplayer_set_audiostream_cb(player, callback, user_param);
196
197         MMPLAYER_CMD_UNLOCK( player );
198
199         return result;
200 }
201
202
203 int mm_player_set_audio_buffer_callback(MMHandleType player, mm_player_audio_stream_callback 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         MMPLAYER_CMD_LOCK( player );
212
213         result = _mmplayer_set_audiobuffer_cb(player, callback, user_param);
214
215         MMPLAYER_CMD_UNLOCK( player );
216
217         return result;
218 }
219
220 int mm_player_set_video_stream_callback(MMHandleType player, mm_player_video_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_videostream_cb(player, callback, user_param);
231
232         MMPLAYER_CMD_UNLOCK( player );
233
234         return result;
235 }
236
237 int mm_player_do_video_capture(MMHandleType player)
238 {
239         int result = MM_ERROR_NONE;
240
241         debug_log("\n");
242
243         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
244
245         MMPLAYER_CMD_LOCK( player );
246
247         result = _mmplayer_do_video_capture(player);
248
249         MMPLAYER_CMD_UNLOCK( player );
250
251         return result;
252 }
253
254 int mm_player_set_buffer_need_data_callback(MMHandleType player, mm_player_buffer_need_data_callback callback, void * user_param)
255 {
256         int result = MM_ERROR_NONE;
257
258         debug_log("\n");
259
260         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
261
262         MMPLAYER_CMD_LOCK( player );
263
264     result = _mmplayer_set_buffer_need_data_cb(player, callback, user_param);
265
266         MMPLAYER_CMD_UNLOCK( player );
267
268         return result;
269 }
270
271
272 int mm_player_set_buffer_enough_data_callback(MMHandleType player, mm_player_buffer_enough_data_callback callback, void * user_param)
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_set_buffer_enough_data_cb(player, callback, user_param);
283
284         MMPLAYER_CMD_UNLOCK( player );
285
286         return result;
287 }
288
289
290 int mm_player_set_buffer_seek_data_callback(MMHandleType player, mm_player_buffer_seek_data_callback callback, void * user_param)
291 {
292         int result = MM_ERROR_NONE;
293
294         debug_log("\n");
295
296         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
297
298         MMPLAYER_CMD_LOCK( player );
299
300     result = _mmplayer_set_buffer_seek_data_cb(player, callback, user_param);
301
302         MMPLAYER_CMD_UNLOCK( player );
303
304         return result;
305 }
306
307
308 int mm_player_apply_sound_filter(MMHandleType player, MMAudioFilterInfo *info)
309 {
310         int result = MM_ERROR_NONE;
311
312         debug_log("\n");
313
314         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
315         return_val_if_fail(info, MM_ERROR_INVALID_ARGUMENT);
316
317         MMPLAYER_CMD_LOCK( player );
318
319         result = _mmplayer_apply_sound_filter(player, info);
320
321         MMPLAYER_CMD_UNLOCK( player );
322
323         return result;
324 }
325
326
327 int mm_player_set_volume(MMHandleType player, MMPlayerVolumeType *volume)
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         return_val_if_fail(volume, MM_ERROR_INVALID_ARGUMENT);
335
336         MMPLAYER_CMD_LOCK( player );
337
338         result = _mmplayer_set_volume(player, *volume);
339
340         MMPLAYER_CMD_UNLOCK( player );
341
342         return result;
343 }
344
345
346 int mm_player_get_volume(MMHandleType player, MMPlayerVolumeType *volume)
347 {
348         int result = MM_ERROR_NONE;
349
350         debug_log("\n");
351
352         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
353         return_val_if_fail(volume, MM_ERROR_INVALID_ARGUMENT);
354
355         MMPLAYER_CMD_LOCK( player );
356
357         result = _mmplayer_get_volume(player, volume);
358
359         MMPLAYER_CMD_UNLOCK( player );
360
361         return result;
362 }
363
364
365 int mm_player_set_mute(MMHandleType player, int mute)
366 {
367         int result = MM_ERROR_NONE;
368
369         debug_log("\n");
370
371         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
372
373         MMPLAYER_CMD_LOCK( player );
374
375         result = _mmplayer_set_mute(player, mute);
376
377         MMPLAYER_CMD_UNLOCK( player );
378
379         return result;
380 }
381
382
383 int mm_player_get_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         return_val_if_fail(mute, MM_ERROR_INVALID_ARGUMENT);
391
392         MMPLAYER_CMD_LOCK( player );
393
394         result = _mmplayer_get_mute(player, mute);
395
396         MMPLAYER_CMD_UNLOCK( player );
397
398         return result;
399 }
400
401
402 int mm_player_get_state(MMHandleType player, MMPlayerStateType *state)
403 {
404         int result = MM_ERROR_NONE;
405
406         debug_log("\n");
407
408         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
409         return_val_if_fail(state, MM_ERROR_COMMON_INVALID_ARGUMENT);
410
411         *state = MM_PLAYER_STATE_NULL;
412
413         MMPLAYER_CMD_LOCK( player );
414
415         result = _mmplayer_get_state(player, (int*)state); /* FIXIT : why int* ? */
416
417         MMPLAYER_CMD_UNLOCK( player );
418
419         return result;
420 }
421
422
423 int mm_player_push_buffer(MMHandleType player, unsigned char *buf, int size)
424 {
425         int result = MM_ERROR_NONE;
426
427         debug_log("\n");
428
429         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
430
431         //MMPLAYER_CMD_LOCK( player );
432
433         //MMTA_ACUM_ITEM_BEGIN("[KPI] start media player service", false);
434         result = _mmplayer_push_buffer(player, buf, size);
435
436         //MMPLAYER_CMD_UNLOCK( player );
437
438         return result;
439 }
440
441
442 int mm_player_start(MMHandleType player)
443 {
444         int result = MM_ERROR_NONE;
445
446         debug_log("\n");
447
448         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
449
450         MMPLAYER_CMD_LOCK( player );
451
452         MMTA_ACUM_ITEM_BEGIN("[KPI] start media player service", false);
453         result = _mmplayer_start(player);
454
455         MMPLAYER_CMD_UNLOCK( player );
456
457         return result;
458 }
459
460
461 int  mm_player_stop(MMHandleType player)
462 {
463         int result = MM_ERROR_NONE;
464
465         debug_log("\n");
466
467         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
468
469         MMPLAYER_CMD_LOCK( player );
470
471         __ta__("[KPI] stop media player service",
472         result = _mmplayer_stop(player);
473         )
474
475         MMPLAYER_CMD_UNLOCK( player );
476
477         return result;
478 }
479
480
481 int mm_player_pause(MMHandleType player)
482 {
483         int result = MM_ERROR_NONE;
484
485         debug_log("\n");
486
487         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
488
489         MMPLAYER_CMD_LOCK( player );
490
491         __ta__("[KPI] pause media player service",
492         result = _mmplayer_pause(player);
493         )
494
495         MMPLAYER_CMD_UNLOCK( player );
496
497         return result;
498 }
499
500
501 int mm_player_resume(MMHandleType player)
502 {
503         int result = MM_ERROR_NONE;
504
505         debug_log("\n");
506
507         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
508
509         MMPLAYER_CMD_LOCK( player );
510
511         __ta__("[KPI] resume media player service",
512         result = _mmplayer_resume(player);
513         )
514
515         MMPLAYER_CMD_UNLOCK( player );
516
517         return result;
518 }
519
520
521 int mm_player_activate_section_repeat(MMHandleType player, int start_pos, int end_pos)
522 {
523         int result = MM_ERROR_NONE;
524
525         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
526
527         MMPLAYER_CMD_LOCK( player );
528
529         result = _mmplayer_activate_section_repeat(player, start_pos, end_pos);
530
531         MMPLAYER_CMD_UNLOCK( player );
532
533         return result;
534 }
535
536
537 int mm_player_deactivate_section_repeat(MMHandleType player)
538 {
539         int result = MM_ERROR_NONE;
540
541         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
542
543         MMPLAYER_CMD_LOCK( player );
544
545         result = _mmplayer_deactivate_section_repeat(player);
546
547         MMPLAYER_CMD_UNLOCK( player );
548
549         return result;
550 }
551
552
553 int mm_player_set_play_speed(MMHandleType player, float rate)
554 {
555         int result = MM_ERROR_NONE;
556
557         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
558
559         MMPLAYER_CMD_LOCK( player );
560
561         result = _mmplayer_set_playspeed(player, rate);
562
563         MMPLAYER_CMD_UNLOCK( player );
564
565         return result;
566 }
567
568
569 int mm_player_set_position(MMHandleType player, MMPlayerPosFormatType format, int pos)
570 {
571         int result = MM_ERROR_NONE;
572
573         debug_log("\n");
574
575         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
576
577         if (format >= MM_PLAYER_POS_FORMAT_NUM)
578         {
579                 debug_error("wrong format\n");
580                 return MM_ERROR_COMMON_INVALID_ARGUMENT;
581         }
582
583         MMPLAYER_CMD_LOCK( player );
584
585         result = _mmplayer_set_position(player, format, pos);
586
587         MMPLAYER_CMD_UNLOCK( player );
588
589         return result;
590 }
591
592
593 int mm_player_get_position(MMHandleType player, MMPlayerPosFormatType format, int *pos)
594 {
595         int result = MM_ERROR_NONE;
596
597         debug_log("\n");
598
599         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
600         return_val_if_fail(pos, MM_ERROR_COMMON_INVALID_ARGUMENT);
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_get_position(player, (int)format, (unsigned long*)pos);
611
612         MMPLAYER_CMD_UNLOCK( player );
613
614         return result;
615 }
616
617 int mm_player_get_buffer_position(MMHandleType player, MMPlayerPosFormatType format, int  *start_pos, int  *stop_pos)
618 {
619         int result = MM_ERROR_NONE;
620
621         debug_log("\n");
622
623         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
624         return_val_if_fail(start_pos && stop_pos, MM_ERROR_COMMON_INVALID_ARGUMENT);
625
626         MMPLAYER_CMD_LOCK( player );
627
628         result = _mmplayer_get_buffer_position(player, (int)format, (unsigned long*)start_pos, (unsigned long*)stop_pos );
629
630         MMPLAYER_CMD_UNLOCK( player );
631
632         return result;
633 }
634
635 int mm_player_set_subtitle_silent(MMHandleType player, int silent)
636 {
637         int result = MM_ERROR_NONE;
638
639         debug_log("\n");
640
641         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
642
643         MMPLAYER_CMD_LOCK( player );
644
645         result = _mmplayer_set_subtitle_silent(player, silent);
646
647         MMPLAYER_CMD_UNLOCK( player );
648
649         return result;
650 }
651
652
653 int mm_player_get_subtitle_silent(MMHandleType player, int* silent)
654 {
655         int result = MM_ERROR_NONE;
656
657         debug_log("\n");
658
659         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
660
661         MMPLAYER_CMD_LOCK( player );
662
663         result = _mmplayer_get_subtitle_silent(player, silent);
664
665         MMPLAYER_CMD_UNLOCK( player );
666
667         return result;
668 }
669
670
671 int mm_player_set_attribute(MMHandleType player,  char **err_attr_name, const char *first_attribute_name, ...)
672 {
673         int result = MM_ERROR_NONE;
674         va_list var_args;
675
676         debug_log("\n");
677
678         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
679         return_val_if_fail(first_attribute_name, MM_ERROR_COMMON_INVALID_ARGUMENT);
680
681         MMPLAYER_CMD_LOCK( player );
682
683         va_start (var_args, first_attribute_name);
684         result = _mmplayer_set_attribute(player, err_attr_name, first_attribute_name, var_args);
685         va_end (var_args);
686
687         MMPLAYER_CMD_UNLOCK( player );
688
689         return result;
690 }
691
692
693 int mm_player_get_attribute(MMHandleType player,  char **err_attr_name, const char *first_attribute_name, ...)
694 {
695         int result = MM_ERROR_NONE;
696         va_list var_args;
697
698         debug_log("\n");
699
700         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
701         return_val_if_fail(first_attribute_name, MM_ERROR_COMMON_INVALID_ARGUMENT);
702
703         MMPLAYER_CMD_LOCK( player );
704
705         va_start (var_args, first_attribute_name);
706         result = _mmplayer_get_attribute(player, err_attr_name, first_attribute_name, var_args);
707         va_end (var_args);
708
709         MMPLAYER_CMD_UNLOCK( player );
710
711         return result;
712 }
713
714
715 int mm_player_get_attribute_info(MMHandleType player,  const char *attribute_name, MMPlayerAttrsInfo *info)
716 {
717         int result = MM_ERROR_NONE;
718         debug_log("\n");
719
720         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
721         return_val_if_fail(attribute_name, MM_ERROR_COMMON_INVALID_ARGUMENT);
722         return_val_if_fail(info, MM_ERROR_COMMON_INVALID_ARGUMENT);
723
724         MMPLAYER_CMD_LOCK( player );
725
726         result = _mmplayer_get_attributes_info((MMHandleType)player, attribute_name, info);
727
728         MMPLAYER_CMD_UNLOCK( player );
729
730         return result;
731 }