AudioPlayer.h revision bfa6b2d7a1be1832ac40ed90aece1834f720b5c6
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    void setListenerCallback(
45            void (*notify)(void *cookie, int what), void *cookie);
46
47    // Return time in us.
48    virtual int64_t getRealTimeUs();
49
50    void start();
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
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    int64_t mSeekTimeUs;
84
85    bool mStarted;
86
87    void (*mListenerCallback)(void *cookie, int what);
88    void *mListenerCookie;
89
90    sp<MediaPlayerBase::AudioSink> mAudioSink;
91
92    static void AudioCallback(int event, void *user, void *info);
93    void AudioCallback(int event, void *info);
94
95    static void AudioSinkCallback(
96            MediaPlayerBase::AudioSink *audioSink,
97            void *data, size_t size, void *me);
98
99    void fillBuffer(void *data, size_t size);
100
101    int64_t getRealTimeUsLocked() const;
102
103    AudioPlayer(const AudioPlayer &);
104    AudioPlayer &operator=(const AudioPlayer &);
105};
106
107}  // namespace android
108
109#endif  // AUDIO_PLAYER_H_
110