blob: ade1aa3c880da2397485c070a9bf589d9ab9e835 [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 MediaSource;
41class MediaBuffer;
42struct MediaClock;
Wei Jia53692fa2017-12-11 10:33:46 -080043
Wei Jia2409c872018-02-02 10:34:33 -080044struct NuPlayer2::GenericSource2 : public NuPlayer2::Source,
45 public MediaBufferObserver // Modular DRM
Wei Jia53692fa2017-12-11 10:33:46 -080046{
Wei Jia003fdb52018-02-06 14:44:32 -080047 GenericSource2(const sp<AMessage> &notify, uid_t uid,
Wei Jia2409c872018-02-02 10:34:33 -080048 const sp<MediaClock> &mediaClock);
Wei Jia53692fa2017-12-11 10:33:46 -080049
50 status_t setDataSource(
Wei Jia53692fa2017-12-11 10:33:46 -080051 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
Wei Jiaf01e3122018-10-18 11:49:44 -070062 virtual void prepareAsync(int64_t startTimeUs);
Wei Jia53692fa2017-12-11 10:33:46 -080063
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
Dongwon Kang1889c3e2018-02-01 13:44:57 -080089 virtual void signalBufferReturned(MediaBufferBase *buffer);
Wei Jia53692fa2017-12-11 10:33:46 -080090
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:
Wei Jia2409c872018-02-02 10:34:33 -0800100 virtual ~GenericSource2();
Wei Jia53692fa2017-12-11 10:33:46 -0800101
102 virtual void onMessageReceived(const sp<AMessage> &msg);
103
Robert Shih0cd95062018-01-21 17:41:16 -0800104 virtual sp<AMessage> getFormat(bool audio);
Wei Jia53692fa2017-12-11 10:33:46 -0800105 virtual sp<MetaData> getFormatMeta(bool audio);
106
107private:
108 enum {
109 kWhatPrepareAsync,
110 kWhatFetchSubtitleData,
111 kWhatFetchTimedTextData,
112 kWhatSendSubtitleData,
113 kWhatSendGlobalTimedTextData,
114 kWhatSendTimedTextData,
115 kWhatChangeAVSource,
116 kWhatPollBuffering,
117 kWhatSeek,
118 kWhatReadBuffer,
119 kWhatStart,
120 kWhatResume,
121 kWhatSecureDecodersInstantiated,
122 };
123
124 struct Track {
125 size_t mIndex;
Robert Shih0cd95062018-01-21 17:41:16 -0800126 sp<AMediaExtractorWrapper> mExtractor;
Wei Jia53692fa2017-12-11 10:33:46 -0800127 sp<AnotherPacketSource> mPackets;
128 };
129
Wei Jia53692fa2017-12-11 10:33:46 -0800130 int64_t mAudioTimeUs;
131 int64_t mAudioLastDequeueTimeUs;
Wei Jia53692fa2017-12-11 10:33:46 -0800132 int64_t mVideoTimeUs;
133 int64_t mVideoLastDequeueTimeUs;
Wei Jia53692fa2017-12-11 10:33:46 -0800134
135 BufferingSettings mBufferingSettings;
136 int32_t mPrevBufferPercentage;
137 int32_t mPollBufferingGeneration;
138 bool mSentPauseOnBuffering;
139
140 int32_t mAudioDataGeneration;
141 int32_t mVideoDataGeneration;
142 int32_t mFetchSubtitleDataGeneration;
143 int32_t mFetchTimedTextDataGeneration;
144 int64_t mDurationUs;
145 bool mAudioIsVorbis;
146 // Secure codec is required.
147 bool mIsSecure;
148 bool mIsStreaming;
Wei Jia53692fa2017-12-11 10:33:46 -0800149 uid_t mUID;
150 const sp<MediaClock> mMediaClock;
Wei Jia53692fa2017-12-11 10:33:46 -0800151 AString mUri;
152 KeyedVector<String8, String8> mUriHeaders;
153 int mFd;
154 int64_t mOffset;
155 int64_t mLength;
156
157 bool mDisconnected;
Wei Jia53692fa2017-12-11 10:33:46 -0800158 sp<MetaData> mFileMeta;
Robert Shih0cd95062018-01-21 17:41:16 -0800159 sp<AMediaDataSourceWrapper> mDataSourceWrapper;
160 sp<AMediaExtractorWrapper> mExtractor;
161 Vector<sp<AMediaExtractorWrapper> > mExtractors;
Wei Jia53692fa2017-12-11 10:33:46 -0800162 bool mStarted;
163 bool mPreparing;
164 int64_t mBitrate;
165 uint32_t mPendingReadBufferTypes;
166 sp<ABuffer> mGlobalTimedText;
167
Robert Shih0cd95062018-01-21 17:41:16 -0800168 Track mVideoTrack;
169 Track mAudioTrack;
170 Track mSubtitleTrack;
171 Track mTimedTextTrack;
172
Wei Jia53692fa2017-12-11 10:33:46 -0800173 mutable Mutex mLock;
174
175 sp<ALooper> mLooper;
176
177 void resetDataSource();
178
179 status_t initFromDataSource();
180 int64_t getLastReadPosition();
181
182 void notifyPreparedAndCleanup(status_t err);
183 void onSecureDecodersInstantiated(status_t err);
184 void finishPrepareAsync();
185 status_t startSources();
186
187 void onSeek(const sp<AMessage>& msg);
188 status_t doSeek(int64_t seekTimeUs, MediaPlayer2SeekMode mode);
189
Wei Jiaf01e3122018-10-18 11:49:44 -0700190 void onPrepareAsync(int64_t startTimeUs);
Wei Jia53692fa2017-12-11 10:33:46 -0800191
192 void fetchTextData(
193 uint32_t what, media_track_type type,
194 int32_t curGen, const sp<AnotherPacketSource>& packets, const sp<AMessage>& msg);
195
196 void sendGlobalTextData(
197 uint32_t what,
198 int32_t curGen, sp<AMessage> msg);
199
200 void sendTextData(
201 uint32_t what, media_track_type type,
202 int32_t curGen, const sp<AnotherPacketSource>& packets, const sp<AMessage>& msg);
203
204 sp<ABuffer> mediaBufferToABuffer(
Dongwon Kang1889c3e2018-02-01 13:44:57 -0800205 MediaBufferBase *mbuf,
Wei Jia53692fa2017-12-11 10:33:46 -0800206 media_track_type trackType);
207
208 void postReadBuffer(media_track_type trackType);
209 void onReadBuffer(const sp<AMessage>& msg);
210 // When |mode| is MediaPlayer2SeekMode::SEEK_CLOSEST, the buffer read shall
211 // include an item indicating skipping rendering all buffers with timestamp
212 // earlier than |seekTimeUs|.
213 // For other modes, the buffer read will not include the item as above in order
214 // to facilitate fast seek operation.
215 void readBuffer(
216 media_track_type trackType,
217 int64_t seekTimeUs = -1ll,
218 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC,
219 int64_t *actualTimeUs = NULL, bool formatChange = false);
220
221 void queueDiscontinuityIfNeeded(
222 bool seeking, bool formatChange, media_track_type trackType, Track *track);
223
224 void schedulePollBuffering();
225 void onPollBuffering();
226 void notifyBufferingUpdate(int32_t percentage);
227
Robert Shih0cd95062018-01-21 17:41:16 -0800228 sp<AMessage> getFormat_l(bool audio);
Wei Jia53692fa2017-12-11 10:33:46 -0800229 sp<MetaData> getFormatMeta_l(bool audio);
230 int32_t getDataGeneration(media_track_type type) const;
231
232 // Modular DRM
233 // The source is DRM protected and is prepared for DRM.
234 bool mIsDrmProtected;
235 // releaseDrm has been processed.
236 bool mIsDrmReleased;
237 Vector<String8> mMimes;
238
239 status_t checkDrmInfo();
240
Wei Jia2409c872018-02-02 10:34:33 -0800241 DISALLOW_EVIL_CONSTRUCTORS(GenericSource2);
Wei Jia53692fa2017-12-11 10:33:46 -0800242};
243
244} // namespace android
245
Wei Jia2409c872018-02-02 10:34:33 -0800246#endif // GENERIC_SOURCE2_H_