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