variablespeed.h revision b83ad73794088498d6d38cd3b4fc9311f505d051
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 35namespace 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. 43class AudioEngine { 44 public: 45 AudioEngine(size_t channels, size_t sampleRate, size_t targetFrames, 46 float windowDuration, float windowOverlapDuration, 47 size_t maxPlayBufferCount, float initialRate, size_t decodeInitialSize, 48 size_t decodeMaxSize, size_t startPositionMillis); 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 void DeleteEngine(); 68 69 private: 70 bool PlayFromThisSource(const SLDataSource& audioSrc); 71 void EnqueueMoreAudioIfNecessary(SLAndroidSimpleBufferQueueItf bufferQueue); 72 bool EnqueueNextBufferOfAudio(SLAndroidSimpleBufferQueueItf bufferQueue); 73 void PrefetchDurationSampleRateAndChannels( 74 SLPlayItf playItf, SLPrefetchStatusItf prefetchItf); 75 video_editing::SolaTimeScaler* GetTimeScaler(); 76 bool Finished(); 77 bool GetWasStartRequested(); 78 bool GetWasStopRequested(); 79 void ClearRequestStart(); 80 void SetEndOfDecoderReached(); 81 bool GetEndOfDecoderReached(); 82 bool DecodeBufferTooFull(); 83 void ClearDecodeBuffer(); 84 bool IsDecodeBufferEmpty(); 85 bool GetHasReachedPlayingBuffersLimit(); 86 87 // The single global audio engine instance. 88 static AudioEngine* audioEngine_; 89 90 // Protects access to the shared decode buffer. 91 android::Mutex decodeBufferLock_; 92 // Buffer into which we put the audio data as we decode. 93 // Protected by decodeBufferLock_. 94 DecodeBuffer decodeBuffer_; 95 96 // Protects access to the playingBuffers_ and freeBuffers_. 97 android::Mutex playBufferLock_; 98 // The buffers we're using for playback. 99 std::queue<int16*> playingBuffers_; 100 std::stack<int16*> freeBuffers_; 101 102 // The time scaler. 103 video_editing::SolaTimeScaler* timeScaler_; 104 105 // The frame buffer, used for converting between PCM data and float for 106 // time scaler. 107 float* floatBuffer_; 108 float* injectBuffer_; 109 110 size_t channels_; 111 size_t sampleRate_; 112 SLuint32 slSampleRate_; 113 SLuint32 slOutputChannels_; 114 size_t targetFrames_; 115 float windowDuration_; 116 float windowOverlapDuration_; 117 size_t maxPlayBufferCount_; 118 float initialRate_; 119 size_t startPositionMillis_; 120 121 // The prefetch callback signal, for letting the prefetch callback method 122 // indicate when it is done. 123 android::Mutex prefetchLock_; 124 android::Condition prefetchCondition_; 125 126 // Protects access to the CallbackContext object. 127 // I don't believe this to be necessary, I think that it's thread-confined, 128 // but it also won't do any harm. 129 android::Mutex callbackLock_; 130 131 // Protects access to the shared member variables below. 132 android::Mutex lock_; 133 // Protected by lock_. 134 // Stores the total duration of the track. 135 SLmillisecond totalDurationMs_; 136 // Protected by lock_. 137 // Set externally via RequestStart(), this determines when we begin to 138 // playback audio. 139 // Until this is set to true, our audio player will remain stopped. 140 bool startRequested_; 141 // Protected by lock_. 142 // Set externally via RequestStop(), this tells us top stop playing 143 // and therefore shut everything down. 144 bool stopRequested_; 145 // Protected by lock_. 146 // This is set to true once we reach the end of the decoder stream. 147 bool finishedDecoding_; 148 149 DISALLOW_COPY_AND_ASSIGN(AudioEngine); 150}; 151 152#endif // FRAMEWORKS_EX_VARIABLESPEED_JNI_VARIABLESPEED_H_ 153