e7d68b823c9d80e8b58b516311244768a21b271b
[platform/core/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 "CAPI_MEDIA_WAV_PLAYER"
19
20 #include <sound_manager.h>
21 #include <sound_manager_internal.h>
22 #include <mm_sound.h>
23 #include <mm_sound_private.h>
24 #include <stdio.h>
25 #include <limits.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <dlog.h>
29 #include "wav_player.h"
30 #include "wav_player_private.h"
31 #include <stdlib.h>
32
33
34 int wav_player_start(const char *path, sound_type_e type, wav_player_playback_completed_cb cb, void *user_data,  int * id)
35 {
36         int ret = MM_ERROR_NONE;
37         int player = -1;
38         char m_path[PATH_MAX];
39         void (*_completed_cb)(void *, int);
40         _completed_cb = NULL;
41         _cb_data *cb_data = NULL;
42
43
44         if( path == NULL)
45                 return __convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_INVALID_PARAMETER);
46
47         if( type < SOUND_TYPE_SYSTEM || type >= SOUND_TYPE_NUM )
48                 return __convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_INVALID_PARAMETER);
49
50         m_path[0] = '\0';
51         if( path[0] != '/' ){
52
53                 if( getcwd(m_path, PATH_MAX) != NULL){
54                         strncat(m_path, "/",PATH_MAX-strlen(m_path) );
55                 }
56         }
57         strncat(m_path, path, PATH_MAX-strlen(m_path));
58
59         if( cb ){
60                 _completed_cb = __internal_complete_cb;
61                 cb_data = (_cb_data *)malloc(sizeof(_cb_data));
62                 if(cb_data == NULL )
63                         return __convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_INVALID_OPERATION);
64                 cb_data->cb = cb;
65                 cb_data->user_data = user_data;
66         }
67
68
69         ret = mm_sound_play_sound(m_path, type, _completed_cb , cb_data, &player);
70
71         if( ret == 0 && id != NULL){
72                 *id = player;
73         }
74
75         if( ret != 0 && cb_data != NULL)
76                 free(cb_data);
77
78         return __convert_wav_player_error_code(__func__, ret);
79 }
80
81 int wav_player_start_with_stream_info(const char *path, sound_stream_info_h stream_info, wav_player_playback_completed_cb cb, void *user_data, int *id)
82 {
83         int ret = MM_ERROR_NONE;
84         int player = -1;
85         char m_path[PATH_MAX];
86         void (*_completed_cb)(void *, int);
87         _completed_cb = NULL;
88         _cb_data *cb_data = NULL;
89         char *stream_type = NULL;
90         int stream_id;
91         bool result = false;
92
93         if( path == NULL)
94                 return __convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_INVALID_PARAMETER);
95
96         ret = sound_manager_is_available_stream_information(stream_info, NATIVE_API_WAV_PLAYER, &result);
97         if ( !result )
98                 return __convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_NOT_SUPPORTED_TYPE);
99
100         ret = sound_manager_get_type_from_stream_information(stream_info, &stream_type);
101         if( ret )
102                 return __convert_wav_player_error_code(__func__, ret);
103         ret = sound_manager_get_index_from_stream_information(stream_info, &stream_id);
104         if( ret )
105                 return __convert_wav_player_error_code(__func__, ret);
106
107         m_path[0] = '\0';
108         if( path[0] != '/' ){
109
110                 if( getcwd(m_path, PATH_MAX) != NULL){
111                         strncat(m_path, "/",PATH_MAX-strlen(m_path) );
112                 }
113         }
114         strncat(m_path, path, PATH_MAX-strlen(m_path));
115
116         if( cb ){
117                 _completed_cb = __internal_complete_cb;
118                 cb_data = (_cb_data *)malloc(sizeof(_cb_data));
119                 if(cb_data == NULL )
120                         return __convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_INVALID_OPERATION);
121                 cb_data->cb = cb;
122                 cb_data->user_data = user_data;
123         }
124
125
126         ret = mm_sound_play_sound_with_stream_info(m_path, stream_type, stream_id, _completed_cb , cb_data, &player);
127
128         if( ret == 0 && id != NULL){
129                 *id = player;
130         }
131
132         if( ret != 0 && cb_data != NULL)
133                 free(cb_data);
134
135         return __convert_wav_player_error_code(__func__, ret);
136
137 }
138
139 int wav_player_stop(int id)
140 {
141         return __convert_wav_player_error_code(__func__, mm_sound_stop_sound(id));
142 }
143