4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Seungbae Shin <seungbae.shin@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
34 #include "include/mm_sound_common.h"
38 #define PULSEAUDIO_READY "/tmp/.pulseaudio_ready"
41 #define PA_BUS_NAME "org.pulseaudio.Server"
42 #define PA_SOUND_PLAYER_OBJECT_PATH "/org/pulseaudio/SoundPlayer"
43 #define PA_SOUND_PLAYER_INTERFACE "org.pulseaudio.SoundPlayer"
44 #define PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_PLAY "SimplePlay"
46 #define KEYTONE_PATH "/tmp/keytone" /* Keytone pipe path */
47 #define FILE_FULL_PATH 1024 /* File path length */
48 #define ROLE_NAME_LEN 64 /* Role name length */
49 #define VOLUME_GAIN_TYPE_LEN 64 /* Volume gain type length */
51 #define AUDIO_VOLUME_CONFIG_TYPE(vol) (vol & 0x00FF)
52 #define AUDIO_VOLUME_CONFIG_GAIN(vol) (vol & 0xFF00)
54 typedef struct ipc_data {
55 char filename[FILE_FULL_PATH];
56 char role[ROLE_NAME_LEN];
57 char volume_gain_type[VOLUME_GAIN_TYPE_LEN];
65 static int _mm_sound_play_keysound(const char *filename, int volume_config, ipc_type_t ipc_type);
67 static const char* convert_volume_type_to_role(int volume_type)
69 debug_log("volume_type(%d)", volume_type);
70 switch (volume_type) {
71 case VOLUME_TYPE_MEDIA:
73 case VOLUME_TYPE_SYSTEM:
75 case VOLUME_TYPE_NOTIFICATION:
76 return "notification";
77 case VOLUME_TYPE_ALARM:
79 case VOLUME_TYPE_VOICE:
81 case VOLUME_TYPE_RINGTONE:
82 return "ringtone-call";
84 debug_warning("not supported type(%d), we change it SYSTEM type forcibly", volume_type);
89 static const char* convert_volume_gain_type_to_string(int volume_gain_type)
91 debug_log("volume_gain_type(0x%x)", volume_gain_type);
92 switch (volume_gain_type) {
93 case VOLUME_GAIN_DEFAULT:
95 case VOLUME_GAIN_DIALER:
97 case VOLUME_GAIN_TOUCH:
101 case VOLUME_GAIN_SHUTTER1:
103 case VOLUME_GAIN_SHUTTER2:
105 case VOLUME_GAIN_CAMCORDING:
107 case VOLUME_GAIN_MIDI:
109 case VOLUME_GAIN_BOOTING:
111 case VOLUME_GAIN_VIDEO:
113 case VOLUME_GAIN_TTS:
121 int mm_sound_play_keysound(const char *filename, int volume_config)
123 return _mm_sound_play_keysound(filename, volume_config, IPC_TYPE_PIPE);
127 static bool _mm_sound_check_pa_ready()
130 static bool is_pa_ready = false;
135 debug_msg("LwipcIsDone start >> ");
136 ret = LwipcIsDone(PULSEAUDIO_READY);
137 debug_msg("LwipcIsDone end << %d", ret);
139 is_pa_ready = (ret > 0) ? true : false;
145 static int _mm_sound_play_keysound(const char *filename, int volume_config, ipc_type_t ipc_type)
147 int ret = MM_ERROR_NONE;
148 const char *role = NULL;
149 const char *vol_gain_type = NULL;
152 if (!_mm_sound_check_pa_ready()) {
153 debug_error("Pulseaudio is not ready!");
154 return MM_ERROR_SOUND_INVALID_STATE;
159 return MM_ERROR_SOUND_INVALID_FILE;
161 /* convert volume type to role/volume gain */
162 role = convert_volume_type_to_role(AUDIO_VOLUME_CONFIG_TYPE(volume_config));
164 vol_gain_type = convert_volume_gain_type_to_string(AUDIO_VOLUME_CONFIG_GAIN(volume_config));
166 if (ipc_type == IPC_TYPE_PIPE) {
170 ipc_t data = { { 0, }, { 0, }, { 0, } };
172 /* Check whether file exists */
173 fd = open(filename, O_RDONLY);
177 strerror_r(errsv, str_error, sizeof(str_error));
178 debug_error("file open failed with [%s][%d]", str_error, errsv);
181 return MM_ERROR_SOUND_FILE_NOT_FOUND;
183 return MM_ERROR_SOUND_INTERNAL;
190 fd = open(KEYTONE_PATH, O_WRONLY | O_NONBLOCK);
192 debug_error("Fail to open pipe");
193 return MM_ERROR_SOUND_FILE_NOT_FOUND;
196 /* convert volume type to role/volume gain */
198 MMSOUND_STRNCPY(data.role, role, ROLE_NAME_LEN);
201 MMSOUND_STRNCPY(data.volume_gain_type, vol_gain_type, VOLUME_GAIN_TYPE_LEN);
203 MMSOUND_STRNCPY(data.filename, filename, FILE_FULL_PATH);
205 debug_msg("filepath=[%s], role=[%s], volume_gain_type=[%s]", data.filename, data.role, data.volume_gain_type);
206 size = sizeof(ipc_t);
209 res = write(fd, &data, size);
212 strerror_r(errno, str_error, sizeof(str_error));
213 debug_error("Fail to write data: [%s][%d]", str_error, errno);
214 ret = MM_ERROR_SOUND_INTERNAL;
219 } else if (ipc_type == IPC_TYPE_DBUS) {
220 GVariant *result = NULL;
221 GDBusConnection *conn = NULL;
225 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
227 debug_error("g_bus_get_sync() error (%s)", err ? err->message : NULL);
228 ret = MM_ERROR_SOUND_INTERNAL;
230 result = g_dbus_connection_call_sync(conn,
232 PA_SOUND_PLAYER_OBJECT_PATH,
233 PA_SOUND_PLAYER_INTERFACE,
234 PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_PLAY,
235 g_variant_new("(sss)", filename, role, vol_gain_type),
237 G_DBUS_CALL_FLAGS_NONE,
241 if (!result || err) {
242 debug_error("g_dbus_connection_call_sync() for SIMPLE_PLAY error (%s)", err ? err->message : NULL);
243 ret = MM_ERROR_SOUND_INTERNAL;
245 g_variant_get(result, "(i)", &idx);
247 debug_error("SIMPLE_PLAY failure, filename(%s)/role(%s)/gain(%s)/stream idx(%d)", filename, role, vol_gain_type, idx);
248 ret = MM_ERROR_SOUND_INTERNAL;
250 debug_msg("SIMPLE_PLAY success, filename(%s)/role(%s)/gain(%s)/stream idx(%d)", filename, role, vol_gain_type, idx);
252 g_variant_unref(result);
254 g_object_unref(conn);