AudioPlayer.h revision 7a8b0ed6419e57dd8b41d3806893d63d3df91aab
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 AudioPlayer(const sp<MediaPlayerBase::AudioSink> &audioSink, 40 AwesomePlayer *audioObserver = NULL); 41 42 virtual ~AudioPlayer(); 43 44 // Caller retains ownership of "source". 45 void setSource(const sp<MediaSource> &source); 46 47 // Return time in us. 48 virtual int64_t getRealTimeUs(); 49 50 status_t start(bool sourceAlreadyStarted = false); 51 52 void pause(bool playPendingSamples = false); 53 void resume(); 54 55 // Returns the timestamp of the last buffer played (in us). 56 int64_t getMediaTimeUs(); 57 58 // Returns true iff a mapping is established, i.e. the AudioPlayer 59 // has played at least one frame of audio. 60 bool getMediaTimeMapping(int64_t *realtime_us, int64_t *mediatime_us); 61 62 status_t seekTo(int64_t time_us); 63 64 bool isSeeking(); 65 bool reachedEOS(status_t *finalStatus); 66 67 status_t setPlaybackRatePermille(int32_t ratePermille); 68 69private: 70 friend class VideoEditorAudioPlayer; 71 sp<MediaSource> mSource; 72 AudioTrack *mAudioTrack; 73 74 MediaBuffer *mInputBuffer; 75 76 int mSampleRate; 77 int64_t mLatencyUs; 78 size_t mFrameSize; 79 80 Mutex mLock; 81 int64_t mNumFramesPlayed; 82 83 int64_t mPositionTimeMediaUs; 84 int64_t mPositionTimeRealUs; 85 86 bool mSeeking; 87 bool mReachedEOS; 88 status_t mFinalStatus; 89 int64_t mSeekTimeUs; 90 91 bool mStarted; 92 93 bool mIsFirstBuffer; 94 status_t mFirstBufferResult; 95 MediaBuffer *mFirstBuffer; 96 97 sp<MediaPlayerBase::AudioSink> mAudioSink; 98 AwesomePlayer *mObserver; 99 100 static void AudioCallback(int event, void *user, void *info); 101 void AudioCallback(int event, void *info); 102 103 static size_t AudioSinkCallback( 104 MediaPlayerBase::AudioSink *audioSink, 105 void *data, size_t size, void *me); 106 107 size_t fillBuffer(void *data, size_t size); 108 109 int64_t getRealTimeUsLocked() const; 110 111 void reset(); 112 113 uint32_t getNumFramesPendingPlayout() const; 114 115 AudioPlayer(const AudioPlayer &); 116 AudioPlayer &operator=(const AudioPlayer &); 117}; 118 119} // namespace android 120 121#endif // AUDIO_PLAYER_H_ 122