• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 "NuPlayer.h"
22 #include "NuPlayerSource.h"
23 
24 #include "ATSParser.h"
25 
26 #include <android-base/unique_fd.h>
27 #include <media/mediaplayer.h>
28 #include <media/stagefright/MediaBuffer.h>
29 
30 namespace android {
31 
32 class DecryptHandle;
33 struct AnotherPacketSource;
34 struct ARTSPController;
35 class DataSource;
36 class IDataSource;
37 struct IMediaHTTPService;
38 struct MediaSource;
39 class IMediaSource;
40 class MediaBuffer;
41 struct MediaClock;
42 struct NuCachedSource2;
43 
44 struct NuPlayer::GenericSource : public NuPlayer::Source,
45                                  public MediaBufferObserver // Modular DRM
46 {
47     GenericSource(const sp<AMessage> &notify, bool uidValid, uid_t uid,
48                   const sp<MediaClock> &mediaClock);
49 
50     status_t setDataSource(
51             const sp<IMediaHTTPService> &httpService,
52             const char *url,
53             const KeyedVector<String8, String8> *headers);
54 
55     status_t setDataSource(int fd, int64_t offset, int64_t length);
56 
57     status_t setDataSource(const sp<DataSource>& dataSource);
58 
59     virtual status_t getBufferingSettings(
60             BufferingSettings* buffering /* nonnull */) override;
61     virtual status_t setBufferingSettings(const BufferingSettings& buffering) override;
62 
63     virtual void prepareAsync();
64 
65     virtual void start();
66     virtual void stop();
67     virtual void pause();
68     virtual void resume();
69 
70     virtual void disconnect();
71 
72     virtual status_t feedMoreTSData();
73 
74     virtual sp<MetaData> getFileFormatMeta() const;
75 
76     virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
77 
78     virtual status_t getDuration(int64_t *durationUs);
79     virtual size_t getTrackCount() const;
80     virtual sp<AMessage> getTrackInfo(size_t trackIndex) const;
81     virtual ssize_t getSelectedTrack(media_track_type type) const;
82     virtual status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs);
83     virtual status_t seekTo(
84         int64_t seekTimeUs,
85         MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC) override;
86 
87     virtual bool isStreaming() const;
88 
89     // Modular DRM
90     virtual void signalBufferReturned(MediaBufferBase *buffer);
91 
92     virtual status_t prepareDrm(
93             const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId, sp<ICrypto> *outCrypto);
94 
95     virtual status_t releaseDrm();
96 
97 
98 protected:
99     virtual ~GenericSource();
100 
101     virtual void onMessageReceived(const sp<AMessage> &msg);
102 
103     virtual sp<MetaData> getFormatMeta(bool audio);
104 
105 private:
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<IMediaHTTPService> mHTTPService;
156     AString mUri;
157     KeyedVector<String8, String8> mUriHeaders;
158     base::unique_fd 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     mutable Mutex mDisconnectLock; // Protects mDataSource, mHttpSource and mDisconnected
175 
176     sp<ALooper> mLooper;
177 
178     void resetDataSource();
179 
180     status_t initFromDataSource();
181     int64_t getLastReadPosition();
182 
183     void notifyPreparedAndCleanup(status_t err);
184     void onSecureDecodersInstantiated(status_t err);
185     void finishPrepareAsync();
186     status_t startSources();
187 
188     void onSeek(const sp<AMessage>& msg);
189     status_t doSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode);
190 
191     void onPrepareAsync();
192 
193     void fetchTextData(
194             uint32_t what, media_track_type type,
195             int32_t curGen, const sp<AnotherPacketSource>& packets, const sp<AMessage>& msg);
196 
197     void sendGlobalTextData(
198             uint32_t what,
199             int32_t curGen, sp<AMessage> msg);
200 
201     void sendTextData(
202             uint32_t what, media_track_type type,
203             int32_t curGen, const sp<AnotherPacketSource>& packets, const sp<AMessage>& msg);
204 
205     sp<ABuffer> mediaBufferToABuffer(
206             MediaBufferBase *mbuf,
207             media_track_type trackType);
208 
209     void postReadBuffer(media_track_type trackType);
210     void onReadBuffer(const sp<AMessage>& msg);
211     // When |mode| is MediaPlayerSeekMode::SEEK_CLOSEST, the buffer read shall
212     // include an item indicating skipping rendering all buffers with timestamp
213     // earlier than |seekTimeUs|.
214     // For other modes, the buffer read will not include the item as above in order
215     // to facilitate fast seek operation.
216     void readBuffer(
217             media_track_type trackType,
218             int64_t seekTimeUs = -1ll,
219             MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC,
220             int64_t *actualTimeUs = NULL, bool formatChange = false);
221 
222     void queueDiscontinuityIfNeeded(
223             bool seeking, bool formatChange, media_track_type trackType, Track *track);
224 
225     void schedulePollBuffering();
226     void onPollBuffering();
227     void notifyBufferingUpdate(int32_t percentage);
228 
229     void sendCacheStats();
230 
231     sp<MetaData> getFormatMeta_l(bool audio);
232     int32_t getDataGeneration(media_track_type type) const;
233 
234     // Modular DRM
235     // The source is DRM protected and is prepared for DRM.
236     bool mIsDrmProtected;
237     // releaseDrm has been processed.
238     bool mIsDrmReleased;
239     Vector<String8> mMimes;
240 
241     status_t checkDrmInfo();
242 
243     DISALLOW_EVIL_CONSTRUCTORS(GenericSource);
244 };
245 
246 }  // namespace android
247 
248 #endif  // GENERIC_SOURCE_H_
249