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.
33 #include "include/mm_sound_common.h"
37 #define PULSEAUDIO_READY "/tmp/.pulseaudio_ready"
40 #define PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_PLAY "SimplePlay"
41 #define PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_STOP "SimpleStop"
42 #define PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_STOP_ALL "SimpleStopAll"
44 #define AUDIO_VOLUME_CONFIG_TYPE(vol) (vol & 0x00FF)
45 #define AUDIO_VOLUME_CONFIG_GAIN(vol) (vol & 0xFF00)
47 #define USE_PIPE /* default is pipe now */
51 #define KEYTONE_PATH "/tmp/keytone"
52 #define FILENAME_LEN 1024
54 #define VOLUME_GAIN_TYPE_LEN 32
57 #define MAX_WRITE_RETRY 50
58 #define WRITE_RETRY_INTERVAL_MS 20
60 #define MIN_KEYTONE_INTERVAL_US 30000
62 typedef struct ipc_data {
63 char filename[FILENAME_LEN];
65 char volume_gain_type[VOLUME_GAIN_TYPE_LEN];
66 char method[METHOD_LEN];
69 static int __mm_sound_simple_pipe(const char *filename, int volume_config, const char *method);
73 #define PA_BUS_NAME "org.pulseaudio.Server"
74 #define PA_SOUND_PLAYER_OBJECT_PATH "/org/pulseaudio/SoundPlayer"
75 #define PA_SOUND_PLAYER_INTERFACE "org.pulseaudio.SoundPlayer"
76 #define PA_SOUND_RESPONSE_TIMEOUT 2000
78 static int __mm_sound_simple_dbus(const char *filename, int volume_config, const char *method);
81 static const char* __convert_volume_type_to_role(int volume_type)
83 debug_log("volume_type(%d)", volume_type);
84 switch (volume_type) {
85 case VOLUME_TYPE_MEDIA:
87 case VOLUME_TYPE_SYSTEM:
89 case VOLUME_TYPE_NOTIFICATION:
90 return "notification";
91 case VOLUME_TYPE_ALARM:
93 case VOLUME_TYPE_VOICE:
95 case VOLUME_TYPE_RINGTONE:
96 return "ringtone-call";
98 debug_warning("not supported type(%d), we change it SYSTEM type forcibly", volume_type);
103 static const char* __convert_volume_gain_type_to_string(int volume_gain_type)
105 debug_log("volume_gain_type(0x%x)", volume_gain_type);
106 switch (volume_gain_type) {
107 case VOLUME_GAIN_DEFAULT:
109 case VOLUME_GAIN_DIALER:
111 case VOLUME_GAIN_TOUCH:
115 case VOLUME_GAIN_SHUTTER1:
117 case VOLUME_GAIN_SHUTTER2:
119 case VOLUME_GAIN_CAMCORDING:
121 case VOLUME_GAIN_MIDI:
123 case VOLUME_GAIN_BOOTING:
125 case VOLUME_GAIN_VIDEO:
127 case VOLUME_GAIN_TTS:
134 static uint64_t __elapsed_time_us(struct timespec *start, struct timespec *end)
136 uint64_t start_us = (uint64_t) start->tv_sec * 1000000 +
137 (uint64_t) start->tv_nsec / 1000;
138 uint64_t end_us = (uint64_t) end->tv_sec * 1000000 +
139 (uint64_t) end->tv_nsec / 1000;
141 return end_us - start_us;
144 static bool __is_interval_too_short(const char *filename)
146 static struct timespec ts_last_played = { .tv_sec = 0, .tv_nsec = 0 };
147 static char file_last_played[FILENAME_LEN] = { '\0', };
149 struct timespec ts_now;
150 clock_gettime(CLOCK_MONOTONIC, &ts_now);
152 if (g_strcmp0(file_last_played, filename) == 0 &&
153 __elapsed_time_us(&ts_last_played, &ts_now) < MIN_KEYTONE_INTERVAL_US) {
157 g_strlcpy(file_last_played, filename, sizeof(file_last_played));
158 ts_last_played = ts_now;
164 int mm_sound_play_keysound(const char *filename, int volume_config)
167 if (__is_interval_too_short(filename)) {
168 debug_warning("Too short time (< %u us) to play same file, try next time again...", MIN_KEYTONE_INTERVAL_US);
169 return MM_ERROR_NONE;
174 return __mm_sound_simple_pipe(filename, volume_config, PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_PLAY);
176 return __mm_sound_simple_dbus(filename, volume_config, PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_PLAY);
181 int mm_sound_stop_keysound(const char *filename)
184 return __mm_sound_simple_pipe(filename, 0,
185 (filename) ? PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_STOP : PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_STOP_ALL);
187 return __mm_sound_simple_dbus(filename, 0,
188 (filename) ? PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_STOP : PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_STOP_ALL);
193 static bool _mm_sound_check_pa_ready()
196 static bool is_pa_ready = false;
201 debug_msg("LwipcIsDone start >> ");
202 ret = LwipcIsDone(PULSEAUDIO_READY);
203 debug_msg("LwipcIsDone end << %d", ret);
205 is_pa_ready = (ret > 0) ? true : false;
211 static int __verify_input_file(const char *filename)
214 return MM_ERROR_SOUND_INVALID_FILE;
216 /* Check whether file exists */
217 if (access(filename, F_OK) == -1) {
220 strerror_r(errno, str_error, sizeof(str_error));
221 debug_error("file [%s] doesn't exists : [%s][%d]", filename, str_error, errno);
223 return MM_ERROR_SOUND_FILE_NOT_FOUND;
226 return MM_ERROR_NONE;
230 static int __mm_sound_simple_pipe(const char *filename, int volume_config, const char *method)
232 int ret = MM_ERROR_NONE;
233 int retry_remaining = MAX_WRITE_RETRY;
234 size_t size_to_write = sizeof(ipc_t);
240 if (!_mm_sound_check_pa_ready()) {
241 debug_error("Pulseaudio is not ready!");
242 return MM_ERROR_SOUND_INVALID_STATE;
247 return MM_ERROR_INVALID_ARGUMENT;
249 /* Prepare data to be send */
250 g_strlcpy(data.method, method, METHOD_LEN);
252 if (g_strcmp0(method, PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_STOP_ALL) != 0) {
253 ret = __verify_input_file(filename);
254 if (ret != MM_ERROR_NONE)
257 g_strlcpy(data.filename, filename, FILENAME_LEN);
260 if (g_strcmp0(method, PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_PLAY) == 0) {
262 __convert_volume_type_to_role(AUDIO_VOLUME_CONFIG_TYPE(volume_config)),
264 g_strlcpy(data.volume_gain_type,
265 __convert_volume_gain_type_to_string(AUDIO_VOLUME_CONFIG_GAIN(volume_config)),
266 VOLUME_GAIN_TYPE_LEN);
269 debug_msg("filepath=[%s], role=[%s], volume_gain_type=[%s], method=[%s]",
270 data.filename, data.role, data.volume_gain_type, data.method);
273 fd = open(KEYTONE_PATH, O_WRONLY | O_NONBLOCK);
277 strerror_r(errno, str_error, sizeof(str_error));
278 debug_error("Fail to open pipe: [%s][%d]", str_error, errno);
280 return MM_ERROR_SOUND_INTERNAL;
285 written = write(fd, &data, size_to_write);
290 strerror_r(errsv, str_error, sizeof(str_error));
291 debug_error("[%d] Fail to write, written = %zd, [%s][%d]",
292 MAX_WRITE_RETRY - retry_remaining, written, str_error, errsv);
294 if (errsv != EAGAIN || --retry_remaining == 0) {
295 ret = MM_ERROR_SOUND_INTERNAL;
299 usleep(WRITE_RETRY_INTERVAL_MS * 1000);
300 } else if (retry_remaining != MAX_WRITE_RETRY) {
301 debug_msg("retry success!!");
303 } while (written != size_to_write);
312 static int __mm_sound_simple_dbus(const char *filename, int volume_config, const char *method)
314 int ret = MM_ERROR_NONE;
315 const char *role = NULL;
316 const char *vol_gain_type = NULL;
317 GVariant *result = NULL;
318 GDBusConnection *conn = NULL;
323 if (!_mm_sound_check_pa_ready()) {
324 debug_error("Pulseaudio is not ready!");
325 return MM_ERROR_SOUND_INVALID_STATE;
329 return MM_ERROR_INVALID_ARGUMENT;
331 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
333 debug_error("g_bus_get_sync() error (%s)", err ? err->message : NULL);
335 return MM_ERROR_SOUND_INTERNAL;
338 if (g_strcmp0(method, PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_PLAY) == 0) {
339 ret = __verify_input_file(filename);
340 if (ret != MM_ERROR_NONE)
343 role = __convert_volume_type_to_role(AUDIO_VOLUME_CONFIG_TYPE(volume_config));
344 vol_gain_type = __convert_volume_gain_type_to_string(AUDIO_VOLUME_CONFIG_GAIN(volume_config));
346 result = g_dbus_connection_call_sync(conn, PA_BUS_NAME,
347 PA_SOUND_PLAYER_OBJECT_PATH,
348 PA_SOUND_PLAYER_INTERFACE,
349 PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_PLAY,
350 g_variant_new("(sss)", filename, role, vol_gain_type),
351 NULL, G_DBUS_CALL_FLAGS_NONE, PA_SOUND_RESPONSE_TIMEOUT, NULL, &err);
352 if (!result || err) {
353 debug_error("g_dbus_connection_call_sync() for %s error (%s)", method, err ? err->message : NULL);
354 ret = MM_ERROR_SOUND_INTERNAL;
358 g_variant_get(result, "(i)", &idx);
360 debug_error("[%s] failure, filename(%s)/role(%s)/gain(%s)/stream idx(%d)",
361 method, filename, role, vol_gain_type, idx);
362 ret = MM_ERROR_SOUND_INTERNAL;
365 debug_msg("[%s] success, filename(%s)/role(%s)/gain(%s)/stream idx(%d)",
366 method, filename, role, vol_gain_type, idx);
368 } else if (g_strcmp0(method, PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_STOP) == 0) {
369 ret = __verify_input_file(filename);
370 if (ret != MM_ERROR_NONE)
373 result = g_dbus_connection_call_sync(conn, PA_BUS_NAME,
374 PA_SOUND_PLAYER_OBJECT_PATH,
375 PA_SOUND_PLAYER_INTERFACE,
376 PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_STOP,
377 g_variant_new("(s)", filename),
378 NULL, G_DBUS_CALL_FLAGS_NONE, PA_SOUND_RESPONSE_TIMEOUT, NULL, &err);
379 if (!result || err) {
380 debug_error("g_dbus_connection_call_sync() for %s error (%s)", method, err ? err->message : NULL);
381 ret = MM_ERROR_SOUND_INTERNAL;
384 debug_msg("[%s] done, filename(%s)", method, filename);
386 } else if (g_strcmp0(method, PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_STOP_ALL) == 0) {
387 result = g_dbus_connection_call_sync(conn, PA_BUS_NAME,
388 PA_SOUND_PLAYER_OBJECT_PATH,
389 PA_SOUND_PLAYER_INTERFACE,
390 PA_SOUND_PLAYER_METHOD_NAME_SIMPLE_STOP_ALL,
392 NULL, G_DBUS_CALL_FLAGS_NONE, PA_SOUND_RESPONSE_TIMEOUT, NULL, &err);
393 if (!result || err) {
394 debug_error("g_dbus_connection_call_sync() for %s error (%s)", method, err ? err->message : NULL);
395 ret = MM_ERROR_SOUND_INTERNAL;
398 debug_msg("[%s] done", method);
401 debug_error("Invalid Method : %s", method);
402 ret = MM_ERROR_INVALID_ARGUMENT;
409 g_variant_unref(result);
411 g_object_unref(conn);
415 #endif /* USE_PIPE */