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/IMediaSource.h>
22#include <media/MediaPlayerInterface.h>
23#include <media/stagefright/MediaBuffer.h>
24#include <utils/threads.h>
25
26namespace android {
27
28struct AudioPlaybackRate;
29class AudioTrack;
30struct AwesomePlayer;
31
32class AudioPlayer {
33public:
34    enum {
35        REACHED_EOS,
36        SEEK_COMPLETE
37    };
38
39    enum {
40        ALLOW_DEEP_BUFFERING = 0x01,
41        USE_OFFLOAD = 0x02,
42        HAS_VIDEO   = 0x1000,
43        IS_STREAMING = 0x2000
44
45    };
46
47    AudioPlayer(const sp<MediaPlayerBase::AudioSink> &audioSink,
48                uint32_t flags = 0);
49
50    virtual ~AudioPlayer();
51
52    // Caller retains ownership of "source".
53    void setSource(const sp<IMediaSource> &source);
54
55    status_t start(bool sourceAlreadyStarted = false);
56
57    void pause(bool playPendingSamples = false);
58    status_t resume();
59
60    status_t seekTo(int64_t time_us);
61
62    bool isSeeking();
63    bool reachedEOS(status_t *finalStatus);
64
65    status_t setPlaybackRate(const AudioPlaybackRate &rate);
66    status_t getPlaybackRate(AudioPlaybackRate *rate /* nonnull */);
67
68private:
69    sp<IMediaSource> mSource;
70    sp<AudioTrack> mAudioTrack;
71
72    MediaBuffer *mInputBuffer;
73
74    int mSampleRate;
75    int64_t mLatencyUs;
76    size_t mFrameSize;
77
78    Mutex mLock;
79    int64_t mNumFramesPlayed;
80    int64_t mNumFramesPlayedSysTimeUs;
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
98    bool mPlaying;
99    int64_t mStartPosUs;
100    const uint32_t mCreateFlags;
101
102    static void AudioCallback(int event, void *user, void *info);
103    void AudioCallback(int event, void *info);
104
105    static size_t AudioSinkCallback(
106            MediaPlayerBase::AudioSink *audioSink,
107            void *data, size_t size, void *me,
108            MediaPlayerBase::AudioSink::cb_event_t event);
109
110    size_t fillBuffer(void *data, size_t size);
111
112    void reset();
113
114    int64_t getOutputPlayPositionUs_l();
115
116    bool allowDeepBuffering() const { return (mCreateFlags & ALLOW_DEEP_BUFFERING) != 0; }
117    bool useOffload() const { return (mCreateFlags & USE_OFFLOAD) != 0; }
118
119    AudioPlayer(const AudioPlayer &);
120    AudioPlayer &operator=(const AudioPlayer &);
121};
122
123}  // namespace android
124
125#endif  // AUDIO_PLAYER_H_
126