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