M3UParser.cpp revision bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0
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#include "include/M3UParser.h"
18
19#include <media/stagefright/foundation/AMessage.h>
20#include <media/stagefright/MediaDebug.h>
21#include <media/stagefright/MediaErrors.h>
22
23namespace android {
24
25M3UParser::M3UParser(
26        const char *baseURI, const void *data, size_t size)
27    : mInitCheck(NO_INIT),
28      mBaseURI(baseURI),
29      mIsExtM3U(false),
30      mIsVariantPlaylist(false),
31      mIsComplete(false) {
32    mInitCheck = parse(data, size);
33}
34
35M3UParser::~M3UParser() {
36}
37
38status_t M3UParser::initCheck() const {
39    return mInitCheck;
40}
41
42bool M3UParser::isExtM3U() const {
43    return mIsExtM3U;
44}
45
46bool M3UParser::isVariantPlaylist() const {
47    return mIsVariantPlaylist;
48}
49
50bool M3UParser::isComplete() const {
51    return mIsComplete;
52}
53
54sp<AMessage> M3UParser::meta() {
55    return mMeta;
56}
57
58size_t M3UParser::size() {
59    return mItems.size();
60}
61
62bool M3UParser::itemAt(size_t index, AString *uri, sp<AMessage> *meta) {
63    uri->clear();
64    if (meta) { *meta = NULL; }
65
66    if (index >= mItems.size()) {
67        return false;
68    }
69
70    *uri = mItems.itemAt(index).mURI;
71
72    if (meta) {
73        *meta = mItems.itemAt(index).mMeta;
74    }
75
76    return true;
77}
78
79static bool MakeURL(const char *baseURL, const char *url, AString *out) {
80    out->clear();
81
82    if (strncasecmp("http://", baseURL, 7)
83            && strncasecmp("file://", baseURL, 7)) {
84        // Base URL must be absolute
85        return false;
86    }
87
88    if (!strncasecmp("http://", url, 7)) {
89        // "url" is already an absolute URL, ignore base URL.
90        out->setTo(url);
91        return true;
92    }
93
94    size_t n = strlen(baseURL);
95    if (baseURL[n - 1] == '/') {
96        out->setTo(baseURL);
97        out->append(url);
98    } else {
99        char *slashPos = strrchr(baseURL, '/');
100
101        if (slashPos > &baseURL[6]) {
102            out->setTo(baseURL, slashPos - baseURL);
103        } else {
104            out->setTo(baseURL);
105        }
106
107        out->append("/");
108        out->append(url);
109    }
110
111    return true;
112}
113
114status_t M3UParser::parse(const void *_data, size_t size) {
115    int32_t lineNo = 0;
116
117    sp<AMessage> itemMeta;
118
119    const char *data = (const char *)_data;
120    size_t offset = 0;
121    while (offset < size) {
122        size_t offsetLF = offset;
123        while (offsetLF < size && data[offsetLF] != '\n') {
124            ++offsetLF;
125        }
126        if (offsetLF >= size) {
127            break;
128        }
129
130        AString line;
131        if (offsetLF > offset && data[offsetLF - 1] == '\r') {
132            line.setTo(&data[offset], offsetLF - offset - 1);
133        } else {
134            line.setTo(&data[offset], offsetLF - offset);
135        }
136
137        // LOGI("#%s#", line.c_str());
138
139        if (line.empty()) {
140            offset = offsetLF + 1;
141            continue;
142        }
143
144        if (lineNo == 0 && line == "#EXTM3U") {
145            mIsExtM3U = true;
146        }
147
148        if (mIsExtM3U) {
149            status_t err = OK;
150
151            if (line.startsWith("#EXT-X-TARGETDURATION")) {
152                if (mIsVariantPlaylist) {
153                    return ERROR_MALFORMED;
154                }
155                err = parseMetaData(line, &mMeta, "target-duration");
156            } else if (line.startsWith("#EXT-X-MEDIA-SEQUENCE")) {
157                if (mIsVariantPlaylist) {
158                    return ERROR_MALFORMED;
159                }
160                err = parseMetaData(line, &mMeta, "media-sequence");
161            } else if (line.startsWith("#EXT-X-ENDLIST")) {
162                mIsComplete = true;
163            } else if (line.startsWith("#EXTINF")) {
164                if (mIsVariantPlaylist) {
165                    return ERROR_MALFORMED;
166                }
167                err = parseMetaData(line, &itemMeta, "duration");
168            } else if (line.startsWith("#EXT-X-DISCONTINUITY")) {
169                if (mIsVariantPlaylist) {
170                    return ERROR_MALFORMED;
171                }
172                if (itemMeta == NULL) {
173                    itemMeta = new AMessage;
174                }
175                itemMeta->setInt32("discontinuity", true);
176            } else if (line.startsWith("#EXT-X-STREAM-INF")) {
177                if (mMeta != NULL) {
178                    return ERROR_MALFORMED;
179                }
180                mIsVariantPlaylist = true;
181                err = parseStreamInf(line, &itemMeta);
182            }
183
184            if (err != OK) {
185                return err;
186            }
187        }
188
189        if (!line.startsWith("#")) {
190            if (!mIsVariantPlaylist) {
191                int32_t durationSecs;
192                if (itemMeta == NULL
193                        || !itemMeta->findInt32("duration", &durationSecs)) {
194                    return ERROR_MALFORMED;
195                }
196            }
197
198            mItems.push();
199            Item *item = &mItems.editItemAt(mItems.size() - 1);
200
201            CHECK(MakeURL(mBaseURI.c_str(), line.c_str(), &item->mURI));
202
203            item->mMeta = itemMeta;
204
205            itemMeta.clear();
206        }
207
208        offset = offsetLF + 1;
209        ++lineNo;
210    }
211
212    return OK;
213}
214
215// static
216status_t M3UParser::parseMetaData(
217        const AString &line, sp<AMessage> *meta, const char *key) {
218    ssize_t colonPos = line.find(":");
219
220    if (colonPos < 0) {
221        return ERROR_MALFORMED;
222    }
223
224    int32_t x;
225    status_t err = ParseInt32(line.c_str() + colonPos + 1, &x);
226
227    if (err != OK) {
228        return err;
229    }
230
231    if (meta->get() == NULL) {
232        *meta = new AMessage;
233    }
234    (*meta)->setInt32(key, x);
235
236    return OK;
237}
238
239// static
240status_t M3UParser::parseStreamInf(
241        const AString &line, sp<AMessage> *meta) {
242    ssize_t colonPos = line.find(":");
243
244    if (colonPos < 0) {
245        return ERROR_MALFORMED;
246    }
247
248    size_t offset = colonPos + 1;
249
250    while (offset < line.size()) {
251        ssize_t end = line.find(",", offset);
252        if (end < 0) {
253            end = line.size();
254        }
255
256        AString attr(line, offset, end - offset);
257        attr.trim();
258
259        offset = end + 1;
260
261        ssize_t equalPos = attr.find("=");
262        if (equalPos < 0) {
263            continue;
264        }
265
266        AString key(attr, 0, equalPos);
267        key.trim();
268
269        AString val(attr, equalPos + 1, attr.size() - equalPos - 1);
270        val.trim();
271
272        LOGV("key=%s value=%s", key.c_str(), val.c_str());
273
274        if (!strcasecmp("bandwidth", key.c_str())) {
275            const char *s = val.c_str();
276            char *end;
277            unsigned long x = strtoul(s, &end, 10);
278
279            if (end == s || *end != '\0') {
280                // malformed
281                continue;
282            }
283
284            if (meta->get() == NULL) {
285                *meta = new AMessage;
286            }
287            (*meta)->setInt32("bandwidth", x);
288        }
289    }
290
291    return OK;
292}
293
294// static
295status_t M3UParser::ParseInt32(const char *s, int32_t *x) {
296    char *end;
297    long lval = strtol(s, &end, 10);
298
299    if (end == s || (*end != '\0' && *end != ',')) {
300        return ERROR_MALFORMED;
301    }
302
303    *x = (int32_t)lval;
304
305    return OK;
306}
307
308}  // namespace android
309