CAudioInput/Output: Apply VoIP latency in case of VoIP audio type
[platform/core/api/audio-io.git] / src / cpp / CAudioInput.cpp
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
18 #include <new>
19
20 #include "CAudioIODef.h"
21 #include <sched.h>
22 #include "cpp_audio_in_privilege.h"
23
24 #define RECORDER_PRIVILEGE "http://tizen.org/privilege/recorder"
25 #define CLIENT_NAME "AUDIO_IO_PA_CLIENT"
26
27 using namespace std;
28 using namespace tizen_media_audio;
29
30 /**
31  * class CAudioInput inherited by CAudioIO
32  */
33 CAudioInput::CAudioInput(CAudioInfo& info) :
34     CAudioIO(info),
35     __mIsUsedSyncRead(true),
36     __mIsInit(false) {
37     mDirection = CAudioInfo::EAudioDirection::AUDIO_DIRECTION_IN;
38 }
39
40 CAudioInput::CAudioInput(
41         unsigned int            sampleRate,
42         CAudioInfo::EChannel    channel,
43         CAudioInfo::ESampleType type,
44         CAudioInfo::EAudioType  audioType) :
45     __mIsUsedSyncRead(true),
46     __mIsInit(false) {
47     mDirection = CAudioInfo::EAudioDirection::AUDIO_DIRECTION_IN;
48     mAudioInfo = CAudioInfo(sampleRate, channel, type, audioType, -1);
49 }
50
51 CAudioInput::~CAudioInput() {
52 }
53
54 void CAudioInput::onStream(CPulseAudioClient* pClient, size_t length) {
55     assert(pClient);
56
57     /*
58      * Does not call CAudioIO::onStream() for synchronization
59      * if a user is using read()
60      */
61     if (__mIsUsedSyncRead == true) {
62 #ifdef _AUDIO_IO_DEBUG_TIMING_
63         AUDIO_IO_LOGD("Sync Read Mode! - pClient:[%p], length:[%d]", pClient, length);
64 #endif
65         return;
66     }
67
68     /*
69      * Accrues callback function
70      */
71 #ifdef _AUDIO_IO_DEBUG_TIMING_
72     AUDIO_IO_LOGD("pClient:[%p], length:[%d]", pClient, length);
73 #endif
74     CAudioIO::onStream(pClient, length);
75 }
76
77 void CAudioInput::__setInit(bool flag) {
78     __mIsInit = flag;
79 }
80
81 bool CAudioInput::__IsInit() {
82     return (CAudioIO::isInit() == true && __mIsInit == true);
83 }
84
85 bool CAudioInput::__IsReady() {
86     return CAudioIO::IsReady();
87 }
88
89 void CAudioInput::initialize() {
90     if (__IsInit() == true)
91         return;
92
93     if (cpp_audio_in_has_record_privilege() == false)
94         THROW_ERROR_MSG(CAudioError::EError::ERROR_PERMISSION_DENIED, "No privilege for record");
95
96     try {
97         CAudioIO::initialize();
98         __setInit(true);
99     } catch (CAudioError& e) {
100         finalize();
101         throw;
102     }
103
104     CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
105 }
106
107 void CAudioInput::finalize() {
108     if (__IsInit() == false) {
109         AUDIO_IO_LOGD("Did not initialize");
110         return;
111     }
112
113     CAudioIO::finalize();
114
115     __setInit(false);
116 }
117
118 void CAudioInput::prepare() {
119     if (__IsInit() == false)
120         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput");
121
122     if (__IsReady() == true) {
123         AUDIO_IO_LOGD("Already prepared CAudioInput");
124         CAudioIO::prepare();
125         return;
126     }
127
128     /* Check invalid AudioType */
129     CAudioInfo::EAudioType audioType = mAudioInfo.getAudioType();
130     if (audioType < CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA ||
131         audioType >= CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA)
132         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
133                                "The audioType is invalid [type:%d]", static_cast<int>(audioType));
134
135     try {
136         /* Init StreamSpec */
137         AUDIO_IO_LOGD("Set Stream Spec : CPulseStreamSpec::STREAM_LATENCY_INPUT_DEFAULT");
138         CPulseStreamSpec::EStreamLatency streamSpec = CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_INPUT_DEFAULT;
139         /* Override the default value by audio type */
140         if (audioType == CAudioInfo::EAudioType::AUDIO_IN_TYPE_VOIP)
141             streamSpec = CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_INPUT_VOIP;
142
143         CPulseStreamSpec spec(streamSpec, mAudioInfo);
144
145         internalLock();
146         mpPulseAudioClient = new CPulseAudioClient(CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_RECORD, spec, this);
147         mpPulseAudioClient->initialize();
148 #ifndef DISABLE_MOBILE_BACK_COMP
149         /* Uncork stream which is created with CORKED flag */
150         mpPulseAudioClient->cork(false);
151 #endif
152         internalUnlock();
153
154         CAudioIO::prepare();
155     } catch (CAudioError& e) {
156         SAFE_FINALIZE(mpPulseAudioClient);
157         SAFE_DELETE(mpPulseAudioClient);
158         internalUnlock();
159         throw;
160     } catch (const std::bad_alloc&) {
161         internalUnlock();
162         THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CPulseAudioClient object");
163     }
164 }
165
166 void CAudioInput::unprepare() {
167     if (__IsInit() == false)
168         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
169                         "Did not initialize CAudioInput");
170
171     if (__IsReady() == false) {
172         AUDIO_IO_LOGD("Already unprepared");
173         return;
174     }
175
176     CAudioIO::unprepare();
177
178     try {
179         internalLock();
180         if (mpPulseAudioClient && mpPulseAudioClient->isInThread())
181             THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't unprepare inside pulseaudio thread");
182         SAFE_FINALIZE(mpPulseAudioClient);
183         SAFE_DELETE(mpPulseAudioClient);
184         internalUnlock();
185     } catch (CAudioError& e) {
186         internalUnlock();
187         throw;
188     }
189
190     CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
191 }
192
193 void CAudioInput::pause() {
194     if (__IsInit() == false || __IsReady() == false)
195         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
196                         "Did not initialize or prepare CAudioInput");
197
198     if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING)
199         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE,
200                         "Can't pause if not in Running state");
201
202     if (mpPulseAudioClient->isInThread() == true)
203         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't pause in thread");
204
205     CAudioIO::pause();
206     CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED);
207 }
208
209 void CAudioInput::resume() {
210     if (__IsInit() == false || __IsReady() == false)
211         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
212                         "Did not initialize or prepare CAudioInput");
213
214     if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED)
215         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE,
216                         "Can't resume if not in Paused state");
217
218     if (mpPulseAudioClient->isInThread() == true)
219         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't resume in thread");
220
221     CAudioIO::resume();
222     CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
223 }
224
225 void CAudioInput::flush() {
226     if (__IsInit() == false || __IsReady() == false)
227         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
228                         "Did not initialize or prepare CAudioInput");
229
230     CAudioIO::flush();
231 }
232
233 int CAudioInput::getBufferSize() {
234     if (__IsInit() == false)
235         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput");
236
237     /* FIXME : return calculated size here to satisfy backward compatibility */
238     return (mAudioInfo.getSampleRate() * DEFAULT_PERIOD_SIZE) / 1000 * mAudioInfo.getSampleSize();
239 }
240
241 void CAudioInput::setStreamCallback(SStreamCallback callback) {
242     if (__IsInit() == false)
243         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput");
244
245     if (callback.onStream == NULL)
246         __mIsUsedSyncRead = true;
247     else
248         __mIsUsedSyncRead = false;
249     AUDIO_IO_LOGD("__mIsUsedSyncRead = %d", __mIsUsedSyncRead);
250
251     CAudioIO::setStreamCallback(callback);
252 }
253
254 size_t CAudioInput::read(void* buffer, size_t length) {
255     if (__IsInit() == false || __IsReady() == false)
256         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
257                         "Did not initialize or prepare CAudioInput");
258
259     if (buffer == NULL)
260         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
261                                "Parameters are NULL buffer:%p", buffer);
262
263     if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING)
264         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION,
265                         "Can't read if not in Running state");
266
267     /* Checks synchronous flag */
268     if (__mIsUsedSyncRead == false)
269         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION,
270                         "Invalid operation of read() if receive stream callback");
271
272     int ret = 0;
273
274     try {
275         internalLock();
276
277         // If another thread did call unprepare, do not read
278         if (mpPulseAudioClient == NULL)
279             THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
280                             "Did not initialize CPulseAudioClient");
281
282         // Block until read done
283         ret = mpPulseAudioClient->read(buffer, length);
284         internalUnlock();
285
286         sched_yield();
287     } catch (CAudioError& e) {
288         internalUnlock();
289         throw;
290     }
291
292     return ret;
293 }
294
295 int CAudioInput::peek(const void** buffer, size_t* length) {
296     if (__IsInit() == false || __IsReady() == false)
297         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
298                         "Did not initialize or prepare CAudioInput");
299
300     if (buffer == NULL || length == NULL)
301         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
302                                "Parameters are NULL buffer:%p, length:%p", buffer, length);
303
304     /* Checks synchronous flag */
305     if (__mIsUsedSyncRead == true)
306         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION,
307                         "Invalid operation of peek() if does not receive a stream callback");
308
309     return mpPulseAudioClient->peek(buffer, length);
310 }
311
312 int CAudioInput::drop() {
313     if (__IsInit() == false || __IsReady() == false)
314         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
315                         "Did not initialize or prepare CAudioInput");
316
317     /* Checks synchronous flag */
318     if (__mIsUsedSyncRead == true)
319         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION,
320                         "Invalid operation of drop() if does not receive a stream callback");
321
322     return mpPulseAudioClient->drop();
323 }