1/*
2 * Copyright (C) 2010 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 MPEG2_TS_EXTRACTOR_H_
18
19#define MPEG2_TS_EXTRACTOR_H_
20
21#include <media/stagefright/foundation/ABase.h>
22#include <media/stagefright/MediaExtractor.h>
23#include <media/stagefright/MediaSource.h>
24#include <utils/threads.h>
25#include <utils/KeyedVector.h>
26#include <utils/Vector.h>
27
28#include "mpeg2ts/ATSParser.h"
29
30namespace android {
31
32struct AMessage;
33struct AnotherPacketSource;
34struct ATSParser;
35class DataSource;
36struct MPEG2TSSource;
37class String8;
38
39struct MPEG2TSExtractor : public MediaExtractor {
40    explicit MPEG2TSExtractor(const sp<DataSource> &source);
41
42    virtual size_t countTracks();
43    virtual sp<IMediaSource> getTrack(size_t index);
44    virtual sp<MetaData> getTrackMetaData(size_t index, uint32_t flags);
45
46    virtual sp<MetaData> getMetaData();
47
48    virtual status_t setMediaCas(const sp<ICas> &cas) override;
49
50    virtual uint32_t flags() const;
51    virtual const char * name() { return "MPEG2TSExtractor"; }
52
53private:
54    friend struct MPEG2TSSource;
55
56    mutable Mutex mLock;
57
58    sp<DataSource> mDataSource;
59
60    sp<ATSParser> mParser;
61
62    // Used to remember SyncEvent occurred in feedMore() when called from init(),
63    // because init() needs to update |mSourceImpls| before adding SyncPoint.
64    ATSParser::SyncEvent mLastSyncEvent;
65
66    Vector<sp<AnotherPacketSource> > mSourceImpls;
67
68    Vector<KeyedVector<int64_t, off64_t> > mSyncPoints;
69    // Sync points used for seeking --- normally one for video track is used.
70    // If no video track is present, audio track will be used instead.
71    KeyedVector<int64_t, off64_t> *mSeekSyncPoints;
72
73    off64_t mOffset;
74
75    static bool isScrambledFormat(const sp<MetaData> &format);
76
77    void init();
78    void addSource(const sp<AnotherPacketSource> &impl);
79    // Try to feed more data from source to parser.
80    // |isInit| means this function is called inside init(). This is a signal to
81    // save SyncEvent so that init() can add SyncPoint after it updates |mSourceImpls|.
82    // This function returns OK if expected amount of data is fed from DataSource to
83    // parser and is successfully parsed. Otherwise, various error codes could be
84    // returned, e.g., ERROR_END_OF_STREAM, or no data availalbe from DataSource, or
85    // the data has syntax error during parsing, etc.
86    status_t feedMore(bool isInit = false);
87    status_t seek(int64_t seekTimeUs,
88            const MediaSource::ReadOptions::SeekMode& seekMode);
89    status_t queueDiscontinuityForSeek(int64_t actualSeekTimeUs);
90    status_t seekBeyond(int64_t seekTimeUs);
91
92    status_t feedUntilBufferAvailable(const sp<AnotherPacketSource> &impl);
93
94    // Add a SynPoint derived from |event|.
95    void addSyncPoint_l(const ATSParser::SyncEvent &event);
96
97    status_t  estimateDurationsFromTimesUsAtEnd();
98
99    DISALLOW_EVIL_CONSTRUCTORS(MPEG2TSExtractor);
100};
101
102bool SniffMPEG2TS(
103        const sp<DataSource> &source, String8 *mimeType, float *confidence,
104        sp<AMessage> *);
105
106}  // namespace android
107
108#endif  // MPEG2_TS_EXTRACTOR_H_
109