AudioPlayer.h revision a99a5bca365277271915cbaeea811ad87131270d
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                bool allowDeepBuffering = false,
41                AwesomePlayer *audioObserver = NULL);
42
43    virtual ~AudioPlayer();
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 AudioPlayer
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    status_t setPlaybackRatePermille(int32_t ratePermille);
69
70private:
71    friend class VideoEditorAudioPlayer;
72    sp<MediaSource> mSource;
73    AudioTrack *mAudioTrack;
74
75    MediaBuffer *mInputBuffer;
76
77    int mSampleRate;
78    int64_t mLatencyUs;
79    size_t mFrameSize;
80
81    Mutex mLock;
82    int64_t mNumFramesPlayed;
83    int64_t mNumFramesPlayedSysTimeUs;
84
85    int64_t mPositionTimeMediaUs;
86    int64_t mPositionTimeRealUs;
87
88    bool mSeeking;
89    bool mReachedEOS;
90    status_t mFinalStatus;
91    int64_t mSeekTimeUs;
92
93    bool mStarted;
94
95    bool mIsFirstBuffer;
96    status_t mFirstBufferResult;
97    MediaBuffer *mFirstBuffer;
98
99    sp<MediaPlayerBase::AudioSink> mAudioSink;
100    bool mAllowDeepBuffering;       // allow audio deep audio buffers. Helps with low power audio
101                                    // playback but implies high latency
102    AwesomePlayer *mObserver;
103    int64_t mPinnedTimeUs;
104
105    static void AudioCallback(int event, void *user, void *info);
106    void AudioCallback(int event, void *info);
107
108    static size_t AudioSinkCallback(
109            MediaPlayerBase::AudioSink *audioSink,
110            void *data, size_t size, void *me);
111
112    size_t fillBuffer(void *data, size_t size);
113
114    int64_t getRealTimeUsLocked() const;
115
116    void reset();
117
118    uint32_t getNumFramesPendingPlayout() const;
119
120    AudioPlayer(const AudioPlayer &);
121    AudioPlayer &operator=(const AudioPlayer &);
122};
123
124}  // namespace android
125
126#endif  // AUDIO_PLAYER_H_
127