Initialize Tizen 2.3
[framework/api/wav-player.git] / wearable / src / wav_player.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
18 #define LOG_TAG "CAPI_MEDIA_WAV_PLAYER"
19
20 #include <mm_sound.h>
21 #include <mm_sound_private.h>
22 #include <stdio.h>
23 #include <limits.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <dlog.h>
27 #include <wav_player.h>
28 #include <stdlib.h>
29
30
31 typedef struct _cb_data_{
32         wav_player_playback_completed_cb cb;
33         void * user_data;
34 } _cb_data;
35
36
37
38 static int __convert_wav_player_error_code(const char *func, int code)
39 {
40         int ret = WAV_PLAYER_ERROR_INVALID_OPERATION;
41         char *errorstr = NULL;
42         switch(code)
43         {
44                 case MM_ERROR_NONE:
45                         ret = WAV_PLAYER_ERROR_NONE;
46                         errorstr = "ERROR_NONE";
47                         break;
48                 case MM_ERROR_INVALID_ARGUMENT: 
49                 case MM_ERROR_SOUND_INVALID_POINTER:
50                 case WAV_PLAYER_ERROR_INVALID_PARAMETER:                        
51                         ret = WAV_PLAYER_ERROR_INVALID_PARAMETER;
52                         errorstr = "INVALID_PARAMETER";                 
53                         break;
54                 case MM_ERROR_SOUND_INTERNAL:
55                         ret = WAV_PLAYER_ERROR_INVALID_OPERATION;
56                         errorstr = "INVALID_OPERATION";                 
57                         break;
58                 case MM_ERROR_SOUND_UNSUPPORTED_MEDIA_TYPE:
59                         ret = WAV_PLAYER_ERROR_FORMAT_NOT_SUPPORTED;
60                         errorstr = "FORMAT_NOT_SUPPORTED";
61                         break;
62                 default:
63                         ret = WAV_PLAYER_ERROR_INVALID_OPERATION;
64                         errorstr = "INVALID_OPERATION";
65                         
66         }       
67         LOGE( "[%s] %s(0x%08x)",func, errorstr, ret);
68         return ret;
69 }
70
71
72 static void __internal_complete_cb(void *user_data, int id)
73 {
74         _cb_data * cb_data = (_cb_data*)user_data;
75         if(!cb_data)
76                 return;
77         
78         if( cb_data->cb ){
79                 LOGD( "user callback for handle %d call", id);
80                 cb_data->cb(id, cb_data->user_data);
81         }
82         free(cb_data);
83 }
84
85
86 int wav_player_start(const char *path, sound_type_e type, wav_player_playback_completed_cb cb, void *user_data,  int * id)
87 {
88         int ret ;
89         int player;
90         char m_path[PATH_MAX];
91         void (*_completed_cb)(void *, int);
92         _completed_cb = NULL;
93         _cb_data *cb_data = NULL;
94         
95         
96         if( path == NULL)
97                 return __convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_INVALID_PARAMETER);
98
99         if( type < SOUND_TYPE_SYSTEM || type > SOUND_TYPE_FIXED_CAMCORDING )
100                 return __convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_INVALID_PARAMETER);
101         if (type >= SOUND_TYPE_FIXED ) {
102                 LOGW( "type is %d, we force to set SOUND_TYPE_FIXED", type);
103                 type = VOLUME_TYPE_FIXED;
104         }
105
106         m_path[0] = '\0';
107         if( path[0] != '/' ){
108
109                 if( getcwd(m_path, PATH_MAX) != NULL){
110                         strncat(m_path, "/",PATH_MAX-strlen(m_path) );
111                 }
112         }
113         strncat(m_path, path, PATH_MAX-strlen(m_path));
114
115         if( cb ){
116                 _completed_cb = __internal_complete_cb;
117                 cb_data = (_cb_data *)malloc(sizeof(_cb_data));
118                 if(cb_data == NULL )
119                         return __convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_INVALID_OPERATION);
120                 cb_data->cb = cb;
121                 cb_data->user_data = user_data;
122         }
123
124         
125         ret = mm_sound_play_sound(m_path, type, _completed_cb , cb_data, &player);
126
127         if( ret == 0 && id != NULL){
128                 *id = player;
129         }
130         
131         if( ret != 0 && cb_data != NULL)
132                 free(cb_data);
133
134         return __convert_wav_player_error_code(__func__, ret);
135 }
136
137 int wav_player_start_solo(const char *path, sound_type_e type, bool force_to_route_spk, wav_player_playback_completed_cb cb, void *user_data, int * id)
138 {
139         int ret ;
140         int player;
141         char m_path[PATH_MAX];
142         void (*_completed_cb)(void *, int);
143         _completed_cb = NULL;
144         _cb_data *cb_data = NULL;
145
146
147         if( path == NULL) {
148                 return __convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_INVALID_PARAMETER);
149         }
150
151         if( type < SOUND_TYPE_SYSTEM || type > SOUND_TYPE_FIXED_CAMCORDING ) {
152                 return __convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_INVALID_PARAMETER);
153         }
154
155         m_path[0] = '\0';
156         if( path[0] != '/' ){
157                 if( getcwd(m_path, PATH_MAX) != NULL){
158                         strncat(m_path, "/",PATH_MAX-strlen(m_path) );
159                 }
160         }
161         strncat(m_path, path, PATH_MAX-strlen(m_path));
162
163         if( cb ){
164                 _completed_cb = __internal_complete_cb;
165                 cb_data = (_cb_data *)malloc(sizeof(_cb_data));
166                 if(cb_data == NULL )
167                         return __convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_INVALID_OPERATION);
168                 cb_data->cb = cb;
169                 cb_data->user_data = user_data;
170         }
171
172         if( type < SOUND_TYPE_FIXED ) {
173                 ret = mm_sound_play_solo_sound(m_path, type, _completed_cb , cb_data, &player);
174         } else {
175                 int volume_config = VOLUME_TYPE_FIXED;
176                 switch (type) {
177                 case SOUND_TYPE_FIXED:
178                         volume_config |= VOLUME_GAIN_DEFAULT;
179                         break;
180                 case SOUND_TYPE_FIXED_AF:
181                         volume_config |= VOLUME_GAIN_AF;
182                         break;
183                 case SOUND_TYPE_FIXED_SHUTTER1:
184                         volume_config |= VOLUME_GAIN_SHUTTER1;
185                         break;
186                 case SOUND_TYPE_FIXED_SHUTTER2:
187                         volume_config |= VOLUME_GAIN_SHUTTER2;
188                         break;
189                 case SOUND_TYPE_FIXED_CAMCORDING:
190                         volume_config |= VOLUME_GAIN_CAMCORDING;
191                         break;
192                 default:
193                         break;
194                 }
195                 if (force_to_route_spk) {
196                         ret = mm_sound_play_loud_solo_sound(m_path, volume_config, _completed_cb , cb_data, &player);
197                 } else {
198                         ret = mm_sound_play_solo_sound(m_path, volume_config, _completed_cb , cb_data, &player);
199                 }
200         }
201         if( ret == 0 && id != NULL) {
202                 *id = player;
203         }
204
205         if( ret != 0 && cb_data != NULL) {
206                 free(cb_data);
207         }
208
209         return __convert_wav_player_error_code(__func__, ret);
210 }
211
212 int wav_player_stop(int id)
213 {
214         return __convert_wav_player_error_code(__func__, mm_sound_stop_sound(id));
215 }
216