AudioPlayer.h revision ed54ad0f8619ae416b0968ade6248894cbfc4dba
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();
53    void resume();
54
55    void stop();
56
57    // Returns the timestamp of the last buffer played (in us).
58    int64_t getMediaTimeUs();
59
60    // Returns true iff a mapping is established, i.e. the AudioPlayer
61    // has played at least one frame of audio.
62    bool getMediaTimeMapping(int64_t *realtime_us, int64_t *mediatime_us);
63
64    status_t seekTo(int64_t time_us);
65
66    bool isSeeking();
67    bool reachedEOS(status_t *finalStatus);
68
69private:
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    AwesomePlayer *mObserver;
98
99    static void AudioCallback(int event, void *user, void *info);
100    void AudioCallback(int event, void *info);
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    AudioPlayer(const AudioPlayer &);
111    AudioPlayer &operator=(const AudioPlayer &);
112};
113
114}  // namespace android
115
116#endif  // AUDIO_PLAYER_H_
117