2 * Copyright (c) 2016 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.
18 #include <restriction.h>
19 #include "media_streamer.h"
20 #include "media_streamer_gst.h"
21 #include "media_streamer_priv.h"
22 #include "media_streamer_node.h"
23 #include "media_streamer_node_policy.h"
26 #define DPM_DISALLOWED 0
29 DPM_POLICY_FOR_CAMERA,
34 static void __ms_node_get_dpm_check_needed(media_streamer_node_s *node, media_streamer_policy_type_e *policy)
40 ms_retm_if(node == NULL, "node is NULL");
41 ms_retm_if(policy == NULL, "policy is NULL");
43 subtype = node->subtype;
45 ms_debug("Checking DPM policy for node type %d, subtype %d", node->type, subtype);
47 /* Check for node types */
49 case MEDIA_STREAMER_NODE_SRC_TYPE_CAMERA:
50 case MEDIA_STREAMER_NODE_SRC_TYPE_VIDEO_CAPTURE:
51 *policy = POLICY_TYPE_CAMERA;
53 case MEDIA_STREAMER_NODE_SRC_TYPE_AUDIO_CAPTURE:
54 *policy = POLICY_TYPE_MIC;
57 *policy = POLICY_TYPE_NONE;
66 static void __ms_node_policy_changed_cb(const char *name, const char *value, void *user_data)
68 media_streamer_node_s *node;
69 media_streamer_s *streamer;
73 ms_retm_if(user_data == NULL, "user_data is NULL");
75 node = (media_streamer_node_s *) user_data;
76 streamer = (media_streamer_s *) node->parent_streamer;
77 ms_info("Received policy_changed_cb from node [%s]", node->name);
79 /* Here we perform action to release resources that
80 were previously acquired. Basically, what we need to do is
81 to unprepare media streamer.*/
82 g_mutex_lock(&streamer->mutex_lock);
83 streamer->is_interrupted = TRUE;
84 if (MEDIA_STREAMER_ERROR_NONE !=
85 ms_pipeline_unprepare(streamer)) {
86 ms_error("Failed to unprepare streamer");
87 streamer->is_interrupted = FALSE;
88 g_mutex_unlock(&streamer->mutex_lock);
91 streamer->is_interrupted = FALSE;
93 /* Call interrupted_cb with appropriate code */
94 media_streamer_interrupted_cb interrupted_cb =
95 (media_streamer_interrupted_cb) streamer->interrupted_cb.callback;
97 interrupted_cb(MEDIA_STREAMER_INTERRUPTED_BY_SECURITY,
98 streamer->interrupted_cb.user_data);
100 ms_info("Interruption will not be handled because interrupted_cb is NULL");
102 g_mutex_unlock(&streamer->mutex_lock);
109 int ms_node_policy_init(media_streamer_node_s *node)
111 media_streamer_policy_type_e policy = POLICY_TYPE_NONE;
112 int ret = MEDIA_STREAMER_ERROR_NONE;
113 const char *policy_name = NULL;
114 static const char* policy_str[DPM_POLICY_MAX] = {"camera", "microphone"};
118 ms_retvm_if(node == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "node is NULL");
120 /* Check if node require policy manager */
121 __ms_node_get_dpm_check_needed(node, &policy);
122 if (POLICY_TYPE_NONE == policy) {
123 ms_info("No policy is needed for %p node type [%d] subtype [%d]",
124 node, node->type, node->subtype);
125 return MEDIA_STREAMER_ERROR_NONE;
129 case POLICY_TYPE_CAMERA:
130 policy_name = policy_str[DPM_POLICY_FOR_CAMERA];
132 case POLICY_TYPE_MIC:
133 policy_name = policy_str[DPM_POLICY_FOR_MIC];
138 /* Initialize policy manager */
139 node->dpm_handle = dpm_manager_create();
140 if (node->dpm_handle) {
141 int dpm_ret = DPM_ERROR_NONE;
142 dpm_ret = dpm_add_policy_changed_cb(node->dpm_handle, policy_name,
143 __ms_node_policy_changed_cb, (void *)node, &node->policy_changed_cb_id);
145 if (dpm_ret != DPM_ERROR_NONE) {
146 ms_error("add DPM changed cb failed, keep going");
147 node->policy_changed_cb_id = 0;
149 ms_debug("DPM initialized");
157 int ms_node_policy_deinit(media_streamer_node_s *node)
159 media_streamer_policy_type_e policy = POLICY_TYPE_NONE;
160 int ret = MEDIA_STREAMER_ERROR_NONE;
164 ms_retvm_if(node == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "node is NULL");
166 __ms_node_get_dpm_check_needed(node, &policy);
167 if (POLICY_TYPE_NONE == policy) {
168 ms_info("No need to check DPM restriction state for node [%p] type [%d] subtype [%d]",
169 node, node->type, node->subtype);
170 return MEDIA_STREAMER_ERROR_NONE;
173 ms_retvm_if(node->dpm_handle == NULL, MEDIA_STREAMER_ERROR_INVALID_OPERATION, "DPM handle is NULL");
175 if (node->policy_changed_cb_id > 0)
176 dpm_remove_policy_changed_cb(node->dpm_handle, node->policy_changed_cb_id);
178 ms_info("invalid dpm cb id");
180 ms_debug("DPM released");
182 dpm_manager_destroy(node->dpm_handle);
183 node->dpm_handle = NULL;
190 int ms_node_policy_check(media_streamer_node_s *node, gboolean *allowed)
192 media_streamer_policy_type_e policy = POLICY_TYPE_NONE;
193 int dpm_state = DPM_ALLOWED;
194 int dpm_ret = DPM_ERROR_NONE;
198 ms_retvm_if(node == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "node is NULL");
199 ms_retvm_if(allowed == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "allowed is NULL");
203 __ms_node_get_dpm_check_needed(node, &policy);
204 if (POLICY_TYPE_NONE == policy) {
205 ms_info("No need to check DPM restriction state for node [%p] type [%d] subtype [%d]",
206 node, node->type, node->subtype);
207 return MEDIA_STREAMER_ERROR_NONE;
210 ms_retvm_if(node->dpm_handle == NULL, MEDIA_STREAMER_ERROR_INVALID_OPERATION, "DPM handle is NULL");
213 case POLICY_TYPE_CAMERA:
214 dpm_ret = dpm_restriction_get_camera_state(node->dpm_handle, &dpm_state);
216 case POLICY_TYPE_MIC:
217 dpm_ret = dpm_restriction_get_microphone_state(node->dpm_handle, &dpm_state);
223 if (dpm_ret != DPM_ERROR_NONE) {
224 ms_error("Failed to get DPM restriction state");
225 return MEDIA_STREAMER_ERROR_INVALID_OPERATION;
228 ms_info("DPM state - %d", dpm_state);
229 if (dpm_state == DPM_DISALLOWED) {
230 ms_error("Policy disallowed by DPM");
236 return MEDIA_STREAMER_ERROR_NONE;