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"
36 #define PA_BUS_NAME "org.pulseaudio.Server"
37 #define PA_SOUND_PLAYER_OBJECT_PATH "/org/pulseaudio/SoundPlayer"
38 #define PA_SOUND_PLAYER_INTERFACE "org.pulseaudio.SoundPlayer"
39 #define PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_PLAY "SimplePlay"
41 #define KEYTONE_PATH "/tmp/keytone" /* Keytone pipe path */
42 #define FILE_FULL_PATH 1024 /* File path length */
43 #define ROLE_NAME_LEN 64 /* Role name length */
44 #define VOLUME_GAIN_TYPE_LEN 64 /* Volume gain type length */
46 #define AUDIO_VOLUME_CONFIG_TYPE(vol) (vol & 0x00FF)
47 #define AUDIO_VOLUME_CONFIG_GAIN(vol) (vol & 0xFF00)
49 typedef struct ipc_data {
50 char filename[FILE_FULL_PATH];
51 char role[ROLE_NAME_LEN];
52 char volume_gain_type[VOLUME_GAIN_TYPE_LEN];
60 static int _mm_sound_play_keysound(const char *filename, int volume_config, ipc_type_t ipc_type);
62 static const char* convert_volume_type_to_role(int volume_type)
64 debug_log("volume_type(%d)", volume_type);
66 case VOLUME_TYPE_MEDIA:
68 case VOLUME_TYPE_SYSTEM:
70 case VOLUME_TYPE_NOTIFICATION:
71 return "notification";
72 case VOLUME_TYPE_ALARM:
74 case VOLUME_TYPE_VOICE:
76 case VOLUME_TYPE_RINGTONE:
77 return "ringtone-call";
79 debug_warning ("not supported type(%d), we change it SYSTEM type forcibly" );
84 static const char* convert_volume_gain_type_to_string(int volume_gain_type)
86 debug_log("volume_gain_type(0x%x)", volume_gain_type);
87 switch(volume_gain_type) {
88 case VOLUME_GAIN_DEFAULT:
90 case VOLUME_GAIN_DIALER:
92 case VOLUME_GAIN_TOUCH:
96 case VOLUME_GAIN_SHUTTER1:
98 case VOLUME_GAIN_SHUTTER2:
100 case VOLUME_GAIN_CAMCORDING:
102 case VOLUME_GAIN_MIDI:
104 case VOLUME_GAIN_BOOTING:
106 case VOLUME_GAIN_VIDEO:
108 case VOLUME_GAIN_TTS:
116 int mm_sound_play_keysound(const char *filename, int volume_config)
118 return _mm_sound_play_keysound(filename, volume_config, IPC_TYPE_PIPE);
121 static int _mm_sound_play_keysound(const char *filename, int volume_config, ipc_type_t ipc_type)
123 int ret = MM_ERROR_NONE;
124 const char *role = NULL;
125 const char *vol_gain_type = NULL;
128 return MM_ERROR_SOUND_INVALID_FILE;
130 /* convert volume type to role/volume gain */
131 role = convert_volume_type_to_role(AUDIO_VOLUME_CONFIG_TYPE(volume_config));
133 vol_gain_type = convert_volume_gain_type_to_string(AUDIO_VOLUME_CONFIG_GAIN(volume_config));
136 if (ipc_type == IPC_TYPE_PIPE) {
140 ipc_t data = {{0,},{0,},{0,}};
142 /* Check whether file exists */
143 fd = open(filename, O_RDONLY);
146 strerror_r(errno, str_error, sizeof(str_error));
147 debug_error("file open failed with [%s][%d]\n", str_error, errno);
150 return MM_ERROR_SOUND_FILE_NOT_FOUND;
152 return MM_ERROR_SOUND_INTERNAL;
159 fd = open(KEYTONE_PATH, O_WRONLY | O_NONBLOCK);
161 debug_error("Fail to open pipe\n");
162 return MM_ERROR_SOUND_FILE_NOT_FOUND;
165 /* convert volume type to role/volume gain */
167 MMSOUND_STRNCPY(data.role, role, ROLE_NAME_LEN);
170 MMSOUND_STRNCPY(data.volume_gain_type, vol_gain_type, VOLUME_GAIN_TYPE_LEN);
173 MMSOUND_STRNCPY(data.filename, filename, FILE_FULL_PATH);
175 debug_msg("filepath=[%s], role=[%s], volume_gain_type=[%s]\n", data.filename, data.role, data.volume_gain_type);
176 size = sizeof(ipc_t);
179 res = write(fd, &data, size);
182 strerror_r(errno, str_error, sizeof(str_error));
183 debug_error("Fail to write data: [%s][%d]\n", str_error, errno);
184 ret = MM_ERROR_SOUND_INTERNAL;
189 } else if (ipc_type == IPC_TYPE_DBUS) {
190 GVariant *result = NULL;
191 GDBusConnection *conn = NULL;
195 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
197 debug_error("g_bus_get_sync() error (%s)", err->message);
198 ret = MM_ERROR_SOUND_INTERNAL;
200 result = g_dbus_connection_call_sync (conn,
202 PA_SOUND_PLAYER_OBJECT_PATH,
203 PA_SOUND_PLAYER_INTERFACE,
204 PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_PLAY,
205 g_variant_new ("(sss)", filename, role, vol_gain_type),
207 G_DBUS_CALL_FLAGS_NONE,
211 if (!result && err) {
212 debug_error("g_dbus_connection_call_sync() for SIMPLE_PLAY error (%s)", err->message);
213 ret = MM_ERROR_SOUND_INTERNAL;
215 g_variant_get(result, "(i)", &idx);
217 debug_error("SIMPLE_PLAY failure, filename(%s)/role(%s)/gain(%s)/stream idx(%d)", filename, role, vol_gain_type, idx);
218 ret = MM_ERROR_SOUND_INTERNAL;
220 debug_msg("SIMPLE_PLAY success, filename(%s)/role(%s)/gain(%s)/stream idx(%d)", filename, role, vol_gain_type, idx);
222 g_variant_unref(result);
224 g_object_unref(conn);