blob: 5a71edb012cc2210c9b9795541baf51442f30ea5 [file] [log] [blame]
Wei Jia53692fa2017-12-11 10:33:46 -08001/*
2 * Copyright 2017 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 GENERIC_SOURCE_H_
18
19#define GENERIC_SOURCE_H_
20
21#include "NuPlayer2.h"
22#include "NuPlayer2Source.h"
23
24#include "ATSParser.h"
25
26#include <media/mediaplayer2.h>
27#include <media/stagefright/MediaBuffer.h>
28
29namespace android {
30
31class DecryptHandle;
32struct AnotherPacketSource;
33struct ARTSPController;
34class DataSource;
35class IDataSource;
Dongwon Kang49ce6712018-01-24 10:16:25 -080036class IMediaSource;
Wei Jia53692fa2017-12-11 10:33:46 -080037struct MediaHTTPService;
38struct MediaSource;
39class MediaBuffer;
40struct MediaClock;
41struct NuCachedSource2;
42
43struct NuPlayer2::GenericSource : public NuPlayer2::Source,
44 public MediaBufferObserver // Modular DRM
45{
46 GenericSource(const sp<AMessage> &notify, bool uidValid, uid_t uid,
47 const sp<MediaClock> &mediaClock);
48
49 status_t setDataSource(
50 const sp<MediaHTTPService> &httpService,
51 const char *url,
52 const KeyedVector<String8, String8> *headers);
53
54 status_t setDataSource(int fd, int64_t offset, int64_t length);
55
56 status_t setDataSource(const sp<DataSource>& dataSource);
57
58 virtual status_t getBufferingSettings(
59 BufferingSettings* buffering /* nonnull */) override;
60 virtual status_t setBufferingSettings(const BufferingSettings& buffering) override;
61
62 virtual void prepareAsync();
63
64 virtual void start();
65 virtual void stop();
66 virtual void pause();
67 virtual void resume();
68
69 virtual void disconnect();
70
71 virtual status_t feedMoreTSData();
72
73 virtual sp<MetaData> getFileFormatMeta() const;
74
75 virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
76
77 virtual status_t getDuration(int64_t *durationUs);
78 virtual size_t getTrackCount() const;
79 virtual sp<AMessage> getTrackInfo(size_t trackIndex) const;
80 virtual ssize_t getSelectedTrack(media_track_type type) const;
81 virtual status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs);
82 virtual status_t seekTo(
83 int64_t seekTimeUs,
84 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC) override;
85
Wei Jia53692fa2017-12-11 10:33:46 -080086 virtual bool isStreaming() const;
87
88 // Modular DRM
89 virtual void signalBufferReturned(MediaBuffer *buffer);
90
91 virtual status_t prepareDrm(
92 const uint8_t uuid[16],
93 const Vector<uint8_t> &drmSessionId,
94 sp<AMediaCryptoWrapper> *outCrypto);
95
96 virtual status_t releaseDrm();
97
98
99protected:
100 virtual ~GenericSource();
101
102 virtual void onMessageReceived(const sp<AMessage> &msg);
103
104 virtual sp<MetaData> getFormatMeta(bool audio);
105
106private:
107 enum {
108 kWhatPrepareAsync,
109 kWhatFetchSubtitleData,
110 kWhatFetchTimedTextData,
111 kWhatSendSubtitleData,
112 kWhatSendGlobalTimedTextData,
113 kWhatSendTimedTextData,
114 kWhatChangeAVSource,
115 kWhatPollBuffering,
116 kWhatSeek,
117 kWhatReadBuffer,
118 kWhatStart,
119 kWhatResume,
120 kWhatSecureDecodersInstantiated,
121 };
122
123 struct Track {
124 size_t mIndex;
125 sp<IMediaSource> mSource;
126 sp<AnotherPacketSource> mPackets;
127 };
128
129 Vector<sp<IMediaSource> > mSources;
130 Track mAudioTrack;
131 int64_t mAudioTimeUs;
132 int64_t mAudioLastDequeueTimeUs;
133 Track mVideoTrack;
134 int64_t mVideoTimeUs;
135 int64_t mVideoLastDequeueTimeUs;
136 Track mSubtitleTrack;
137 Track mTimedTextTrack;
138
139 BufferingSettings mBufferingSettings;
140 int32_t mPrevBufferPercentage;
141 int32_t mPollBufferingGeneration;
142 bool mSentPauseOnBuffering;
143
144 int32_t mAudioDataGeneration;
145 int32_t mVideoDataGeneration;
146 int32_t mFetchSubtitleDataGeneration;
147 int32_t mFetchTimedTextDataGeneration;
148 int64_t mDurationUs;
149 bool mAudioIsVorbis;
150 // Secure codec is required.
151 bool mIsSecure;
152 bool mIsStreaming;
153 bool mUIDValid;
154 uid_t mUID;
155 const sp<MediaClock> mMediaClock;
156 sp<MediaHTTPService> mHTTPService;
157 AString mUri;
158 KeyedVector<String8, String8> mUriHeaders;
159 int mFd;
160 int64_t mOffset;
161 int64_t mLength;
162
163 bool mDisconnected;
164 sp<DataSource> mDataSource;
165 sp<NuCachedSource2> mCachedSource;
166 sp<DataSource> mHttpSource;
167 sp<MetaData> mFileMeta;
168 bool mStarted;
169 bool mPreparing;
170 int64_t mBitrate;
171 uint32_t mPendingReadBufferTypes;
172 sp<ABuffer> mGlobalTimedText;
173
174 mutable Mutex mLock;
175
176 sp<ALooper> mLooper;
177
178 void resetDataSource();
179
180 status_t initFromDataSource();
181 int64_t getLastReadPosition();
182
183 void notifyPreparedAndCleanup(status_t err);
184 void onSecureDecodersInstantiated(status_t err);
185 void finishPrepareAsync();
186 status_t startSources();
187
188 void onSeek(const sp<AMessage>& msg);
189 status_t doSeek(int64_t seekTimeUs, MediaPlayer2SeekMode mode);
190
191 void onPrepareAsync();
192
193 void fetchTextData(
194 uint32_t what, media_track_type type,
195 int32_t curGen, const sp<AnotherPacketSource>& packets, const sp<AMessage>& msg);
196
197 void sendGlobalTextData(
198 uint32_t what,
199 int32_t curGen, sp<AMessage> msg);
200
201 void sendTextData(
202 uint32_t what, media_track_type type,
203 int32_t curGen, const sp<AnotherPacketSource>& packets, const sp<AMessage>& msg);
204
205 sp<ABuffer> mediaBufferToABuffer(
206 MediaBuffer *mbuf,
207 media_track_type trackType);
208
209 void postReadBuffer(media_track_type trackType);
210 void onReadBuffer(const sp<AMessage>& msg);
211 // When |mode| is MediaPlayer2SeekMode::SEEK_CLOSEST, the buffer read shall
212 // include an item indicating skipping rendering all buffers with timestamp
213 // earlier than |seekTimeUs|.
214 // For other modes, the buffer read will not include the item as above in order
215 // to facilitate fast seek operation.
216 void readBuffer(
217 media_track_type trackType,
218 int64_t seekTimeUs = -1ll,
219 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC,
220 int64_t *actualTimeUs = NULL, bool formatChange = false);
221
222 void queueDiscontinuityIfNeeded(
223 bool seeking, bool formatChange, media_track_type trackType, Track *track);
224
225 void schedulePollBuffering();
226 void onPollBuffering();
227 void notifyBufferingUpdate(int32_t percentage);
228
229 void sendCacheStats();
230
231 sp<MetaData> getFormatMeta_l(bool audio);
232 int32_t getDataGeneration(media_track_type type) const;
233
234 // Modular DRM
235 // The source is DRM protected and is prepared for DRM.
236 bool mIsDrmProtected;
237 // releaseDrm has been processed.
238 bool mIsDrmReleased;
239 Vector<String8> mMimes;
240
241 status_t checkDrmInfo();
242
243 DISALLOW_EVIL_CONSTRUCTORS(GenericSource);
244};
245
246} // namespace android
247
248#endif // GENERIC_SOURCE_H_