M3UParser.cpp revision 9067e30b3ccb3a07e41b61af22c036378053a9a3
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//#define LOG_NDEBUG 0
18#define LOG_TAG "M3UParser"
19#include <utils/Log.h>
20
21#include "include/M3UParser.h"
22
23#include <media/stagefright/foundation/ADebug.h>
24#include <media/stagefright/foundation/AMessage.h>
25#include <media/stagefright/MediaErrors.h>
26
27namespace android {
28
29M3UParser::M3UParser(
30        const char *baseURI, const void *data, size_t size)
31    : mInitCheck(NO_INIT),
32      mBaseURI(baseURI),
33      mIsExtM3U(false),
34      mIsVariantPlaylist(false),
35      mIsComplete(false) {
36    mInitCheck = parse(data, size);
37}
38
39M3UParser::~M3UParser() {
40}
41
42status_t M3UParser::initCheck() const {
43    return mInitCheck;
44}
45
46bool M3UParser::isExtM3U() const {
47    return mIsExtM3U;
48}
49
50bool M3UParser::isVariantPlaylist() const {
51    return mIsVariantPlaylist;
52}
53
54bool M3UParser::isComplete() const {
55    return mIsComplete;
56}
57
58sp<AMessage> M3UParser::meta() {
59    return mMeta;
60}
61
62size_t M3UParser::size() {
63    return mItems.size();
64}
65
66bool M3UParser::itemAt(size_t index, AString *uri, sp<AMessage> *meta) {
67    uri->clear();
68    if (meta) { *meta = NULL; }
69
70    if (index >= mItems.size()) {
71        return false;
72    }
73
74    *uri = mItems.itemAt(index).mURI;
75
76    if (meta) {
77        *meta = mItems.itemAt(index).mMeta;
78    }
79
80    return true;
81}
82
83static bool MakeURL(const char *baseURL, const char *url, AString *out) {
84    out->clear();
85
86    if (strncasecmp("http://", baseURL, 7)
87            && strncasecmp("https://", baseURL, 8)
88            && strncasecmp("file://", baseURL, 7)) {
89        // Base URL must be absolute
90        return false;
91    }
92
93    if (!strncasecmp("http://", url, 7) || !strncasecmp("https://", url, 8)) {
94        // "url" is already an absolute URL, ignore base URL.
95        out->setTo(url);
96
97        LOGV("base:'%s', url:'%s' => '%s'", baseURL, url, out->c_str());
98
99        return true;
100    }
101
102    size_t n = strlen(baseURL);
103    if (baseURL[n - 1] == '/') {
104        out->setTo(baseURL);
105        out->append(url);
106    } else {
107        const char *slashPos = strrchr(baseURL, '/');
108
109        if (slashPos > &baseURL[6]) {
110            out->setTo(baseURL, slashPos - baseURL);
111        } else {
112            out->setTo(baseURL);
113        }
114
115        out->append("/");
116        out->append(url);
117    }
118
119    LOGV("base:'%s', url:'%s' => '%s'", baseURL, url, out->c_str());
120
121    return true;
122}
123
124status_t M3UParser::parse(const void *_data, size_t size) {
125    int32_t lineNo = 0;
126
127    sp<AMessage> itemMeta;
128
129    const char *data = (const char *)_data;
130    size_t offset = 0;
131    while (offset < size) {
132        size_t offsetLF = offset;
133        while (offsetLF < size && data[offsetLF] != '\n') {
134            ++offsetLF;
135        }
136        if (offsetLF >= size) {
137            break;
138        }
139
140        AString line;
141        if (offsetLF > offset && data[offsetLF - 1] == '\r') {
142            line.setTo(&data[offset], offsetLF - offset - 1);
143        } else {
144            line.setTo(&data[offset], offsetLF - offset);
145        }
146
147        // LOGI("#%s#", line.c_str());
148
149        if (line.empty()) {
150            offset = offsetLF + 1;
151            continue;
152        }
153
154        if (lineNo == 0 && line == "#EXTM3U") {
155            mIsExtM3U = true;
156        }
157
158        if (mIsExtM3U) {
159            status_t err = OK;
160
161            if (line.startsWith("#EXT-X-TARGETDURATION")) {
162                if (mIsVariantPlaylist) {
163                    return ERROR_MALFORMED;
164                }
165                err = parseMetaData(line, &mMeta, "target-duration");
166            } else if (line.startsWith("#EXT-X-MEDIA-SEQUENCE")) {
167                if (mIsVariantPlaylist) {
168                    return ERROR_MALFORMED;
169                }
170                err = parseMetaData(line, &mMeta, "media-sequence");
171            } else if (line.startsWith("#EXT-X-KEY")) {
172                if (mIsVariantPlaylist) {
173                    return ERROR_MALFORMED;
174                }
175                err = parseCipherInfo(line, &itemMeta, mBaseURI);
176            } else if (line.startsWith("#EXT-X-ENDLIST")) {
177                mIsComplete = true;
178            } else if (line.startsWith("#EXTINF")) {
179                if (mIsVariantPlaylist) {
180                    return ERROR_MALFORMED;
181                }
182                err = parseMetaDataDuration(line, &itemMeta, "durationUs");
183            } else if (line.startsWith("#EXT-X-DISCONTINUITY")) {
184                if (mIsVariantPlaylist) {
185                    return ERROR_MALFORMED;
186                }
187                if (itemMeta == NULL) {
188                    itemMeta = new AMessage;
189                }
190                itemMeta->setInt32("discontinuity", true);
191            } else if (line.startsWith("#EXT-X-STREAM-INF")) {
192                if (mMeta != NULL) {
193                    return ERROR_MALFORMED;
194                }
195                mIsVariantPlaylist = true;
196                err = parseStreamInf(line, &itemMeta);
197            }
198
199            if (err != OK) {
200                return err;
201            }
202        }
203
204        if (!line.startsWith("#")) {
205            if (!mIsVariantPlaylist) {
206                int64_t durationUs;
207                if (itemMeta == NULL
208                        || !itemMeta->findInt64("durationUs", &durationUs)) {
209                    return ERROR_MALFORMED;
210                }
211            }
212
213            mItems.push();
214            Item *item = &mItems.editItemAt(mItems.size() - 1);
215
216            CHECK(MakeURL(mBaseURI.c_str(), line.c_str(), &item->mURI));
217
218            item->mMeta = itemMeta;
219
220            itemMeta.clear();
221        }
222
223        offset = offsetLF + 1;
224        ++lineNo;
225    }
226
227    return OK;
228}
229
230// static
231status_t M3UParser::parseMetaData(
232        const AString &line, sp<AMessage> *meta, const char *key) {
233    ssize_t colonPos = line.find(":");
234
235    if (colonPos < 0) {
236        return ERROR_MALFORMED;
237    }
238
239    int32_t x;
240    status_t err = ParseInt32(line.c_str() + colonPos + 1, &x);
241
242    if (err != OK) {
243        return err;
244    }
245
246    if (meta->get() == NULL) {
247        *meta = new AMessage;
248    }
249    (*meta)->setInt32(key, x);
250
251    return OK;
252}
253
254// static
255status_t M3UParser::parseMetaDataDuration(
256        const AString &line, sp<AMessage> *meta, const char *key) {
257    ssize_t colonPos = line.find(":");
258
259    if (colonPos < 0) {
260        return ERROR_MALFORMED;
261    }
262
263    double x;
264    status_t err = ParseDouble(line.c_str() + colonPos + 1, &x);
265
266    if (err != OK) {
267        return err;
268    }
269
270    if (meta->get() == NULL) {
271        *meta = new AMessage;
272    }
273    (*meta)->setInt64(key, (int64_t)x * 1E6);
274
275    return OK;
276}
277
278// static
279status_t M3UParser::parseStreamInf(
280        const AString &line, sp<AMessage> *meta) {
281    ssize_t colonPos = line.find(":");
282
283    if (colonPos < 0) {
284        return ERROR_MALFORMED;
285    }
286
287    size_t offset = colonPos + 1;
288
289    while (offset < line.size()) {
290        ssize_t end = line.find(",", offset);
291        if (end < 0) {
292            end = line.size();
293        }
294
295        AString attr(line, offset, end - offset);
296        attr.trim();
297
298        offset = end + 1;
299
300        ssize_t equalPos = attr.find("=");
301        if (equalPos < 0) {
302            continue;
303        }
304
305        AString key(attr, 0, equalPos);
306        key.trim();
307
308        AString val(attr, equalPos + 1, attr.size() - equalPos - 1);
309        val.trim();
310
311        LOGV("key=%s value=%s", key.c_str(), val.c_str());
312
313        if (!strcasecmp("bandwidth", key.c_str())) {
314            const char *s = val.c_str();
315            char *end;
316            unsigned long x = strtoul(s, &end, 10);
317
318            if (end == s || *end != '\0') {
319                // malformed
320                continue;
321            }
322
323            if (meta->get() == NULL) {
324                *meta = new AMessage;
325            }
326            (*meta)->setInt32("bandwidth", x);
327        }
328    }
329
330    return OK;
331}
332
333// Find the next occurence of the character "what" at or after "offset",
334// but ignore occurences between quotation marks.
335// Return the index of the occurrence or -1 if not found.
336static ssize_t FindNextUnquoted(
337        const AString &line, char what, size_t offset) {
338    CHECK_NE((int)what, (int)'"');
339
340    bool quoted = false;
341    while (offset < line.size()) {
342        char c = line.c_str()[offset];
343
344        if (c == '"') {
345            quoted = !quoted;
346        } else if (c == what && !quoted) {
347            return offset;
348        }
349
350        ++offset;
351    }
352
353    return -1;
354}
355
356// static
357status_t M3UParser::parseCipherInfo(
358        const AString &line, sp<AMessage> *meta, const AString &baseURI) {
359    ssize_t colonPos = line.find(":");
360
361    if (colonPos < 0) {
362        return ERROR_MALFORMED;
363    }
364
365    size_t offset = colonPos + 1;
366
367    while (offset < line.size()) {
368        ssize_t end = FindNextUnquoted(line, ',', offset);
369        if (end < 0) {
370            end = line.size();
371        }
372
373        AString attr(line, offset, end - offset);
374        attr.trim();
375
376        offset = end + 1;
377
378        ssize_t equalPos = attr.find("=");
379        if (equalPos < 0) {
380            continue;
381        }
382
383        AString key(attr, 0, equalPos);
384        key.trim();
385
386        AString val(attr, equalPos + 1, attr.size() - equalPos - 1);
387        val.trim();
388
389        LOGV("key=%s value=%s", key.c_str(), val.c_str());
390
391        key.tolower();
392
393        if (key == "method" || key == "uri" || key == "iv") {
394            if (meta->get() == NULL) {
395                *meta = new AMessage;
396            }
397
398            if (key == "uri") {
399                if (val.size() >= 2
400                        && val.c_str()[0] == '"'
401                        && val.c_str()[val.size() - 1] == '"') {
402                    // Remove surrounding quotes.
403                    AString tmp(val, 1, val.size() - 2);
404                    val = tmp;
405                }
406
407                AString absURI;
408                if (MakeURL(baseURI.c_str(), val.c_str(), &absURI)) {
409                    val = absURI;
410                } else {
411                    LOGE("failed to make absolute url for '%s'.",
412                         val.c_str());
413                }
414            }
415
416            key.insert(AString("cipher-"), 0);
417
418            (*meta)->setString(key.c_str(), val.c_str(), val.size());
419        }
420    }
421
422    return OK;
423}
424
425// static
426status_t M3UParser::ParseInt32(const char *s, int32_t *x) {
427    char *end;
428    long lval = strtol(s, &end, 10);
429
430    if (end == s || (*end != '\0' && *end != ',')) {
431        return ERROR_MALFORMED;
432    }
433
434    *x = (int32_t)lval;
435
436    return OK;
437}
438
439// static
440status_t M3UParser::ParseDouble(const char *s, double *x) {
441    char *end;
442    double dval = strtod(s, &end);
443
444    if (end == s || (*end != '\0' && *end != ',')) {
445        return ERROR_MALFORMED;
446    }
447
448    *x = dval;
449
450    return OK;
451}
452
453}  // namespace android
454