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 SM_SAFE_FREE(x_val) do { \
122 #define SOUND_SESSION_TYPE_DEFAULT SOUND_SESSION_TYPE_MEDIA
123 #define SOUND_STREAM_INFO_ARR_MAX 128
124 #define SOUND_STREAM_TYPE_LEN 64
125 #define SOUND_STREAM_DIRECTION_MAX 2
126 #define SOUND_DEVICE_TYPE_LEN 64
128 #define DIRECTION_OUT_STR "out"
129 #define SOUND_TYPE_MASTER_STR "master"
131 typedef enum _sound_stream_direction {
132 SOUND_STREAM_DIRECTION_OUTPUT = 1,
133 SOUND_STREAM_DIRECTION_INPUT
134 } sound_stream_direction_e;
136 /* it should be synchronized with pulseaudio's */
137 typedef enum stream_route_type {
138 STREAM_ROUTE_TYPE_AUTO, /* A stream is routed automatically to a particular device which has the highest priority. */
139 STREAM_ROUTE_TYPE_AUTO_LAST_CONNECTED,/* A stream is routed automatically to a particular device which has the latest connection time. */
140 STREAM_ROUTE_TYPE_AUTO_ALL, /* A stream is routed automatically to several devices simultaneously. */
141 STREAM_ROUTE_TYPE_MANUAL, /* A stream is routed manually to the device(s) selected by user. */
142 STREAM_ROUTE_TYPE_MANUAL_EXT, /* A stream is routed manually to the external device(s) selected by user. */
144 #define AVAIL_DEVICES_MAX 16
145 #define AVAIL_FRAMEWORKS_MAX 16
147 typedef struct _stream_conf_info_s {
151 char *avail_in_devices[AVAIL_DEVICES_MAX];
152 char *avail_out_devices[AVAIL_DEVICES_MAX];
153 char *avail_frameworks[AVAIL_FRAMEWORKS_MAX];
154 } stream_conf_info_s;
156 typedef struct _manual_route_info_s {
157 unsigned int route_in_devices[AVAIL_DEVICES_MAX];
158 unsigned int route_out_devices[AVAIL_DEVICES_MAX];
160 } manual_route_info_s;
162 typedef struct _sound_stream_info_s {
165 bool is_focus_unavailable;
166 pa_threaded_mainloop *pa_mainloop;
167 pa_context *pa_context;
168 stream_conf_info_s stream_conf_info;
169 unsigned int acquired_focus;
170 sound_stream_focus_state_changed_cb user_cb;
172 manual_route_info_s manual_route_info;
173 } sound_stream_info_s;
176 _VSTREAM_STATE_READY,
177 _VSTREAM_STATE_RUNNING,
180 typedef struct _virtual_stream_info_s {
181 _vstream_state state;
183 pa_threaded_mainloop *pa_mainloop;
184 pa_context *pa_context;
185 pa_stream *pa_stream[SOUND_STREAM_DIRECTION_MAX];
186 pa_proplist *pa_proplist;
187 sound_stream_info_s *stream_info;
188 } virtual_sound_stream_info_s;
191 _SESSION_MODE_RINGTONE = 0, /**< session mode(voip/call) for ringtone */
192 _SESSION_MODE_VOICE_WITH_BUILTIN_RECEIVER, /**< session mode(voip/call) for during call with built-in receiver */
193 _SESSION_MODE_VOICE_WITH_BUILTIN_SPEAKER, /**< session mode(voip/call) for during call with built-in speaker */
194 _SESSION_MODE_VOICE_WITH_AUDIO_JACK, /**< session mode(voip/call) for during call with audio jack */
195 _SESSION_MODE_VOICE_WITH_BLUETOOTH_SCO, /**< session mode(voip/call) for during call with bluetooth */
200 unsigned int subs_id; /* for internal device connected subscription */
202 sound_session_interrupted_cb user_cb;
203 } _session_interrupt_info_s;
208 sound_stream_focus_state_watch_cb user_cb;
209 } _focus_watch_info_s;
212 unsigned int subs_id;
214 sound_manager_volume_changed_cb user_cb;
215 } _volume_changed_info_s;
218 unsigned int subs_id;
220 sound_device_connected_cb user_cb;
221 } _device_connected_info_s;
224 unsigned int subs_id;
226 sound_device_information_changed_cb user_cb;
227 } _device_changed_info_s;
229 void _focus_session_interrupt_cb(mm_sound_focus_state_e state, const char *reason, bool is_wcb, void *user_data);
231 void _voip_focus_state_change_callback(sound_stream_info_h stream_info,
232 sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state,
233 sound_stream_focus_change_reason_e reason, int sound_behavior, const char *extra_info, void *user_data);
235 void _device_connected_cb(sound_device_h device, bool is_connected, void *user_data);
237 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);
239 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);
241 int _convert_sound_manager_error_code(const char *func, int code);
243 int _convert_stream_type(sound_stream_type_e enum_type, char **stream_type);
245 int _convert_stream_type_for_internal(sound_stream_type_internal_e stream_type_enum, char **stream_type);
247 void _set_focus_availability(sound_stream_info_s *stream_info);
249 int _convert_stream_type_to_change_reason(const char *stream_type, sound_stream_focus_change_reason_e *change_reason);
251 int _convert_device_type_enum_to_str(sound_device_type_e device_type, char **device_type_str);
253 int _convert_device_type(mm_sound_device_type_e device_type, sound_device_type_e *sound_device_type);
255 int _convert_device_type_str_to_enum(const char *device_type_str, sound_device_type_e *device_type);
257 int _convert_device_io_direction(mm_sound_device_io_direction_e io_direction, sound_device_io_direction_e *sound_io_direction);
259 const char* _convert_api_name(native_api_e api_name);
261 int _get_stream_conf_info(const char *stream_type, stream_conf_info_s *info);
263 int _set_manual_route_info(unsigned int index, manual_route_info_s *info);
265 int _set_route_option(unsigned int index, const char *key, int value);
267 int _convert_sound_type(sound_type_e sound_type, const char **volume_type);
269 int _convert_sound_type_to_enum(const char *sound_type, sound_type_e *sound_type_enum);
271 int _get_volume_max_level(const char *direction, const char *volume_type, unsigned int *max_level);
273 int _get_volume_level(const char *direction, const char *volume_type, unsigned int *level);
275 int _set_volume_level(const char *direction, const char *volume_type, unsigned int level);
277 int _get_current_volume_type(const char *direction, char **volume_type);
279 int _get_current_media_routing_path(const char *direction, sound_device_type_e *device_type);
281 void _update_focus_status(unsigned int index, unsigned int acquired_focus_status);
283 void _pa_context_state_cb(pa_context *c, void *userdata);
285 void _pa_stream_state_cb(pa_stream *s, void * userdata);
287 int _set_session_mode(_session_mode_e mode);
289 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);
291 int _destroy_pa_connection_and_unregister_focus(sound_stream_info_s *stream_h);
293 int _add_device_for_stream_routing(sound_stream_info_s *stream_info, sound_device_h device);
295 int _remove_device_for_stream_routing(sound_stream_info_s *stream_info, sound_device_h device);
297 int _apply_stream_routing(sound_stream_info_s *stream_info);
299 int _create_virtual_stream(sound_stream_info_s *stream_info, virtual_sound_stream_info_s **virtual_stream);
301 int _destroy_virtual_stream(virtual_sound_stream_info_s *virtual_stream);
303 int _start_virtual_stream(virtual_sound_stream_info_s *virtual_stream);
305 int _stop_virtual_stream(virtual_sound_stream_info_s *virtual_stream);
307 int _make_progress_virtual_stream(sound_stream_info_s *stream_info, virtual_sound_stream_info_s **virtual_stream);
309 int _stop_progress_virtual_stream(virtual_sound_stream_info_s *virtual_stream);
315 #endif /* __TIZEN_MEDIA_SOUND_MANAGER_PRIVATE_H__ */