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