Divide case for registering focus node depending on session backward compatibility
[platform/core/api/sound-manager.git] / src / sound_manager_internal.c
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 #include <sound_manager.h>
18 #include <sound_manager_private.h>
19 #include <sound_manager_internal.h>
20 #include <mm_sound.h>
21
22 /* These variables will be removed when session features are deprecated. */
23 extern int g_stream_info_count;
24 extern pthread_mutex_t g_stream_info_count_mutex;
25
26 #ifndef TIZEN_FEATURE_TV_PROD
27 int sound_manager_get_max_master_volume(int *max_level)
28 {
29         int ret = MM_ERROR_NONE;
30         unsigned int volume_level = 0;
31
32         LOGI(">> enter");
33
34         SM_NULL_ARG_CHECK(max_level);
35
36         ret = _get_volume_max_level(DIRECTION_OUT_STR, SOUND_TYPE_MASTER_STR, &volume_level);
37         if (ret == MM_ERROR_NONE)
38                 *max_level = (int)volume_level;
39
40         return _convert_sound_manager_error_code(__func__, ret);
41 }
42
43 int sound_manager_set_master_volume(int level)
44 {
45         int ret = MM_ERROR_NONE;
46
47         LOGI(">> enter");
48
49         ret = _set_volume_level(DIRECTION_OUT_STR, SOUND_TYPE_MASTER_STR, (unsigned int)level);
50
51         return _convert_sound_manager_error_code(__func__, ret);
52 }
53
54 int sound_manager_get_master_volume(int *level)
55 {
56         int ret = MM_ERROR_NONE;
57         unsigned int volume_level = 0;
58
59         LOGI(">> enter");
60
61         SM_NULL_ARG_CHECK(level);
62
63         ret = _get_volume_level(DIRECTION_OUT_STR, SOUND_TYPE_MASTER_STR, &volume_level);
64         if (ret == MM_ERROR_NONE)
65                 *level = (int)volume_level;
66
67         return _convert_sound_manager_error_code(__func__, ret);
68 }
69 #endif
70
71 int sound_manager_create_stream_information_internal(sound_stream_type_internal_e stream_type, sound_stream_focus_state_changed_cb callback, void *user_data, sound_stream_info_h *stream_info)
72 {
73         int ret = MM_ERROR_NONE;
74
75         LOGI(">> enter");
76
77         SM_NULL_ARG_CHECK(stream_info);
78
79         SM_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_stream_info_count_mutex, MM_ERROR_SOUND_INTERNAL);
80
81         sound_stream_info_s *stream_h = malloc(sizeof(sound_stream_info_s));
82         if (!stream_h) {
83                 ret = MM_ERROR_OUT_OF_MEMORY;
84                 goto LEAVE;
85         }
86
87         memset(stream_h, 0, sizeof(sound_stream_info_s));
88         ret = _convert_stream_type_for_internal(stream_type, &stream_h->stream_type);
89         if (ret == MM_ERROR_NONE) {
90                 _set_focus_availability(stream_h);
91                 ret = _make_pa_connection_and_register_focus(stream_h, false, callback, user_data);
92                 if (!ret) {
93                         *stream_info = (sound_stream_info_h)stream_h;
94                         LOGI("stream_h(%p), index(%u), user_cb(%p), ret(0x%x)", stream_h, stream_h->index, stream_h->user_cb, ret);
95                 }
96         }
97
98 LEAVE:
99         if (ret && stream_h)
100                 free(stream_h);
101
102         SM_LEAVE_CRITICAL_SECTION(&g_stream_info_count_mutex);
103
104         return _convert_sound_manager_error_code(__func__, ret);
105 }
106
107 int sound_manager_set_stream_routing_option(sound_stream_info_h stream_info, const char *name, int value)
108 {
109         int ret = MM_ERROR_NONE;
110         sound_stream_info_s *stream_h = (sound_stream_info_s*)stream_info;
111
112         LOGI(">> enter");
113
114         SM_INSTANCE_CHECK(stream_h);
115         SM_NULL_ARG_CHECK(name);
116
117         ret = _set_route_option(stream_h->index, name, value);
118
119         return _convert_sound_manager_error_code(__func__, ret);
120 }
121
122 int sound_manager_is_available_stream_information(sound_stream_info_h stream_info, native_api_e api_name, bool *is_available)
123 {
124         int ret = MM_ERROR_NONE;
125         int i = 0;
126         const char *name = NULL;
127         sound_stream_info_s* stream_h = (sound_stream_info_s*)stream_info;
128
129         SM_INSTANCE_CHECK(stream_h);
130         SM_NULL_ARG_CHECK(is_available);
131         *is_available = false;
132
133         name = _convert_api_name(api_name);
134         for (i = 0; i < AVAIL_FRAMEWORKS_MAX; i++) {
135                 if (stream_h->stream_conf_info.avail_frameworks[i] && !strncmp(stream_h->stream_conf_info.avail_frameworks[i], name, strlen(name))) {
136                         *is_available = true;
137                         break;
138                 }
139         }
140         LOGI("stream_type[%s], native api[%s], is_available[%d]", stream_h->stream_type, name, *is_available);
141
142         return _convert_sound_manager_error_code(__func__, ret);
143 }
144
145 int sound_manager_get_type_from_stream_information(sound_stream_info_h stream_info, char **type)
146 {
147         int ret = MM_ERROR_NONE;
148         sound_stream_info_s* stream_h = (sound_stream_info_s*)stream_info;
149
150         SM_INSTANCE_CHECK(stream_h);
151         SM_NULL_ARG_CHECK(type);
152
153         *type = stream_h->stream_type;
154         LOGI("stream_type[%s]", *type);
155
156         return _convert_sound_manager_error_code(__func__, ret);
157 }
158 int sound_manager_get_index_from_stream_information(sound_stream_info_h stream_info, int *index)
159 {
160         int ret = MM_ERROR_NONE;
161         sound_stream_info_s* stream_h = (sound_stream_info_s*)stream_info;
162         SM_INSTANCE_CHECK(stream_h);
163         SM_NULL_ARG_CHECK(index);
164
165         *index = stream_h->index;
166         LOGI("stream_index[%d]", stream_h->index);
167
168         return _convert_sound_manager_error_code(__func__, ret);
169 }
170
171 int sound_manager_create_virtual_stream(sound_stream_info_h stream_info, virtual_sound_stream_h *virtual_stream)
172 {
173         int ret = MM_ERROR_NONE;
174         virtual_sound_stream_info_s *vstream_h = NULL;
175         sound_stream_info_s *stream_h = (sound_stream_info_s*)stream_info;
176
177         LOGI(">> enter");
178
179         ret = _create_virtual_stream(stream_h, &vstream_h);
180         if (ret == MM_ERROR_NONE)
181                 *virtual_stream = (virtual_sound_stream_h)vstream_h;
182
183         return _convert_sound_manager_error_code(__func__, ret);
184 }
185
186 int sound_manager_destroy_virtual_stream(virtual_sound_stream_h virtual_stream)
187 {
188         int ret = MM_ERROR_NONE;
189         virtual_sound_stream_info_s *vstream_h = (virtual_sound_stream_info_s*)virtual_stream;
190
191         LOGI(">> enter");
192
193         ret = _destroy_virtual_stream(vstream_h);
194
195         return _convert_sound_manager_error_code(__func__, ret);
196 }
197
198 int sound_manager_start_virtual_stream(virtual_sound_stream_h virtual_stream)
199 {
200         int ret = MM_ERROR_NONE;
201         virtual_sound_stream_info_s *vstream_h = (virtual_sound_stream_info_s*)virtual_stream;
202
203         LOGI(">> enter");
204
205         ret = _start_virtual_stream(vstream_h);
206
207         return _convert_sound_manager_error_code(__func__, ret);
208 }
209
210 int sound_manager_stop_virtual_stream(virtual_sound_stream_h virtual_stream)
211 {
212         int ret = MM_ERROR_NONE;
213         virtual_sound_stream_info_s *vstream_h = (virtual_sound_stream_info_s*)virtual_stream;
214
215         LOGI(">> enter");
216
217         ret = _stop_virtual_stream(vstream_h);
218
219         return _convert_sound_manager_error_code(__func__, ret);
220 }