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.
17 #ifndef __TIZEN_MEDIA_AUDIO_IO_CAUDIO_IO_H__
18 #define __TIZEN_MEDIA_AUDIO_IO_CAUDIO_IO_H__
22 #include <sound_manager.h>
24 #include <condition_variable>
26 namespace tizen_media_audio {
31 * You can't take this object directly.
33 class IPulseStreamListener;
34 class CAudioIO : public IPulseStreamListener {
36 struct SStreamCallback {
38 void (*onStream)(size_t nbytes, void* user_data);
40 SStreamCallback() : mUserData(nullptr), onStream(nullptr) {
44 void set(void (*callback)(size_t, void*), void* userdata) {
55 struct SStateChangedCallback {
57 void (*onStateChanged)(CAudioInfo::EAudioIOState state, CAudioInfo::EAudioIOState statePrev, bool byPolicy, void* user_data);
59 SStateChangedCallback() : mUserData(nullptr), onStateChanged(nullptr) {
63 void set(void (*callback)(CAudioInfo::EAudioIOState, CAudioInfo::EAudioIOState, bool, void*), void* userdata) {
64 onStateChanged = callback;
69 onStateChanged = nullptr;
74 /* Constructor & Destructor */
76 explicit CAudioIO(CAudioInfo& audioInfo);
77 virtual ~CAudioIO() = default;
79 /* Implemented Handlers */
80 virtual void onStream(CPulseAudioClient* pClient, size_t length);
81 virtual void onStateChanged(CAudioInfo::EAudioIOState state, bool byPolicy);
82 virtual void onStateChanged(CAudioInfo::EAudioIOState state);
85 virtual void initialize();
86 virtual void finalize();
87 virtual void prepare();
88 virtual void unprepare();
90 virtual void resume();
92 virtual int getBufferSize() = 0;
94 CAudioInfo& getAudioInfo();
96 virtual void setStreamCallback(SStreamCallback callback);
97 SStreamCallback getStreamCallback();
99 virtual void setStateChangedCallback(SStateChangedCallback callback);
100 SStateChangedCallback getStateChangedCallback();
102 void setStreamInfo(sound_stream_info_h stream_info);
104 void setState(CAudioInfo::EAudioIOState state);
105 CAudioInfo::EAudioIOState getState() noexcept;
108 /* Protected Methods */
109 virtual void setInit(bool flag);
110 virtual bool isInit();
111 virtual bool IsReady();
113 CPulseAudioClient* mpPulseAudioClient;
114 CAudioInfo mAudioInfo;
116 SStreamCallback mStreamCallback;
117 SStateChangedCallback mStateChangedCallback;
119 CAudioInfo::EAudioDirection mDirection;
120 CAudioInfo::EAudioIOState mState;
121 CAudioInfo::EAudioIOState mStatePrev;
123 std::timed_mutex mMutex;
124 std::mutex mCondMutex;
125 std::condition_variable mCond;
132 } /* namespace tizen_media_audio */
135 #endif /* __TIZEN_MEDIA_AUDIO_IO_CAUDIO_IO_H__ */