Apply tizen coding convention for C++ and header
[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             /* Updates ASM to PLAYING */
264             mpAudioSessionHandler->updatePlaying();
265         }
266
267         internalUnlock();
268
269         CAudioIO::resume();
270
271         CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
272     } catch (CAudioError e) {
273         internalUnlock();
274         throw e;
275     }
276 }
277
278 void CAudioInput::drain() throw(CAudioError) {
279     THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "Did not support drain of CAudioInput");
280 }
281
282 void CAudioInput::flush() throw(CAudioError) {
283     if (__IsInit() == false || __IsReady() == false) {
284         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput");
285     }
286
287     try {
288         CAudioIO::flush();
289     } catch (CAudioError e) {
290         throw e;
291     }
292 }
293
294 int CAudioInput::getBufferSize() throw(CAudioError) {
295     if (__IsInit() == false) {
296         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput");
297     }
298
299     if (__IsReady() == false) {
300         AUDIO_IO_LOGD("Warning: Did not prepare CAudioInput, then return zero");
301         return 0;
302     }
303
304     int size = 0;
305
306     try {
307         size = mpPulseAudioClient->getBufferSize();
308     } catch (CAudioError err) {
309         throw err;
310     }
311
312     return size;
313 }
314
315 void CAudioInput::setStreamCallback(SStreamCallback callback) throw(CAudioError) {
316     if (__IsInit() == false) {
317         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput");
318     }
319
320     if (callback.onStream == NULL) {
321         AUDIO_IO_LOGD("__mIsUsedSyncRead = true");
322         __mIsUsedSyncRead = true;
323     } else {
324         AUDIO_IO_LOGD("__mIsUsedSyncRead = false");
325         __mIsUsedSyncRead = false;
326     }
327
328     CAudioIO::setStreamCallback(callback);
329 }
330
331 size_t CAudioInput::read(void* buffer, size_t length) throw(CAudioError) {
332     if (__IsInit() == false || __IsReady() == false) {
333         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput");
334     }
335
336     if (buffer == NULL) {
337         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL buffer:%p", buffer);
338     }
339
340     /* Checks synchronous flag */
341     if (__mIsUsedSyncRead == false) {
342         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Invalid operation of read() if receive stream callback");
343     }
344
345     int ret = 0;
346
347     try {
348         // Block until read done
349         ret = mpPulseAudioClient->read(buffer, length);
350     } catch (CAudioError e) {
351         throw e;
352     }
353
354     return ret;
355 }
356
357 int CAudioInput::peek(const void** buffer, size_t* length) throw(CAudioError) {
358     if (__IsInit() == false || __IsReady() == false) {
359         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput");
360     }
361
362     if (buffer == NULL || length == NULL) {
363         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL buffer:%p, length:%p", buffer, length);
364     }
365
366     /* Checks synchronous flag */
367     if (__mIsUsedSyncRead == true) {
368         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Invalid operation of peek() if does not receive a stream callback");
369     }
370
371     int ret = 0;
372
373     try {
374         ret = mpPulseAudioClient->peek(buffer, length);
375     } catch (CAudioError e) {
376         throw e;
377     }
378
379     return ret;
380 }
381
382 int CAudioInput::drop() throw(CAudioError) {
383     if (__IsInit() == false || __IsReady() == false) {
384         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput");
385     }
386
387     /* Checks synchronous flag */
388     if (__mIsUsedSyncRead == true) {
389         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Invalid operation of drop() if does not receive a stream callback");
390     }
391
392     int ret = 0;
393
394     try {
395         ret = mpPulseAudioClient->drop();
396     } catch (CAudioError e) {
397         throw e;
398     }
399
400     return ret;
401 }