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);
65 switch (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", volume_type);
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));
135 if (ipc_type == IPC_TYPE_PIPE) {
139 ipc_t data = { { 0, }, { 0, }, { 0, } };
141 /* Check whether file exists */
142 fd = open(filename, O_RDONLY);
145 strerror_r(errno, str_error, sizeof(str_error));
146 debug_error("file open failed with [%s][%d]", str_error, errno);
149 return MM_ERROR_SOUND_FILE_NOT_FOUND;
151 return MM_ERROR_SOUND_INTERNAL;
158 fd = open(KEYTONE_PATH, O_WRONLY | O_NONBLOCK);
160 debug_error("Fail to open pipe");
161 return MM_ERROR_SOUND_FILE_NOT_FOUND;
164 /* convert volume type to role/volume gain */
166 MMSOUND_STRNCPY(data.role, role, ROLE_NAME_LEN);
169 MMSOUND_STRNCPY(data.volume_gain_type, vol_gain_type, VOLUME_GAIN_TYPE_LEN);
171 MMSOUND_STRNCPY(data.filename, filename, FILE_FULL_PATH);
173 debug_msg("filepath=[%s], role=[%s], volume_gain_type=[%s]", data.filename, data.role, data.volume_gain_type);
174 size = sizeof(ipc_t);
177 res = write(fd, &data, size);
180 strerror_r(errno, str_error, sizeof(str_error));
181 debug_error("Fail to write data: [%s][%d]", str_error, errno);
182 ret = MM_ERROR_SOUND_INTERNAL;
187 } else if (ipc_type == IPC_TYPE_DBUS) {
188 GVariant *result = NULL;
189 GDBusConnection *conn = NULL;
193 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
195 debug_error("g_bus_get_sync() error (%s)", err ? err->message : NULL);
196 ret = MM_ERROR_SOUND_INTERNAL;
198 result = g_dbus_connection_call_sync(conn,
200 PA_SOUND_PLAYER_OBJECT_PATH,
201 PA_SOUND_PLAYER_INTERFACE,
202 PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_PLAY,
203 g_variant_new("(sss)", filename, role, vol_gain_type),
205 G_DBUS_CALL_FLAGS_NONE,
209 if (!result || err) {
210 debug_error("g_dbus_connection_call_sync() for SIMPLE_PLAY error (%s)", err ? err->message : NULL);
211 ret = MM_ERROR_SOUND_INTERNAL;
213 g_variant_get(result, "(i)", &idx);
215 debug_error("SIMPLE_PLAY failure, filename(%s)/role(%s)/gain(%s)/stream idx(%d)", filename, role, vol_gain_type, idx);
216 ret = MM_ERROR_SOUND_INTERNAL;
218 debug_msg("SIMPLE_PLAY success, filename(%s)/role(%s)/gain(%s)/stream idx(%d)", filename, role, vol_gain_type, idx);
220 g_variant_unref(result);
222 g_object_unref(conn);