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.google.sample.oboe.manualtest;
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     // Write disabled because the synth is in native code.
50     @Override
write(float[] buffer, int offset, int length)51     public int write(float[] buffer, int offset, int length) {
52         return 0;
53     }
54 
55     @Override
open(StreamConfiguration requestedConfiguration, StreamConfiguration actualConfiguration, int bufferSizeInFrames)56     public void open(StreamConfiguration requestedConfiguration,
57                      StreamConfiguration actualConfiguration, int bufferSizeInFrames) throws IOException {
58         super.open(requestedConfiguration, actualConfiguration, bufferSizeInFrames);
59         int result = openNative(requestedConfiguration.getNativeApi(),
60                 requestedConfiguration.getSampleRate(),
61                 requestedConfiguration.getChannelCount(),
62                 requestedConfiguration.getFormat(),
63                 requestedConfiguration.getSharingMode(),
64                 requestedConfiguration.getPerformanceMode(),
65                 requestedConfiguration.getInputPreset(),
66                 requestedConfiguration.getDeviceId(),
67                 requestedConfiguration.getSessionId(),
68                 requestedConfiguration.getFramesPerBurst(),
69                 requestedConfiguration.getChannelConversionAllowed(),
70                 requestedConfiguration.getFormatConversionAllowed(),
71                 requestedConfiguration.getRateConversionQuality(),
72                 requestedConfiguration.isMMap(),
73                 isInput()
74         );
75         if (result < 0) {
76             streamIndex = INVALID_STREAM_INDEX;
77             throw new IOException("Open failed! result = " + result);
78         } else {
79             streamIndex = result;
80         }
81         actualConfiguration.setNativeApi(getNativeApi());
82         actualConfiguration.setSampleRate(getSampleRate());
83         actualConfiguration.setSharingMode(getSharingMode());
84         actualConfiguration.setPerformanceMode(getPerformanceMode());
85         actualConfiguration.setInputPreset(getInputPreset());
86         actualConfiguration.setFramesPerBurst(getFramesPerBurst());
87         actualConfiguration.setBufferCapacityInFrames(getBufferCapacityInFrames());
88         actualConfiguration.setChannelCount(getChannelCount());
89         actualConfiguration.setDeviceId(getDeviceId());
90         actualConfiguration.setSessionId(getSessionId());
91         actualConfiguration.setFormat(getFormat());
92         actualConfiguration.setMMap(isMMap());
93         actualConfiguration.setDirection(isInput()
94                 ? StreamConfiguration.DIRECTION_INPUT
95                 : StreamConfiguration.DIRECTION_OUTPUT);
96     }
97 
openNative( int nativeApi, int sampleRate, int channelCount, int format, int sharingMode, int performanceMode, int inputPreset, int deviceId, int sessionId, int framesPerRead, boolean channelConversionAllowed, boolean formatConversionAllowed, int rateConversionQuality, boolean isMMap, boolean isInput)98     private native int openNative(
99             int nativeApi,
100             int sampleRate,
101             int channelCount,
102             int format,
103             int sharingMode,
104             int performanceMode,
105             int inputPreset,
106             int deviceId,
107             int sessionId,
108             int framesPerRead,
109             boolean channelConversionAllowed,
110             boolean formatConversionAllowed,
111             int rateConversionQuality,
112             boolean isMMap,
113             boolean isInput);
114 
115     @Override
close()116     public void close() {
117         if (streamIndex >= 0) {
118             close(streamIndex);
119             streamIndex = INVALID_STREAM_INDEX;
120         }
121     }
close(int streamIndex)122     public native void close(int streamIndex);
123 
124     @Override
getBufferCapacityInFrames()125     public int getBufferCapacityInFrames() {
126         return getBufferCapacityInFrames(streamIndex);
127     }
getBufferCapacityInFrames(int streamIndex)128     private native int getBufferCapacityInFrames(int streamIndex);
129 
130     @Override
getBufferSizeInFrames()131     public int getBufferSizeInFrames() {
132         return getBufferSizeInFrames(streamIndex);
133     }
getBufferSizeInFrames(int streamIndex)134     private native int getBufferSizeInFrames(int streamIndex);
135 
136     @Override
isThresholdSupported()137     public boolean isThresholdSupported() {
138         return true;
139     }
140 
141     @Override
setBufferSizeInFrames(int thresholdFrames)142     public int setBufferSizeInFrames(int thresholdFrames) {
143         return setBufferSizeInFrames(streamIndex, thresholdFrames);
144     }
setBufferSizeInFrames(int streamIndex, int thresholdFrames)145     private native int setBufferSizeInFrames(int streamIndex, int thresholdFrames);
146 
getNativeApi()147     public int getNativeApi() {
148         return getNativeApi(streamIndex);
149     }
getNativeApi(int streamIndex)150     public native int getNativeApi(int streamIndex);
151 
152     @Override
getFramesPerBurst()153     public int getFramesPerBurst() {
154         return getFramesPerBurst(streamIndex);
155     }
getFramesPerBurst(int streamIndex)156     public native int getFramesPerBurst(int streamIndex);
157 
getSharingMode()158     public int getSharingMode() {
159         return getSharingMode(streamIndex);
160     }
getSharingMode(int streamIndex)161     public native int getSharingMode(int streamIndex);
162 
getPerformanceMode()163     public int getPerformanceMode() {
164         return getPerformanceMode(streamIndex);
165     }
getPerformanceMode(int streamIndex)166     public native int getPerformanceMode(int streamIndex);
167 
getInputPreset()168     public int getInputPreset() {
169         return getInputPreset(streamIndex);
170     }
getInputPreset(int streamIndex)171     public native int getInputPreset(int streamIndex);
172 
getSampleRate()173     public int getSampleRate() {
174         return getSampleRate(streamIndex);
175     }
getSampleRate(int streamIndex)176     public native int getSampleRate(int streamIndex);
177 
getFormat()178     public int getFormat() {
179         return getFormat(streamIndex);
180     }
getFormat(int streamIndex)181     public native int getFormat(int streamIndex);
182 
getChannelCount()183     public int getChannelCount() {
184         return getChannelCount(streamIndex);
185     }
getChannelCount(int streamIndex)186     public native int getChannelCount(int streamIndex);
187 
getDeviceId()188     public int getDeviceId() {
189         return getDeviceId(streamIndex);
190     }
getDeviceId(int streamIndex)191     public native int getDeviceId(int streamIndex);
192 
getSessionId()193     public int getSessionId() {
194         return getSessionId(streamIndex);
195     }
getSessionId(int streamIndex)196     public native int getSessionId(int streamIndex);
197 
isMMap()198     public boolean isMMap() {
199         return isMMap(streamIndex);
200     }
isMMap(int streamIndex)201     public native boolean isMMap(int streamIndex);
202 
203     @Override
getCallbackCount()204     public native long getCallbackCount(); // TODO Move to another class?
205 
206     @Override
getLastErrorCallbackResult()207     public int getLastErrorCallbackResult() {
208         return getLastErrorCallbackResult(streamIndex);
209     }
getLastErrorCallbackResult(int streamIndex)210     public native int getLastErrorCallbackResult(int streamIndex);
211 
212     @Override
getFramesWritten()213     public long getFramesWritten() {
214         return getFramesWritten(streamIndex);
215     }
getFramesWritten(int streamIndex)216     public native long getFramesWritten(int streamIndex);
217 
218     @Override
getFramesRead()219     public long getFramesRead() {
220         return getFramesRead(streamIndex);
221     }
getFramesRead(int streamIndex)222     public native long getFramesRead(int streamIndex);
223 
224     @Override
getXRunCount()225     public int getXRunCount() {
226         return getXRunCount(streamIndex);
227     }
getXRunCount(int streamIndex)228     public native int getXRunCount(int streamIndex);
229 
230     @Override
getLatency()231     public double getLatency() {
232         return getTimestampLatency(streamIndex);
233     }
getTimestampLatency(int streamIndex)234     public native double getTimestampLatency(int streamIndex);
235 
236     @Override
getCpuLoad()237     public double getCpuLoad() {
238         return getCpuLoad(streamIndex);
239     }
getCpuLoad(int streamIndex)240     public native double getCpuLoad(int streamIndex);
241 
242     @Override
setWorkload(double workload)243     public native void setWorkload(double workload);
244 
245     @Override
getState()246     public int getState() {
247         return getState(streamIndex);
248     }
getState(int streamIndex)249     public native int getState(int streamIndex);
250 
setCallbackReturnStop(boolean b)251     public static native void setCallbackReturnStop(boolean b);
252 
setUseCallback(boolean checked)253     public static native void setUseCallback(boolean checked);
254 
setCallbackSize(int callbackSize)255     public static native void setCallbackSize(int callbackSize);
256 
getOboeVersionNumber()257     public static native int getOboeVersionNumber();
258 
259 }
260