M3UParser.cpp revision 0a73d81f213fb2d6f2fdd59c4dda047bf453bb1c
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 = parseMetaData(line, &itemMeta, "duration");
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                int32_t durationSecs;
207                if (itemMeta == NULL
208                        || !itemMeta->findInt32("duration", &durationSecs)) {
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::parseStreamInf(
256        const AString &line, sp<AMessage> *meta) {
257    ssize_t colonPos = line.find(":");
258
259    if (colonPos < 0) {
260        return ERROR_MALFORMED;
261    }
262
263    size_t offset = colonPos + 1;
264
265    while (offset < line.size()) {
266        ssize_t end = line.find(",", offset);
267        if (end < 0) {
268            end = line.size();
269        }
270
271        AString attr(line, offset, end - offset);
272        attr.trim();
273
274        offset = end + 1;
275
276        ssize_t equalPos = attr.find("=");
277        if (equalPos < 0) {
278            continue;
279        }
280
281        AString key(attr, 0, equalPos);
282        key.trim();
283
284        AString val(attr, equalPos + 1, attr.size() - equalPos - 1);
285        val.trim();
286
287        LOGV("key=%s value=%s", key.c_str(), val.c_str());
288
289        if (!strcasecmp("bandwidth", key.c_str())) {
290            const char *s = val.c_str();
291            char *end;
292            unsigned long x = strtoul(s, &end, 10);
293
294            if (end == s || *end != '\0') {
295                // malformed
296                continue;
297            }
298
299            if (meta->get() == NULL) {
300                *meta = new AMessage;
301            }
302            (*meta)->setInt32("bandwidth", x);
303        }
304    }
305
306    return OK;
307}
308
309// Find the next occurence of the character "what" at or after "offset",
310// but ignore occurences between quotation marks.
311// Return the index of the occurrence or -1 if not found.
312static ssize_t FindNextUnquoted(
313        const AString &line, char what, size_t offset) {
314    CHECK_NE((int)what, (int)'"');
315
316    bool quoted = false;
317    while (offset < line.size()) {
318        char c = line.c_str()[offset];
319
320        if (c == '"') {
321            quoted = !quoted;
322        } else if (c == what && !quoted) {
323            return offset;
324        }
325
326        ++offset;
327    }
328
329    return -1;
330}
331
332// static
333status_t M3UParser::parseCipherInfo(
334        const AString &line, sp<AMessage> *meta, const AString &baseURI) {
335    ssize_t colonPos = line.find(":");
336
337    if (colonPos < 0) {
338        return ERROR_MALFORMED;
339    }
340
341    size_t offset = colonPos + 1;
342
343    while (offset < line.size()) {
344        ssize_t end = FindNextUnquoted(line, ',', offset);
345        if (end < 0) {
346            end = line.size();
347        }
348
349        AString attr(line, offset, end - offset);
350        attr.trim();
351
352        offset = end + 1;
353
354        ssize_t equalPos = attr.find("=");
355        if (equalPos < 0) {
356            continue;
357        }
358
359        AString key(attr, 0, equalPos);
360        key.trim();
361
362        AString val(attr, equalPos + 1, attr.size() - equalPos - 1);
363        val.trim();
364
365        LOGV("key=%s value=%s", key.c_str(), val.c_str());
366
367        key.tolower();
368
369        if (key == "method" || key == "uri" || key == "iv") {
370            if (meta->get() == NULL) {
371                *meta = new AMessage;
372            }
373
374            if (key == "uri") {
375                if (val.size() >= 2
376                        && val.c_str()[0] == '"'
377                        && val.c_str()[val.size() - 1] == '"') {
378                    // Remove surrounding quotes.
379                    AString tmp(val, 1, val.size() - 2);
380                    val = tmp;
381                }
382
383                AString absURI;
384                if (MakeURL(baseURI.c_str(), val.c_str(), &absURI)) {
385                    val = absURI;
386                } else {
387                    LOGE("failed to make absolute url for '%s'.",
388                         val.c_str());
389                }
390            }
391
392            key.insert(AString("cipher-"), 0);
393
394            (*meta)->setString(key.c_str(), val.c_str(), val.size());
395        }
396    }
397
398    return OK;
399}
400
401// static
402status_t M3UParser::ParseInt32(const char *s, int32_t *x) {
403    char *end;
404    long lval = strtol(s, &end, 10);
405
406    if (end == s || (*end != '\0' && *end != ',')) {
407        return ERROR_MALFORMED;
408    }
409
410    *x = (int32_t)lval;
411
412    return OK;
413}
414
415}  // namespace android
416