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>
39 #include <mm_session.h>
40 #include <mm_session_private.h>
42 #include <mm_sound_focus.h>
43 #include <mm_sound_private.h>
44 #include "sound_manager_internal.h"
46 #define _CHECK_CONDITION(condition, error, msg) \
49 LOGE("[%s] %s(0x%08x)", __FUNCTION__, msg, error); \
53 #define SM_INSTANCE_CHECK(handle) \
54 _CHECK_CONDITION(handle != NULL, SOUND_MANAGER_ERROR_INVALID_PARAMETER, "SOUND_MANAGER_ERROR_INVALID_PARAMETER")
56 #define SM_NULL_ARG_CHECK(arg) \
57 _CHECK_CONDITION(arg != NULL, SOUND_MANAGER_ERROR_INVALID_PARAMETER, "SOUND_MANAGER_ERROR_INVALID_PARAMETER")
59 #define SM_STATE_CHECK(handle, expected_state) \
60 _CHECK_CONDITION(handle->state == expected_state, SOUND_MANAGER_ERROR_INVALID_STATE, "SOUND_MANAGER_ERROR_INVALID_STATE")
62 #define SM_RANGE_ARG_CHECK(arg, min, max) \
63 _CHECK_CONDITION(arg <= max, SOUND_MANAGER_ERROR_INVALID_PARAMETER, "SOUND_MANAGER_ERROR_INVALID_PARAMETER") \
64 _CHECK_CONDITION(arg >= min, SOUND_MANAGER_ERROR_INVALID_PARAMETER, "SOUND_MANAGER_ERROR_INVALID_PARAMETER")
66 #define SM_INSTANCE_CHECK_FOR_PRIV(handle) \
67 _CHECK_CONDITION(handle != NULL, MM_ERROR_INVALID_ARGUMENT, "MM_ERROR_INVALID_ARGUMENT")
69 #define SM_NULL_ARG_CHECK_FOR_PRIV(arg) \
70 _CHECK_CONDITION(arg != NULL, MM_ERROR_INVALID_ARGUMENT, "MM_ERROR_INVALID_ARGUMENT")
72 #define SM_STATE_CHECK_FOR_PRIV(handle, expected_state) \
73 _CHECK_CONDITION(handle->state == expected_state, MM_ERROR_SOUND_INVALID_STATE, "MM_ERROR_SOUND_INVALID_STATE")
75 #define SM_ENTER_CRITICAL_SECTION_WITH_RETURN(x_mutex, x_return) \
76 switch (pthread_mutex_lock(x_mutex)) { \
78 LOGW("try mutex init..\n"); \
79 if (0 > pthread_mutex_init(x_mutex, NULL)) { \
88 LOGE("mutex lock failed\n"); \
92 #define SM_LEAVE_CRITICAL_SECTION(x_mutex) \
93 if (pthread_mutex_unlock(x_mutex)) { \
94 LOGE("mutex unlock failed\n"); \
97 #define SM_REF_FOR_STREAM_INFO(x_count, x_return) \
100 /* send signal to other framework to release internal focus */ \
101 x_return = mm_sound_send_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, 1); \
104 LOGW("failed to send signal for stream info creation"); \
106 g_stream_info_count++; \
109 #define SM_UNREF_FOR_STREAM_INFO(x_count) \
115 #define SOUND_SESSION_TYPE_DEFAULT SOUND_SESSION_TYPE_MEDIA
116 #define SOUND_STREAM_INFO_ARR_MAX 128
117 #define SOUND_STREAM_TYPE_LEN 64
118 #define SOUND_STREAM_DIRECTION_MAX 2
119 #define SOUND_DEVICE_TYPE_LEN 64
121 #define DIRECTION_OUT_STR "out"
122 #define SOUND_TYPE_MASTER_STR "master"
124 typedef enum _sound_stream_direction {
125 SOUND_STREAM_DIRECTION_OUTPUT = 1,
126 SOUND_STREAM_DIRECTION_INPUT
127 } sound_stream_direction_e;
129 /* it should be synchronized with pulseaudio's */
130 typedef enum stream_route_type {
131 STREAM_ROUTE_TYPE_AUTO, /* A stream is routed automatically to a particular device which has the highest priority. */
132 STREAM_ROUTE_TYPE_AUTO_LAST_CONNECTED,/* A stream is routed automatically to a particular device which has the latest connection time. */
133 STREAM_ROUTE_TYPE_AUTO_ALL, /* A stream is routed automatically to several devices simultaneously. */
134 STREAM_ROUTE_TYPE_MANUAL, /* A stream is routed manually to the device(s) selected by user. */
135 STREAM_ROUTE_TYPE_MANUAL_EXT, /* A stream is routed manually to the external device(s) selected by user. */
137 #define AVAIL_DEVICES_MAX 16
138 #define AVAIL_FRAMEWORKS_MAX 16
140 typedef struct _stream_conf_info_s {
143 const gchar *volume_type;
144 gchar *avail_in_devices[AVAIL_DEVICES_MAX];
145 gchar *avail_out_devices[AVAIL_DEVICES_MAX];
146 gchar *avail_frameworks[AVAIL_FRAMEWORKS_MAX];
147 } stream_conf_info_s;
149 typedef struct _manual_route_info_s {
150 unsigned int route_in_devices[AVAIL_DEVICES_MAX];
151 unsigned int route_out_devices[AVAIL_DEVICES_MAX];
153 } manual_route_info_s;
155 typedef struct _sound_stream_info_s {
158 bool is_focus_unavailable;
159 pa_threaded_mainloop *pa_mainloop;
160 pa_context *pa_context;
161 stream_conf_info_s stream_conf_info;
162 unsigned int acquired_focus;
163 sound_stream_focus_state_changed_cb user_cb;
165 manual_route_info_s manual_route_info;
166 } sound_stream_info_s;
169 _VSTREAM_STATE_READY,
170 _VSTREAM_STATE_RUNNING,
173 typedef struct _virtual_stream_info_s {
174 _vstream_state state;
176 pa_threaded_mainloop *pa_mainloop;
177 pa_context *pa_context;
178 pa_stream *pa_stream[SOUND_STREAM_DIRECTION_MAX];
179 pa_proplist *pa_proplist;
180 sound_stream_info_s *stream_info;
181 } virtual_sound_stream_info_s;
184 _SESSION_MODE_RINGTONE = 0, /**< session mode(voip/call) for ringtone */
185 _SESSION_MODE_VOICE_WITH_BUILTIN_RECEIVER, /**< session mode(voip/call) for during call with built-in receiver */
186 _SESSION_MODE_VOICE_WITH_BUILTIN_SPEAKER, /**< session mode(voip/call) for during call with built-in speaker */
187 _SESSION_MODE_VOICE_WITH_AUDIO_JACK, /**< session mode(voip/call) for during call with audio jack */
188 _SESSION_MODE_VOICE_WITH_BLUETOOTH_SCO, /**< session mode(voip/call) for during call with bluetooth */
193 unsigned int subs_id; /* for internal device connected subscription */
195 sound_session_interrupted_cb user_cb;
196 } _session_interrupt_info_s;
201 sound_stream_focus_state_watch_cb user_cb;
202 } _focus_watch_info_s;
205 unsigned int subs_id;
207 sound_manager_volume_changed_cb user_cb;
208 } _volume_changed_info_s;
211 unsigned int subs_id;
213 sound_device_connected_cb user_cb;
214 } _device_connected_info_s;
217 unsigned int subs_id;
219 sound_device_information_changed_cb user_cb;
220 } _device_changed_info_s;
222 void _focus_session_interrupt_cb(mm_sound_focus_state_e state, const char *reason, bool is_wcb, void *user_data);
224 void _voip_focus_state_change_callback(sound_stream_info_h stream_info,
225 sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state,
226 sound_stream_focus_change_reason_e reason, int sound_behavior, const char *extra_info, void *user_data);
228 void _device_connected_cb(sound_device_h device, bool is_connected, void *user_data);
230 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);
232 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);
234 int _convert_sound_manager_error_code(const char *func, int code);
236 int _convert_stream_type(sound_stream_type_e enum_type, char **stream_type);
238 int _convert_stream_type_for_internal(sound_stream_type_internal_e stream_type_enum, char **stream_type);
240 void _set_focus_availability(sound_stream_info_s *stream_info);
242 int _convert_stream_type_to_change_reason(const char *stream_type, sound_stream_focus_change_reason_e *change_reason);
244 int _convert_device_type_enum_to_str(sound_device_type_e device_type, char **device_type_str);
246 int _convert_device_type(mm_sound_device_type_e device_type, sound_device_type_e *sound_device_type);
248 int _convert_device_type_str_to_enum(const char *device_type_str, sound_device_type_e *device_type);
250 int _convert_device_io_direction(mm_sound_device_io_direction_e io_direction, sound_device_io_direction_e *sound_io_direction);
252 const char* _convert_api_name(native_api_e api_name);
254 int _get_stream_conf_info(const char *stream_type, stream_conf_info_s *info);
256 int _set_manual_route_info(unsigned int index, manual_route_info_s *info);
258 int _set_route_option(unsigned int index, const char *key, int value);
260 int _convert_sound_type(sound_type_e sound_type, const char **volume_type);
262 int _convert_sound_type_to_enum(const char *sound_type, sound_type_e *sound_type_enum);
264 int _get_volume_max_level(const char *direction, const char *volume_type, unsigned int *max_level);
266 int _get_volume_level(const char *direction, const char *volume_type, unsigned int *level);
268 int _set_volume_level(const char *direction, const char *volume_type, unsigned int level);
270 int _get_current_volume_type(const char *direction, char **volume_type);
272 int _get_current_media_routing_path(const char *direction, sound_device_type_e *device_type);
274 void _update_focus_status(unsigned int index, unsigned int acquired_focus_status);
276 void _pa_context_state_cb(pa_context *c, void *userdata);
278 void _pa_stream_state_cb(pa_stream *s, void * userdata);
280 int _set_session_mode(_session_mode_e mode);
282 int _make_pa_connection_and_register_focus(sound_stream_info_s *stream_h, bool is_for_session, sound_stream_focus_state_changed_cb callback, void *user_data);
284 int _destroy_pa_connection_and_unregister_focus(sound_stream_info_s *stream_h);
286 int _add_device_for_stream_routing(sound_stream_info_s *stream_info, sound_device_h device);
288 int _remove_device_for_stream_routing(sound_stream_info_s *stream_info, sound_device_h device);
290 int _apply_stream_routing(sound_stream_info_s *stream_info);
292 int _create_virtual_stream(sound_stream_info_s *stream_info, virtual_sound_stream_info_s **virtual_stream);
294 int _destroy_virtual_stream(virtual_sound_stream_info_s *virtual_stream);
296 int _start_virtual_stream(virtual_sound_stream_info_s *virtual_stream);
298 int _stop_virtual_stream(virtual_sound_stream_info_s *virtual_stream);
304 #endif /* __TIZEN_MEDIA_SOUND_MANAGER_PRIVATE_H__ */