1 /* 2 * Copyright (C) 2011 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 #ifndef FRAMEWORKS_EX_VARIABLESPEED_JNI_VARIABLESPEED_H_ 18 #define FRAMEWORKS_EX_VARIABLESPEED_JNI_VARIABLESPEED_H_ 19 20 #include <jni.h> 21 22 #include <SLES/OpenSLES.h> 23 #include <SLES/OpenSLES_Android.h> 24 #include <SLES/OpenSLES_AndroidConfiguration.h> 25 26 #include <integral_types.h> 27 #include <utils/threads.h> 28 29 #include <profile_timer.h> 30 #include <decode_buffer.h> 31 32 #include <queue> 33 #include <stack> 34 35 namespace video_editing { 36 class SolaTimeScaler; 37 } 38 39 // This is the audio engine class. 40 // It forms the bulk of the variablespeed library. 41 // It should not be used directly, but rather used indirectly from the java 42 // native methods. 43 class AudioEngine { 44 public: 45 AudioEngine(size_t targetFrames, float windowDuration, 46 float windowOverlapDuration, size_t maxPlayBufferCount, 47 float initialRate, size_t decodeInitialSize, size_t decodeMaxSize, 48 size_t startPositionMillis, int audioStreamType); 49 virtual ~AudioEngine(); 50 51 bool PlayUri(const char* uri); 52 bool PlayFileDescriptor(int fd, int64 offset, int64 length); 53 void SetVariableSpeed(float speed); 54 void RequestStart(); 55 void RequestStop(); 56 int GetCurrentPosition(); 57 int GetTotalDuration(); 58 59 void DecodingBufferQueueCallback( 60 SLAndroidSimpleBufferQueueItf queueItf, void *context); 61 void DecodingEventCallback(SLPlayItf caller, SLuint32 event); 62 void PrefetchEventCallback(SLPrefetchStatusItf caller, SLuint32 event); 63 void PlayingBufferQueueCallback(); 64 65 static AudioEngine* GetEngine(); 66 static void SetEngine(AudioEngine* engine); 67 static bool CompareAndSetEngine(AudioEngine* expect, AudioEngine* update); 68 static void DeleteEngine(); 69 70 private: 71 bool PlayFromThisSource(const SLDataSource& audioSrc); 72 void EnqueueMoreAudioIfNecessary(SLAndroidSimpleBufferQueueItf bufferQueue); 73 bool EnqueueNextBufferOfAudio(SLAndroidSimpleBufferQueueItf bufferQueue); 74 void PrefetchDurationSampleRateAndChannels( 75 SLPlayItf playItf, SLPrefetchStatusItf prefetchItf); 76 video_editing::SolaTimeScaler* GetTimeScaler(); 77 bool Finished(); 78 bool GetWasStartRequested(); 79 bool GetWasStopRequested(); 80 void ClearRequestStart(); 81 void SetEndOfDecoderReached(); 82 bool GetEndOfDecoderReached(); 83 bool DecodeBufferTooFull(); 84 void ClearDecodeBuffer(); 85 bool IsDecodeBufferEmpty(); 86 bool GetHasReachedPlayingBuffersLimit(); 87 bool HasSampleRateAndChannels(); 88 SLuint32 GetSLSampleRate(); 89 SLuint32 GetSLChannels(); 90 SLuint32 GetChannelCount(); 91 92 // The single global audio engine instance. 93 static AudioEngine* audioEngine_; 94 95 // Protects access to the shared decode buffer. 96 android::Mutex decodeBufferLock_; 97 // Buffer into which we put the audio data as we decode. 98 // Protected by decodeBufferLock_. 99 DecodeBuffer decodeBuffer_; 100 101 // Protects access to the playingBuffers_ and freeBuffers_. 102 android::Mutex playBufferLock_; 103 // The buffers we're using for playback. 104 std::queue<int16*> playingBuffers_; 105 std::stack<int16*> freeBuffers_; 106 107 // The time scaler. 108 video_editing::SolaTimeScaler* timeScaler_; 109 110 // The frame buffer, used for converting between PCM data and float for 111 // time scaler. 112 float* floatBuffer_; 113 float* injectBuffer_; 114 115 // Required when we create the audio player. 116 // Set during the first callback from the decoder. 117 // Guarded by callbackLock_. 118 SLuint32 mSampleRate; 119 SLuint32 mChannels; 120 121 size_t targetFrames_; 122 float windowDuration_; 123 float windowOverlapDuration_; 124 size_t maxPlayBufferCount_; 125 float initialRate_; 126 size_t startPositionMillis_; 127 // The type of audio stream as defined by the STREAM_XXX constants in 128 // android.media.AudioManager. These constant values actually match the 129 // corresponding SL_ANDROID_STREAM_XXX constants defined by 130 // include/SLES/OpenSLES_AndroidConfiguration.h 131 int audioStreamType_; 132 133 // The prefetch callback signal, for letting the prefetch callback method 134 // indicate when it is done. 135 android::Mutex prefetchLock_; 136 android::Condition prefetchCondition_; 137 138 // Protects access to the CallbackContext object. 139 // I don't believe this to be necessary, I think that it's thread-confined, 140 // but it also won't do any harm. 141 android::Mutex callbackLock_; 142 143 // Protects access to the shared member variables below. 144 android::Mutex lock_; 145 // Protected by lock_. 146 // Stores the total duration of the track. 147 SLmillisecond totalDurationMs_; 148 // Protected by lock_. 149 // Stores the current position of the decoder head. 150 SLmillisecond decoderCurrentPosition_; 151 // Protected by lock_. 152 // Set externally via RequestStart(), this determines when we begin to 153 // playback audio. 154 // Until this is set to true, our audio player will remain stopped. 155 bool startRequested_; 156 // Protected by lock_. 157 // Set externally via RequestStop(), this tells us top stop playing 158 // and therefore shut everything down. 159 bool stopRequested_; 160 // Protected by lock_. 161 // This is set to true once we reach the end of the decoder stream. 162 bool finishedDecoding_; 163 164 DISALLOW_COPY_AND_ASSIGN(AudioEngine); 165 }; 166 167 #endif // FRAMEWORKS_EX_VARIABLESPEED_JNI_VARIABLESPEED_H_ 168