Remove unnecessary input drain code
[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 == NULL)
78         THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_threaded_mainloop_new()");
79
80     c = pa_context_new(pa_threaded_mainloop_get_api(prData.paMainloop), CLIENT_NAME);
81     if (c == NULL)
82         THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_new()");
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()");
88
89     pa_threaded_mainloop_lock(prData.paMainloop);
90
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()");
94     }
95
96     while (true) {
97         pa_context_state_t state;
98         state = pa_context_get_state(c);
99
100         if (state == PA_CONTEXT_READY)
101             break;
102
103         if (!PA_CONTEXT_IS_GOOD(state)) {
104             err = pa_context_errno(c);
105             pa_threaded_mainloop_unlock(prData.paMainloop);
106             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION,
107                                    "pa_context's state is not good : err[%d]", err);
108         }
109
110         /* Wait until the context is ready */
111         pa_threaded_mainloop_wait(prData.paMainloop);
112     }
113
114     o = pa_context_check_privilege(c, RECORDER_PRIVILEGE, __checkPrivilegeCb, &prData);
115     if (!o) {
116         pa_threaded_mainloop_unlock(prData.paMainloop);
117         THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed to pa_context_check_privilege()");
118     }
119     while (pa_operation_get_state(o) == PA_OPERATION_RUNNING)
120         pa_threaded_mainloop_wait(prData.paMainloop);
121     pa_operation_unref(o);
122
123     pa_threaded_mainloop_unlock(prData.paMainloop);
124     pa_threaded_mainloop_stop(prData.paMainloop);
125     pa_context_disconnect(c);
126     pa_context_unref(c);
127     pa_threaded_mainloop_free(prData.paMainloop);
128
129     return prData.isPrivilegeAllowed;
130 }