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