AudioPlayer.h revision 5295c0c55d41a2906ea7f65a3f22e6278cb17d4b
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;
30
31class AudioPlayer : public TimeSource {
32public:
33    enum {
34        REACHED_EOS,
35        SEEK_COMPLETE
36    };
37
38    AudioPlayer(const sp<MediaPlayerBase::AudioSink> &audioSink);
39    virtual ~AudioPlayer();
40
41    // Caller retains ownership of "source".
42    void setSource(const sp<MediaSource> &source);
43
44    // Return time in us.
45    virtual int64_t getRealTimeUs();
46
47    status_t start();
48
49    void pause();
50    void resume();
51
52    void stop();
53
54    // Returns the timestamp of the last buffer played (in us).
55    int64_t getMediaTimeUs();
56
57    // Returns true iff a mapping is established, i.e. the AudioPlayer
58    // has played at least one frame of audio.
59    bool getMediaTimeMapping(int64_t *realtime_us, int64_t *mediatime_us);
60
61    status_t seekTo(int64_t time_us);
62
63    bool isSeeking();
64    bool reachedEOS(status_t *finalStatus);
65
66private:
67    sp<MediaSource> mSource;
68    AudioTrack *mAudioTrack;
69
70    MediaBuffer *mInputBuffer;
71
72    int mSampleRate;
73    int64_t mLatencyUs;
74    size_t mFrameSize;
75
76    Mutex mLock;
77    int64_t mNumFramesPlayed;
78
79    int64_t mPositionTimeMediaUs;
80    int64_t mPositionTimeRealUs;
81
82    bool mSeeking;
83    bool mReachedEOS;
84    status_t mFinalStatus;
85    int64_t mSeekTimeUs;
86
87    bool mStarted;
88
89    sp<MediaPlayerBase::AudioSink> mAudioSink;
90
91    static void AudioCallback(int event, void *user, void *info);
92    void AudioCallback(int event, void *info);
93
94    static size_t AudioSinkCallback(
95            MediaPlayerBase::AudioSink *audioSink,
96            void *data, size_t size, void *me);
97
98    size_t fillBuffer(void *data, size_t size);
99
100    int64_t getRealTimeUsLocked() const;
101
102    AudioPlayer(const AudioPlayer &);
103    AudioPlayer &operator=(const AudioPlayer &);
104};
105
106}  // namespace android
107
108#endif  // AUDIO_PLAYER_H_
109