Generate /tmp/.sound_server_ready as well as /tmp/.focus_server_ready for compatibility
[platform/core/multimedia/libmm-sound.git] / mm_sound_keysound.c
1 /*
2  * libmm-sound
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungbae Shin <seungbae.shin@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include <stdlib.h>
23 #include <memory.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <vconf.h>
27
28 #include <gio/gio.h>
29
30 #include <mm_error.h>
31 #include <mm_debug.h>
32 #include <mm_sound.h>
33
34 #include "include/mm_sound_common.h"
35
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"
40
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 */
45
46 #define AUDIO_VOLUME_CONFIG_TYPE(vol) (vol & 0x00FF)
47 #define AUDIO_VOLUME_CONFIG_GAIN(vol) (vol & 0xFF00)
48
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];
53 }ipc_t;
54
55 typedef enum {
56         IPC_TYPE_PIPE,
57         IPC_TYPE_DBUS,
58 }ipc_type_t;
59
60 static int _mm_sound_play_keysound(const char *filename, int volume_config, ipc_type_t ipc_type);
61
62 static const char* convert_volume_type_to_role(int volume_type)
63 {
64         debug_log("volume_type(%d)", volume_type);
65         switch(volume_type) {
66         case VOLUME_TYPE_MEDIA:
67                 return "media";
68         case VOLUME_TYPE_SYSTEM:
69                 return "system";
70         case VOLUME_TYPE_NOTIFICATION:
71                 return "notification";
72         case VOLUME_TYPE_ALARM:
73                 return "alarm";
74         case VOLUME_TYPE_VOICE:
75                 return "voice";
76         case VOLUME_TYPE_RINGTONE:
77                 return "ringtone-call";
78         default:
79                 debug_warning ("not supported type(%d), we change it SYSTEM type forcibly" );
80                 return "system";
81         }
82 }
83
84 static const char* convert_volume_gain_type_to_string(int volume_gain_type)
85 {
86         debug_log("volume_gain_type(0x%x)", volume_gain_type);
87         switch(volume_gain_type) {
88         case VOLUME_GAIN_DEFAULT:
89                 return "";
90         case VOLUME_GAIN_DIALER:
91                 return "dialer";
92         case VOLUME_GAIN_TOUCH:
93                 return "touch";
94         case VOLUME_GAIN_AF:
95                 return "af";
96         case VOLUME_GAIN_SHUTTER1:
97                 return "shutter1";
98         case VOLUME_GAIN_SHUTTER2:
99                 return "shutter2";
100         case VOLUME_GAIN_CAMCORDING:
101                 return "camcording";
102         case VOLUME_GAIN_MIDI:
103                 return "midi";
104         case VOLUME_GAIN_BOOTING:
105                 return "booting";
106         case VOLUME_GAIN_VIDEO:
107                 return "video";
108         case VOLUME_GAIN_TTS:
109                 return "tts";
110         default:
111                 return "";
112         }
113 }
114
115 EXPORT_API
116 int mm_sound_play_keysound(const char *filename, int volume_config)
117 {
118         return _mm_sound_play_keysound(filename, volume_config, IPC_TYPE_PIPE);
119 }
120
121 static int _mm_sound_play_keysound(const char *filename, int volume_config, ipc_type_t ipc_type)
122 {
123         int ret = MM_ERROR_NONE;
124         const char *role = NULL;
125         const char *vol_gain_type = NULL;
126
127         if (!filename)
128                 return MM_ERROR_SOUND_INVALID_FILE;
129
130         /* convert volume type to role/volume gain */
131         role = convert_volume_type_to_role(AUDIO_VOLUME_CONFIG_TYPE(volume_config));
132         if (role) {
133                 vol_gain_type = convert_volume_gain_type_to_string(AUDIO_VOLUME_CONFIG_GAIN(volume_config));
134         }
135
136         if (ipc_type == IPC_TYPE_PIPE) {
137                 int res = 0;
138                 int fd = -1;
139                 int size = 0;
140                 ipc_t data = {{0,},{0,},{0,}};
141
142                 /* Check whether file exists */
143                 fd = open(filename, O_RDONLY);
144                 if (fd == -1) {
145                         char str_error[256];
146                         strerror_r(errno, str_error, sizeof(str_error));
147                         debug_error("file open failed with [%s][%d]\n", str_error, errno);
148                         switch (errno) {
149                         case ENOENT:
150                                 return MM_ERROR_SOUND_FILE_NOT_FOUND;
151                         default:
152                                 return MM_ERROR_SOUND_INTERNAL;
153                         }
154                 }
155                 close(fd);
156                 fd = -1;
157
158                 /* Open PIPE */
159                 fd = open(KEYTONE_PATH, O_WRONLY | O_NONBLOCK);
160                 if (fd == -1) {
161                         debug_error("Fail to open pipe\n");
162                         return MM_ERROR_SOUND_FILE_NOT_FOUND;
163                 }
164
165                 /* convert volume type to role/volume gain */
166                 if (role) {
167                         MMSOUND_STRNCPY(data.role, role, ROLE_NAME_LEN);
168                 }
169                 if (vol_gain_type) {
170                         MMSOUND_STRNCPY(data.volume_gain_type, vol_gain_type, VOLUME_GAIN_TYPE_LEN);
171                 }
172
173                 MMSOUND_STRNCPY(data.filename, filename, FILE_FULL_PATH);
174
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);
177
178                 /* Write to PIPE */
179                 res = write(fd, &data, size);
180                 if (res < 0) {
181                         char str_error[256];
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;
185                 }
186                 /* Close PIPE */
187                 close(fd);
188
189         } else if (ipc_type == IPC_TYPE_DBUS) {
190                 GVariant *result = NULL;
191                 GDBusConnection *conn = NULL;
192                 GError *err = NULL;
193                 int idx = 0;
194
195                 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
196                 if (!conn && err) {
197                         debug_error("g_bus_get_sync() error (%s)", err->message);
198                         ret = MM_ERROR_SOUND_INTERNAL;
199                 } else {
200                         result = g_dbus_connection_call_sync (conn,
201                                                                         PA_BUS_NAME,
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),
206                                                                         NULL,
207                                                                         G_DBUS_CALL_FLAGS_NONE,
208                                                                         2000,
209                                                                         NULL,
210                                                                         &err);
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;
214                         } else {
215                                 g_variant_get(result, "(i)", &idx);
216                                 if (idx == -1) {
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;
219                                 } else {
220                                         debug_msg("SIMPLE_PLAY success, filename(%s)/role(%s)/gain(%s)/stream idx(%d)", filename, role, vol_gain_type, idx);
221                                 }
222                                 g_variant_unref(result);
223                         }
224                         g_object_unref(conn);
225                 }
226                 if (err) {
227                         g_error_free(err);
228                 }
229         }
230
231         return ret;
232 }