Fix SVACE defects
[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 "CAudioIODef.h"
19
20
21 using namespace std;
22 using namespace tizen_media_audio;
23
24
25 /**
26  * class CAudioInput inherited by CAudioIO
27  */
28 CAudioInput::CAudioInput(CAudioInfo& info) :
29     CAudioIO(info),
30     __mIsUsedSyncRead(true),
31     __mIsInit(false) {
32 }
33
34 CAudioInput::CAudioInput(
35         unsigned int            sampleRate,
36         CAudioInfo::EChannel    channel,
37         CAudioInfo::ESampleType type,
38         CAudioInfo::EAudioType  audioType) :
39     __mIsUsedSyncRead(true),
40     __mIsInit(false) {
41     mAudioInfo = CAudioInfo(sampleRate, channel, type, audioType, -1);
42 }
43
44 CAudioInput::~CAudioInput() {
45 }
46
47 void CAudioInput::onStream(CPulseAudioClient* pClient, size_t length) {
48     assert(pClient);
49
50     /*
51      * Does not call CAudioIO::onStream() for synchronization
52      * if a user is using read()
53      */
54     if (__mIsUsedSyncRead == true) {
55 #ifdef _AUDIO_IO_DEBUG_TIMING_
56         AUDIO_IO_LOGD("Sync Read Mode! - pClient:[%p], length:[%d]", pClient, length);
57 #endif
58         return;
59     }
60
61     /*
62      * Accrues callback function
63      */
64 #ifdef _AUDIO_IO_DEBUG_TIMING_
65     AUDIO_IO_LOGD("pClient:[%p], length:[%d]", pClient, length);
66 #endif
67     CAudioIO::onStream(pClient, length);
68 }
69
70 void CAudioInput::onInterrupt(CAudioSessionHandler* pHandler, int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state, const char *reason_for_change, const char *additional_info) {
71     assert(pHandler);
72     AUDIO_IO_LOGD("[pHandler:0x%x], [focus_type:%d], [state:%d], [reason_for_change:%s], [additional_info:%s]", pHandler, focus_type, state, reason_for_change, additional_info);
73     CAudioIO::onInterrupt(pHandler, id, focus_type, state, reason_for_change, additional_info);
74 }
75
76 void CAudioInput::onSignal(CAudioSessionHandler* pHandler, mm_sound_signal_name_t signal, int value) {
77     assert(pHandler);
78     AUDIO_IO_LOGD("[pHandler:0x%x], [signal:%d], [value:%d]", pHandler, signal, value);
79     CAudioIO::onSignal(pHandler, signal, value);
80 }
81
82 void CAudioInput::__setInit(bool flag) {
83     __mIsInit = flag;
84 }
85
86 bool CAudioInput::__IsInit() {
87     return (CAudioIO::isInit() == true && __mIsInit == true);
88 }
89
90 bool CAudioInput::__IsReady() {
91     return CAudioIO::IsReady();
92 }
93
94 void CAudioInput::initialize() throw (CAudioError) {
95     if (__IsInit() == true) {
96         return;
97     }
98
99     try {
100         CAudioIO::initialize();
101
102         // Create ASM Handler
103         mpAudioSessionHandler = new CAudioSessionHandler(CAudioSessionHandler::EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE, mAudioInfo, this);
104         if (mpAudioSessionHandler == NULL) {
105             THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CAudioSessionHandler object");
106         }
107
108         // Initialize ASM Handler
109         mpAudioSessionHandler->initialize();
110
111         __setInit(true);
112         CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
113     } catch (CAudioError err) {
114         finalize();
115         throw err;
116     }
117 }
118
119 void CAudioInput::finalize() {
120     if (__IsInit() == false) {
121         AUDIO_IO_LOGD("Did not initialize");
122         return;
123     }
124
125     SAFE_FINALIZE(mpAudioSessionHandler);
126     SAFE_DELETE(mpAudioSessionHandler);
127
128     CAudioIO::finalize();
129
130     __setInit(false);
131 }
132
133 void CAudioInput::prepare() throw (CAudioError) {
134     if (__IsInit() == false) {
135         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput");
136     }
137
138     if (__IsReady() == true) {
139         AUDIO_IO_LOGD("Already prepared CAudioInput");
140         return;
141     }
142
143     try {
144         internalLock();
145
146         // Check to invalid AudioType
147         CAudioInfo::EAudioType audioType = mAudioInfo.getAudioType();
148         if (audioType < CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA || audioType > CAudioInfo::EAudioType::AUDIO_IN_TYPE_LOOPBACK) {
149             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The audioType is invalid [type:%d]", static_cast<int>(audioType));
150         }
151
152         if (mpAudioSessionHandler->getId() < 0) {  //Did not registerSound()
153             // Check session to skip registration
154             if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSessionEvent() == false) {
155                 // Register ASM Listener
156                 AUDIO_IO_LOGD("Register ASM Listener");
157                 mpAudioSessionHandler->registerSound();
158             }
159         }
160
161         // Init StreamSpec
162         AUDIO_IO_LOGD("Set Stream Spec : CPulseStreamSpec::STREAM_LATENCY_INPUT_MID");
163         CPulseStreamSpec::EStreamLatency streamSpec = CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_INPUT_MID;
164         CPulseStreamSpec spec(streamSpec, mAudioInfo);
165
166         // Create PulseAudio Handler
167         mpPulseAudioClient = new CPulseAudioClient(CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_RECORD, spec, this);
168         if (mpPulseAudioClient == NULL) {
169             THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CPulseAudioClient object");
170         }
171
172         // Initialize PulseAudio Handler
173         mpPulseAudioClient->initialize();
174
175         if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSessionEvent() == false) {
176             /* Updates ASM to PLAYING */
177             mpAudioSessionHandler->updatePlaying();
178         }
179
180         internalUnlock();
181
182         // Do Prepare
183         CAudioIO::prepare();
184     } catch (CAudioError e) {
185         internalUnlock();
186         throw e;
187     }
188 }
189
190 void CAudioInput::unprepare() throw (CAudioError) {
191     if (__IsInit() == false) {
192         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput");
193     }
194
195     if (__IsReady() == false) {
196         AUDIO_IO_LOGD("Already unprepared");
197         return;
198     }
199
200     try{
201         // Do unprepare
202         CAudioIO::unprepare();
203
204         internalLock();
205
206         SAFE_FINALIZE(mpPulseAudioClient);
207         SAFE_DELETE(mpPulseAudioClient);
208
209         if (mpAudioSessionHandler->getId() >= 0) {
210             /* Updates ASM to STOP */
211             if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSessionEvent() == false) {
212                 mpAudioSessionHandler->updateStop();
213             }
214
215             bool isSkip = mpAudioSessionHandler->isSkipSessionEvent();
216             if (isSkip == false) {
217                 mpAudioSessionHandler->unregisterSound();
218             }
219         }
220
221         internalUnlock();
222
223         CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
224     } catch (CAudioError e) {
225         internalUnlock();
226         throw e;
227     }
228 }
229
230 void CAudioInput::pause() throw (CAudioError) {
231     if (__IsInit() == false || __IsReady() == false) {
232         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput");
233     }
234
235     try{
236         CAudioIO::pause();
237
238         internalLock();
239
240         /* Updates ASM to STOP */
241         if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSessionEvent() == false) {
242             mpAudioSessionHandler->updateStop();
243         }
244
245         internalUnlock();
246
247         CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED);
248     } catch (CAudioError e) {
249         internalUnlock();
250         throw e;
251     }
252 }
253
254 void CAudioInput::resume() throw (CAudioError) {
255     if (__IsInit() == false || __IsReady() == false) {
256         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput");
257     }
258
259     try {
260         internalLock();
261
262         if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSessionEvent() == false) {
263
264             /* Updates ASM to PLAYING */
265             mpAudioSessionHandler->updatePlaying();
266         }
267
268         internalUnlock();
269
270         CAudioIO::resume();
271
272         CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
273     } catch (CAudioError e) {
274         internalUnlock();
275         throw e;
276     }
277 }
278
279 void CAudioInput::drain() throw (CAudioError) {
280     THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "Did not support drain of CAudioInput");
281 }
282
283 void CAudioInput::flush() throw (CAudioError) {
284     if (__IsInit() == false || __IsReady() == false) {
285         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput");
286     }
287
288     try {
289         CAudioIO::flush();
290     } catch (CAudioError e) {
291         throw e;
292     }
293 }
294
295 int CAudioInput::getBufferSize() throw (CAudioError) {
296     if (__IsInit() == false) {
297         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput");
298     }
299
300     if (__IsReady() == false) {
301         AUDIO_IO_LOGD("Warning: Did not prepare CAudioInput, then return zero");
302         return 0;
303     }
304
305     int size = 0;
306
307     try {
308         size = mpPulseAudioClient->getBufferSize();
309     } catch (CAudioError err) {
310         throw err;
311     }
312
313     return size;
314 }
315
316 void CAudioInput::setStreamCallback(SStreamCallback callback) throw (CAudioError) {
317     if (__IsInit() == false) {
318         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput");
319     }
320
321     if (callback.onStream == NULL) {
322         AUDIO_IO_LOGD("__mIsUsedSyncRead = true");
323         __mIsUsedSyncRead = true;
324     } else {
325         AUDIO_IO_LOGD("__mIsUsedSyncRead = false");
326         __mIsUsedSyncRead = false;
327     }
328
329     CAudioIO::setStreamCallback(callback);
330 }
331
332 size_t CAudioInput::read(void* buffer, size_t length) throw (CAudioError) {
333     if (__IsInit() == false || __IsReady() == false) {
334         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput");
335     }
336
337     if (buffer == NULL) {
338         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL buffer:%p", buffer);
339     }
340
341     /* Checks synchronous flag */
342     if (__mIsUsedSyncRead == false) {
343         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Invalid operation of read() if receive stream callback");
344     }
345
346     int ret = 0;
347
348     try {
349         // Block until read done
350         ret = mpPulseAudioClient->read(buffer, length);
351     } catch (CAudioError e) {
352         throw e;
353     }
354
355     return ret;
356 }
357
358 int CAudioInput::peek(const void** buffer, size_t* length) throw (CAudioError) {
359     if (__IsInit() == false || __IsReady() == false) {
360         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput");
361     }
362
363     if (buffer == NULL || length == NULL) {
364         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL buffer:%p, length:%p", buffer, length);
365     }
366
367     /* Checks synchronous flag */
368     if (__mIsUsedSyncRead == true) {
369         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Invalid operation of peek() if does not receive a stream callback");
370     }
371
372     int ret = 0;
373
374     try {
375         ret = mpPulseAudioClient->peek(buffer, length);
376     } catch (CAudioError e) {
377         throw e;
378     }
379
380     return ret;
381 }
382
383 int CAudioInput::drop() throw (CAudioError) {
384     if (__IsInit() == false || __IsReady() == false) {
385         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput");
386     }
387
388     /* Checks synchronous flag */
389     if (__mIsUsedSyncRead == true) {
390         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Invalid operation of drop() if does not receive a stream callback");
391     }
392
393     int ret = 0;
394
395     try {
396         ret = mpPulseAudioClient->drop();
397     } catch (CAudioError e) {
398         throw e;
399     }
400
401     return ret;
402 }