AudioPlayer.h revision ad3af3305f024bcbbd55c894a4995e449498e1ba
1/* 2 * Copyright (C) 2009 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 AUDIO_PLAYER_H_ 18 19#define AUDIO_PLAYER_H_ 20 21#include <media/MediaPlayerInterface.h> 22#include <media/stagefright/MediaBuffer.h> 23#include <media/stagefright/TimeSource.h> 24#include <utils/threads.h> 25 26namespace android { 27 28class MediaSource; 29class AudioTrack; 30class AwesomePlayer; 31 32class AudioPlayer : public TimeSource { 33public: 34 enum { 35 REACHED_EOS, 36 SEEK_COMPLETE 37 }; 38 39 enum { 40 ALLOW_DEEP_BUFFERING = 0x01, 41 USE_OFFLOAD = 0x02 42 }; 43 44 AudioPlayer(const sp<MediaPlayerBase::AudioSink> &audioSink, 45 uint32_t flags = 0, 46 AwesomePlayer *audioObserver = NULL); 47 48 virtual ~AudioPlayer(); 49 50 // Caller retains ownership of "source". 51 void setSource(const sp<MediaSource> &source); 52 53 // Return time in us. 54 virtual int64_t getRealTimeUs(); 55 56 status_t start(bool sourceAlreadyStarted = false); 57 58 void pause(bool playPendingSamples = false); 59 void resume(); 60 61 // Returns the timestamp of the last buffer played (in us). 62 int64_t getMediaTimeUs(); 63 64 // Returns true iff a mapping is established, i.e. the AudioPlayer 65 // has played at least one frame of audio. 66 bool getMediaTimeMapping(int64_t *realtime_us, int64_t *mediatime_us); 67 68 status_t seekTo(int64_t time_us); 69 70 bool isSeeking(); 71 bool reachedEOS(status_t *finalStatus); 72 73 status_t setPlaybackRatePermille(int32_t ratePermille); 74 75 void notifyAudioEOS(); 76 77private: 78 friend class VideoEditorAudioPlayer; 79 sp<MediaSource> mSource; 80 sp<AudioTrack> mAudioTrack; 81 82 MediaBuffer *mInputBuffer; 83 84 int mSampleRate; 85 int64_t mLatencyUs; 86 size_t mFrameSize; 87 88 Mutex mLock; 89 int64_t mNumFramesPlayed; 90 int64_t mNumFramesPlayedSysTimeUs; 91 92 int64_t mPositionTimeMediaUs; 93 int64_t mPositionTimeRealUs; 94 95 bool mSeeking; 96 bool mReachedEOS; 97 status_t mFinalStatus; 98 int64_t mSeekTimeUs; 99 100 bool mStarted; 101 102 bool mIsFirstBuffer; 103 status_t mFirstBufferResult; 104 MediaBuffer *mFirstBuffer; 105 106 sp<MediaPlayerBase::AudioSink> mAudioSink; 107 bool mAllowDeepBuffering; // allow audio deep audio buffers. Helps with low power audio 108 // playback but implies high latency 109 AwesomePlayer *mObserver; 110 int64_t mPinnedTimeUs; 111 112 static void AudioCallback(int event, void *user, void *info); 113 void AudioCallback(int event, void *info); 114 115 static size_t AudioSinkCallback( 116 MediaPlayerBase::AudioSink *audioSink, 117 void *data, size_t size, void *me, 118 MediaPlayerBase::AudioSink::cb_event_t event); 119 120 size_t fillBuffer(void *data, size_t size); 121 122 int64_t getRealTimeUsLocked() const; 123 124 void reset(); 125 126 uint32_t getNumFramesPendingPlayout() const; 127 int64_t getOutputPlayPositionUs_l() const; 128 129 AudioPlayer(const AudioPlayer &); 130 AudioPlayer &operator=(const AudioPlayer &); 131}; 132 133} // namespace android 134 135#endif // AUDIO_PLAYER_H_ 136