Fix webaudio buffer missed issue
[platform/core/api/player.git] / src / player_internal.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <dlog.h>
21
22 #include <mm.h>
23 #include <mm_player.h>
24 #include <mm_player_internal.h>
25 #include <mm_types.h>
26 #include <player.h>
27 #include <player_internal.h>
28 #include <player_private.h>
29
30
31 /*
32 * Internal Macros
33 */
34 #define PLAYER_SET_CALLBACK(event_type, handle, callback, user_data) \
35 do \
36 { \
37         PLAYER_INSTANCE_CHECK(handle); \
38         PLAYER_NULL_ARG_CHECK(callback); \
39         handle->user_cb[event_type] = callback; \
40         handle->user_data[event_type] = user_data; \
41         LOGI("[%s] Event type : %d ",__FUNCTION__, event_type); \
42 }while(0) \
43
44 bool  __audio_stream_callback_ex(MMPlayerAudioStreamDataType *stream, void *user_data)
45 {
46         player_s * handle = (player_s*)user_data;
47
48         if (!__player_state_validate(handle, PLAYER_STATE_READY))
49         {
50                 LOGE("[%s] PLAYER_ERROR_INVALID_STATE(0x%08x) : current state - %d" ,__FUNCTION__,PLAYER_ERROR_INVALID_STATE, handle->state);
51                 return TRUE;
52         }
53
54         if( handle->user_cb[_PLAYER_EVENT_TYPE_AUDIO_FRAME] )
55         {
56                 ((player_audio_pcm_extraction_cb)handle->user_cb[_PLAYER_EVENT_TYPE_AUDIO_FRAME])((player_audio_raw_data_s *)stream, handle->user_data[_PLAYER_EVENT_TYPE_AUDIO_FRAME]);
57         }
58         return TRUE;
59 }
60
61 int player_set_pcm_extraction_mode(player_h player, bool sync, player_audio_pcm_extraction_cb callback, void *user_data)
62 {
63         PLAYER_INSTANCE_CHECK(player);
64         PLAYER_NULL_ARG_CHECK(callback);
65         player_s * handle = (player_s *) player;
66         int ret = MM_ERROR_NONE;
67
68         PLAYER_STATE_CHECK(handle, PLAYER_STATE_IDLE);
69
70         ret = mm_player_set_attribute(handle->mm_handle, NULL, "pcm_extraction",TRUE, "pcm_extraction_start_msec", 0, "pcm_extraction_end_msec", 0, NULL);
71         if(ret != MM_ERROR_NONE)
72                 return __player_convert_error_code(ret,(char*)__FUNCTION__);
73
74         ret = mm_player_set_audio_stream_callback_ex(handle->mm_handle, sync,  __audio_stream_callback_ex, (void*)handle);
75         if(ret != MM_ERROR_NONE)
76                 return __player_convert_error_code(ret,(char*)__FUNCTION__);
77
78         PLAYER_SET_CALLBACK(_PLAYER_EVENT_TYPE_AUDIO_FRAME, handle, callback, user_data);
79         return PLAYER_ERROR_NONE;
80 }
81
82 int player_set_pcm_spec(player_h player, const char *format, int samplerate, int channel)
83 {
84         PLAYER_INSTANCE_CHECK(player);
85
86         player_s * handle = (player_s *) player;
87         int ret = MM_ERROR_NONE;
88
89         LOGE("[%s] player_set_pcm_spec %s %d %d", __FUNCTION__, format, samplerate, channel);
90         ret = mm_player_set_attribute(handle->mm_handle, NULL, "pcm_audioformat", format , strlen(format), NULL);
91         if(ret != MM_ERROR_NONE)
92                 return __player_convert_error_code(ret,(char*)__FUNCTION__);
93
94         ret = mm_player_set_pcm_spec(handle->mm_handle, samplerate, channel);
95         if(ret != MM_ERROR_NONE)
96                 return __player_convert_error_code(ret,(char*)__FUNCTION__);
97
98         return PLAYER_ERROR_NONE;
99 }
100
101 int player_set_streaming_playback_rate(player_h player, float rate)
102 {
103         LOGI("[%s] rate : %0.1f", __FUNCTION__, rate);
104         PLAYER_INSTANCE_CHECK(player);
105         player_s * handle = (player_s *) player;
106
107         if (!__player_state_validate(handle, PLAYER_STATE_READY))
108         {
109                 LOGE("[%s] PLAYER_ERROR_INVALID_STATE(0x%08x) : current state - %d" ,__FUNCTION__,PLAYER_ERROR_INVALID_STATE, handle->state);
110                 return PLAYER_ERROR_INVALID_STATE;
111         }
112
113         int ret = mm_player_set_play_speed(handle->mm_handle, rate, TRUE);
114
115         switch (ret)
116         {
117                 case MM_ERROR_NONE:
118                 case MM_ERROR_PLAYER_NO_OP:
119                         ret = PLAYER_ERROR_NONE;
120                         break;
121                 case MM_ERROR_NOT_SUPPORT_API:
122                 case MM_ERROR_PLAYER_SEEK:
123                         LOGE("[%s] PLAYER_ERROR_INVALID_OPERATION(0x%08x) : seek error",__FUNCTION__, PLAYER_ERROR_INVALID_OPERATION);
124                         ret = PLAYER_ERROR_INVALID_OPERATION;
125                         break;
126                 default:
127                         return __player_convert_error_code(ret,(char*)__FUNCTION__);
128         }
129         return ret;
130 }
131
132