Modification for Tizen Coding Rule
[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", volume_type);
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         if (ipc_type == IPC_TYPE_PIPE) {
136                 int res = 0;
137                 int fd = -1;
138                 int size = 0;
139                 ipc_t data = { { 0, }, { 0, }, { 0, } };
140
141                 /* Check whether file exists */
142                 fd = open(filename, O_RDONLY);
143                 if (fd == -1) {
144                         char str_error[256];
145                         strerror_r(errno, str_error, sizeof(str_error));
146                         debug_error("file open failed with [%s][%d]", str_error, errno);
147                         switch (errno) {
148                         case ENOENT:
149                                 return MM_ERROR_SOUND_FILE_NOT_FOUND;
150                         default:
151                                 return MM_ERROR_SOUND_INTERNAL;
152                         }
153                 }
154                 close(fd);
155                 fd = -1;
156
157                 /* Open PIPE */
158                 fd = open(KEYTONE_PATH, O_WRONLY | O_NONBLOCK);
159                 if (fd == -1) {
160                         debug_error("Fail to open pipe");
161                         return MM_ERROR_SOUND_FILE_NOT_FOUND;
162                 }
163
164                 /* convert volume type to role/volume gain */
165                 if (role)
166                         MMSOUND_STRNCPY(data.role, role, ROLE_NAME_LEN);
167
168                 if (vol_gain_type)
169                         MMSOUND_STRNCPY(data.volume_gain_type, vol_gain_type, VOLUME_GAIN_TYPE_LEN);
170
171                 MMSOUND_STRNCPY(data.filename, filename, FILE_FULL_PATH);
172
173                 debug_msg("filepath=[%s], role=[%s], volume_gain_type=[%s]", data.filename, data.role, data.volume_gain_type);
174                 size = sizeof(ipc_t);
175
176                 /* Write to PIPE */
177                 res = write(fd, &data, size);
178                 if (res < 0) {
179                         char str_error[256];
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;
183                 }
184                 /* Close PIPE */
185                 close(fd);
186
187         } else if (ipc_type == IPC_TYPE_DBUS) {
188                 GVariant *result = NULL;
189                 GDBusConnection *conn = NULL;
190                 GError *err = NULL;
191                 int idx = 0;
192
193                 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
194                 if (!conn || err) {
195                         debug_error("g_bus_get_sync() error (%s)", err ? err->message : NULL);
196                         ret = MM_ERROR_SOUND_INTERNAL;
197                 } else {
198                         result = g_dbus_connection_call_sync(conn,
199                                                                         PA_BUS_NAME,
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),
204                                                                         NULL,
205                                                                         G_DBUS_CALL_FLAGS_NONE,
206                                                                         2000,
207                                                                         NULL,
208                                                                         &err);
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;
212                         } else {
213                                 g_variant_get(result, "(i)", &idx);
214                                 if (idx == -1) {
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;
217                                 } else {
218                                         debug_msg("SIMPLE_PLAY success, filename(%s)/role(%s)/gain(%s)/stream idx(%d)", filename, role, vol_gain_type, idx);
219                                 }
220                                 g_variant_unref(result);
221                         }
222                         g_object_unref(conn);
223                 }
224                 g_error_free(err);
225         }
226
227         return ret;
228 }