1 /*
2  * Copyright 2020 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 package org.hyphonate.megaaudio.player;
17 
18 import android.media.AudioTimestamp;
19 
20 public class OboePlayer extends Player {
21     boolean mPlaying;
22 
23     private int mPlayerSubtype;
24     private long mNativePlayer;
25 
26     private AudioSource mAudioSource;
27 
OboePlayer(AudioSourceProvider sourceProvider, int playerSubtype)28     public OboePlayer(AudioSourceProvider sourceProvider, int playerSubtype) {
29         super(sourceProvider);
30 
31         mPlayerSubtype = playerSubtype;
32         NativeAudioSource nativeAudioSource = mSourceProvider.getNativeSource();
33         if (nativeAudioSource != null) {
34             mAudioSource = nativeAudioSource;
35             mNativePlayer = allocNativePlayer(nativeAudioSource.getNativeObject(), mPlayerSubtype);
36         } else {
37             mAudioSource = mSourceProvider.getJavaSource();
38             mNativePlayer = allocNativePlayer(
39                     JavaSourceProxy.allocNativeSource(mAudioSource), mPlayerSubtype);
40         }
41     }
42 
43     @Override
getNumBufferFrames()44     public int getNumBufferFrames() {
45         return getBufferFrameCountN(mNativePlayer);
46     }
47 
48     @Override
getRoutedDeviceId()49     public int getRoutedDeviceId() {
50         return getRoutedDeviceIdN(mNativePlayer);
51     }
52 
53     @Override
getAudioSource()54     public AudioSource getAudioSource() {
55         return mAudioSource;
56     }
57 
58     @Override
isPlaying()59     public boolean isPlaying() { return mPlaying; }
60 
61     @Override
setupStream(int channelCount, int sampleRate, int numBurstFrames)62     public int setupStream(int channelCount, int sampleRate, int numBurstFrames) {
63         mChannelCount = channelCount;
64         mSampleRate = sampleRate;
65         return setupStreamN(
66                 mNativePlayer, channelCount, sampleRate,
67                 mRouteDevice == null ? -1 : mRouteDevice.getId());
68     }
69 
70     @Override
teardownStream()71     public int teardownStream() {
72         int errCode = teardownStreamN(mNativePlayer);
73 
74         mChannelCount = 0;
75         mSampleRate = 0;
76 
77         return errCode;
78     }
79 
80     @Override
startStream()81     public int startStream() {
82         return startStreamN(mNativePlayer, mPlayerSubtype);
83     }
84 
85     @Override
stopStream()86     public int stopStream() {
87         mPlaying = false;
88 
89         return stopN(mNativePlayer);
90     }
91 
92     /**
93      * Gets a timestamp from the audio stream
94      * @param timestamp
95      * @return
96      */
getTimestamp(AudioTimestamp timestamp)97     public boolean getTimestamp(AudioTimestamp timestamp) {
98         return getTimestampN(mNativePlayer, timestamp);
99     }
100 
allocNativePlayer(long nativeSource, int playerSubtype)101     private native long allocNativePlayer(long nativeSource, int playerSubtype);
102 
setupStreamN(long nativePlayer, int channelCount, int sampleRate, int routeDeviceId)103     private native int setupStreamN(long nativePlayer, int channelCount, int sampleRate, int routeDeviceId);
teardownStreamN(long nativePlayer)104     private native int teardownStreamN(long nativePlayer);
105 
startStreamN(long nativePlayer, int playerSubtype)106     private native int startStreamN(long nativePlayer, int playerSubtype);
stopN(long nativePlayer)107     private native int stopN(long nativePlayer);
108 
getBufferFrameCountN(long mNativePlayer)109     private native int getBufferFrameCountN(long mNativePlayer);
110 
getRoutedDeviceIdN(long nativePlayer)111     private native int getRoutedDeviceIdN(long nativePlayer);
112 
getTimestampN(long nativePlayer, AudioTimestamp timestamp)113     private native boolean getTimestampN(long nativePlayer, AudioTimestamp timestamp);
114 }
115