blob: 896c39786b1468ad86bfdd2628a49500654b7ab9 [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>
Wei Jia53692fa2017-12-11 10:33:46 -080028
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
Wei Jia2409c872018-02-02 10:34:33 -080043struct NuPlayer2::GenericSource2 : public NuPlayer2::Source,
44 public MediaBufferObserver // Modular DRM
Wei Jia53692fa2017-12-11 10:33:46 -080045{
Wei Jia003fdb52018-02-06 14:44:32 -080046 GenericSource2(const sp<AMessage> &notify, uid_t uid,
Wei Jia2409c872018-02-02 10:34:33 -080047 const sp<MediaClock> &mediaClock);
Wei Jia53692fa2017-12-11 10:33:46 -080048
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
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
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;
Wei Jia53692fa2017-12-11 10:33:46 -0800153 uid_t mUID;
154 const sp<MediaClock> mMediaClock;
155 sp<MediaHTTPService> mHTTPService;
156 AString mUri;
157 KeyedVector<String8, String8> mUriHeaders;
158 int mFd;
159 int64_t mOffset;
160 int64_t mLength;
161
162 bool mDisconnected;
163 sp<DataSource> mDataSource;
164 sp<NuCachedSource2> mCachedSource;
165 sp<DataSource> mHttpSource;
166 sp<MetaData> mFileMeta;
167 bool mStarted;
168 bool mPreparing;
169 int64_t mBitrate;
170 uint32_t mPendingReadBufferTypes;
171 sp<ABuffer> mGlobalTimedText;
172
173 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
190 void onPrepareAsync();
191
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
228 void sendCacheStats();
229
230 sp<MetaData> getFormatMeta_l(bool audio);
231 int32_t getDataGeneration(media_track_type type) const;
232
233 // Modular DRM
234 // The source is DRM protected and is prepared for DRM.
235 bool mIsDrmProtected;
236 // releaseDrm has been processed.
237 bool mIsDrmReleased;
238 Vector<String8> mMimes;
239
240 status_t checkDrmInfo();
241
Wei Jia2409c872018-02-02 10:34:33 -0800242 DISALLOW_EVIL_CONSTRUCTORS(GenericSource2);
Wei Jia53692fa2017-12-11 10:33:46 -0800243};
244
245} // namespace android
246
Wei Jia2409c872018-02-02 10:34:33 -0800247#endif // GENERIC_SOURCE2_H_