1/*
2
3 * Copyright (C) 2010 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef MPEG2_TS_EXTRACTOR_H_
19
20#define MPEG2_TS_EXTRACTOR_H_
21
22#include <media/stagefright/foundation/ABase.h>
23#include <media/MediaExtractor.h>
24#include <media/MediaTrack.h>
25#include <media/stagefright/MetaDataBase.h>
26#include <utils/threads.h>
27#include <utils/KeyedVector.h>
28#include <utils/Vector.h>
29
30#include "mpeg2ts/ATSParser.h"
31
32namespace android {
33
34struct AMessage;
35struct AnotherPacketSource;
36struct ATSParser;
37class DataSourceBase;
38struct MPEG2TSSource;
39class String8;
40
41struct MPEG2TSExtractor : public MediaExtractor {
42    explicit MPEG2TSExtractor(DataSourceBase *source);
43
44    virtual size_t countTracks();
45    virtual MediaTrack *getTrack(size_t index);
46    virtual status_t getTrackMetaData(MetaDataBase &meta, size_t index, uint32_t flags);
47
48    virtual status_t getMetaData(MetaDataBase& meta);
49
50    virtual status_t setMediaCas(const uint8_t* /*casToken*/, size_t /*size*/) override;
51
52    virtual uint32_t flags() const;
53    virtual const char * name() { return "MPEG2TSExtractor"; }
54
55private:
56    friend struct MPEG2TSSource;
57
58    mutable Mutex mLock;
59
60    DataSourceBase *mDataSource;
61
62    sp<ATSParser> mParser;
63
64    // Used to remember SyncEvent occurred in feedMore() when called from init(),
65    // because init() needs to update |mSourceImpls| before adding SyncPoint.
66    ATSParser::SyncEvent mLastSyncEvent;
67
68    Vector<sp<AnotherPacketSource> > mSourceImpls;
69
70    Vector<KeyedVector<int64_t, off64_t> > mSyncPoints;
71    // Sync points used for seeking --- normally one for video track is used.
72    // If no video track is present, audio track will be used instead.
73    KeyedVector<int64_t, off64_t> *mSeekSyncPoints;
74
75    off64_t mOffset;
76
77    static bool isScrambledFormat(MetaDataBase &format);
78
79    void init();
80    void addSource(const sp<AnotherPacketSource> &impl);
81    // Try to feed more data from source to parser.
82    // |isInit| means this function is called inside init(). This is a signal to
83    // save SyncEvent so that init() can add SyncPoint after it updates |mSourceImpls|.
84    // This function returns OK if expected amount of data is fed from DataSourceBase to
85    // parser and is successfully parsed. Otherwise, various error codes could be
86    // returned, e.g., ERROR_END_OF_STREAM, or no data availalbe from DataSourceBase, or
87    // the data has syntax error during parsing, etc.
88    status_t feedMore(bool isInit = false);
89    status_t seek(int64_t seekTimeUs,
90            const MediaSource::ReadOptions::SeekMode& seekMode);
91    status_t queueDiscontinuityForSeek(int64_t actualSeekTimeUs);
92    status_t seekBeyond(int64_t seekTimeUs);
93
94    status_t feedUntilBufferAvailable(const sp<AnotherPacketSource> &impl);
95
96    // Add a SynPoint derived from |event|.
97    void addSyncPoint_l(const ATSParser::SyncEvent &event);
98
99    status_t  estimateDurationsFromTimesUsAtEnd();
100
101    DISALLOW_EVIL_CONSTRUCTORS(MPEG2TSExtractor);
102};
103
104bool SniffMPEG2TS(DataSourceBase *source, float *confidence);
105
106}  // namespace android
107
108#endif  // MPEG2_TS_EXTRACTOR_H_
109