bca6f85ca180b242994b29464676fcc6f445e24f
[platform/core/api/tone-player.git] / src / tone_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
19
20 #define LOG_TAG "TIZEN_N_TONE_PLAYER"
21
22 #include <sound_manager.h>
23 #include <sound_manager_internal.h>
24 #include <tone_player.h>
25 #include <mm_sound.h>
26 #include <mm_sound_private.h>
27 #include <stdio.h>
28 #include <limits.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <dlog.h>
32
33
34 #define DEPRECATED_WARN_INSTEAD(msg) do { \
35         LOGW("DEPRECATION WARNING: %s() is deprecated and will be removed from next release. Use %s() instead.", __func__, msg); \
36 } while (0)
37
38 static int __convert_tone_player_error_code(const char *func, int code)
39 {
40         int ret = TONE_PLAYER_ERROR_NONE;
41         char *errorstr = NULL;
42         switch (code) {
43         case MM_ERROR_NONE:
44                 ret = TONE_PLAYER_ERROR_NONE;
45                 errorstr = "ERROR_NONE";
46                 break;
47         case TONE_PLAYER_ERROR_INVALID_PARAMETER:
48         case MM_ERROR_INVALID_ARGUMENT:
49         case MM_ERROR_SOUND_INVALID_POINTER:
50                 ret = TONE_PLAYER_ERROR_INVALID_PARAMETER;
51                 errorstr = "INVALID_PARAMETER";
52                 break;
53         case MM_ERROR_SOUND_INTERNAL:
54                 ret = TONE_PLAYER_ERROR_INVALID_OPERATION;
55                 errorstr = "INVALID_OPERATION";
56                 break;
57         }
58         LOGE("[%s] %s(0x%08x) : core frameworks error code(0x%08x)", func, errorstr, ret, code);
59         return ret;
60 }
61
62 int tone_player_start(tone_type_e tone, sound_type_e type, int duration, int *id)
63 {
64         int ret;
65         int player;
66
67         DEPRECATED_WARN_INSTEAD("tone_player_start_new");
68
69         if (tone < TONE_TYPE_DEFAULT || tone > TONE_TYPE_USER_DEFINED_HIGH_FRE)
70                 return __convert_tone_player_error_code(__func__, TONE_PLAYER_ERROR_INVALID_PARAMETER);
71
72         ret = mm_sound_play_tone(tone, type , 1, duration, &player);
73
74         if (ret == 0 && id != NULL)
75         *id = player;
76
77         return __convert_tone_player_error_code(__func__, ret);
78 }
79
80 int tone_player_start_new(tone_type_e tone, sound_stream_info_h stream_info, int duration, int *id)
81 {
82         int ret;
83         int player;
84         double vol = 1.0;
85         char *stream_type = NULL;
86         int stream_id;
87         bool result = false;
88
89         if (tone < TONE_TYPE_DEFAULT || tone > TONE_TYPE_USER_DEFINED_HIGH_FRE || stream_info == NULL)
90                 return __convert_tone_player_error_code(__func__, TONE_PLAYER_ERROR_INVALID_PARAMETER);
91
92         ret = sound_manager_is_available_stream_information(stream_info, NATIVE_API_TONE_PLAYER, &result);
93         if (!result)
94                 return __convert_tone_player_error_code(__func__, TONE_PLAYER_ERROR_NOT_SUPPORTED_TYPE);
95
96         ret = sound_manager_get_type_from_stream_information(stream_info, &stream_type);
97         if (ret)
98                 return __convert_tone_player_error_code(__func__, ret);
99
100         ret = sound_manager_get_index_from_stream_information(stream_info, &stream_id);
101         if (ret)
102                 return __convert_tone_player_error_code(__func__, ret);
103
104         ret = mm_sound_play_tone_with_stream_info(tone, stream_type, stream_id, vol, duration, &player);
105         if (ret == 0 && id != NULL)
106                 *id = player;
107
108         return __convert_tone_player_error_code(__func__, ret);
109 }
110
111 /* Note : Will be removed after migration to tone_player_start_new */
112 int tone_player_start_with_stream_info(tone_type_e tone, sound_stream_info_h stream_info, int duration, int *id)
113 {
114         return tone_player_start_new(tone, stream_info, duration, id);
115 }
116
117
118 int tone_player_stop(int id)
119 {
120         return __convert_tone_player_error_code(__func__, mm_sound_stop_sound(id));
121 }
122