1 /*
2  * AudioSystem.cpp
3  *
4  * Copyright (C) 2009-2012  Belledonne Communications, Grenoble, France
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20 
21 #include "AudioSystem.h"
22 #include "mediastreamer2/mscommon.h"
23 
24 namespace fake_android{
25 
getOutputSamplingRate(int * rate,audio_stream_type_t streamType)26 status_t AudioSystem::getOutputSamplingRate(int *rate, audio_stream_type_t streamType){
27 	return AudioSystemImpl::get()->mGetOutputSamplingRate.invoke(rate,streamType);
28 }
29 
getOutputSamplingRate(int * rate,int streamType)30 status_t AudioSystem::getOutputSamplingRate(int *rate, int streamType){
31 	return AudioSystemImpl::get()->mGetOutputSamplingRate.invoke(rate,streamType);
32 }
33 
getOutputFrameCount(int * frameCount,audio_stream_type_t streamType)34 status_t AudioSystem::getOutputFrameCount(int *frameCount, audio_stream_type_t streamType) {
35 	return AudioSystemImpl::get()->mGetOutputFrameCount.invoke(frameCount, streamType);
36 }
37 
getOutputFrameCount(int * frameCount,int streamType)38 status_t AudioSystem::getOutputFrameCount(int *frameCount, int streamType) {
39 	return AudioSystemImpl::get()->mGetOutputFrameCount.invoke(frameCount, streamType);
40 }
41 
getOutputLatency(uint32_t * latency,audio_stream_type_t streamType)42 status_t AudioSystem::getOutputLatency(uint32_t *latency, audio_stream_type_t streamType) {
43 	return AudioSystemImpl::get()->mGetOutputLatency.invoke(latency, streamType);
44 }
45 
getOutputLatency(uint32_t * latency,int streamType)46 status_t AudioSystem::getOutputLatency(uint32_t *latency, int streamType) {
47 	return AudioSystemImpl::get()->mGetOutputLatency.invoke(latency, streamType);
48 }
49 
setParameters(audio_io_handle_t ioHandle,const String8 & keyValuePairs)50 status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs){
51 	return AudioSystemImpl::get()->mSetParameters.invoke(ioHandle,keyValuePairs);
52 }
53 
setPhoneState(audio_mode_t state)54 status_t AudioSystem::setPhoneState(audio_mode_t state) {
55 	return AudioSystemImpl::get()->mSetPhoneState.invoke(state);
56 }
57 
setForceUse(audio_policy_force_use_t usage,audio_policy_forced_cfg_t config)58 status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) {
59 	return AudioSystemImpl::get()->mSetForceUse.invoke(usage, config);
60 }
61 
newAudioSessionId()62 int AudioSystem::newAudioSessionId(){
63 	if (AudioSystemImpl::get()->mNewAudioSessionId.isFound())
64 		return AudioSystemImpl::get()->mNewAudioSessionId.invoke();
65 	else{
66 		ms_warning("AudioSystem::newAudioSessionId() not found.");
67 		return -1;
68 	}
69 }
70 
getInput(audio_source_t inputSource,uint32_t samplingRate,audio_format_t format,uint32_t channels,audio_in_acoustics_t acoustics,int sessionId)71 audio_io_handle_t AudioSystem::getInput(audio_source_t inputSource,
72                                     uint32_t samplingRate,
73                                     audio_format_t format,
74                                     uint32_t channels,
75                                     audio_in_acoustics_t acoustics,
76                                     int sessionId){
77 	ms_error("AudioSystem::getInput() not implemented.");
78 	return 0;
79 	//return AudioSystemImpl::get()->mGetInput.invoke(inputSource,samplingRate,format,channels,acoustics,sessionId);
80 }
81 
82 
AudioSystemImpl(Library * lib)83 AudioSystemImpl::AudioSystemImpl(Library *lib) :
84 	// By default, try to load Android 2.3 symbols
85 	mGetOutputSamplingRate(lib,"_ZN7android11AudioSystem21getOutputSamplingRateEPii"),
86 	mGetOutputFrameCount(lib, "_ZN7android11AudioSystem19getOutputFrameCountEPii"),
87 	mGetOutputLatency(lib, "_ZN7android11AudioSystem16getOutputLatencyEPji"),
88 	mSetParameters(lib,"_ZN7android11AudioSystem13setParametersEiRKNS_7String8E"),
89 	mSetPhoneState(lib, "_ZN7android11AudioSystem13setPhoneStateEi"),
90 	mSetForceUse(lib, "_ZN7android11AudioSystem11setForceUseENS0_9force_useENS0_13forced_configE"),
91 	mNewAudioSessionId(lib,"_ZN7android11AudioSystem17newAudioSessionIdEv"){
92 	//mGetInput(lib,"_ZN7android11AudioSystem8getInputEijjjNS0_18audio_in_acousticsE"){
93 	mApi18=false;
94 	// Try some Android 4.0 symbols if not found
95 	if (!mSetForceUse.isFound()) {
96 		mSetForceUse.load(lib, "_ZN7android11AudioSystem11setForceUseE24audio_policy_force_use_t25audio_policy_forced_cfg_t");
97 	}
98 
99 	// Then try some Android 4.1 symbols if still not found
100 	if (!mGetOutputSamplingRate.isFound()) {
101 		mGetOutputSamplingRate.load(lib, "_ZN7android11AudioSystem21getOutputSamplingRateEPi19audio_stream_type_t");
102 		if (!mGetOutputSamplingRate.isFound()){
103 			mGetOutputSamplingRate.load(lib,"_ZN7android11AudioSystem21getOutputSamplingRateEPj19audio_stream_type_t");
104 			mApi18=true;
105 		}
106 	}
107 	if (!mGetOutputFrameCount.isFound()) {
108 		mGetOutputFrameCount.load(lib, "_ZN7android11AudioSystem19getOutputFrameCountEPi19audio_stream_type_t");
109 		if (!mGetOutputFrameCount.isFound())
110 			mGetOutputFrameCount.load(lib,"_ZN7android11AudioSystem19getOutputFrameCountEPj19audio_stream_type_t");
111 	}
112 	if (!mGetOutputLatency.isFound()) {
113 		mGetOutputLatency.load(lib, "_ZN7android11AudioSystem16getOutputLatencyEPj19audio_stream_type_t");
114 	}
115 	if (!mSetPhoneState.isFound()) {
116 		mSetPhoneState.load(lib, "_ZN7android11AudioSystem13setPhoneStateE12audio_mode_t");
117 	}
118 	if (!mNewAudioSessionId.isFound()){
119 		//android 5.0 symbol
120 		mNewAudioSessionId.load(lib, "_ZN7android11AudioSystem16newAudioUniqueIdEv");
121 	}
122 }
123 
init(Library * lib)124 bool AudioSystemImpl::init(Library *lib){
125 	bool fail=false;
126 	AudioSystemImpl *impl=new AudioSystemImpl(lib);
127 
128 	if (!impl->mGetOutputSamplingRate.isFound()){
129 		ms_error("AudioSystem::getOutputSamplingRate() not found.");
130 		fail=true;
131 	}
132 	if (!impl->mGetOutputFrameCount.isFound()) {
133 		ms_error("AudioSystem::getOutputFrameCount() not found.");
134 		fail=true;
135 	}
136 	if (!impl->mGetOutputLatency.isFound()){
137 		ms_error("AudioSystem::getOutputLatency() not found.");
138 		fail=true;
139 	}
140 	if (!impl->mSetParameters.isFound()) {
141 		ms_error("AudioSystem::setParameters() not found.");
142 		fail=true;
143 	}
144 	if (!impl->mSetPhoneState.isFound()){
145 		ms_error("AudioSystem::setPhoneState() not found.");
146 		fail=true;
147 	}
148 	if (!impl->mSetForceUse.isFound()) {
149 		ms_error("AudioSystem::setForceUse() not found.");
150 		fail=true;
151 	}
152 	//if (!impl->mGetInput.isFound()) goto fail;
153 
154 	if (!fail){
155 		sImpl=impl;
156 		return true;
157 	}else{
158 		delete impl;
159 		return false;
160 	}
161 }
162 
163 AudioSystemImpl *AudioSystemImpl::sImpl=NULL;
164 
init(Library * lib)165 bool RefBaseImpl::init(Library *lib){
166 	RefBaseImpl *impl=new RefBaseImpl(lib);
167 	bool fail=false;
168 
169 	if (!impl->mIncStrong.isFound()){
170 		ms_error("RefBase::incStrong() not found");
171 		fail=true;
172 	}
173 	if (!impl->mDecStrong.isFound()){
174 		ms_error("RefBase::decStrong() not found");
175 		fail=true;
176 	}
177 	if (fail){
178 		delete impl;
179 		return false;
180 	}else{
181 		sImpl=impl;
182 		return true;
183 	}
184 }
185 
186 RefBaseImpl * RefBaseImpl::sImpl=0;
187 
188 
RefBaseImpl(Library * lib)189 RefBaseImpl::RefBaseImpl(Library *lib) :
190 	mCtor(lib,"_ZN7android7RefBaseC2Ev"),
191 	mIncStrong(lib,"_ZNK7android7RefBase9incStrongEPKv"),
192 	mDecStrong(lib,"_ZNK7android7RefBase9decStrongEPKv"),
193 	mGetStrongCount(lib,"_ZNK7android7RefBase14getStrongCountEv")
194 {
195 
196 }
197 
RefBase()198 RefBase::RefBase(){
199 	mImpl=RefBaseImpl::get();
200 	mCnt=0;
201 }
202 
~RefBase()203 RefBase::~RefBase(){
204 }
205 
incStrong(const void * id) const206 void RefBase::incStrong(const void* id) const{
207 	mCnt++;
208 	if (isRefCounted()) {
209 		ms_message("incStrong(%p)",getRealThis());
210 		mImpl->mIncStrong.invoke(getRealThis(),this);
211 	}
212 }
213 
decStrong(const void * id) const214 void RefBase::decStrong(const void* id) const{
215 	if (isRefCounted()) {
216 		ms_message("decStrong(%p)",getRealThis());
217 		mImpl->mDecStrong.invoke(getRealThis(),this);
218 	}
219 	mCnt--;
220 	if (mCnt==0){
221 		if (!isRefCounted()){
222 			destroy();
223 		}
224 		delete this;
225 	}
226 }
227 
getStrongCount() const228 int32_t RefBase::getStrongCount() const{
229 	return mImpl->mGetStrongCount.invoke(getRealThis());
230 }
231 
findRefbaseOffset(void * obj,size_t size)232 ptrdiff_t findRefbaseOffset(void *obj, size_t size){
233 	uint8_t *base_vptr=(uint8_t*)*(void **)obj;
234 	const long vptrMemRange=0x1000000;
235 	size_t i;
236 	int ret=-1;
237 
238 	if (base_vptr==NULL){
239 		ms_warning("findRefbaseOffset(): no base vptr");
240 	}
241 	ms_message("base_vptr is %p for obj %p",base_vptr, obj);
242 	for (i=((size/sizeof(void*))-1)*sizeof(void*);i>0;i-=sizeof(void*)){
243 		uint8_t *ptr= ((uint8_t*)obj) + i;
244 		uint8_t *candidate=(uint8_t*)*(void**)ptr;
245 		if (i!=0 && labs((ptrdiff_t)(candidate-base_vptr))<vptrMemRange){
246 			ret=i;
247 			break;
248 		}
249 	}
250 	if (ret==-1) ms_message("findRefbaseOffset(): no refbase vptr found");
251 	return ret;
252 }
253 
dumpMemory(void * obj,size_t size)254 void dumpMemory(void *obj, size_t size){
255 	size_t i;
256 	ms_message("Dumping memory at %p",obj);
257 	for (i=0;i<size;i+=sizeof(long)){
258 		ms_message("%4i\t%lx",(int)i,*(long*)(((uint8_t*)obj)+i));
259 	}
260 }
261 
262 }
263 
264