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 26 namespace android { 27 28 class MediaSource; 29 class AudioTrack; 30 class AwesomePlayer; 31 32 class AudioPlayer : public TimeSource { 33 public: 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 private: 68 sp<MediaSource> mSource; 69 AudioTrack *mAudioTrack; 70 71 MediaBuffer *mInputBuffer; 72 73 int mSampleRate; 74 int64_t mLatencyUs; 75 size_t mFrameSize; 76 77 Mutex mLock; 78 int64_t mNumFramesPlayed; 79 80 int64_t mPositionTimeMediaUs; 81 int64_t mPositionTimeRealUs; 82 83 bool mSeeking; 84 bool mReachedEOS; 85 status_t mFinalStatus; 86 int64_t mSeekTimeUs; 87 88 bool mStarted; 89 90 bool mIsFirstBuffer; 91 status_t mFirstBufferResult; 92 MediaBuffer *mFirstBuffer; 93 94 sp<MediaPlayerBase::AudioSink> mAudioSink; 95 AwesomePlayer *mObserver; 96 97 static void AudioCallback(int event, void *user, void *info); 98 void AudioCallback(int event, void *info); 99 100 static size_t AudioSinkCallback( 101 MediaPlayerBase::AudioSink *audioSink, 102 void *data, size_t size, void *me); 103 104 size_t fillBuffer(void *data, size_t size); 105 106 int64_t getRealTimeUsLocked() const; 107 108 void reset(); 109 110 AudioPlayer(const AudioPlayer &); 111 AudioPlayer &operator=(const AudioPlayer &); 112 }; 113 114 } // namespace android 115 116 #endif // AUDIO_PLAYER_H_ 117