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 M3U_PARSER_H_
18
19#define M3U_PARSER_H_
20
21#include <media/stagefright/foundation/ABase.h>
22#include <media/stagefright/foundation/AMessage.h>
23#include <media/stagefright/foundation/AString.h>
24#include <media/mediaplayer.h>
25#include <utils/Vector.h>
26
27namespace android {
28
29struct M3UParser : public RefBase {
30    M3UParser(const char *baseURI, const void *data, size_t size);
31
32    status_t initCheck() const;
33
34    bool isExtM3U() const;
35    bool isVariantPlaylist() const;
36    bool isComplete() const;
37    bool isEvent() const;
38    size_t getDiscontinuitySeq() const;
39    int64_t getTargetDuration() const;
40    int32_t getFirstSeqNumber() const;
41    void getSeqNumberRange(int32_t *firstSeq, int32_t *lastSeq) const;
42
43    sp<AMessage> meta();
44
45    size_t size();
46    bool itemAt(size_t index, AString *uri, sp<AMessage> *meta = NULL);
47
48    void pickRandomMediaItems();
49    status_t selectTrack(size_t index, bool select);
50    size_t getTrackCount() const;
51    sp<AMessage> getTrackInfo(size_t index) const;
52    ssize_t getSelectedIndex() const;
53    ssize_t getSelectedTrack(media_track_type /* type */) const;
54
55    bool getTypeURI(size_t index, const char *key, AString *uri) const;
56    bool hasType(size_t index, const char *key) const;
57
58protected:
59    virtual ~M3UParser();
60
61private:
62    struct MediaGroup;
63
64    struct Item {
65        AString mURI;
66        sp<AMessage> mMeta;
67    };
68
69    status_t mInitCheck;
70
71    AString mBaseURI;
72    bool mIsExtM3U;
73    bool mIsVariantPlaylist;
74    bool mIsComplete;
75    bool mIsEvent;
76    int32_t mFirstSeqNumber;
77    int32_t mLastSeqNumber;
78    int64_t mTargetDurationUs;
79    size_t mDiscontinuitySeq;
80    int32_t mDiscontinuityCount;
81
82    sp<AMessage> mMeta;
83    Vector<Item> mItems;
84    ssize_t mSelectedIndex;
85
86    // Media groups keyed by group ID.
87    KeyedVector<AString, sp<MediaGroup> > mMediaGroups;
88
89    status_t parse(const void *data, size_t size);
90
91    static status_t parseMetaData(
92            const AString &line, sp<AMessage> *meta, const char *key);
93
94    static status_t parseMetaDataDuration(
95            const AString &line, sp<AMessage> *meta, const char *key);
96
97    status_t parseStreamInf(
98            const AString &line, sp<AMessage> *meta) const;
99
100    static status_t parseCipherInfo(
101            const AString &line, sp<AMessage> *meta, const AString &baseURI);
102
103    static status_t parseByteRange(
104            const AString &line, uint64_t curOffset,
105            uint64_t *length, uint64_t *offset);
106
107    status_t parseMedia(const AString &line);
108
109    static status_t parseDiscontinuitySequence(const AString &line, size_t *seq);
110
111    static status_t ParseInt32(const char *s, int32_t *x);
112    static status_t ParseDouble(const char *s, double *x);
113
114    static bool isQuotedString(const AString &str);
115    static AString unquoteString(const AString &str);
116    static bool codecIsType(const AString &codec, const char *type);
117
118    DISALLOW_EVIL_CONSTRUCTORS(M3UParser);
119};
120
121}  // namespace android
122
123#endif  // M3U_PARSER_H_
124