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