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(CAudioInfo& audioInfo) :
33 mAudioInfo(audioInfo) {
36 void CAudioIO::setInit(bool flag) {
40 bool CAudioIO::isInit() {
44 bool CAudioIO::IsReady() {
45 return ((mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING ||
46 mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED));
49 void CAudioIO::initialize() {
53 AUDIO_IO_LOGD("initialize");
57 void CAudioIO::finalize() {
61 AUDIO_IO_LOGD("finalize");
65 void CAudioIO::onStream(CPulseAudioClient* pClient, size_t length) {
70 #ifdef _AUDIO_IO_DEBUG_TIMING_
71 AUDIO_IO_LOGD("mStreamCallback.onStream(%p), pClient(%p), length(%zu)", mStreamCallback.onStream, pClient, length);
74 if (mStreamCallback.onStream)
75 mStreamCallback.onStream(length, mStreamCallback.mUserData);
78 void CAudioIO::onStateChanged(CAudioInfo::EAudioIOState state, bool byPolicy) {
80 assert(state >= CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE && state < CAudioInfo::EAudioIOState::AUDIO_IO_STATE_MAX);
84 if (mState == mStatePrev)
87 static const char* state_string[] = { "NONE", "IDLE", "RUNNING", "PAUSED" };
89 AUDIO_IO_LOGD("previous(%s,%d) ===> current(%s,%d), by_policy(%d)",
90 state_string[static_cast<int>(mStatePrev)],
91 static_cast<int>(mStatePrev),
92 state_string[static_cast<int>(mState)],
93 static_cast<int>(mState),
96 if (mStateChangedCallback.onStateChanged)
97 mStateChangedCallback.onStateChanged(mState, mStatePrev, mByPolicy, mStateChangedCallback.mUserData);
100 void CAudioIO::setState(CAudioInfo::EAudioIOState state) {
105 void CAudioIO::onStateChanged(CAudioInfo::EAudioIOState state) {
106 onStateChanged(state, false);
109 CAudioInfo::EAudioIOState CAudioIO::getState() noexcept {
113 void CAudioIO::prepare() {
115 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
118 void CAudioIO::unprepare() {
120 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
123 void CAudioIO::pause() {
124 if (!__mIsInit || !IsReady())
125 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO"); //LCOV_EXCL_LINE
127 AUDIO_IO_LOGD("pause");
128 mpPulseAudioClient->cork(true);
131 void CAudioIO::resume() {
132 if (!__mIsInit || !IsReady())
133 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO"); //LCOV_EXCL_LINE
135 mpPulseAudioClient->cork(false);
138 void CAudioIO::flush() {
139 if (!__mIsInit || !IsReady())
140 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO"); //LCOV_EXCL_LINE
142 mpPulseAudioClient->flush();
145 CAudioInfo& CAudioIO::getAudioInfo() {
147 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
152 void CAudioIO::setStreamCallback(SStreamCallback callback) {
154 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
156 mStreamCallback = callback;
159 CAudioIO::SStreamCallback CAudioIO::getStreamCallback() {
161 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
163 return mStreamCallback;
166 void CAudioIO::setStateChangedCallback(SStateChangedCallback callback) {
168 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
170 mStateChangedCallback = callback;
173 CAudioIO::SStateChangedCallback CAudioIO::getStateChangedCallback() {
175 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
177 return mStateChangedCallback;
180 void CAudioIO::setStreamInfo(sound_stream_info_h stream_info) {
182 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_ARGUMENT, "stream_info is NULL"); //LCOV_EXCL_LINE
185 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
187 if (mState != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE)
188 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE, "it is not permitted while started"); //LCOV_EXCL_LINE
190 int errorCode = SOUND_MANAGER_ERROR_NONE;
191 char *type = nullptr;
195 if ((errorCode = sound_manager_is_available_stream_information(stream_info, NATIVE_API_AUDIO_IO, &avail)) != SOUND_MANAGER_ERROR_NONE)
196 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info is invalid [ret:%d]", errorCode); //LCOV_EXCL_LINE
198 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE, "Input stream is not supported"); //LCOV_EXCL_LINE
200 if ((errorCode = sound_manager_get_type_from_stream_information(stream_info, &type)) != SOUND_MANAGER_ERROR_NONE)
201 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->stream_type is invalid [ret:%d]", errorCode); //LCOV_EXCL_LINE
202 if (mDirection == CAudioInfo::EAudioDirection::AUDIO_DIRECTION_IN)
203 getAudioInfo().setAudioTypeByInputStreamType(type);
205 getAudioInfo().setAudioTypeByOutputStreamType(type);
207 if ((errorCode = sound_manager_get_index_from_stream_information(stream_info, &index)) != SOUND_MANAGER_ERROR_NONE)
208 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->index is invalid [ret:%d]", errorCode); //LCOV_EXCL_LINE
209 getAudioInfo().setAudioIndex(index);
211 if (mDirection == CAudioInfo::EAudioDirection::AUDIO_DIRECTION_IN) {
214 sound_effect_method_with_reference_e method_reference;
216 if (sound_manager_get_effect_method(stream_info, &method) == SOUND_MANAGER_ERROR_NONE)
217 getAudioInfo().setEffectMethod(method);
219 if (sound_manager_get_effect_method_with_reference(stream_info, &method_reference, &device_id) == SOUND_MANAGER_ERROR_NONE)
220 getAudioInfo().setEffectMethodWithReference(method_reference, device_id);