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.
19 #include "CAudioIODef.h"
20 #include <sound_manager_internal.h>
25 using namespace tizen_media_audio;
32 CAudioIO::CAudioIO() :
33 mpPulseAudioClient(nullptr),
35 mDirection = CAudioInfo::EAudioDirection::AUDIO_DIRECTION_MAX;
36 mState = CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE;
37 mStatePrev = CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE;
42 CAudioIO::CAudioIO(CAudioInfo& audioInfo) :
43 mpPulseAudioClient(nullptr),
45 mAudioInfo = audioInfo;
46 mDirection = CAudioInfo::EAudioDirection::AUDIO_DIRECTION_MAX;
47 mState = CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE;
48 mStatePrev = CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE;
52 void CAudioIO::setInit(bool flag) {
56 bool CAudioIO::isInit() {
60 bool CAudioIO::IsReady() {
61 return ((mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING ||
62 mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED));
65 void CAudioIO::initialize() {
69 AUDIO_IO_LOGD("initialize");
73 void CAudioIO::finalize() {
77 AUDIO_IO_LOGD("finalize");
81 void CAudioIO::onStream(CPulseAudioClient* pClient, size_t length) {
86 #ifdef _AUDIO_IO_DEBUG_TIMING_
87 AUDIO_IO_LOGD("mStreamCallback.onStream(%p), pClient(%p), length(%zu)", mStreamCallback.onStream, pClient, length);
90 if (mStreamCallback.onStream)
91 mStreamCallback.onStream(length, mStreamCallback.mUserData);
94 void CAudioIO::onStateChanged(CAudioInfo::EAudioIOState state, bool byPolicy) {
96 assert(state >= CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE && state < CAudioInfo::EAudioIOState::AUDIO_IO_STATE_MAX);
100 if (mState == mStatePrev)
103 static const char* state_string[] = { "NONE", "IDLE", "RUNNING", "PAUSED" };
105 AUDIO_IO_LOGD("previous(%s,%d) ===> current(%s,%d), by_policy(%d)",
106 state_string[static_cast<int>(mStatePrev)],
107 static_cast<int>(mStatePrev),
108 state_string[static_cast<int>(mState)],
109 static_cast<int>(mState),
112 if (mStateChangedCallback.onStateChanged)
113 mStateChangedCallback.onStateChanged(mState, mStatePrev, mByPolicy, mStateChangedCallback.mUserData);
116 void CAudioIO::setState(CAudioInfo::EAudioIOState state) {
121 void CAudioIO::onStateChanged(CAudioInfo::EAudioIOState state) {
122 onStateChanged(state, false);
125 CAudioInfo::EAudioIOState CAudioIO::getState() noexcept {
129 void CAudioIO::prepare() {
131 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
134 void CAudioIO::unprepare() {
136 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
139 void CAudioIO::pause() {
140 if (!__mIsInit || !IsReady())
141 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO"); //LCOV_EXCL_LINE
143 AUDIO_IO_LOGD("pause");
144 mpPulseAudioClient->cork(true);
147 void CAudioIO::resume() {
148 if (!__mIsInit || !IsReady())
149 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO"); //LCOV_EXCL_LINE
151 mpPulseAudioClient->cork(false);
154 void CAudioIO::flush() {
155 if (!__mIsInit || !IsReady())
156 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO"); //LCOV_EXCL_LINE
158 mpPulseAudioClient->flush();
161 CAudioInfo& CAudioIO::getAudioInfo() {
163 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
168 void CAudioIO::setStreamCallback(SStreamCallback callback) {
170 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
172 mStreamCallback = callback;
175 CAudioIO::SStreamCallback CAudioIO::getStreamCallback() {
177 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
179 return mStreamCallback;
182 void CAudioIO::setStateChangedCallback(SStateChangedCallback callback) {
184 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
186 mStateChangedCallback = callback;
189 CAudioIO::SStateChangedCallback CAudioIO::getStateChangedCallback() {
191 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
193 return mStateChangedCallback;
196 void CAudioIO::setStreamInfo(sound_stream_info_h stream_info) {
198 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_ARGUMENT, "stream_info is NULL"); //LCOV_EXCL_LINE
201 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
203 if (mState != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE)
204 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE, "it is not permitted while started"); //LCOV_EXCL_LINE
206 int errorCode = SOUND_MANAGER_ERROR_NONE;
207 char *type = nullptr;
211 if ((errorCode = sound_manager_is_available_stream_information(stream_info, NATIVE_API_AUDIO_IO, &avail)) != SOUND_MANAGER_ERROR_NONE)
212 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info is invalid [ret:%d]", errorCode); //LCOV_EXCL_LINE
214 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE, "Input stream is not supported"); //LCOV_EXCL_LINE
216 if ((errorCode = sound_manager_get_type_from_stream_information(stream_info, &type)) != SOUND_MANAGER_ERROR_NONE)
217 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->stream_type is invalid [ret:%d]", errorCode); //LCOV_EXCL_LINE
218 if (mDirection == CAudioInfo::EAudioDirection::AUDIO_DIRECTION_IN)
219 getAudioInfo().setAudioTypeByInputStreamType(type);
221 getAudioInfo().setAudioTypeByOutputStreamType(type);
223 if ((errorCode = sound_manager_get_index_from_stream_information(stream_info, &index)) != SOUND_MANAGER_ERROR_NONE)
224 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->index is invalid [ret:%d]", errorCode); //LCOV_EXCL_LINE
225 getAudioInfo().setAudioIndex(index);
227 if (mDirection == CAudioInfo::EAudioDirection::AUDIO_DIRECTION_IN) {
230 sound_effect_method_with_reference_e method_reference;
232 if (sound_manager_get_effect_method(stream_info, &method) == SOUND_MANAGER_ERROR_NONE)
233 getAudioInfo().setEffectMethod(method);
235 if (sound_manager_get_effect_method_with_reference(stream_info, &method_reference, &device_id) == SOUND_MANAGER_ERROR_NONE)
236 getAudioInfo().setEffectMethodWithReference(method_reference, device_id);