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 void CAudioInput::onStream(CPulseAudioClient* pClient, size_t length) {
44 * Does not call CAudioIO::onStream() for synchronization
45 * if a user is using read()
47 if (__mIsUsedSyncRead) {
48 #ifdef _AUDIO_IO_DEBUG_TIMING_
49 AUDIO_IO_LOGD("Sync Read Mode! - pClient:[%p], length:[%zu]", pClient, length);
55 * Accrues callback function
57 #ifdef _AUDIO_IO_DEBUG_TIMING_
58 AUDIO_IO_LOGD("pClient:[%p], length:[%zu]", pClient, length);
60 CAudioIO::onStream(pClient, length);
63 void CAudioInput::__setInit(bool flag) noexcept {
67 bool CAudioInput::__IsInit() noexcept {
68 return (CAudioIO::isInit() && __mIsInit);
71 bool CAudioInput::__IsReady() noexcept {
72 return CAudioIO::IsReady();
75 void CAudioInput::initialize() {
79 if (!cpp_audio_in_has_record_privilege())
80 THROW_ERROR_MSG(CAudioError::EError::ERROR_PERMISSION_DENIED, "No privilege for record");
83 CAudioIO::initialize();
85 } catch (const CAudioError& e) {
90 CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
93 void CAudioInput::finalize() {
95 AUDIO_IO_LOGD("Did not initialize");
104 void CAudioInput::prepare() {
106 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput");
109 AUDIO_IO_LOGD("Already prepared CAudioInput");
114 /* Check invalid AudioType */
115 CAudioInfo::EAudioType audioType = mAudioInfo.getAudioType();
116 if (audioType < CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA ||
117 audioType >= CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA)
118 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
119 "The audioType is invalid [type:%d]", static_cast<int>(audioType));
122 /* Init StreamSpec */
123 AUDIO_IO_LOGD("Set Stream Spec : CPulseStreamSpec::STREAM_LATENCY_INPUT_DEFAULT");
124 CPulseStreamSpec::EStreamLatency streamSpec = CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_INPUT_DEFAULT;
125 /* Override the default value by audio type */
126 if (audioType == CAudioInfo::EAudioType::AUDIO_IN_TYPE_VOIP)
127 streamSpec = CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_INPUT_VOIP;
129 CPulseStreamSpec spec(streamSpec, mAudioInfo);
132 mpPulseAudioClient = new CPulseAudioClient(CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_RECORD, spec, this);
133 mpPulseAudioClient->initialize();
134 #ifndef DISABLE_MOBILE_BACK_COMP
135 /* Uncork stream which is created with CORKED flag */
136 mpPulseAudioClient->cork(false);
141 } catch (const CAudioError& e) {
142 SAFE_FINALIZE(mpPulseAudioClient);
143 SAFE_DELETE(mpPulseAudioClient);
146 } catch (const std::bad_alloc&) {
149 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CPulseAudioClient object");
154 void CAudioInput::unprepare() {
156 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
157 "Did not initialize CAudioInput");
160 AUDIO_IO_LOGD("Already unprepared");
164 CAudioIO::unprepare();
168 if (mpPulseAudioClient && mpPulseAudioClient->isInThread())
169 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't unprepare inside pulseaudio thread");
170 SAFE_FINALIZE(mpPulseAudioClient);
171 SAFE_DELETE(mpPulseAudioClient);
173 } catch (const CAudioError& e) {
178 CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
181 void CAudioInput::pause() {
182 if (!__IsInit() || !__IsReady())
183 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
184 "Did not initialize or prepare CAudioInput");
186 if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING)
187 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE,
188 "Can't pause if not in Running state");
190 if (mpPulseAudioClient->isInThread())
191 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't pause in thread");
194 CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED);
197 void CAudioInput::resume() {
198 if (!__IsInit() || !__IsReady())
199 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
200 "Did not initialize or prepare CAudioInput");
202 if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED)
203 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE,
204 "Can't resume if not in Paused state");
206 if (mpPulseAudioClient->isInThread())
207 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't resume in thread");
210 CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
213 void CAudioInput::flush() {
214 if (!__IsInit() || !__IsReady())
215 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
216 "Did not initialize or prepare CAudioInput");
221 int CAudioInput::getBufferSize() {
223 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput");
225 /* FIXME : return calculated size here to satisfy backward compatibility */
226 return (mAudioInfo.getSampleRate() * DEFAULT_PERIOD_SIZE) / 1000 * mAudioInfo.getSampleSize();
229 void CAudioInput::setStreamCallback(SStreamCallback callback) {
231 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput");
233 __mIsUsedSyncRead = (callback.onStream == nullptr);
235 AUDIO_IO_LOGD("__mIsUsedSyncRead = %d", __mIsUsedSyncRead);
237 CAudioIO::setStreamCallback(callback);
240 size_t CAudioInput::read(void* buffer, size_t length) {
241 if (!__IsInit() || !__IsReady())
242 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
243 "Did not initialize or prepare CAudioInput");
246 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
247 "Parameters are NULL buffer:%p", buffer);
249 if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING)
250 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION,
251 "Can't read if not in Running state");
253 /* Checks synchronous flag */
254 if (!__mIsUsedSyncRead)
255 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION,
256 "Invalid operation of read() if receive stream callback");
263 // If another thread did call unprepare, do not read
264 if (!mpPulseAudioClient)
265 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
266 "Did not initialize CPulseAudioClient");
268 // Block until read done
269 ret = mpPulseAudioClient->read(buffer, length);
273 } catch (const CAudioError& e) {
281 int CAudioInput::peek(const void** buffer, size_t* length) {
282 if (!__IsInit() || !__IsReady())
283 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
284 "Did not initialize or prepare CAudioInput");
286 if (buffer == nullptr || length == nullptr)
287 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
288 "Parameters are NULL buffer:%p, length:%p", buffer, length);
290 /* Checks synchronous flag */
291 if (__mIsUsedSyncRead)
292 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION,
293 "Invalid operation of peek() if does not receive a stream callback");
295 return mpPulseAudioClient->peek(buffer, length);
298 int CAudioInput::drop() {
299 if (!__IsInit() || !__IsReady())
300 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
301 "Did not initialize or prepare CAudioInput");
303 /* Checks synchronous flag */
304 if (__mIsUsedSyncRead)
305 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION,
306 "Invalid operation of drop() if does not receive a stream callback");
308 return mpPulseAudioClient->drop();