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