AudioPlayer.h revision 1862a33b246249630b654182afb5914da3480d4c
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();
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    int64_t mSeekTimeUs;
85
86    bool mStarted;
87
88    sp<MediaPlayerBase::AudioSink> mAudioSink;
89
90    static void AudioCallback(int event, void *user, void *info);
91    void AudioCallback(int event, void *info);
92
93    static void AudioSinkCallback(
94            MediaPlayerBase::AudioSink *audioSink,
95            void *data, size_t size, void *me);
96
97    void fillBuffer(void *data, size_t size);
98
99    int64_t getRealTimeUsLocked() const;
100
101    AudioPlayer(const AudioPlayer &);
102    AudioPlayer &operator=(const AudioPlayer &);
103};
104
105}  // namespace android
106
107#endif  // AUDIO_PLAYER_H_
108