blob: 3cac8094d8a03b99fa23174da53d8beab63d6d43 [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
Wei Jia2409c872018-02-02 10:34:33 -080017#ifndef GENERIC_SOURCE2_H_
Wei Jia53692fa2017-12-11 10:33:46 -080018
Wei Jia2409c872018-02-02 10:34:33 -080019#define GENERIC_SOURCE2_H_
Wei Jia53692fa2017-12-11 10:33:46 -080020
21#include "NuPlayer2.h"
22#include "NuPlayer2Source.h"
23
24#include "ATSParser.h"
25
Wei Jia53692fa2017-12-11 10:33:46 -080026#include <media/stagefright/MediaBuffer.h>
Wei Jia51b69562018-02-05 16:17:13 -080027#include <mediaplayer2/mediaplayer2.h>
Robert Shih0cd95062018-01-21 17:41:16 -080028#include <media/NdkMediaDataSource.h>
29#include <media/NdkMediaExtractor.h>
30#include <media/NdkWrapper.h>
Wei Jia53692fa2017-12-11 10:33:46 -080031
32namespace android {
33
34class DecryptHandle;
35struct AnotherPacketSource;
36struct ARTSPController;
37class DataSource;
38class IDataSource;
Dongwon Kang49ce6712018-01-24 10:16:25 -080039class IMediaSource;
Wei Jia53692fa2017-12-11 10:33:46 -080040struct MediaHTTPService;
41struct MediaSource;
42class MediaBuffer;
43struct MediaClock;
44struct NuCachedSource2;
45
Wei Jia2409c872018-02-02 10:34:33 -080046struct NuPlayer2::GenericSource2 : public NuPlayer2::Source,
47 public MediaBufferObserver // Modular DRM
Wei Jia53692fa2017-12-11 10:33:46 -080048{
Wei Jia003fdb52018-02-06 14:44:32 -080049 GenericSource2(const sp<AMessage> &notify, uid_t uid,
Wei Jia2409c872018-02-02 10:34:33 -080050 const sp<MediaClock> &mediaClock);
Wei Jia53692fa2017-12-11 10:33:46 -080051
52 status_t setDataSource(
53 const sp<MediaHTTPService> &httpService,
54 const char *url,
55 const KeyedVector<String8, String8> *headers);
56
57 status_t setDataSource(int fd, int64_t offset, int64_t length);
58
59 status_t setDataSource(const sp<DataSource>& dataSource);
60
61 virtual status_t getBufferingSettings(
62 BufferingSettings* buffering /* nonnull */) override;
63 virtual status_t setBufferingSettings(const BufferingSettings& buffering) override;
64
Wei Jiaf01e3122018-10-18 11:49:44 -070065 virtual void prepareAsync(int64_t startTimeUs);
Wei Jia53692fa2017-12-11 10:33:46 -080066
67 virtual void start();
68 virtual void stop();
69 virtual void pause();
70 virtual void resume();
71
72 virtual void disconnect();
73
74 virtual status_t feedMoreTSData();
75
76 virtual sp<MetaData> getFileFormatMeta() const;
77
78 virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
79
80 virtual status_t getDuration(int64_t *durationUs);
81 virtual size_t getTrackCount() const;
82 virtual sp<AMessage> getTrackInfo(size_t trackIndex) const;
83 virtual ssize_t getSelectedTrack(media_track_type type) const;
84 virtual status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs);
85 virtual status_t seekTo(
86 int64_t seekTimeUs,
87 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC) override;
88
Wei Jia53692fa2017-12-11 10:33:46 -080089 virtual bool isStreaming() const;
90
91 // Modular DRM
Dongwon Kang1889c3e2018-02-01 13:44:57 -080092 virtual void signalBufferReturned(MediaBufferBase *buffer);
Wei Jia53692fa2017-12-11 10:33:46 -080093
94 virtual status_t prepareDrm(
95 const uint8_t uuid[16],
96 const Vector<uint8_t> &drmSessionId,
97 sp<AMediaCryptoWrapper> *outCrypto);
98
99 virtual status_t releaseDrm();
100
101
102protected:
Wei Jia2409c872018-02-02 10:34:33 -0800103 virtual ~GenericSource2();
Wei Jia53692fa2017-12-11 10:33:46 -0800104
105 virtual void onMessageReceived(const sp<AMessage> &msg);
106
Robert Shih0cd95062018-01-21 17:41:16 -0800107 virtual sp<AMessage> getFormat(bool audio);
Wei Jia53692fa2017-12-11 10:33:46 -0800108 virtual sp<MetaData> getFormatMeta(bool audio);
109
110private:
111 enum {
112 kWhatPrepareAsync,
113 kWhatFetchSubtitleData,
114 kWhatFetchTimedTextData,
115 kWhatSendSubtitleData,
116 kWhatSendGlobalTimedTextData,
117 kWhatSendTimedTextData,
118 kWhatChangeAVSource,
119 kWhatPollBuffering,
120 kWhatSeek,
121 kWhatReadBuffer,
122 kWhatStart,
123 kWhatResume,
124 kWhatSecureDecodersInstantiated,
125 };
126
127 struct Track {
128 size_t mIndex;
Robert Shih0cd95062018-01-21 17:41:16 -0800129 sp<AMediaExtractorWrapper> mExtractor;
Wei Jia53692fa2017-12-11 10:33:46 -0800130 sp<AnotherPacketSource> mPackets;
131 };
132
Wei Jia53692fa2017-12-11 10:33:46 -0800133 int64_t mAudioTimeUs;
134 int64_t mAudioLastDequeueTimeUs;
Wei Jia53692fa2017-12-11 10:33:46 -0800135 int64_t mVideoTimeUs;
136 int64_t mVideoLastDequeueTimeUs;
Wei Jia53692fa2017-12-11 10:33:46 -0800137
138 BufferingSettings mBufferingSettings;
139 int32_t mPrevBufferPercentage;
140 int32_t mPollBufferingGeneration;
141 bool mSentPauseOnBuffering;
142
143 int32_t mAudioDataGeneration;
144 int32_t mVideoDataGeneration;
145 int32_t mFetchSubtitleDataGeneration;
146 int32_t mFetchTimedTextDataGeneration;
147 int64_t mDurationUs;
148 bool mAudioIsVorbis;
149 // Secure codec is required.
150 bool mIsSecure;
151 bool mIsStreaming;
Wei Jia53692fa2017-12-11 10:33:46 -0800152 uid_t mUID;
153 const sp<MediaClock> mMediaClock;
154 sp<MediaHTTPService> mHTTPService;
155 AString mUri;
156 KeyedVector<String8, String8> mUriHeaders;
157 int mFd;
158 int64_t mOffset;
159 int64_t mLength;
160
161 bool mDisconnected;
162 sp<DataSource> mDataSource;
163 sp<NuCachedSource2> mCachedSource;
164 sp<DataSource> mHttpSource;
165 sp<MetaData> mFileMeta;
Robert Shih0cd95062018-01-21 17:41:16 -0800166 sp<AMediaDataSourceWrapper> mDataSourceWrapper;
167 sp<AMediaExtractorWrapper> mExtractor;
168 Vector<sp<AMediaExtractorWrapper> > mExtractors;
Wei Jia53692fa2017-12-11 10:33:46 -0800169 bool mStarted;
170 bool mPreparing;
171 int64_t mBitrate;
172 uint32_t mPendingReadBufferTypes;
173 sp<ABuffer> mGlobalTimedText;
174
Robert Shih0cd95062018-01-21 17:41:16 -0800175 Track mVideoTrack;
176 Track mAudioTrack;
177 Track mSubtitleTrack;
178 Track mTimedTextTrack;
179
Wei Jia53692fa2017-12-11 10:33:46 -0800180 mutable Mutex mLock;
181
182 sp<ALooper> mLooper;
183
184 void resetDataSource();
185
186 status_t initFromDataSource();
187 int64_t getLastReadPosition();
188
189 void notifyPreparedAndCleanup(status_t err);
190 void onSecureDecodersInstantiated(status_t err);
191 void finishPrepareAsync();
192 status_t startSources();
193
194 void onSeek(const sp<AMessage>& msg);
195 status_t doSeek(int64_t seekTimeUs, MediaPlayer2SeekMode mode);
196
Wei Jiaf01e3122018-10-18 11:49:44 -0700197 void onPrepareAsync(int64_t startTimeUs);
Wei Jia53692fa2017-12-11 10:33:46 -0800198
199 void fetchTextData(
200 uint32_t what, media_track_type type,
201 int32_t curGen, const sp<AnotherPacketSource>& packets, const sp<AMessage>& msg);
202
203 void sendGlobalTextData(
204 uint32_t what,
205 int32_t curGen, sp<AMessage> msg);
206
207 void sendTextData(
208 uint32_t what, media_track_type type,
209 int32_t curGen, const sp<AnotherPacketSource>& packets, const sp<AMessage>& msg);
210
211 sp<ABuffer> mediaBufferToABuffer(
Dongwon Kang1889c3e2018-02-01 13:44:57 -0800212 MediaBufferBase *mbuf,
Wei Jia53692fa2017-12-11 10:33:46 -0800213 media_track_type trackType);
214
215 void postReadBuffer(media_track_type trackType);
216 void onReadBuffer(const sp<AMessage>& msg);
217 // When |mode| is MediaPlayer2SeekMode::SEEK_CLOSEST, the buffer read shall
218 // include an item indicating skipping rendering all buffers with timestamp
219 // earlier than |seekTimeUs|.
220 // For other modes, the buffer read will not include the item as above in order
221 // to facilitate fast seek operation.
222 void readBuffer(
223 media_track_type trackType,
224 int64_t seekTimeUs = -1ll,
225 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC,
226 int64_t *actualTimeUs = NULL, bool formatChange = false);
227
228 void queueDiscontinuityIfNeeded(
229 bool seeking, bool formatChange, media_track_type trackType, Track *track);
230
231 void schedulePollBuffering();
232 void onPollBuffering();
233 void notifyBufferingUpdate(int32_t percentage);
234
235 void sendCacheStats();
236
Robert Shih0cd95062018-01-21 17:41:16 -0800237 sp<AMessage> getFormat_l(bool audio);
Wei Jia53692fa2017-12-11 10:33:46 -0800238 sp<MetaData> getFormatMeta_l(bool audio);
239 int32_t getDataGeneration(media_track_type type) const;
240
241 // Modular DRM
242 // The source is DRM protected and is prepared for DRM.
243 bool mIsDrmProtected;
244 // releaseDrm has been processed.
245 bool mIsDrmReleased;
246 Vector<String8> mMimes;
247
248 status_t checkDrmInfo();
249
Wei Jia2409c872018-02-02 10:34:33 -0800250 DISALLOW_EVIL_CONSTRUCTORS(GenericSource2);
Wei Jia53692fa2017-12-11 10:33:46 -0800251};
252
253} // namespace android
254
Wei Jia2409c872018-02-02 10:34:33 -0800255#endif // GENERIC_SOURCE2_H_