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