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.
27 #include <sys/types.h>
37 #include <mm_message.h>
40 #include <mm_sound_private.h>
42 #include "include/mm_sound_common.h"
44 #define PA_BUS_NAME "org.pulseaudio.Server"
45 #define PA_SOUND_PLAYER_OBJECT_PATH "/org/pulseaudio/SoundPlayer"
46 #define PA_SOUND_PLAYER_INTERFACE "org.pulseaudio.SoundPlayer"
47 #define PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_PLAY "SimplePlay"
49 #define KEYTONE_PATH "/tmp/keytone" /* Keytone pipe path */
50 #define FILE_FULL_PATH 1024 /* File path length */
51 #define ROLE_NAME_LEN 64 /* Role name length */
52 #define VOLUME_GAIN_TYPE_LEN 64 /* Volume gain type length */
54 #define AUDIO_VOLUME_CONFIG_TYPE(vol) (vol & 0x00FF)
55 #define AUDIO_VOLUME_CONFIG_GAIN(vol) (vol & 0xFF00)
57 typedef struct ipc_data {
58 char filename[FILE_FULL_PATH];
59 char role[ROLE_NAME_LEN];
60 char volume_gain_type[VOLUME_GAIN_TYPE_LEN];
68 static int _mm_sound_play_keysound(const char *filename, int volume_config, ipc_type_t ipc_type);
70 static const char* convert_volume_type_to_role(int volume_type)
72 debug_warning ("volume_type(%d)", volume_type);
74 case VOLUME_TYPE_MEDIA:
76 case VOLUME_TYPE_SYSTEM:
78 case VOLUME_TYPE_NOTIFICATION:
79 return "notification";
80 case VOLUME_TYPE_ALARM:
82 case VOLUME_TYPE_VOICE:
85 debug_warning ("not supported type(%d), we change it SYSTEM type forcibly" );
90 static const char* convert_volume_gain_type_to_string(int volume_gain_type)
92 debug_warning ("volume_gain_type(0x%x)", volume_gain_type);
93 switch(volume_gain_type) {
94 case VOLUME_GAIN_DEFAULT:
96 case VOLUME_GAIN_DIALER:
98 case VOLUME_GAIN_TOUCH:
102 case VOLUME_GAIN_SHUTTER1:
104 case VOLUME_GAIN_SHUTTER2:
106 case VOLUME_GAIN_CAMCORDING:
108 case VOLUME_GAIN_MIDI:
110 case VOLUME_GAIN_BOOTING:
112 case VOLUME_GAIN_VIDEO:
114 case VOLUME_GAIN_TTS:
122 int mm_sound_play_keysound(const char *filename, int volume_config)
124 return _mm_sound_play_keysound(filename, volume_config, IPC_TYPE_DBUS);
127 static int _mm_sound_play_keysound(const char *filename, int volume_config, ipc_type_t ipc_type)
129 int ret = MM_ERROR_NONE;
130 const char *role = NULL;
131 const char *vol_gain_type = NULL;
134 return MM_ERROR_SOUND_INVALID_FILE;
136 /* convert volume type to role/volume gain */
137 role = convert_volume_type_to_role(AUDIO_VOLUME_CONFIG_TYPE(volume_config));
139 vol_gain_type = convert_volume_gain_type_to_string(AUDIO_VOLUME_CONFIG_GAIN(volume_config));
142 if (ipc_type == IPC_TYPE_PIPE) {
146 ipc_t data = {{0,},{0,},{0,}};
148 /* Check whether file exists */
149 fd = open(filename, O_RDONLY);
151 debug_error("file open failed with [%s][%d]\n", strerror(errno), errno);
154 return MM_ERROR_SOUND_FILE_NOT_FOUND;
156 return MM_ERROR_SOUND_INTERNAL;
163 fd = open(KEYTONE_PATH, O_WRONLY | O_NONBLOCK);
165 debug_error("Fail to open pipe\n");
166 return MM_ERROR_SOUND_FILE_NOT_FOUND;
169 /* convert volume type to role/volume gain */
171 MMSOUND_STRNCPY(data.role, role, ROLE_NAME_LEN);
174 MMSOUND_STRNCPY(data.volume_gain_type, vol_gain_type, VOLUME_GAIN_TYPE_LEN);
177 MMSOUND_STRNCPY(data.filename, filename, FILE_FULL_PATH);
179 debug_msg("filepath=[%s], role=[%s], volume_gain_type=[%s]\n", data.filename, data.role, data.volume_gain_type);
180 size = sizeof(ipc_t);
183 res = write(fd, &data, size);
185 debug_error("Fail to write data: [%s][%d]\n", strerror(errno), errno);
187 ret = MM_ERROR_SOUND_INTERNAL;
192 } else if (ipc_type == IPC_TYPE_DBUS) {
193 GVariant *result = NULL;
194 GDBusConnection *conn = NULL;
198 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
200 debug_error("g_bus_get_sync() error (%s)", err->message);
201 ret = MM_ERROR_SOUND_INTERNAL;
203 result = g_dbus_connection_call_sync (conn,
205 PA_SOUND_PLAYER_OBJECT_PATH,
206 PA_SOUND_PLAYER_INTERFACE,
207 PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_PLAY,
208 g_variant_new ("(sss)", filename, role, vol_gain_type),
210 G_DBUS_CALL_FLAGS_NONE,
214 if (!result && err) {
215 debug_error("g_dbus_connection_call_sync() for SIMPLE_PLAY error (%s)", err->message);
216 ret = MM_ERROR_SOUND_INTERNAL;
218 g_variant_get(result, "(i)", &idx);
220 debug_error("SIMPLE_PLAY failure, filename(%s)/role(%s)/gain(%s)/stream idx(%d)", filename, role, vol_gain_type, idx);
221 ret = MM_ERROR_SOUND_INTERNAL;
223 debug_msg("SIMPLE_PLAY success, filename(%s)/role(%s)/gain(%s)/stream idx(%d)", filename, role, vol_gain_type, idx);
225 g_variant_unref(result);
227 g_object_unref(conn);