Remove unused include statement
[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_warning ("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         default:
77                 debug_warning ("not supported type(%d), we change it SYSTEM type forcibly" );
78                 return "system";
79         }
80 }
81
82 static const char* convert_volume_gain_type_to_string(int volume_gain_type)
83 {
84         debug_warning ("volume_gain_type(0x%x)", volume_gain_type);
85         switch(volume_gain_type) {
86         case VOLUME_GAIN_DEFAULT:
87                 return NULL;
88         case VOLUME_GAIN_DIALER:
89                 return "dialer";
90         case VOLUME_GAIN_TOUCH:
91                 return "touch";
92         case VOLUME_GAIN_AF:
93                 return "af";
94         case VOLUME_GAIN_SHUTTER1:
95                 return "shutter1";
96         case VOLUME_GAIN_SHUTTER2:
97                 return "shutter2";
98         case VOLUME_GAIN_CAMCORDING:
99                 return "camcording";
100         case VOLUME_GAIN_MIDI:
101                 return "midi";
102         case VOLUME_GAIN_BOOTING:
103                 return "booting";
104         case VOLUME_GAIN_VIDEO:
105                 return "video";
106         case VOLUME_GAIN_TTS:
107                 return "tts";
108         default:
109                 return NULL;
110         }
111 }
112
113 EXPORT_API
114 int mm_sound_play_keysound(const char *filename, int volume_config)
115 {
116         return _mm_sound_play_keysound(filename, volume_config, IPC_TYPE_DBUS);
117 }
118
119 static int _mm_sound_play_keysound(const char *filename, int volume_config, ipc_type_t ipc_type)
120 {
121         int ret = MM_ERROR_NONE;
122         const char *role = NULL;
123         const char *vol_gain_type = NULL;
124
125         if (!filename)
126                 return MM_ERROR_SOUND_INVALID_FILE;
127
128         /* convert volume type to role/volume gain */
129         role = convert_volume_type_to_role(AUDIO_VOLUME_CONFIG_TYPE(volume_config));
130         if (role) {
131                 vol_gain_type = convert_volume_gain_type_to_string(AUDIO_VOLUME_CONFIG_GAIN(volume_config));
132         }
133
134         if (ipc_type == IPC_TYPE_PIPE) {
135                 int res = 0;
136                 int fd = -1;
137                 int size = 0;
138                 ipc_t data = {{0,},{0,},{0,}};
139
140                 /* Check whether file exists */
141                 fd = open(filename, O_RDONLY);
142                 if (fd == -1) {
143                         char str_error[256];
144                         strerror_r(errno, str_error, sizeof(str_error));
145                         debug_error("file open failed with [%s][%d]\n", str_error, errno);
146                         switch (errno) {
147                         case ENOENT:
148                                 return MM_ERROR_SOUND_FILE_NOT_FOUND;
149                         default:
150                                 return MM_ERROR_SOUND_INTERNAL;
151                         }
152                 }
153                 close(fd);
154                 fd = -1;
155
156                 /* Open PIPE */
157                 fd = open(KEYTONE_PATH, O_WRONLY | O_NONBLOCK);
158                 if (fd == -1) {
159                         debug_error("Fail to open pipe\n");
160                         return MM_ERROR_SOUND_FILE_NOT_FOUND;
161                 }
162
163                 /* convert volume type to role/volume gain */
164                 if (role) {
165                         MMSOUND_STRNCPY(data.role, role, ROLE_NAME_LEN);
166                 }
167                 if (vol_gain_type) {
168                         MMSOUND_STRNCPY(data.volume_gain_type, vol_gain_type, VOLUME_GAIN_TYPE_LEN);
169                 }
170
171                 MMSOUND_STRNCPY(data.filename, filename, FILE_FULL_PATH);
172
173                 debug_msg("filepath=[%s], role=[%s], volume_gain_type=[%s]\n", 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]\n", 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->message);
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->message);
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                 if (err) {
225                         g_error_free(err);
226                 }
227         }
228
229         return ret;
230 }