ee2a469ba7636db7d5e90abd22a3c1a7e1918de2
[framework/api/wav-player.git] / 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 "TIZEN_N_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 * uesr_data;
34         int id;
35 } _cb_data;
36
37
38
39 int _convert_wav_player_error_code(const char *func, int code){
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 void _internal_complete_cb(void *user_data){
73         _cb_data * cb_data = (_cb_data*)user_data;
74         if(!cb_data)
75                 return;
76         
77         if( cb_data->cb )
78                 cb_data->cb(cb_data->id, cb_data->uesr_data);
79         free(cb_data);  
80 }
81
82
83 int wav_player_start(const char *path,  sound_type_e type , wav_player_playback_completed_cb cb, void *user_data,  int * id){
84         int ret ; 
85         int player;
86         char m_path[PATH_MAX];
87         void (*_completed_cb)(void *);
88         _completed_cb = NULL;
89         _cb_data *cb_data = NULL;
90         
91         
92         if( path == NULL)
93                 return _convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_INVALID_PARAMETER);
94
95         if( type < SOUND_TYPE_SYSTEM || type >  SOUND_TYPE_CALL )
96                 return _convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_INVALID_PARAMETER);
97
98         m_path[0] = '\0';
99         if( path[0] != '/' ){
100
101                 if( getcwd(m_path, PATH_MAX) != NULL){
102                         strncat(m_path, "/",PATH_MAX-strlen(m_path) );
103                 }
104         }
105         strncat(m_path, path, PATH_MAX-strlen(m_path));
106
107         if( cb ){
108                 _completed_cb = _internal_complete_cb;
109                 cb_data = (_cb_data *)malloc(sizeof(_cb_data));
110                 if(cb_data == NULL )
111                         return _convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_INVALID_OPERATION);
112                 cb_data->cb = cb;
113                 cb_data->uesr_data = user_data;         
114         }
115         
116         ret = mm_sound_play_sound(m_path, type, _completed_cb , cb_data, &player);
117         if( ret == 0 && id != NULL)
118                 *id = player;           
119         if( ret == 0 && cb_data )
120                 cb_data->id = player;
121         
122         if( ret != 0 && cb_data != NULL)
123                 free(cb_data);
124
125                         
126         return _convert_wav_player_error_code(__func__, ret);
127 }
128
129 int wav_player_stop(int id){
130         return _convert_wav_player_error_code(__func__, mm_sound_stop_sound(id));
131 }
132