blob: 0666d276d1e5e57af9aeb36fbaeb19c7a0940468 [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
Wei Jia53692fa2017-12-11 10:33:46 -080085 virtual bool isStreaming() const;
86
87 // Modular DRM
88 virtual void signalBufferReturned(MediaBuffer *buffer);
89
90 virtual status_t prepareDrm(
91 const uint8_t uuid[16],
92 const Vector<uint8_t> &drmSessionId,
93 sp<AMediaCryptoWrapper> *outCrypto);
94
95 virtual status_t releaseDrm();
96
97
98protected:
99 virtual ~GenericSource();
100
101 virtual void onMessageReceived(const sp<AMessage> &msg);
102
103 virtual sp<MetaData> getFormatMeta(bool audio);
104
105private:
106 enum {
107 kWhatPrepareAsync,
108 kWhatFetchSubtitleData,
109 kWhatFetchTimedTextData,
110 kWhatSendSubtitleData,
111 kWhatSendGlobalTimedTextData,
112 kWhatSendTimedTextData,
113 kWhatChangeAVSource,
114 kWhatPollBuffering,
115 kWhatSeek,
116 kWhatReadBuffer,
117 kWhatStart,
118 kWhatResume,
119 kWhatSecureDecodersInstantiated,
120 };
121
122 struct Track {
123 size_t mIndex;
124 sp<IMediaSource> mSource;
125 sp<AnotherPacketSource> mPackets;
126 };
127
128 Vector<sp<IMediaSource> > mSources;
129 Track mAudioTrack;
130 int64_t mAudioTimeUs;
131 int64_t mAudioLastDequeueTimeUs;
132 Track mVideoTrack;
133 int64_t mVideoTimeUs;
134 int64_t mVideoLastDequeueTimeUs;
135 Track mSubtitleTrack;
136 Track mTimedTextTrack;
137
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;
152 bool mUIDValid;
153 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(
205 MediaBuffer *mbuf,
206 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
242 DISALLOW_EVIL_CONSTRUCTORS(GenericSource);
243};
244
245} // namespace android
246
247#endif // GENERIC_SOURCE_H_