James Dong | c9dedc4 | 2011-05-01 12:36:22 -0700 | [diff] [blame] | 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 AUDIO_PLAYER_BASE_H_ |
| 18 | |
| 19 | #define AUDIO_PLAYER_BASE_H_ |
| 20 | |
Glenn Kasten | 984eae0 | 2011-05-04 15:37:39 -0700 | [diff] [blame^] | 21 | #include <media/AudioTrack.h> |
James Dong | c9dedc4 | 2011-05-01 12:36:22 -0700 | [diff] [blame] | 22 | #include <media/MediaPlayerInterface.h> |
| 23 | #include <media/stagefright/MediaBuffer.h> |
| 24 | #include <media/stagefright/TimeSource.h> |
| 25 | #include <utils/threads.h> |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | class MediaSource; |
| 30 | class AudioTrack; |
| 31 | class PreviewPlayerBase; |
| 32 | |
| 33 | class AudioPlayerBase : public TimeSource { |
| 34 | public: |
| 35 | enum { |
| 36 | REACHED_EOS, |
| 37 | SEEK_COMPLETE |
| 38 | }; |
| 39 | |
| 40 | AudioPlayerBase(const sp<MediaPlayerBase::AudioSink> &audioSink, |
| 41 | PreviewPlayerBase *audioObserver = NULL); |
| 42 | |
| 43 | virtual ~AudioPlayerBase(); |
| 44 | |
| 45 | // Caller retains ownership of "source". |
| 46 | void setSource(const sp<MediaSource> &source); |
| 47 | |
| 48 | // Return time in us. |
| 49 | virtual int64_t getRealTimeUs(); |
| 50 | |
| 51 | status_t start(bool sourceAlreadyStarted = false); |
| 52 | |
| 53 | void pause(bool playPendingSamples = false); |
| 54 | void resume(); |
| 55 | |
| 56 | // Returns the timestamp of the last buffer played (in us). |
| 57 | int64_t getMediaTimeUs(); |
| 58 | |
| 59 | // Returns true iff a mapping is established, i.e. the AudioPlayerBase |
| 60 | // has played at least one frame of audio. |
| 61 | bool getMediaTimeMapping(int64_t *realtime_us, int64_t *mediatime_us); |
| 62 | |
| 63 | status_t seekTo(int64_t time_us); |
| 64 | |
| 65 | bool isSeeking(); |
| 66 | bool reachedEOS(status_t *finalStatus); |
| 67 | |
| 68 | private: |
| 69 | friend class VideoEditorAudioPlayer; |
| 70 | sp<MediaSource> mSource; |
| 71 | AudioTrack *mAudioTrack; |
| 72 | |
| 73 | MediaBuffer *mInputBuffer; |
| 74 | |
| 75 | int mSampleRate; |
| 76 | int64_t mLatencyUs; |
| 77 | size_t mFrameSize; |
| 78 | |
| 79 | Mutex mLock; |
| 80 | int64_t mNumFramesPlayed; |
| 81 | |
| 82 | int64_t mPositionTimeMediaUs; |
| 83 | int64_t mPositionTimeRealUs; |
| 84 | |
| 85 | bool mSeeking; |
| 86 | bool mReachedEOS; |
| 87 | status_t mFinalStatus; |
| 88 | int64_t mSeekTimeUs; |
| 89 | |
| 90 | bool mStarted; |
| 91 | |
| 92 | bool mIsFirstBuffer; |
| 93 | status_t mFirstBufferResult; |
| 94 | MediaBuffer *mFirstBuffer; |
| 95 | |
| 96 | sp<MediaPlayerBase::AudioSink> mAudioSink; |
| 97 | PreviewPlayerBase *mObserver; |
| 98 | |
Glenn Kasten | 984eae0 | 2011-05-04 15:37:39 -0700 | [diff] [blame^] | 99 | static void AudioCallback(AudioTrack::event_type event, void *user, void *info); |
| 100 | void AudioCallback(AudioTrack::event_type event, void *info); |
James Dong | c9dedc4 | 2011-05-01 12:36:22 -0700 | [diff] [blame] | 101 | |
| 102 | static size_t AudioSinkCallback( |
| 103 | MediaPlayerBase::AudioSink *audioSink, |
| 104 | void *data, size_t size, void *me); |
| 105 | |
| 106 | size_t fillBuffer(void *data, size_t size); |
| 107 | |
| 108 | int64_t getRealTimeUsLocked() const; |
| 109 | |
| 110 | void reset(); |
| 111 | |
| 112 | uint32_t getNumFramesPendingPlayout() const; |
| 113 | |
| 114 | AudioPlayerBase(const AudioPlayerBase &); |
| 115 | AudioPlayerBase &operator=(const AudioPlayerBase &); |
| 116 | }; |
| 117 | |
| 118 | } // namespace android |
| 119 | |
| 120 | #endif // AUDIO_PLAYER_BASE_H_ |