Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 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; |
Andreas Huber | 2b359ed | 2010-09-28 11:56:39 -0700 | [diff] [blame] | 30 | class AwesomePlayer; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 31 | |
| 32 | class AudioPlayer : public TimeSource { |
| 33 | public: |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 34 | enum { |
| 35 | REACHED_EOS, |
| 36 | SEEK_COMPLETE |
| 37 | }; |
| 38 | |
Andreas Huber | 2b359ed | 2010-09-28 11:56:39 -0700 | [diff] [blame] | 39 | AudioPlayer(const sp<MediaPlayerBase::AudioSink> &audioSink, |
| 40 | AwesomePlayer *audioObserver = NULL); |
| 41 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 42 | virtual ~AudioPlayer(); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 43 | |
| 44 | // Caller retains ownership of "source". |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 45 | void setSource(const sp<MediaSource> &source); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 46 | |
| 47 | // Return time in us. |
| 48 | virtual int64_t getRealTimeUs(); |
| 49 | |
Andreas Huber | dc9927d | 2010-03-08 15:46:13 -0800 | [diff] [blame] | 50 | status_t start(bool sourceAlreadyStarted = false); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 51 | |
Andreas Huber | c743f45 | 2010-10-05 10:25:34 -0700 | [diff] [blame] | 52 | void pause(bool playPendingSamples = false); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 53 | void resume(); |
| 54 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 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 | |
Andreas Huber | 70d10c0 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 64 | bool isSeeking(); |
Andreas Huber | d7d22eb | 2010-02-23 13:45:33 -0800 | [diff] [blame] | 65 | bool reachedEOS(status_t *finalStatus); |
Andreas Huber | 70d10c0 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 66 | |
Jean-Michel Trivi | e901a5c | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 67 | status_t setPlaybackRatePermille(int32_t ratePermille); |
| 68 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 69 | private: |
Dharmaray Kundargi | 3b3cddc | 2011-01-16 16:43:20 -0800 | [diff] [blame] | 70 | friend class VideoEditorAudioPlayer; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 71 | sp<MediaSource> mSource; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 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; |
Andreas Huber | 70d10c0 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 87 | bool mReachedEOS; |
Andreas Huber | d7d22eb | 2010-02-23 13:45:33 -0800 | [diff] [blame] | 88 | status_t mFinalStatus; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 89 | int64_t mSeekTimeUs; |
| 90 | |
| 91 | bool mStarted; |
| 92 | |
Andreas Huber | cc14a83 | 2010-07-20 09:21:17 -0700 | [diff] [blame] | 93 | bool mIsFirstBuffer; |
| 94 | status_t mFirstBufferResult; |
| 95 | MediaBuffer *mFirstBuffer; |
| 96 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 97 | sp<MediaPlayerBase::AudioSink> mAudioSink; |
Andreas Huber | 2b359ed | 2010-09-28 11:56:39 -0700 | [diff] [blame] | 98 | AwesomePlayer *mObserver; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 99 | |
Glenn Kasten | e46a86f | 2011-06-01 15:20:35 -0700 | [diff] [blame] | 100 | static void AudioCallback(int event, void *user, void *info); |
| 101 | void AudioCallback(int event, void *info); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 102 | |
Andreas Huber | 6ed937e | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 103 | static size_t AudioSinkCallback( |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 104 | MediaPlayerBase::AudioSink *audioSink, |
| 105 | void *data, size_t size, void *me); |
| 106 | |
Andreas Huber | 6ed937e | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 107 | size_t fillBuffer(void *data, size_t size); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 108 | |
| 109 | int64_t getRealTimeUsLocked() const; |
| 110 | |
Andreas Huber | c743f45 | 2010-10-05 10:25:34 -0700 | [diff] [blame] | 111 | void reset(); |
| 112 | |
Andreas Huber | 4dc482d | 2011-03-22 10:31:21 -0700 | [diff] [blame] | 113 | uint32_t getNumFramesPendingPlayout() const; |
| 114 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 115 | AudioPlayer(const AudioPlayer &); |
| 116 | AudioPlayer &operator=(const AudioPlayer &); |
| 117 | }; |
| 118 | |
| 119 | } // namespace android |
| 120 | |
| 121 | #endif // AUDIO_PLAYER_H_ |