modify progressive download api
[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_downloader_message_cb(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 /* NOTE : Not supported */
434 int mm_player_change_videosink(MMHandleType player, MMDisplaySurfaceType display_surface_type, void *display_overlay)
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         return MM_ERROR_NOT_SUPPORT_API;
443 }
444
445 int mm_player_push_buffer(MMHandleType player, unsigned char *buf, int size)
446 {
447         int result = MM_ERROR_NONE;
448
449         debug_log("\n");
450
451         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
452
453         //MMPLAYER_CMD_LOCK( player );
454
455         //MMTA_ACUM_ITEM_BEGIN("[KPI] start media player service", false);
456         result = _mmplayer_push_buffer(player, buf, size);
457
458         //MMPLAYER_CMD_UNLOCK( player );
459
460         return result;
461 }
462
463
464 int mm_player_start(MMHandleType player)
465 {
466         int result = MM_ERROR_NONE;
467
468         debug_log("\n");
469
470         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
471
472         MMPLAYER_CMD_LOCK( player );
473
474         MMTA_ACUM_ITEM_BEGIN("[KPI] start media player service", false);
475         result = _mmplayer_start(player);
476
477         MMPLAYER_CMD_UNLOCK( player );
478
479         return result;
480 }
481
482
483 int  mm_player_stop(MMHandleType player)
484 {
485         int result = MM_ERROR_NONE;
486
487         debug_log("\n");
488
489         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
490
491         MMPLAYER_CMD_LOCK( player );
492
493         __ta__("[KPI] stop media player service",
494         result = _mmplayer_stop(player);
495         )
496
497         MMPLAYER_CMD_UNLOCK( player );
498
499         return result;
500 }
501
502
503 int mm_player_pause(MMHandleType player)
504 {
505         int result = MM_ERROR_NONE;
506
507         debug_log("\n");
508
509         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
510
511         MMPLAYER_CMD_LOCK( player );
512
513         __ta__("[KPI] pause media player service",
514         result = _mmplayer_pause(player);
515         )
516
517         MMPLAYER_CMD_UNLOCK( player );
518
519         return result;
520 }
521
522
523 int mm_player_resume(MMHandleType player)
524 {
525         int result = MM_ERROR_NONE;
526
527         debug_log("\n");
528
529         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
530
531         MMPLAYER_CMD_LOCK( player );
532
533         __ta__("[KPI] resume media player service",
534         result = _mmplayer_resume(player);
535         )
536
537         MMPLAYER_CMD_UNLOCK( player );
538
539         return result;
540 }
541
542
543 int mm_player_activate_section_repeat(MMHandleType player, int start_pos, int end_pos)
544 {
545         int result = MM_ERROR_NONE;
546
547         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
548
549         MMPLAYER_CMD_LOCK( player );
550
551         result = _mmplayer_activate_section_repeat(player, start_pos, end_pos);
552
553         MMPLAYER_CMD_UNLOCK( player );
554
555         return result;
556 }
557
558
559 int mm_player_deactivate_section_repeat(MMHandleType player)
560 {
561         int result = MM_ERROR_NONE;
562
563         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
564
565         MMPLAYER_CMD_LOCK( player );
566
567         result = _mmplayer_deactivate_section_repeat(player);
568
569         MMPLAYER_CMD_UNLOCK( player );
570
571         return result;
572 }
573
574
575 int mm_player_set_play_speed(MMHandleType player, float rate)
576 {
577         int result = MM_ERROR_NONE;
578
579         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
580
581         MMPLAYER_CMD_LOCK( player );
582
583         result = _mmplayer_set_playspeed(player, rate);
584
585         MMPLAYER_CMD_UNLOCK( player );
586
587         return result;
588 }
589
590
591 int mm_player_set_position(MMHandleType player, MMPlayerPosFormatType format, int pos)
592 {
593         int result = MM_ERROR_NONE;
594
595         debug_log("\n");
596
597         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
598
599         if (format >= MM_PLAYER_POS_FORMAT_NUM)
600         {
601                 debug_error("wrong format\n");
602                 return MM_ERROR_COMMON_INVALID_ARGUMENT;
603         }
604
605         MMPLAYER_CMD_LOCK( player );
606
607         result = _mmplayer_set_position(player, format, pos);
608
609         MMPLAYER_CMD_UNLOCK( player );
610
611         return result;
612 }
613
614
615 int mm_player_get_position(MMHandleType player, MMPlayerPosFormatType format, int *pos)
616 {
617         int result = MM_ERROR_NONE;
618
619         debug_log("\n");
620
621         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
622         return_val_if_fail(pos, MM_ERROR_COMMON_INVALID_ARGUMENT);
623
624         if (format >= MM_PLAYER_POS_FORMAT_NUM)
625         {
626                 debug_error("wrong format\n");
627                 return MM_ERROR_COMMON_INVALID_ARGUMENT;
628         }
629
630         MMPLAYER_CMD_LOCK( player );
631
632         result = _mmplayer_get_position(player, (int)format, (unsigned long*)pos);
633
634         MMPLAYER_CMD_UNLOCK( player );
635
636         return result;
637 }
638
639 int mm_player_get_buffer_position(MMHandleType player, MMPlayerPosFormatType format, int  *start_pos, int  *stop_pos)
640 {
641         int result = MM_ERROR_NONE;
642
643         debug_log("\n");
644
645         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
646         return_val_if_fail(start_pos && stop_pos, MM_ERROR_COMMON_INVALID_ARGUMENT);
647
648         MMPLAYER_CMD_LOCK( player );
649
650         result = _mmplayer_get_buffer_position(player, (int)format, (unsigned long*)start_pos, (unsigned long*)stop_pos );
651
652         MMPLAYER_CMD_UNLOCK( player );
653
654         return result;
655 }
656
657 int mm_player_adjust_subtitle_position(MMHandleType player, MMPlayerPosFormatType format, int pos)
658 {
659         int result = MM_ERROR_NONE;
660
661         debug_log("\n");
662
663         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
664
665         if (format >= MM_PLAYER_POS_FORMAT_NUM)
666         {
667                 debug_error("wrong format\n");
668                 return MM_ERROR_COMMON_INVALID_ARGUMENT;
669         }
670
671         MMPLAYER_CMD_LOCK( player );
672
673         result = _mmplayer_adjust_subtitle_postion(player, format, pos);
674
675         MMPLAYER_CMD_UNLOCK( player );
676
677         return result;
678 }
679
680
681 int mm_player_set_subtitle_silent(MMHandleType player, int silent)
682 {
683         int result = MM_ERROR_NONE;
684
685         debug_log("\n");
686
687         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
688
689         MMPLAYER_CMD_LOCK( player );
690
691         result = _mmplayer_set_subtitle_silent(player, silent);
692
693         MMPLAYER_CMD_UNLOCK( player );
694
695         return result;
696 }
697
698
699 int mm_player_get_subtitle_silent(MMHandleType player, int* silent)
700 {
701         int result = MM_ERROR_NONE;
702
703         debug_log("\n");
704
705         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
706
707         MMPLAYER_CMD_LOCK( player );
708
709         result = _mmplayer_get_subtitle_silent(player, silent);
710
711         MMPLAYER_CMD_UNLOCK( player );
712
713         return result;
714 }
715
716
717 int mm_player_set_attribute(MMHandleType player,  char **err_attr_name, const char *first_attribute_name, ...)
718 {
719         int result = MM_ERROR_NONE;
720         va_list var_args;
721
722         debug_log("\n");
723
724         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
725         return_val_if_fail(first_attribute_name, MM_ERROR_COMMON_INVALID_ARGUMENT);
726
727         va_start (var_args, first_attribute_name);
728         result = _mmplayer_set_attribute(player, err_attr_name, first_attribute_name, var_args);
729         va_end (var_args);
730
731         return result;
732 }
733
734
735 int mm_player_get_attribute(MMHandleType player,  char **err_attr_name, const char *first_attribute_name, ...)
736 {
737         int result = MM_ERROR_NONE;
738         va_list var_args;
739
740         debug_log("\n");
741
742         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
743         return_val_if_fail(first_attribute_name, MM_ERROR_COMMON_INVALID_ARGUMENT);
744
745         va_start (var_args, first_attribute_name);
746         result = _mmplayer_get_attribute(player, err_attr_name, first_attribute_name, var_args);
747         va_end (var_args);
748
749         return result;
750 }
751
752
753 int mm_player_get_attribute_info(MMHandleType player,  const char *attribute_name, MMPlayerAttrsInfo *info)
754 {
755         int result = MM_ERROR_NONE;
756         debug_log("\n");
757
758         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
759         return_val_if_fail(attribute_name, MM_ERROR_COMMON_INVALID_ARGUMENT);
760         return_val_if_fail(info, MM_ERROR_COMMON_INVALID_ARGUMENT);
761
762         result = _mmplayer_get_attributes_info((MMHandleType)player, attribute_name, info);
763
764         return result;
765 }
766
767 int mm_player_get_pd_status(MMHandleType player, guint64 *current_pos, guint64 *total_size)
768 {
769         int result = MM_ERROR_NONE;
770
771         debug_log("\n");
772
773         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
774         return_val_if_fail(current_pos, MM_ERROR_COMMON_INVALID_ARGUMENT);
775         return_val_if_fail(total_size, MM_ERROR_COMMON_INVALID_ARGUMENT);
776
777         result = _mmplayer_get_pd_downloader_status(player, current_pos, total_size);
778
779         return result;
780 }
781
782 int mm_player_get_track_count(MMHandleType player,  MMPlayerTrackType track_type, int *count)
783 {
784         int result = MM_ERROR_NONE;
785
786         debug_log("\n");
787
788         return_val_if_fail(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
789         return_val_if_fail(count, MM_ERROR_COMMON_INVALID_ARGUMENT);
790
791         result = _mmplayer_get_track_count(player, track_type, count);
792
793         return result;
794
795 }