2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef __TIZEN_MEDIA_SOUND_MANAGER_PRIVATE_H__
18 #define __TIZEN_MEDIA_SOUND_MANAGER_PRIVATE_H__
24 #define LOG_TAG "TIZEN_N_SOUND_MANAGER"
32 #include <pulse/error.h>
33 #include <pulse/proplist.h>
34 #include <pulse/channelmap.h>
35 #include <pulse/pulseaudio.h>
40 #include <mm_sound_focus.h>
41 #include <mm_sound_private.h>
42 #include "sound_manager_internal.h"
44 #define _CHECK_CONDITION(condition, error, msg) \
47 LOGE("[%s] %s(0x%08x)", __FUNCTION__, msg, error); \
51 #define SM_INSTANCE_CHECK(handle) \
52 _CHECK_CONDITION(handle != NULL, SOUND_MANAGER_ERROR_INVALID_PARAMETER, "SOUND_MANAGER_ERROR_INVALID_PARAMETER")
54 #define SM_NULL_ARG_CHECK(arg) \
55 _CHECK_CONDITION(!!(arg) != false, SOUND_MANAGER_ERROR_INVALID_PARAMETER, "SOUND_MANAGER_ERROR_INVALID_PARAMETER")
57 #define SM_STATE_CHECK(handle, expected_state) \
58 _CHECK_CONDITION(handle->state == expected_state, SOUND_MANAGER_ERROR_INVALID_STATE, "SOUND_MANAGER_ERROR_INVALID_STATE")
60 #define SM_RANGE_ARG_CHECK(arg, min, max) \
61 _CHECK_CONDITION(arg <= max, SOUND_MANAGER_ERROR_INVALID_PARAMETER, "SOUND_MANAGER_ERROR_INVALID_PARAMETER") \
62 _CHECK_CONDITION(arg >= min, SOUND_MANAGER_ERROR_INVALID_PARAMETER, "SOUND_MANAGER_ERROR_INVALID_PARAMETER")
64 #define SM_INSTANCE_CHECK_FOR_PRIV(handle) \
65 _CHECK_CONDITION(handle != NULL, MM_ERROR_INVALID_ARGUMENT, "MM_ERROR_INVALID_ARGUMENT")
67 #define SM_NULL_ARG_CHECK_FOR_PRIV(arg) \
68 _CHECK_CONDITION(!!(arg) != false, MM_ERROR_INVALID_ARGUMENT, "MM_ERROR_INVALID_ARGUMENT")
70 #define SM_STATE_CHECK_FOR_PRIV(handle, expected_state) \
71 _CHECK_CONDITION(handle->state == expected_state, MM_ERROR_SOUND_INVALID_STATE, "MM_ERROR_SOUND_INVALID_STATE")
73 #define SM_ENTER_CRITICAL_SECTION_WITH_UNLOCK_AND_RETURN(x_mutex, x_mutex_to_unlock, x_return) \
74 switch (pthread_mutex_lock(x_mutex)) { \
76 LOGW("try mutex init.."); \
77 if (pthread_mutex_init(x_mutex, NULL)) { \
78 if (pthread_mutex_unlock(x_mutex_to_unlock)) \
79 LOGE("mutex unlock failed"); \
82 if (pthread_mutex_lock(x_mutex)) { \
83 LOGE("mutex lock failed"); \
84 if (pthread_mutex_unlock(x_mutex_to_unlock)) \
85 LOGE("mutex unlock failed"); \
90 if (pthread_mutex_unlock(x_mutex_to_unlock)) \
91 LOGE("mutex unlock failed"); \
96 LOGE("mutex lock failed"); \
97 if (pthread_mutex_unlock(x_mutex_to_unlock)) \
98 LOGE("mutex unlock failed"); \
102 #define SM_ENTER_CRITICAL_SECTION_WITH_RETURN(x_mutex, x_return) \
103 switch (pthread_mutex_lock(x_mutex)) { \
105 LOGW("try mutex init.."); \
106 if (pthread_mutex_init(x_mutex, NULL)) { \
109 if (pthread_mutex_lock(x_mutex)) { \
110 LOGE("mutex lock failed"); \
119 LOGE("mutex lock failed"); \
123 #define SM_ENTER_CRITICAL_SECTION(x_mutex) \
124 switch (pthread_mutex_lock(x_mutex)) { \
126 LOGW("try mutex init.."); \
127 if (pthread_mutex_init(x_mutex, NULL)) { \
130 if (pthread_mutex_lock(x_mutex)) { \
131 LOGE("mutex lock failed"); \
140 LOGE("mutex lock failed"); \
144 #define SM_LEAVE_CRITICAL_SECTION(x_mutex) \
145 if (pthread_mutex_unlock(x_mutex)) { \
146 LOGE("mutex unlock failed"); \
149 #define SM_SAFE_FREE(x_val) do { \
156 #define SOUND_SESSION_TYPE_DEFAULT SOUND_SESSION_TYPE_MEDIA
157 #define SOUND_STREAM_INFO_ARR_MAX 128
158 #define SOUND_STREAM_TYPE_LEN 64
159 #define SOUND_STREAM_DIRECTION_MAX 2
160 #define SOUND_DEVICE_TYPE_LEN 64
161 #define SOUND_SAMPLE_FORMAT_LEN 12
162 #define SOUND_DUCKING_ARR_MAX 128
164 #define DIRECTION_OUT_STR "out"
165 #define SOUND_TYPE_MASTER_STR "master"
167 typedef enum _sound_stream_direction {
168 SOUND_STREAM_DIRECTION_OUTPUT = 1,
169 SOUND_STREAM_DIRECTION_INPUT
170 } sound_stream_direction_e;
172 /* it should be synchronized with pulseaudio's */
173 typedef enum stream_route_type {
174 STREAM_ROUTE_TYPE_AUTO, /* A stream is routed automatically to a particular device which has the highest priority. */
175 STREAM_ROUTE_TYPE_AUTO_LAST_CONNECTED,/* A stream is routed automatically to a particular device which has the latest connection time. */
176 STREAM_ROUTE_TYPE_AUTO_ALL, /* A stream is routed automatically to several devices simultaneously. */
177 STREAM_ROUTE_TYPE_MANUAL, /* A stream is routed manually to the device(s) selected by user. */
178 STREAM_ROUTE_TYPE_MANUAL_EXT, /* A stream is routed manually to the external device(s) selected by user. */
180 #define AVAIL_DEVICES_MAX 16
181 #define AVAIL_FRAMEWORKS_MAX 16
183 typedef struct _stream_conf_info_s {
187 char *avail_in_devices[AVAIL_DEVICES_MAX];
188 char *avail_out_devices[AVAIL_DEVICES_MAX];
189 char *avail_frameworks[AVAIL_FRAMEWORKS_MAX];
190 } stream_conf_info_s;
192 typedef struct _manual_route_info_s {
193 unsigned int route_in_devices[AVAIL_DEVICES_MAX];
194 unsigned int route_out_devices[AVAIL_DEVICES_MAX];
196 } manual_route_info_s;
198 typedef struct _sound_pa_info_s {
199 pa_threaded_mainloop *mainloop;
204 typedef struct _preferred_device_info_s {
207 } preferred_device_info_s;
209 typedef struct _sound_stream_info_s {
212 bool is_focus_unavailable;
214 sound_pa_info_s pa_info;
215 stream_conf_info_s stream_conf_info;
216 unsigned int prev_acquired_focus;
217 unsigned int acquired_focus;
218 sound_stream_focus_state_changed_cb user_cb;
220 manual_route_info_s manual_route_info;
221 preferred_device_info_s preferred_device_info;
222 pthread_mutex_t focus_state_mutex;
223 pthread_mutex_t focus_cb_mutex;
224 } sound_stream_info_s;
226 typedef struct _sound_stream_ducking_s {
229 unsigned int duration;
231 sound_pa_info_s pa_info;
232 sound_stream_ducking_state_changed_cb user_cb;
234 } sound_stream_ducking_s;
237 _VSTREAM_STATE_READY,
238 _VSTREAM_STATE_RUNNING,
241 typedef struct _virtual_stream_info_s {
242 _vstream_state state;
244 pa_threaded_mainloop *pa_mainloop;
245 pa_context *pa_context;
246 pa_stream *pa_stream[SOUND_STREAM_DIRECTION_MAX];
247 pa_proplist *pa_proplist;
248 sound_stream_info_s *stream_info;
249 } virtual_sound_stream_info_s;
254 sound_stream_focus_state_watch_cb user_cb;
255 } _focus_watch_info_s;
257 void _voip_focus_state_change_callback(sound_stream_info_h stream_info,
258 sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state,
259 sound_stream_focus_change_reason_e reason, int sound_behavior, const char *extra_info, void *user_data);
261 void _focus_state_change_callback(int index, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state, const char *reason, int option, const char *extra_info, void *user_data);
263 void _focus_watch_callback(int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state, const char *reason, const char *extra_info, void *user_data);
265 int _convert_error_from_dbus_error(const char *error_msg);
267 int _convert_sound_manager_error_code(const char *func, int code);
269 int _convert_stream_type(sound_stream_type_e enum_type, char **stream_type);
271 int _convert_stream_type_for_internal(sound_stream_type_internal_e stream_type_enum, char **stream_type);
273 void _set_focus_availability(sound_stream_info_s *stream_info);
275 int _convert_stream_type_to_change_reason(const char *stream_type, sound_stream_focus_change_reason_e *change_reason);
277 int _convert_device_type_enum_to_str(sound_device_type_e device_type, char **device_type_str);
279 int _convert_device_type(mm_sound_device_type_e device_type, sound_device_type_e *sound_device_type);
281 int _convert_device_type_str_to_enum(const char *device_type_str, sound_device_type_e *device_type);
283 int _convert_device_io_direction(mm_sound_device_io_direction_e io_direction, sound_device_io_direction_e *sound_io_direction);
285 const char* _convert_api_name(native_api_e api_name);
287 int _return_val_if_not_usb_device(sound_device_h device, int ret_val);
289 int _get_stream_conf_info(const char *stream_type, stream_conf_info_s *info);
291 int _set_manual_route_info(unsigned int index, manual_route_info_s *info);
293 int _set_route_option(unsigned int index, const char *key, int value);
295 int _convert_sound_type(sound_type_e sound_type, const char **volume_type);
297 int _convert_sound_type_to_enum(const char *sound_type, sound_type_e *sound_type_enum);
299 int _get_volume_max_level(const char *direction, const char *volume_type, unsigned int *max_level);
301 int _get_volume_level(const char *direction, const char *volume_type, unsigned int *level);
303 int _set_volume_level(const char *direction, const char *volume_type, unsigned int level);
305 int _get_current_volume_type(const char *direction, char **volume_type);
307 int _get_current_media_routing_path(const char *direction, sound_device_type_e *device_type);
309 void _update_focus_status(unsigned int index, unsigned int acquired_focus_status);
311 int _is_device_running_by_id(int device_id, bool *is_running);
313 int _get_supported_sample_formats(int device_id, sound_sample_format_e **formats, unsigned int *num);
315 int _set_sample_format(int device_id, sound_sample_format_e format);
317 int _get_sample_format(int device_id, sound_sample_format_e *format);
319 int _get_supported_sample_rates(int device_id, sound_sample_rate_e **rates, unsigned int *num);
321 int _set_sample_rate(int device_id, sound_sample_rate_e rate);
323 int _get_sample_rate(int device_id, sound_sample_rate_e *rate);
325 int _set_avoid_resampling(int device_id, bool enable);
327 int _get_avoid_resampling(int device_id, bool *enabled);
329 int _set_media_stream_only(int device_id, bool enable);
331 int _get_media_stream_only(int device_id, bool *enabled);
333 void _pa_context_state_cb(pa_context *c, void *userdata);
335 void _pa_stream_state_cb(pa_stream *s, void * userdata);
337 int _make_pa_connection(sound_pa_info_s *pa_info, const char *context_name);
339 int _make_pa_connection_and_register_focus(sound_stream_info_s *stream_h, sound_stream_focus_state_changed_cb callback, void *user_data);
341 void _destroy_pa_connection(sound_pa_info_s *pa_info);
343 int _destroy_pa_connection_and_unregister_focus(sound_stream_info_s *stream_h);
345 int _add_device_for_stream_routing(sound_stream_info_s *stream_info, sound_device_h device);
347 int _remove_device_for_stream_routing(sound_stream_info_s *stream_info, sound_device_h device);
349 int _remove_all_devices_for_stream_routing(sound_stream_info_s *stream_info);
351 int _add_device_id_for_stream_routing(sound_stream_info_s *stream_info, int device_id);
353 int _remove_device_id_for_stream_routing(sound_stream_info_s *stream_info, int device_id);
355 int _apply_stream_routing(sound_stream_info_s *stream_info);
357 int _create_virtual_stream(sound_stream_info_s *stream_info, virtual_sound_stream_info_s **virtual_stream);
359 int _destroy_virtual_stream(virtual_sound_stream_info_s *virtual_stream);
361 int _start_virtual_stream(virtual_sound_stream_info_s *virtual_stream);
363 int _stop_virtual_stream(virtual_sound_stream_info_s *virtual_stream);
365 int _set_volume_ratio(uint32_t stream_index, sound_stream_direction_e direction, double ratio);
367 int _set_virtual_stream_volume(virtual_sound_stream_info_s *virtual_stream, double ratio);
369 int _set_acm_master_mode(bool on);
371 int _activate_ducking(uint32_t stream_index, bool enable, const char *target_stream, uint32_t duration, double ratio);
373 int _get_ducking_state(uint32_t stream_index, bool *is_ducked);
375 int _set_preferred_device(sound_stream_info_s *stream_info, sound_device_io_direction_e direction, sound_device_h device);
377 int _get_preferred_device(sound_stream_info_s *stream_info, int *in_device_id, int *out_device_id);
383 #endif /* __TIZEN_MEDIA_SOUND_MANAGER_PRIVATE_H__ */