1 /* 2 * Copyright 2017 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17 package com.mobileer.oboetester; 18 19 import java.io.IOException; 20 21 /** 22 * Implementation of an AudioStreamBase using Oboe. 23 */ 24 25 abstract class OboeAudioStream extends AudioStreamBase { 26 private static final int INVALID_STREAM_INDEX = -1; 27 int streamIndex = INVALID_STREAM_INDEX; 28 29 @Override stopPlayback()30 public void stopPlayback() throws IOException { 31 int result = stopPlaybackNative(); 32 if (result != 0) { 33 throw new IOException("Stop Playback failed! result = " + result); 34 } 35 } 36 stopPlaybackNative()37 public native int stopPlaybackNative(); 38 39 @Override startPlayback()40 public void startPlayback() throws IOException { 41 int result = startPlaybackNative(); 42 if (result != 0) { 43 throw new IOException("Start Playback failed! result = " + result); 44 } 45 } 46 startPlaybackNative()47 public native int startPlaybackNative(); 48 49 @Override open(StreamConfiguration requestedConfiguration, StreamConfiguration actualConfiguration, int bufferSizeInFrames)50 public void open(StreamConfiguration requestedConfiguration, 51 StreamConfiguration actualConfiguration, int bufferSizeInFrames) throws IOException { 52 super.open(requestedConfiguration, actualConfiguration, bufferSizeInFrames); 53 int result = openNative(requestedConfiguration.getNativeApi(), 54 requestedConfiguration.getSampleRate(), 55 requestedConfiguration.getChannelCount(), 56 requestedConfiguration.getChannelMask(), 57 requestedConfiguration.getFormat(), 58 requestedConfiguration.getSharingMode(), 59 requestedConfiguration.getPerformanceMode(), 60 requestedConfiguration.getInputPreset(), 61 requestedConfiguration.getUsage(), 62 requestedConfiguration.getContentType(), 63 requestedConfiguration.getBufferCapacityInFrames(), 64 requestedConfiguration.getDeviceId(), 65 requestedConfiguration.getSessionId(), 66 requestedConfiguration.getChannelConversionAllowed(), 67 requestedConfiguration.getFormatConversionAllowed(), 68 requestedConfiguration.getRateConversionQuality(), 69 requestedConfiguration.isMMap(), 70 isInput(), 71 requestedConfiguration.getSpatializationBehavior() 72 ); 73 if (result < 0) { 74 streamIndex = INVALID_STREAM_INDEX; 75 String message = "Open " 76 + (isInput() ? "Input" : "Output") 77 + " failed! result = " + result + ", " 78 + StreamConfiguration.convertErrorToText(result); 79 throw new IOException(message); 80 } else { 81 streamIndex = result; 82 } 83 actualConfiguration.setNativeApi(getNativeApi()); 84 actualConfiguration.setSampleRate(getSampleRate()); 85 actualConfiguration.setSharingMode(getSharingMode()); 86 actualConfiguration.setPerformanceMode(getPerformanceMode()); 87 actualConfiguration.setInputPreset(getInputPreset()); 88 actualConfiguration.setUsage(getUsage()); 89 actualConfiguration.setContentType(getContentType()); 90 actualConfiguration.setFramesPerBurst(getFramesPerBurst()); 91 actualConfiguration.setBufferCapacityInFrames(getBufferCapacityInFrames()); 92 actualConfiguration.setChannelCount(getChannelCount()); 93 actualConfiguration.setChannelMask(getChannelMask()); 94 actualConfiguration.setDeviceId(getDeviceId()); 95 actualConfiguration.setSessionId(getSessionId()); 96 actualConfiguration.setFormat(getFormat()); 97 actualConfiguration.setMMap(isMMap()); 98 actualConfiguration.setDirection(isInput() 99 ? StreamConfiguration.DIRECTION_INPUT 100 : StreamConfiguration.DIRECTION_OUTPUT); 101 actualConfiguration.setHardwareChannelCount(getHardwareChannelCount()); 102 actualConfiguration.setHardwareSampleRate(getHardwareSampleRate()); 103 actualConfiguration.setHardwareFormat(getHardwareFormat()); 104 actualConfiguration.setSpatializationBehavior(getSpatializationBehavior()); 105 } 106 openNative( int nativeApi, int sampleRate, int channelCount, int channelMask, int format, int sharingMode, int performanceMode, int inputPreset, int usage, int contentType, int bufferCapacityInFrames, int deviceId, int sessionId, boolean channelConversionAllowed, boolean formatConversionAllowed, int rateConversionQuality, boolean isMMap, boolean isInput, int spatializationBehavior)107 private native int openNative( 108 int nativeApi, 109 int sampleRate, 110 int channelCount, 111 int channelMask, 112 int format, 113 int sharingMode, 114 int performanceMode, 115 int inputPreset, 116 int usage, 117 int contentType, 118 int bufferCapacityInFrames, 119 int deviceId, 120 int sessionId, 121 boolean channelConversionAllowed, 122 boolean formatConversionAllowed, 123 int rateConversionQuality, 124 boolean isMMap, 125 boolean isInput, 126 int spatializationBehavior); 127 128 @Override close()129 public void close() { 130 if (streamIndex >= 0) { 131 close(streamIndex); 132 streamIndex = INVALID_STREAM_INDEX; 133 } 134 } close(int streamIndex)135 public native void close(int streamIndex); 136 137 @Override getBufferCapacityInFrames()138 public int getBufferCapacityInFrames() { 139 return getBufferCapacityInFrames(streamIndex); 140 } getBufferCapacityInFrames(int streamIndex)141 private native int getBufferCapacityInFrames(int streamIndex); 142 143 @Override getBufferSizeInFrames()144 public int getBufferSizeInFrames() { 145 return getBufferSizeInFrames(streamIndex); 146 } getBufferSizeInFrames(int streamIndex)147 private native int getBufferSizeInFrames(int streamIndex); 148 149 @Override setBufferSizeInFrames(int thresholdFrames)150 public int setBufferSizeInFrames(int thresholdFrames) { 151 return setBufferSizeInFrames(streamIndex, thresholdFrames); 152 } setBufferSizeInFrames(int streamIndex, int thresholdFrames)153 private native int setBufferSizeInFrames(int streamIndex, int thresholdFrames); 154 155 @Override setPerformanceHintEnabled(boolean checked)156 public void setPerformanceHintEnabled(boolean checked) { 157 setPerformanceHintEnabled(streamIndex, checked); 158 } setPerformanceHintEnabled(int streamIndex, boolean checked)159 private native void setPerformanceHintEnabled(int streamIndex, boolean checked); 160 161 @Override setHearWorkload(boolean checked)162 public void setHearWorkload(boolean checked) { 163 setHearWorkload(streamIndex, checked); 164 } setHearWorkload(int streamIndex, boolean checked)165 private native void setHearWorkload(int streamIndex, boolean checked); 166 getNativeApi()167 public int getNativeApi() { 168 return getNativeApi(streamIndex); 169 } getNativeApi(int streamIndex)170 private native int getNativeApi(int streamIndex); 171 172 @Override getFramesPerBurst()173 public int getFramesPerBurst() { 174 return getFramesPerBurst(streamIndex); 175 } getFramesPerBurst(int streamIndex)176 private native int getFramesPerBurst(int streamIndex); 177 getSharingMode()178 public int getSharingMode() { 179 return getSharingMode(streamIndex); 180 } getSharingMode(int streamIndex)181 private native int getSharingMode(int streamIndex); 182 getPerformanceMode()183 public int getPerformanceMode() { 184 return getPerformanceMode(streamIndex); 185 } getPerformanceMode(int streamIndex)186 private native int getPerformanceMode(int streamIndex); 187 getInputPreset()188 public int getInputPreset() { 189 return getInputPreset(streamIndex); 190 } getInputPreset(int streamIndex)191 private native int getInputPreset(int streamIndex); 192 getSpatializationBehavior()193 public int getSpatializationBehavior() { 194 return getSpatializationBehavior(streamIndex); 195 } getSpatializationBehavior(int streamIndex)196 private native int getSpatializationBehavior(int streamIndex); 197 getSampleRate()198 public int getSampleRate() { 199 return getSampleRate(streamIndex); 200 } getSampleRate(int streamIndex)201 private native int getSampleRate(int streamIndex); 202 getFormat()203 public int getFormat() { 204 return getFormat(streamIndex); 205 } getFormat(int streamIndex)206 private native int getFormat(int streamIndex); 207 getUsage()208 public int getUsage() { 209 return getUsage(streamIndex); 210 } getUsage(int streamIndex)211 private native int getUsage(int streamIndex); 212 getContentType()213 public int getContentType() { 214 return getContentType(streamIndex); 215 } getContentType(int streamIndex)216 private native int getContentType(int streamIndex); 217 getChannelCount()218 public int getChannelCount() { 219 return getChannelCount(streamIndex); 220 } getChannelCount(int streamIndex)221 private native int getChannelCount(int streamIndex); 222 getChannelMask()223 public int getChannelMask() { 224 return getChannelMask(streamIndex); 225 } getChannelMask(int streamIndex)226 private native int getChannelMask(int streamIndex); 227 getHardwareChannelCount()228 public int getHardwareChannelCount() { 229 return getHardwareChannelCount(streamIndex); 230 } getHardwareChannelCount(int streamIndex)231 private native int getHardwareChannelCount(int streamIndex); 232 getHardwareSampleRate()233 public int getHardwareSampleRate() { 234 return getHardwareSampleRate(streamIndex); 235 } getHardwareSampleRate(int streamIndex)236 private native int getHardwareSampleRate(int streamIndex); 237 getHardwareFormat()238 public int getHardwareFormat() { 239 return getHardwareFormat(streamIndex); 240 } getHardwareFormat(int streamIndex)241 private native int getHardwareFormat(int streamIndex); 242 getDeviceId()243 public int getDeviceId() { 244 return getDeviceId(streamIndex); 245 } getDeviceId(int streamIndex)246 private native int getDeviceId(int streamIndex); 247 getSessionId()248 public int getSessionId() { 249 return getSessionId(streamIndex); 250 } getSessionId(int streamIndex)251 private native int getSessionId(int streamIndex); 252 253 isMMap()254 public boolean isMMap() { 255 return isMMap(streamIndex); 256 } isMMap(int streamIndex)257 private native boolean isMMap(int streamIndex); 258 259 @Override getCallbackCount()260 public native long getCallbackCount(); // TODO Move to another class? 261 262 @Override getLastErrorCallbackResult()263 public int getLastErrorCallbackResult() { 264 return getLastErrorCallbackResult(streamIndex); 265 } getLastErrorCallbackResult(int streamIndex)266 private native int getLastErrorCallbackResult(int streamIndex); 267 268 @Override getFramesWritten()269 public long getFramesWritten() { 270 return getFramesWritten(streamIndex); 271 } getFramesWritten(int streamIndex)272 private native long getFramesWritten(int streamIndex); 273 274 @Override getFramesRead()275 public long getFramesRead() { 276 return getFramesRead(streamIndex); 277 } getFramesRead(int streamIndex)278 private native long getFramesRead(int streamIndex); 279 280 @Override getXRunCount()281 public int getXRunCount() { 282 return getXRunCount(streamIndex); 283 } getXRunCount(int streamIndex)284 private native int getXRunCount(int streamIndex); 285 286 @Override getLatency()287 public double getLatency() { 288 return getTimestampLatency(streamIndex); 289 } getTimestampLatency(int streamIndex)290 private native double getTimestampLatency(int streamIndex); 291 292 @Override getCpuLoad()293 public float getCpuLoad() { 294 return getCpuLoad(streamIndex); 295 } getCpuLoad(int streamIndex)296 private native float getCpuLoad(int streamIndex); 297 298 @Override getAndResetMaxCpuLoad()299 public float getAndResetMaxCpuLoad() { 300 return getAndResetMaxCpuLoad(streamIndex); 301 } getAndResetMaxCpuLoad(int streamIndex)302 private native float getAndResetMaxCpuLoad(int streamIndex); 303 304 @Override getAndResetCpuMask()305 public int getAndResetCpuMask() { 306 return getAndResetCpuMask(streamIndex); 307 } getAndResetCpuMask(int streamIndex)308 private native int getAndResetCpuMask(int streamIndex); 309 310 @Override getCallbackTimeStr()311 public String getCallbackTimeStr() { 312 return getCallbackTimeString(); 313 } getCallbackTimeString()314 public native String getCallbackTimeString(); 315 316 @Override setWorkload(int workload)317 public native void setWorkload(int workload); 318 319 @Override getState()320 public int getState() { 321 return getState(streamIndex); 322 } getState(int streamIndex)323 private native int getState(int streamIndex); 324 setCallbackReturnStop(boolean b)325 public static native void setCallbackReturnStop(boolean b); 326 setHangTimeMillis(int hangTimeMillis)327 public static native void setHangTimeMillis(int hangTimeMillis); 328 setUseCallback(boolean checked)329 public static native void setUseCallback(boolean checked); 330 setCallbackSize(int callbackSize)331 public static native void setCallbackSize(int callbackSize); 332 getOboeVersionNumber()333 public static native int getOboeVersionNumber(); 334 335 } 336