a8f3049e8ba64bfa62d9871830e15215ee7b9279
[platform/core/api/wav-player.git] / src / wav_player.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 <sound_manager.h>
20 #include <sound_manager_internal.h>
21 #include <mm_sound.h>
22 #include <mm_sound_private.h>
23 #include <stdio.h>
24 #include <limits.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <dlog.h>
28 #include <stdlib.h>
29 #include "wav_player.h"
30 #include "wav_player_private.h"
31
32 #define DEPRECATED_WARN_INSTEAD(msg) do { \
33         LOGW("DEPRECATION WARNING: %s() is deprecated and will be removed from next release. Use %s() instead.", __func__, msg); \
34 } while (0)
35
36 int wav_player_start(const char *path, sound_type_e type, wav_player_playback_completed_cb cb, void *user_data,  int *id)
37 {
38         int ret = MM_ERROR_NONE;
39         int player = -1;
40         char m_path[PATH_MAX];
41         void (*_completed_cb)(void *, int);
42         _completed_cb = NULL;
43         _cb_data *cb_data = NULL;
44
45         DEPRECATED_WARN_INSTEAD("wav_player_start_new");
46
47         if (path == NULL)
48                 return _convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_INVALID_PARAMETER);
49
50         if (type < SOUND_TYPE_SYSTEM || type >= SOUND_TYPE_NUM)
51                 return _convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_INVALID_PARAMETER);
52
53         m_path[0] = '\0';
54         if (path[0] != '/') {
55                 if (getcwd(m_path, PATH_MAX) != NULL)
56                         strncat(m_path, "/", PATH_MAX-strlen(m_path));
57         }
58         strncat(m_path, path, PATH_MAX-strlen(m_path));
59
60         if (cb) {
61                 _completed_cb = _internal_complete_cb;
62                 cb_data = (_cb_data *)malloc(sizeof(_cb_data));
63                 if (cb_data == NULL)
64                         return _convert_wav_player_error_code(__func__, WAV_PLAYER_ERROR_INVALID_OPERATION);
65                 cb_data->cb = cb;
66                 cb_data->user_data = user_data;
67         }
68
69         ret = mm_sound_play_sound(m_path, type, _completed_cb , cb_data, &player);
70         if (ret == 0 && id != NULL)
71                 *id = player;
72
73         if (ret != 0 && cb_data != NULL)
74                 free(cb_data);
75
76         return _convert_wav_player_error_code(__func__, ret);
77 }
78
79 int wav_player_start_new(const char *path, sound_stream_info_h stream_info, wav_player_playback_completed_cb callback, void *user_data, int *id)
80 {
81         return _start_with_stream_info(path, stream_info, 1, callback, user_data, id);
82 }
83
84 int wav_player_stop(int id)
85 {
86         return _convert_wav_player_error_code(__func__, mm_sound_stop_sound(id));
87 }
88