Update coverage exception macros
[platform/core/api/audio-io.git] / src / cpp / cpp_audio_in_privilege.cpp
1 /*
2  * Copyright (c) 2018 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 "cpp_audio_in_privilege.h"
18 #include "CAudioIODef.h"
19 #include <stdbool.h>
20 #include <pulse/pulseaudio.h>
21
22 #define RECORDER_PRIVILEGE "http://tizen.org/privilege/recorder"
23 #define CLIENT_NAME "AUDIO_IO_PA_CLIENT"
24
25 using namespace std;
26 using namespace tizen_media_audio;
27
28
29 struct PrivilegeData {
30     bool isPrivilegeAllowed;
31     pa_threaded_mainloop *paMainloop;
32 };
33
34 static void __contextStateChangeCb(pa_context* c, void* user_data) {
35     pa_threaded_mainloop *paMainloop = (pa_threaded_mainloop *)user_data;
36     assert(paMainloop);
37     assert(c);
38
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);
43         break;
44
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);
49         break;
50
51     case PA_CONTEXT_UNCONNECTED:
52     case PA_CONTEXT_CONNECTING:
53     case PA_CONTEXT_AUTHORIZING:
54     case PA_CONTEXT_SETTING_NAME:
55         break;
56     }
57 }
58
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);
61     assert(c);
62     assert(user_data);
63
64     PrivilegeData *prData = (PrivilegeData *)user_data;
65     prData->isPrivilegeAllowed = success ? true : false;
66
67     pa_threaded_mainloop_signal(prData->paMainloop, 0);
68 }
69
70 bool cpp_audio_in_has_record_privilege(void) {
71     pa_operation *o;
72     pa_context *c;
73     int err = 0;
74     PrivilegeData prData;
75
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()"); // LCOV_EXCL_LINE
79
80     c = pa_context_new(pa_threaded_mainloop_get_api(prData.paMainloop), CLIENT_NAME);
81     if (!c)
82         THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_new()"); // LCOV_EXCL_LINE
83
84     pa_context_set_state_callback(c, __contextStateChangeCb, prData.paMainloop);
85
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
88
89     pa_threaded_mainloop_lock(prData.paMainloop);
90
91     if (pa_threaded_mainloop_start(prData.paMainloop) < 0) {
92 // LCOV_EXCL_START
93         pa_threaded_mainloop_unlock(prData.paMainloop);
94         THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_threaded_mainloop_start()");
95 // LCOV_EXCL_STOP
96     }
97
98     while (true) {
99         pa_context_state_t state;
100         state = pa_context_get_state(c);
101
102         if (state == PA_CONTEXT_READY)
103             break;
104
105         if (!PA_CONTEXT_IS_GOOD(state)) {
106 //LCOV_EXCL_START
107             err = pa_context_errno(c);
108             pa_threaded_mainloop_unlock(prData.paMainloop);
109             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION,
110                                    "pa_context's state is not good : err[%d]", err);
111 //LCOV_EXCL_STOP
112         }
113
114         /* Wait until the context is ready */
115         pa_threaded_mainloop_wait(prData.paMainloop);
116     }
117
118     o = pa_context_check_privilege(c, RECORDER_PRIVILEGE, __checkPrivilegeCb, &prData);
119     if (!o) {
120 //LCOV_EXCL_START
121         pa_threaded_mainloop_unlock(prData.paMainloop);
122         THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed to pa_context_check_privilege()");
123 //LCOV_EXCL_STOP
124     }
125     while (pa_operation_get_state(o) == PA_OPERATION_RUNNING)
126         pa_threaded_mainloop_wait(prData.paMainloop);
127     pa_operation_unref(o);
128
129     pa_threaded_mainloop_unlock(prData.paMainloop);
130     pa_threaded_mainloop_stop(prData.paMainloop);
131     pa_context_disconnect(c);
132     pa_context_unref(c);
133     pa_threaded_mainloop_free(prData.paMainloop);
134
135     return prData.isPrivilegeAllowed;
136 }