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