GenericSource.h revision 2a3cc9a64330dd36e466fe5e1b634146f2d641c1
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 <media/mediaplayer.h>
27
28namespace android {
29
30struct AnotherPacketSource;
31struct ARTSPController;
32struct DataSource;
33struct IMediaHTTPService;
34struct MediaSource;
35class MediaBuffer;
36struct NuCachedSource2;
37struct WVMExtractor;
38
39struct NuPlayer::GenericSource : public NuPlayer::Source {
40    GenericSource(const sp<AMessage> &notify, bool uidValid, uid_t uid);
41
42    status_t setDataSource(
43            const sp<IMediaHTTPService> &httpService,
44            const char *url,
45            const KeyedVector<String8, String8> *headers);
46
47    status_t setDataSource(int fd, int64_t offset, int64_t length);
48
49    virtual void prepareAsync();
50
51    virtual void start();
52
53    virtual status_t feedMoreTSData();
54
55    virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
56
57    virtual status_t getDuration(int64_t *durationUs);
58    virtual size_t getTrackCount() const;
59    virtual sp<AMessage> getTrackInfo(size_t trackIndex) const;
60    virtual ssize_t getSelectedTrack(media_track_type type) const;
61    virtual status_t selectTrack(size_t trackIndex, bool select);
62    virtual status_t seekTo(int64_t seekTimeUs);
63
64    virtual status_t setBuffers(bool audio, Vector<MediaBuffer *> &buffers);
65
66protected:
67    virtual ~GenericSource();
68
69    virtual void onMessageReceived(const sp<AMessage> &msg);
70
71    virtual sp<MetaData> getFormatMeta(bool audio);
72
73private:
74    enum {
75        kWhatPrepareAsync,
76        kWhatFetchSubtitleData,
77        kWhatFetchTimedTextData,
78        kWhatSendSubtitleData,
79        kWhatSendTimedTextData,
80        kWhatChangeAVSource,
81        kWhatPollBuffering,
82    };
83
84    Vector<sp<MediaSource> > mSources;
85
86    struct Track {
87        size_t mIndex;
88        sp<MediaSource> mSource;
89        sp<AnotherPacketSource> mPackets;
90    };
91
92    Track mAudioTrack;
93    Track mVideoTrack;
94    Track mSubtitleTrack;
95    Track mTimedTextTrack;
96
97    int32_t mFetchSubtitleDataGeneration;
98    int32_t mFetchTimedTextDataGeneration;
99    int64_t mDurationUs;
100    bool mAudioIsVorbis;
101    bool mIsWidevine;
102    bool mUIDValid;
103    uid_t mUID;
104    sp<IMediaHTTPService> mHTTPService;
105    AString mUri;
106    KeyedVector<String8, String8> mUriHeaders;
107    int mFd;
108    int64_t mOffset;
109    int64_t mLength;
110
111    sp<DataSource> mDataSource;
112    sp<NuCachedSource2> mCachedSource;
113    sp<WVMExtractor> mWVMExtractor;
114    String8 mContentType;
115    AString mSniffedMIME;
116    off64_t mMetaDataSize;
117    int64_t mBitrate;
118    int32_t mPollBufferingGeneration;
119
120    sp<ALooper> mLooper;
121
122    void resetDataSource();
123
124    status_t initFromDataSource();
125
126    status_t prefillCacheIfNecessary();
127
128    void notifyPreparedAndCleanup(status_t err);
129
130    void onPrepareAsync();
131
132    void fetchTextData(
133            uint32_t what, media_track_type type,
134            int32_t curGen, sp<AnotherPacketSource> packets, sp<AMessage> msg);
135
136    void sendTextData(
137            uint32_t what, media_track_type type,
138            int32_t curGen, sp<AnotherPacketSource> packets, sp<AMessage> msg);
139
140    sp<ABuffer> mediaBufferToABuffer(
141            MediaBuffer *mbuf,
142            media_track_type trackType,
143            int64_t *actualTimeUs = NULL);
144
145    void readBuffer(
146            media_track_type trackType,
147            int64_t seekTimeUs = -1ll, int64_t *actualTimeUs = NULL, bool formatChange = false);
148
149    void schedulePollBuffering();
150    void cancelPollBuffering();
151    void onPollBuffering();
152    void notifyBufferingUpdate(int percentage);
153
154    DISALLOW_EVIL_CONSTRUCTORS(GenericSource);
155};
156
157}  // namespace android
158
159#endif  // GENERIC_SOURCE_H_
160