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 
27 namespace android {
28 
29 struct 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 
58 protected:
59     virtual ~M3UParser();
60 
61 private:
62     struct MediaGroup;
63 
64     struct Item {
65         AString mURI;
66         sp<AMessage> mMeta;
67         AString makeURL(const char *baseURL) const;
68     };
69 
70     status_t mInitCheck;
71 
72     AString mBaseURI;
73     bool mIsExtM3U;
74     bool mIsVariantPlaylist;
75     bool mIsComplete;
76     bool mIsEvent;
77     int32_t mFirstSeqNumber;
78     int32_t mLastSeqNumber;
79     int64_t mTargetDurationUs;
80     size_t mDiscontinuitySeq;
81     int32_t mDiscontinuityCount;
82 
83     sp<AMessage> mMeta;
84     Vector<Item> mItems;
85     ssize_t mSelectedIndex;
86 
87     // Media groups keyed by group ID.
88     KeyedVector<AString, sp<MediaGroup> > mMediaGroups;
89 
90     status_t parse(const void *data, size_t size);
91 
92     static status_t parseMetaData(
93             const AString &line, sp<AMessage> *meta, const char *key);
94 
95     static status_t parseMetaDataDuration(
96             const AString &line, sp<AMessage> *meta, const char *key);
97 
98     status_t parseStreamInf(
99             const AString &line, sp<AMessage> *meta) const;
100 
101     static status_t parseCipherInfo(
102             const AString &line, sp<AMessage> *meta, const AString &baseURI);
103 
104     static status_t parseByteRange(
105             const AString &line, uint64_t curOffset,
106             uint64_t *length, uint64_t *offset);
107 
108     status_t parseMedia(const AString &line);
109 
110     static status_t parseDiscontinuitySequence(const AString &line, size_t *seq);
111 
112     static status_t ParseInt32(const char *s, int32_t *x);
113     static status_t ParseDouble(const char *s, double *x);
114 
115     static bool isQuotedString(const AString &str);
116     static AString unquoteString(const AString &str);
117     static bool codecIsType(const AString &codec, const char *type);
118 
119     DISALLOW_EVIL_CONSTRUCTORS(M3UParser);
120 };
121 
122 }  // namespace android
123 
124 #endif  // M3U_PARSER_H_
125