Fix invalid format string
[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:[%zu]", 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:[%zu]", 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         CPulseStreamSpec spec(streamSpec, mAudioInfo);
140
141         internalLock();
142         mpPulseAudioClient = new CPulseAudioClient(CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_RECORD, spec, this);
143         mpPulseAudioClient->initialize();
144 #ifndef DISABLE_MOBILE_BACK_COMP
145         /* Uncork stream which is created with CORKED flag */
146         mpPulseAudioClient->cork(false);
147 #endif
148         internalUnlock();
149
150         CAudioIO::prepare();
151     } catch (CAudioError& e) {
152         SAFE_FINALIZE(mpPulseAudioClient);
153         SAFE_DELETE(mpPulseAudioClient);
154         internalUnlock();
155         throw;
156     } catch (const std::bad_alloc&) {
157         internalUnlock();
158         THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CPulseAudioClient object");
159     }
160 }
161
162 void CAudioInput::unprepare() {
163     if (__IsInit() == false)
164         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
165                         "Did not initialize CAudioInput");
166
167     if (__IsReady() == false) {
168         AUDIO_IO_LOGD("Already unprepared");
169         return;
170     }
171
172     CAudioIO::unprepare();
173
174     try {
175         internalLock();
176         if (mpPulseAudioClient && mpPulseAudioClient->isInThread())
177             THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't unprepare inside pulseaudio thread");
178         SAFE_FINALIZE(mpPulseAudioClient);
179         SAFE_DELETE(mpPulseAudioClient);
180         internalUnlock();
181     } catch (CAudioError& e) {
182         internalUnlock();
183         throw;
184     }
185
186     CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
187 }
188
189 void CAudioInput::pause() {
190     if (__IsInit() == false || __IsReady() == false)
191         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
192                         "Did not initialize or prepare CAudioInput");
193
194     if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING)
195         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE,
196                         "Can't pause if not in Running state");
197
198     if (mpPulseAudioClient->isInThread() == true)
199         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't pause in thread");
200
201     CAudioIO::pause();
202     CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED);
203 }
204
205 void CAudioInput::resume() {
206     if (__IsInit() == false || __IsReady() == false)
207         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
208                         "Did not initialize or prepare CAudioInput");
209
210     if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED)
211         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE,
212                         "Can't resume if not in Paused state");
213
214     if (mpPulseAudioClient->isInThread() == true)
215         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't resume in thread");
216
217     CAudioIO::resume();
218     CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
219 }
220
221 void CAudioInput::flush() {
222     if (__IsInit() == false || __IsReady() == false)
223         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
224                         "Did not initialize or prepare CAudioInput");
225
226     CAudioIO::flush();
227 }
228
229 int CAudioInput::getBufferSize() {
230     if (__IsInit() == false)
231         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput");
232
233     /* FIXME : return calculated size here to satisfy backward compatibility */
234     return (mAudioInfo.getSampleRate() * DEFAULT_PERIOD_SIZE) / 1000 * mAudioInfo.getSampleSize();
235 }
236
237 void CAudioInput::setStreamCallback(SStreamCallback callback) {
238     if (__IsInit() == false)
239         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput");
240
241     if (callback.onStream == NULL)
242         __mIsUsedSyncRead = true;
243     else
244         __mIsUsedSyncRead = false;
245     AUDIO_IO_LOGD("__mIsUsedSyncRead = %d", __mIsUsedSyncRead);
246
247     CAudioIO::setStreamCallback(callback);
248 }
249
250 size_t CAudioInput::read(void* buffer, size_t length) {
251     if (__IsInit() == false || __IsReady() == false)
252         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
253                         "Did not initialize or prepare CAudioInput");
254
255     if (buffer == NULL)
256         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
257                                "Parameters are NULL buffer:%p", buffer);
258
259     if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING)
260         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION,
261                         "Can't read if not in Running state");
262
263     /* Checks synchronous flag */
264     if (__mIsUsedSyncRead == false)
265         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION,
266                         "Invalid operation of read() if receive stream callback");
267
268     int ret = 0;
269
270     try {
271         internalLock();
272
273         // If another thread did call unprepare, do not read
274         if (mpPulseAudioClient == NULL)
275             THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
276                             "Did not initialize CPulseAudioClient");
277
278         // Block until read done
279         ret = mpPulseAudioClient->read(buffer, length);
280         internalUnlock();
281
282         sched_yield();
283     } catch (CAudioError& e) {
284         internalUnlock();
285         throw;
286     }
287
288     return ret;
289 }
290
291 int CAudioInput::peek(const void** buffer, size_t* length) {
292     if (__IsInit() == false || __IsReady() == false)
293         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
294                         "Did not initialize or prepare CAudioInput");
295
296     if (buffer == NULL || length == NULL)
297         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
298                                "Parameters are NULL buffer:%p, length:%p", buffer, length);
299
300     /* Checks synchronous flag */
301     if (__mIsUsedSyncRead == true)
302         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION,
303                         "Invalid operation of peek() if does not receive a stream callback");
304
305     return mpPulseAudioClient->peek(buffer, length);
306 }
307
308 int CAudioInput::drop() {
309     if (__IsInit() == false || __IsReady() == false)
310         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
311                         "Did not initialize or prepare CAudioInput");
312
313     /* Checks synchronous flag */
314     if (__mIsUsedSyncRead == true)
315         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION,
316                         "Invalid operation of drop() if does not receive a stream callback");
317
318     return mpPulseAudioClient->drop();
319 }