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/AudioResamplerPublic.h>
22 #include <media/AudioTrack.h>
23 #include <media/stagefright/MediaSource.h>
24 #include <media/MediaPlayerInterface.h>
25 #include <media/stagefright/MediaBuffer.h>
26 #include <utils/threads.h>
27 
28 namespace android {
29 
30 struct AwesomePlayer;
31 
32 class AudioPlayer : AudioTrack::IAudioTrackCallback {
33 public:
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<MediaSource> &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 
68 private:
69     friend sp<AudioPlayer>;
70     size_t onMoreData(const AudioTrack::Buffer& buffer) override;
71     void onStreamEnd() override;
72     sp<MediaSource> mSource;
73     sp<AudioTrack> mAudioTrack;
74 
75     MediaBufferBase *mInputBuffer;
76 
77     int mSampleRate;
78     int64_t mLatencyUs;
79     size_t mFrameSize;
80 
81     Mutex mLock;
82     int64_t mNumFramesPlayed;
83     int64_t mNumFramesPlayedSysTimeUs;
84 
85     int64_t mPositionTimeMediaUs;
86     int64_t mPositionTimeRealUs;
87 
88     bool mSeeking;
89     bool mReachedEOS;
90     status_t mFinalStatus;
91     int64_t mSeekTimeUs;
92 
93     bool mStarted;
94 
95     bool mIsFirstBuffer;
96     status_t mFirstBufferResult;
97     MediaBufferBase *mFirstBuffer;
98 
99     sp<MediaPlayerBase::AudioSink> mAudioSink;
100 
101     bool mPlaying;
102     int64_t mStartPosUs;
103     const uint32_t mCreateFlags;
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 
allowDeepBuffering()116     bool allowDeepBuffering() const { return (mCreateFlags & ALLOW_DEEP_BUFFERING) != 0; }
useOffload()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