2 * Copyright (c) 2018 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 #include "cpp_audio_in_privilege.h"
18 #include "CAudioIODef.h"
20 #include <pulse/pulseaudio.h>
22 #define RECORDER_PRIVILEGE "http://tizen.org/privilege/recorder"
23 #define CLIENT_NAME "AUDIO_IO_PA_CLIENT"
26 using namespace tizen_media_audio;
29 struct PrivilegeData {
30 bool isPrivilegeAllowed;
31 pa_threaded_mainloop *paMainloop;
34 static void __contextStateChangeCb(pa_context* c, void* user_data) {
35 pa_threaded_mainloop *paMainloop = (pa_threaded_mainloop *)user_data;
39 switch (pa_context_get_state(c)) {
40 case PA_CONTEXT_READY:
41 AUDIO_IO_LOGD("The context is ready");
42 pa_threaded_mainloop_signal(paMainloop, 0);
45 case PA_CONTEXT_FAILED:
46 case PA_CONTEXT_TERMINATED:
47 AUDIO_IO_LOGD("The context is lost");
48 pa_threaded_mainloop_signal(paMainloop, 0);
51 case PA_CONTEXT_UNCONNECTED:
52 case PA_CONTEXT_CONNECTING:
53 case PA_CONTEXT_AUTHORIZING:
54 case PA_CONTEXT_SETTING_NAME:
59 static void __checkPrivilegeCb(pa_context *c, int success, void *user_data) {
60 AUDIO_IO_LOGD("pa_context[%p], success[%d], user_data[%p]", c, success, user_data);
64 PrivilegeData *prData = (PrivilegeData *)user_data;
65 prData->isPrivilegeAllowed = success ? true : false;
67 pa_threaded_mainloop_signal(prData->paMainloop, 0);
70 bool cpp_audio_in_has_record_privilege(void) {
76 prData.paMainloop = pa_threaded_mainloop_new();
77 if (!prData.paMainloop)
78 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_threaded_mainloop_new()");
80 c = pa_context_new(pa_threaded_mainloop_get_api(prData.paMainloop), CLIENT_NAME);
82 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_new()");
84 pa_context_set_state_callback(c, __contextStateChangeCb, prData.paMainloop);
86 if (pa_context_connect(c, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0)
87 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_connect()"); //LCOV_EXCL_LINE
89 pa_threaded_mainloop_lock(prData.paMainloop);
91 if (pa_threaded_mainloop_start(prData.paMainloop) < 0) {
92 pa_threaded_mainloop_unlock(prData.paMainloop);
93 THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_threaded_mainloop_start()"); //LCOV_EXCL_LINE
97 pa_context_state_t state;
98 state = pa_context_get_state(c);
100 if (state == PA_CONTEXT_READY)
103 if (!PA_CONTEXT_IS_GOOD(state)) {
105 err = pa_context_errno(c);
106 pa_threaded_mainloop_unlock(prData.paMainloop);
107 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION,
108 "pa_context's state is not good : err[%d]", err);
112 /* Wait until the context is ready */
113 pa_threaded_mainloop_wait(prData.paMainloop);
116 o = pa_context_check_privilege(c, RECORDER_PRIVILEGE, __checkPrivilegeCb, &prData);
119 pa_threaded_mainloop_unlock(prData.paMainloop);
120 THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed to pa_context_check_privilege()");
123 while (pa_operation_get_state(o) == PA_OPERATION_RUNNING)
124 pa_threaded_mainloop_wait(prData.paMainloop);
125 pa_operation_unref(o);
127 pa_threaded_mainloop_unlock(prData.paMainloop);
128 pa_threaded_mainloop_stop(prData.paMainloop);
129 pa_context_disconnect(c);
131 pa_threaded_mainloop_free(prData.paMainloop);
133 return prData.isPrivilegeAllowed;