Divide case for registering focus node depending on session backward compatibility
[platform/core/api/sound-manager.git] / include / sound_manager_private.h
1 /*
2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #ifndef __TIZEN_MEDIA_SOUND_MANAGER_PRIVATE_H__
18 #define __TIZEN_MEDIA_SOUND_MANAGER_PRIVATE_H__
19
20 #ifdef __cplusplus
21 extern "C"
22 {
23 #endif
24 #define LOG_TAG "TIZEN_N_SOUND_MANAGER"
25
26 #include <stdio.h>
27 #include <limits.h>
28 #include <string.h>
29 #include <malloc.h>
30 #include <unistd.h>
31 #include <dlog.h>
32 #include <pulse/error.h>
33 #include <pulse/proplist.h>
34 #include <pulse/channelmap.h>
35 #include <pulse/pulseaudio.h>
36
37 #include <gio/gio.h>
38 #include <glib.h>
39 #include <mm_session.h>
40 #include <mm_session_private.h>
41 #include <mm_sound.h>
42 #include <mm_sound_focus.h>
43 #include <mm_sound_private.h>
44 #include "sound_manager_internal.h"
45
46 #define _CHECK_CONDITION(condition, error, msg)     \
47 if (condition) { \
48 } else { \
49         LOGE("[%s] %s(0x%08x)", __FUNCTION__, msg, error); \
50         return error; \
51 }; \
52
53 #define SM_INSTANCE_CHECK(handle) \
54 _CHECK_CONDITION(handle != NULL, SOUND_MANAGER_ERROR_INVALID_PARAMETER, "SOUND_MANAGER_ERROR_INVALID_PARAMETER")
55
56 #define SM_NULL_ARG_CHECK(arg) \
57 _CHECK_CONDITION(arg != NULL, SOUND_MANAGER_ERROR_INVALID_PARAMETER, "SOUND_MANAGER_ERROR_INVALID_PARAMETER")
58
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")
61
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")
65
66 #define SM_INSTANCE_CHECK_FOR_PRIV(handle) \
67 _CHECK_CONDITION(handle != NULL, MM_ERROR_INVALID_ARGUMENT, "MM_ERROR_INVALID_ARGUMENT")
68
69 #define SM_NULL_ARG_CHECK_FOR_PRIV(arg) \
70 _CHECK_CONDITION(arg != NULL, MM_ERROR_INVALID_ARGUMENT, "MM_ERROR_INVALID_ARGUMENT")
71
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")
74
75 #define SM_ENTER_CRITICAL_SECTION_WITH_RETURN(x_mutex, x_return) \
76 switch (pthread_mutex_lock(x_mutex)) { \
77 case EINVAL: \
78         LOGW("try mutex init..\n"); \
79         if (0 > pthread_mutex_init(x_mutex, NULL)) { \
80                 return x_return; \
81         } else { \
82                 break; \
83         } \
84         return x_return; \
85 case 0: \
86         break; \
87 default: \
88         LOGE("mutex lock failed\n"); \
89         return x_return; \
90 }
91
92 #define SM_LEAVE_CRITICAL_SECTION(x_mutex) \
93 if (pthread_mutex_unlock(x_mutex)) { \
94         LOGE("mutex unlock failed\n"); \
95 }
96
97 #define SM_REF_FOR_STREAM_INFO(x_count, x_return) \
98 { \
99         if (!x_count) { \
100                 /* send signal to other framework to release internal focus */ \
101                 x_return = mm_sound_send_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, 1); \
102         } \
103         if (x_return) \
104                 LOGW("failed to send signal for stream info creation"); \
105         else \
106                 g_stream_info_count++; \
107 } \
108
109 #define SM_UNREF_FOR_STREAM_INFO(x_count) \
110 { \
111         if (x_count > 0) \
112                 x_count--; \
113 } \
114
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
120
121 #define DIRECTION_OUT_STR         "out"
122 #define SOUND_TYPE_MASTER_STR     "master"
123
124 typedef enum _sound_stream_direction {
125         SOUND_STREAM_DIRECTION_OUTPUT = 1,
126         SOUND_STREAM_DIRECTION_INPUT
127 } sound_stream_direction_e;
128
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. */
136 } stream_route_type;
137 #define AVAIL_DEVICES_MAX 16
138 #define AVAIL_FRAMEWORKS_MAX 16
139
140 typedef struct _stream_conf_info_s {
141         int priority;
142         int route_type;
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;
148
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];
152         bool is_set;
153 } manual_route_info_s;
154
155 typedef struct _sound_stream_info_s {
156         unsigned int index;
157         char *stream_type;
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;
164         void *user_data;
165         manual_route_info_s manual_route_info;
166 } sound_stream_info_s;
167
168 typedef enum {
169         _VSTREAM_STATE_READY,
170         _VSTREAM_STATE_RUNNING,
171 } _vstream_state;
172
173 typedef struct _virtual_stream_info_s {
174         _vstream_state state;
175         char *stream_type;
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;
182
183 typedef enum {
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 */
189 } _session_mode_e;
190
191 typedef struct {
192         int is_registered;
193         unsigned int subs_id; /* for internal device connected subscription */
194         void *user_data;
195         sound_session_interrupted_cb user_cb;
196 } _session_interrupt_info_s;
197
198 typedef struct {
199         int id;
200         void *user_data;
201         sound_stream_focus_state_watch_cb user_cb;
202 } _focus_watch_info_s;
203
204 typedef struct {
205         unsigned int subs_id;
206         void *user_data;
207         sound_manager_volume_changed_cb user_cb;
208 } _volume_changed_info_s;
209
210 typedef struct {
211         unsigned int subs_id;
212         void *user_data;
213         sound_device_connected_cb user_cb;
214 } _device_connected_info_s;
215
216 typedef struct {
217         unsigned int subs_id;
218         void *user_data;
219         sound_device_information_changed_cb user_cb;
220 } _device_changed_info_s;
221
222 void _focus_session_interrupt_cb(mm_sound_focus_state_e state, const char *reason, bool is_wcb, void *user_data);
223
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);
227
228 void _device_connected_cb(sound_device_h device, bool is_connected, void *user_data);
229
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);
231
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);
233
234 int _convert_sound_manager_error_code(const char *func, int code);
235
236 int _convert_stream_type(sound_stream_type_e enum_type, char **stream_type);
237
238 int _convert_stream_type_for_internal(sound_stream_type_internal_e stream_type_enum, char **stream_type);
239
240 void _set_focus_availability(sound_stream_info_s *stream_info);
241
242 int _convert_stream_type_to_change_reason(const char *stream_type, sound_stream_focus_change_reason_e *change_reason);
243
244 int _convert_device_type_enum_to_str(sound_device_type_e device_type, char **device_type_str);
245
246 int _convert_device_type(mm_sound_device_type_e device_type, sound_device_type_e *sound_device_type);
247
248 int _convert_device_type_str_to_enum(const char *device_type_str, sound_device_type_e *device_type);
249
250 int _convert_device_io_direction(mm_sound_device_io_direction_e io_direction, sound_device_io_direction_e *sound_io_direction);
251
252 const char* _convert_api_name(native_api_e api_name);
253
254 int _get_stream_conf_info(const char *stream_type, stream_conf_info_s *info);
255
256 int _set_manual_route_info(unsigned int index, manual_route_info_s *info);
257
258 int _set_route_option(unsigned int index, const char *key, int value);
259
260 int _convert_sound_type(sound_type_e sound_type, const char **volume_type);
261
262 int _convert_sound_type_to_enum(const char *sound_type, sound_type_e *sound_type_enum);
263
264 int _get_volume_max_level(const char *direction, const char *volume_type, unsigned int *max_level);
265
266 int _get_volume_level(const char *direction, const char *volume_type, unsigned int *level);
267
268 int _set_volume_level(const char *direction, const char *volume_type, unsigned int level);
269
270 int _get_current_volume_type(const char *direction, char **volume_type);
271
272 int _get_current_media_routing_path(const char *direction, sound_device_type_e *device_type);
273
274 void _update_focus_status(unsigned int index, unsigned int acquired_focus_status);
275
276 void _pa_context_state_cb(pa_context *c, void *userdata);
277
278 void _pa_stream_state_cb(pa_stream *s, void * userdata);
279
280 int _set_session_mode(_session_mode_e mode);
281
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);
283
284 int _destroy_pa_connection_and_unregister_focus(sound_stream_info_s *stream_h);
285
286 int _add_device_for_stream_routing(sound_stream_info_s *stream_info, sound_device_h device);
287
288 int _remove_device_for_stream_routing(sound_stream_info_s *stream_info, sound_device_h device);
289
290 int _apply_stream_routing(sound_stream_info_s *stream_info);
291
292 int _create_virtual_stream(sound_stream_info_s *stream_info, virtual_sound_stream_info_s **virtual_stream);
293
294 int _destroy_virtual_stream(virtual_sound_stream_info_s *virtual_stream);
295
296 int _start_virtual_stream(virtual_sound_stream_info_s *virtual_stream);
297
298 int _stop_virtual_stream(virtual_sound_stream_info_s *virtual_stream);
299
300 #ifdef __cplusplus
301 }
302 #endif
303
304 #endif /* __TIZEN_MEDIA_SOUND_MANAGER_PRIVATE_H__ */