2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "CAudioIODef.h"
22 #include "cpp_audio_in_privilege.h"
24 #define RECORDER_PRIVILEGE "http://tizen.org/privilege/recorder"
25 #define CLIENT_NAME "AUDIO_IO_PA_CLIENT"
28 using namespace tizen_media_audio;
31 * class CAudioInput inherited by CAudioIO
33 CAudioInput::CAudioInput(CAudioInfo& info) :
35 __mIsUsedSyncRead(true),
37 mDirection = CAudioInfo::EAudioDirection::AUDIO_DIRECTION_IN;
40 CAudioInput::CAudioInput(
41 unsigned int sampleRate,
42 CAudioInfo::EChannel channel,
43 CAudioInfo::ESampleType type,
44 CAudioInfo::EAudioType audioType) :
45 __mIsUsedSyncRead(true),
47 mDirection = CAudioInfo::EAudioDirection::AUDIO_DIRECTION_IN;
48 mAudioInfo = CAudioInfo(sampleRate, channel, type, audioType, -1);
51 CAudioInput::~CAudioInput() {
54 void CAudioInput::onStream(CPulseAudioClient* pClient, size_t length) {
58 * Does not call CAudioIO::onStream() for synchronization
59 * if a user is using read()
61 if (__mIsUsedSyncRead == true) {
62 #ifdef _AUDIO_IO_DEBUG_TIMING_
63 AUDIO_IO_LOGD("Sync Read Mode! - pClient:[%p], length:[%d]", pClient, length);
69 * Accrues callback function
71 #ifdef _AUDIO_IO_DEBUG_TIMING_
72 AUDIO_IO_LOGD("pClient:[%p], length:[%d]", pClient, length);
74 CAudioIO::onStream(pClient, length);
77 void CAudioInput::__setInit(bool flag) {
81 bool CAudioInput::__IsInit() {
82 return (CAudioIO::isInit() == true && __mIsInit == true);
85 bool CAudioInput::__IsReady() {
86 return CAudioIO::IsReady();
89 void CAudioInput::initialize() {
90 if (__IsInit() == true)
93 if (cpp_audio_in_has_record_privilege() == false)
94 THROW_ERROR_MSG(CAudioError::EError::ERROR_PERMISSION_DENIED, "No privilege for record");
97 CAudioIO::initialize();
99 } catch (CAudioError& e) {
104 CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
107 void CAudioInput::finalize() {
108 if (__IsInit() == false) {
109 AUDIO_IO_LOGD("Did not initialize");
113 CAudioIO::finalize();
118 void CAudioInput::prepare() {
119 if (__IsInit() == false)
120 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput");
122 if (__IsReady() == true) {
123 AUDIO_IO_LOGD("Already prepared CAudioInput");
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));
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);
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);
151 } catch (CAudioError& e) {
152 SAFE_FINALIZE(mpPulseAudioClient);
153 SAFE_DELETE(mpPulseAudioClient);
156 } catch (const std::bad_alloc&) {
158 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CPulseAudioClient object");
162 void CAudioInput::unprepare() {
163 if (__IsInit() == false)
164 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
165 "Did not initialize CAudioInput");
167 if (__IsReady() == false) {
168 AUDIO_IO_LOGD("Already unprepared");
172 CAudioIO::unprepare();
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);
181 } catch (CAudioError& e) {
186 CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
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");
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");
198 if (mpPulseAudioClient->isInThread() == true)
199 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't pause in thread");
202 CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED);
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");
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");
214 if (mpPulseAudioClient->isInThread() == true)
215 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't resume in thread");
218 CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
221 void CAudioInput::drain() {
222 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "Did not support drain of CAudioInput");
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");
233 int CAudioInput::getBufferSize() {
234 if (__IsInit() == false)
235 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput");
237 /* FIXME : return calculated size here to satisfy backward compatibility */
238 return (mAudioInfo.getSampleRate() * DEFAULT_PERIOD_SIZE) / 1000 * mAudioInfo.getSampleSize();
241 void CAudioInput::setStreamCallback(SStreamCallback callback) {
242 if (__IsInit() == false)
243 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput");
245 if (callback.onStream == NULL)
246 __mIsUsedSyncRead = true;
248 __mIsUsedSyncRead = false;
249 AUDIO_IO_LOGD("__mIsUsedSyncRead = %d", __mIsUsedSyncRead);
251 CAudioIO::setStreamCallback(callback);
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");
260 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
261 "Parameters are NULL buffer:%p", buffer);
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");
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");
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");
282 // Block until read done
283 ret = mpPulseAudioClient->read(buffer, length);
287 } catch (CAudioError& e) {
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");
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);
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");
309 return mpPulseAudioClient->peek(buffer, length);
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");
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");
322 return mpPulseAudioClient->drop();