Revise AUTHOR and wav_player_test, remove redundant codes and change file mode
[platform/core/api/wav-player.git] / src / wav_player_private.c
1 /*
2 * Copyright (c) 2011-2016 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 #define LOG_TAG "TIZEN_N_WAV_PLAYER"
18
19 #include <mm_sound.h>
20 #include <mm_sound_private.h>
21 #include <stdio.h>
22 #include <limits.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <dlog.h>
26 #include <stdlib.h>
27 #include "wav_player_private.h"
28
29 int _convert_wav_player_error_code(const char *func, int code)
30 {
31         int ret = WAV_PLAYER_ERROR_INVALID_OPERATION;
32         char *errorstr = NULL;
33         switch (code) {
34         case MM_ERROR_NONE:
35                 ret = WAV_PLAYER_ERROR_NONE;
36                 errorstr = "ERROR_NONE";
37                 break;
38         case MM_ERROR_INVALID_ARGUMENT:
39         case MM_ERROR_SOUND_INVALID_POINTER:
40         case WAV_PLAYER_ERROR_INVALID_PARAMETER:
41                 ret = WAV_PLAYER_ERROR_INVALID_PARAMETER;
42                 errorstr = "INVALID_PARAMETER";
43                 break;
44         case MM_ERROR_SOUND_INTERNAL:
45                 ret = WAV_PLAYER_ERROR_INVALID_OPERATION;
46                 errorstr = "INVALID_OPERATION";
47                 break;
48         case MM_ERROR_SOUND_UNSUPPORTED_MEDIA_TYPE:
49                 ret = WAV_PLAYER_ERROR_FORMAT_NOT_SUPPORTED;
50                 errorstr = "FORMAT_NOT_SUPPORTED";
51                 break;
52         case WAV_PLAYER_ERROR_NOT_SUPPORTED_TYPE:
53                 ret = WAV_PLAYER_ERROR_NOT_SUPPORTED_TYPE;
54                 errorstr = "NOT_SUPPORTED_TYPE";
55                 break;
56         default:
57                 ret = WAV_PLAYER_ERROR_INVALID_OPERATION;
58                 errorstr = "INVALID_OPERATION";
59                 break;
60         }
61         LOGE("[%s] %s(0x%08x)", func, errorstr, ret);
62         return ret;
63 }
64
65 void _internal_complete_cb(void *user_data, int id)
66 {
67         _cb_data *cb_data = (_cb_data*)user_data;
68         if (!cb_data)
69                 return;
70
71         if (cb_data->cb) {
72                 LOGD("user callback for handle %d call %p", id, cb_data->cb);
73                 cb_data->cb(id, cb_data->user_data);
74         }
75         free(cb_data);
76 }
77
78 int _start_with_stream_info(const char *path, sound_stream_info_h stream_info, unsigned loop_count, wav_player_playback_completed_cb callback, void *user_data, int *id)
79 {
80         int ret = MM_ERROR_NONE;
81         int player = -1;
82         char m_path[PATH_MAX];
83         void (*_completed_cb)(void *, int);
84         _completed_cb = NULL;
85         _cb_data *cb_data = NULL;
86         char *stream_type = NULL;
87         int stream_id;
88         bool result = false;
89
90         if (path == NULL || stream_info == NULL)
91                 return _convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_INVALID_PARAMETER);
92
93         ret = sound_manager_is_available_stream_information(stream_info, NATIVE_API_WAV_PLAYER, &result);
94         if (!result)
95                 return _convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_NOT_SUPPORTED_TYPE);
96
97         ret = sound_manager_get_type_from_stream_information(stream_info, &stream_type);
98         if (ret)
99                 return _convert_wav_player_error_code(__func__, ret);
100         ret = sound_manager_get_index_from_stream_information(stream_info, &stream_id);
101         if (ret)
102                 return _convert_wav_player_error_code(__func__, ret);
103
104         m_path[0] = '\0';
105         if (path[0] != '/') {
106
107                 if (getcwd(m_path, PATH_MAX) != NULL)
108                         strncat(m_path, "/", PATH_MAX-strlen(m_path));
109         }
110         strncat(m_path, path, PATH_MAX-strlen(m_path));
111
112         if (callback) {
113                 _completed_cb = _internal_complete_cb;
114                 cb_data = (_cb_data *)malloc(sizeof(_cb_data));
115                 if (cb_data == NULL)
116                         return _convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_INVALID_OPERATION);
117                 cb_data->cb = callback;
118                 cb_data->user_data = user_data;
119         }
120
121         ret = mm_sound_play_sound_with_stream_info(m_path, stream_type, stream_id, loop_count, _completed_cb , cb_data, &player);
122
123         if (ret == 0 && id != NULL)
124                 *id = player;
125
126         if (ret != 0 && cb_data != NULL)
127                 free(cb_data);
128
129         return _convert_wav_player_error_code(__func__, ret);
130 }