M3UParser.cpp revision f4431278a9613f55ecd944ab2e3eb615b372f269
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 "M3UParser.h"
22#include <binder/Parcel.h>
23#include <cutils/properties.h>
24#include <media/stagefright/foundation/ADebug.h>
25#include <media/stagefright/foundation/AMessage.h>
26#include <media/stagefright/MediaErrors.h>
27#include <media/stagefright/Utils.h>
28#include <media/mediaplayer.h>
29
30namespace android {
31
32struct M3UParser::MediaGroup : public RefBase {
33    enum Type {
34        TYPE_AUDIO,
35        TYPE_VIDEO,
36        TYPE_SUBS,
37    };
38
39    enum FlagBits {
40        FLAG_AUTOSELECT         = 1,
41        FLAG_DEFAULT            = 2,
42        FLAG_FORCED             = 4,
43        FLAG_HAS_LANGUAGE       = 8,
44        FLAG_HAS_URI            = 16,
45    };
46
47    MediaGroup(Type type);
48
49    Type type() const;
50
51    status_t addMedia(
52            const char *name,
53            const char *uri,
54            const char *language,
55            uint32_t flags);
56
57    bool getActiveURI(AString *uri) const;
58
59    void pickRandomMediaItems();
60    status_t selectTrack(size_t index, bool select);
61    void getTrackInfo(Parcel* reply) const;
62    size_t countTracks() const;
63
64protected:
65    virtual ~MediaGroup();
66
67private:
68    struct Media {
69        AString mName;
70        AString mURI;
71        AString mLanguage;
72        uint32_t mFlags;
73    };
74
75    Type mType;
76    Vector<Media> mMediaItems;
77
78    ssize_t mSelectedIndex;
79
80    DISALLOW_EVIL_CONSTRUCTORS(MediaGroup);
81};
82
83M3UParser::MediaGroup::MediaGroup(Type type)
84    : mType(type),
85      mSelectedIndex(-1) {
86}
87
88M3UParser::MediaGroup::~MediaGroup() {
89}
90
91M3UParser::MediaGroup::Type M3UParser::MediaGroup::type() const {
92    return mType;
93}
94
95status_t M3UParser::MediaGroup::addMedia(
96        const char *name,
97        const char *uri,
98        const char *language,
99        uint32_t flags) {
100    mMediaItems.push();
101    Media &item = mMediaItems.editItemAt(mMediaItems.size() - 1);
102
103    item.mName = name;
104
105    if (uri) {
106        item.mURI = uri;
107    }
108
109    if (language) {
110        item.mLanguage = language;
111    }
112
113    item.mFlags = flags;
114
115    return OK;
116}
117
118void M3UParser::MediaGroup::pickRandomMediaItems() {
119#if 1
120    switch (mType) {
121        case TYPE_AUDIO:
122        {
123            char value[PROPERTY_VALUE_MAX];
124            if (property_get("media.httplive.audio-index", value, NULL)) {
125                char *end;
126                mSelectedIndex = strtoul(value, &end, 10);
127                CHECK(end > value && *end == '\0');
128
129                if (mSelectedIndex >= mMediaItems.size()) {
130                    mSelectedIndex = mMediaItems.size() - 1;
131                }
132            } else {
133                mSelectedIndex = 0;
134            }
135            break;
136        }
137
138        case TYPE_VIDEO:
139        {
140            mSelectedIndex = 0;
141            break;
142        }
143
144        case TYPE_SUBS:
145        {
146            mSelectedIndex = -1;
147            break;
148        }
149
150        default:
151            TRESPASS();
152    }
153#else
154    mSelectedIndex = (rand() * mMediaItems.size()) / RAND_MAX;
155#endif
156}
157
158status_t M3UParser::MediaGroup::selectTrack(size_t index, bool select) {
159    if (mType != TYPE_SUBS) {
160        ALOGE("only select subtitile tracks for now!");
161        return INVALID_OPERATION;
162    }
163
164    if (select) {
165        if (index >= mMediaItems.size()) {
166            ALOGE("track %d does not exist", index);
167            return INVALID_OPERATION;
168        }
169        if (mSelectedIndex == index) {
170            ALOGE("track %d already selected", index);
171            return BAD_VALUE;
172        }
173        ALOGV("selected track %d", index);
174        mSelectedIndex = index;
175    } else {
176        if (mSelectedIndex != index) {
177            ALOGE("track %d is not selected", index);
178            return BAD_VALUE;
179        }
180        ALOGV("unselected track %d", index);
181        mSelectedIndex = -1;
182    }
183
184    return OK;
185}
186
187void M3UParser::MediaGroup::getTrackInfo(Parcel* reply) const {
188    for (size_t i = 0; i < mMediaItems.size(); ++i) {
189        reply->writeInt32(2); // 2 fields
190
191        if (mType == TYPE_AUDIO) {
192            reply->writeInt32(MEDIA_TRACK_TYPE_AUDIO);
193        } else if (mType == TYPE_VIDEO) {
194            reply->writeInt32(MEDIA_TRACK_TYPE_VIDEO);
195        } else if (mType == TYPE_SUBS) {
196            reply->writeInt32(MEDIA_TRACK_TYPE_SUBTITLE);
197        } else {
198            reply->writeInt32(MEDIA_TRACK_TYPE_UNKNOWN);
199        }
200
201        const Media &item = mMediaItems.itemAt(i);
202        const char *lang = item.mLanguage.empty() ? "und" : item.mLanguage.c_str();
203        reply->writeString16(String16(lang));
204
205        if (mType == TYPE_SUBS) {
206            // TO-DO: pass in a MediaFormat instead
207            reply->writeInt32(!!(item.mFlags & MediaGroup::FLAG_AUTOSELECT));
208            reply->writeInt32(!!(item.mFlags & MediaGroup::FLAG_DEFAULT));
209            reply->writeInt32(!!(item.mFlags & MediaGroup::FLAG_FORCED));
210        }
211    }
212}
213
214size_t M3UParser::MediaGroup::countTracks() const {
215    return mMediaItems.size();
216}
217
218bool M3UParser::MediaGroup::getActiveURI(AString *uri) const {
219    for (size_t i = 0; i < mMediaItems.size(); ++i) {
220        if (mSelectedIndex >= 0 && i == (size_t)mSelectedIndex) {
221            const Media &item = mMediaItems.itemAt(i);
222
223            *uri = item.mURI;
224            return true;
225        }
226    }
227
228    return false;
229}
230
231////////////////////////////////////////////////////////////////////////////////
232
233M3UParser::M3UParser(
234        const char *baseURI, const void *data, size_t size)
235    : mInitCheck(NO_INIT),
236      mBaseURI(baseURI),
237      mIsExtM3U(false),
238      mIsVariantPlaylist(false),
239      mIsComplete(false),
240      mIsEvent(false),
241      mSelectedIndex(-1) {
242    mInitCheck = parse(data, size);
243}
244
245M3UParser::~M3UParser() {
246}
247
248status_t M3UParser::initCheck() const {
249    return mInitCheck;
250}
251
252bool M3UParser::isExtM3U() const {
253    return mIsExtM3U;
254}
255
256bool M3UParser::isVariantPlaylist() const {
257    return mIsVariantPlaylist;
258}
259
260bool M3UParser::isComplete() const {
261    return mIsComplete;
262}
263
264bool M3UParser::isEvent() const {
265    return mIsEvent;
266}
267
268sp<AMessage> M3UParser::meta() {
269    return mMeta;
270}
271
272size_t M3UParser::size() {
273    return mItems.size();
274}
275
276bool M3UParser::itemAt(size_t index, AString *uri, sp<AMessage> *meta) {
277    if (uri) {
278        uri->clear();
279    }
280
281    if (meta) {
282        *meta = NULL;
283    }
284
285    if (index >= mItems.size()) {
286        return false;
287    }
288
289    if (uri) {
290        *uri = mItems.itemAt(index).mURI;
291    }
292
293    if (meta) {
294        *meta = mItems.itemAt(index).mMeta;
295    }
296
297    return true;
298}
299
300void M3UParser::pickRandomMediaItems() {
301    for (size_t i = 0; i < mMediaGroups.size(); ++i) {
302        mMediaGroups.valueAt(i)->pickRandomMediaItems();
303    }
304}
305
306status_t M3UParser::selectTrack(size_t index, bool select) {
307    for (size_t i = 0, ii = index; i < mMediaGroups.size(); ++i) {
308        sp<MediaGroup> group = mMediaGroups.valueAt(i);
309        size_t tracks = group->countTracks();
310        if (ii < tracks) {
311            status_t err = group->selectTrack(ii, select);
312            if (err == OK) {
313                mSelectedIndex = select ? index : -1;
314            }
315            return err;
316        }
317        ii -= tracks;
318    }
319    return INVALID_OPERATION;
320}
321
322status_t M3UParser::getTrackInfo(Parcel* reply) const {
323    size_t trackCount = 0;
324    for (size_t i = 0; i < mMediaGroups.size(); ++i) {
325        trackCount += mMediaGroups.valueAt(i)->countTracks();
326    }
327    reply->writeInt32(trackCount);
328
329    for (size_t i = 0; i < mMediaGroups.size(); ++i) {
330        mMediaGroups.valueAt(i)->getTrackInfo(reply);
331    }
332    return OK;
333}
334
335ssize_t M3UParser::getSelectedIndex() const {
336    return mSelectedIndex;
337}
338
339bool M3UParser::getTypeURI(size_t index, const char *key, AString *uri) const {
340    if (!mIsVariantPlaylist) {
341        *uri = mBaseURI;
342
343        // Assume media without any more specific attribute contains
344        // audio and video, but no subtitles.
345        return !strcmp("audio", key) || !strcmp("video", key);
346    }
347
348    CHECK_LT(index, mItems.size());
349
350    sp<AMessage> meta = mItems.itemAt(index).mMeta;
351
352    AString groupID;
353    if (!meta->findString(key, &groupID)) {
354        *uri = mItems.itemAt(index).mURI;
355
356        AString codecs;
357        if (!meta->findString("codecs", &codecs)) {
358            // Assume media without any more specific attribute contains
359            // audio and video, but no subtitles.
360            return !strcmp("audio", key) || !strcmp("video", key);
361        } else {
362            // Split the comma separated list of codecs.
363            size_t offset = 0;
364            ssize_t commaPos = -1;
365            codecs.append(',');
366            while ((commaPos = codecs.find(",", offset)) >= 0) {
367                AString codec(codecs, offset, commaPos - offset);
368                // return true only if a codec of type `key` ("audio"/"video")
369                // is found.
370                if (codecIsType(codec, key)) {
371                    return true;
372                }
373                offset = commaPos + 1;
374            }
375            return false;
376        }
377    }
378
379    sp<MediaGroup> group = mMediaGroups.valueFor(groupID);
380    if (!group->getActiveURI(uri)) {
381        return false;
382    }
383
384    if ((*uri).empty()) {
385        *uri = mItems.itemAt(index).mURI;
386    }
387
388    return true;
389}
390
391static bool MakeURL(const char *baseURL, const char *url, AString *out) {
392    out->clear();
393
394    if (strncasecmp("http://", baseURL, 7)
395            && strncasecmp("https://", baseURL, 8)
396            && strncasecmp("file://", baseURL, 7)) {
397        // Base URL must be absolute
398        return false;
399    }
400
401    if (!strncasecmp("http://", url, 7) || !strncasecmp("https://", url, 8)) {
402        // "url" is already an absolute URL, ignore base URL.
403        out->setTo(url);
404
405        ALOGV("base:'%s', url:'%s' => '%s'", baseURL, url, out->c_str());
406
407        return true;
408    }
409
410    if (url[0] == '/') {
411        // URL is an absolute path.
412
413        char *protocolEnd = strstr(baseURL, "//") + 2;
414        char *pathStart = strchr(protocolEnd, '/');
415
416        if (pathStart != NULL) {
417            out->setTo(baseURL, pathStart - baseURL);
418        } else {
419            out->setTo(baseURL);
420        }
421
422        out->append(url);
423    } else {
424        // URL is a relative path
425
426        // Check for a possible query string
427        const char *qsPos = strchr(baseURL, '?');
428        size_t end;
429        if (qsPos != NULL) {
430            end = qsPos - baseURL;
431        } else {
432            end = strlen(baseURL);
433        }
434        // Check for the last slash before a potential query string
435        for (ssize_t pos = end - 1; pos >= 0; pos--) {
436            if (baseURL[pos] == '/') {
437                end = pos;
438                break;
439            }
440        }
441
442        // Check whether the found slash actually is part of the path
443        // and not part of the "http://".
444        if (end > 6) {
445            out->setTo(baseURL, end);
446        } else {
447            out->setTo(baseURL);
448        }
449
450        out->append("/");
451        out->append(url);
452    }
453
454    ALOGV("base:'%s', url:'%s' => '%s'", baseURL, url, out->c_str());
455
456    return true;
457}
458
459status_t M3UParser::parse(const void *_data, size_t size) {
460    int32_t lineNo = 0;
461
462    sp<AMessage> itemMeta;
463
464    const char *data = (const char *)_data;
465    size_t offset = 0;
466    uint64_t segmentRangeOffset = 0;
467    while (offset < size) {
468        size_t offsetLF = offset;
469        while (offsetLF < size && data[offsetLF] != '\n') {
470            ++offsetLF;
471        }
472
473        AString line;
474        if (offsetLF > offset && data[offsetLF - 1] == '\r') {
475            line.setTo(&data[offset], offsetLF - offset - 1);
476        } else {
477            line.setTo(&data[offset], offsetLF - offset);
478        }
479
480        // ALOGI("#%s#", line.c_str());
481
482        if (line.empty()) {
483            offset = offsetLF + 1;
484            continue;
485        }
486
487        if (lineNo == 0 && line == "#EXTM3U") {
488            mIsExtM3U = true;
489        }
490
491        if (mIsExtM3U) {
492            status_t err = OK;
493
494            if (line.startsWith("#EXT-X-TARGETDURATION")) {
495                if (mIsVariantPlaylist) {
496                    return ERROR_MALFORMED;
497                }
498                err = parseMetaData(line, &mMeta, "target-duration");
499            } else if (line.startsWith("#EXT-X-MEDIA-SEQUENCE")) {
500                if (mIsVariantPlaylist) {
501                    return ERROR_MALFORMED;
502                }
503                err = parseMetaData(line, &mMeta, "media-sequence");
504            } else if (line.startsWith("#EXT-X-KEY")) {
505                if (mIsVariantPlaylist) {
506                    return ERROR_MALFORMED;
507                }
508                err = parseCipherInfo(line, &itemMeta, mBaseURI);
509            } else if (line.startsWith("#EXT-X-ENDLIST")) {
510                mIsComplete = true;
511            } else if (line.startsWith("#EXT-X-PLAYLIST-TYPE:EVENT")) {
512                mIsEvent = true;
513            } else if (line.startsWith("#EXTINF")) {
514                if (mIsVariantPlaylist) {
515                    return ERROR_MALFORMED;
516                }
517                err = parseMetaDataDuration(line, &itemMeta, "durationUs");
518            } else if (line.startsWith("#EXT-X-DISCONTINUITY")) {
519                if (mIsVariantPlaylist) {
520                    return ERROR_MALFORMED;
521                }
522                if (itemMeta == NULL) {
523                    itemMeta = new AMessage;
524                }
525                itemMeta->setInt32("discontinuity", true);
526            } else if (line.startsWith("#EXT-X-STREAM-INF")) {
527                if (mMeta != NULL) {
528                    return ERROR_MALFORMED;
529                }
530                mIsVariantPlaylist = true;
531                err = parseStreamInf(line, &itemMeta);
532            } else if (line.startsWith("#EXT-X-BYTERANGE")) {
533                if (mIsVariantPlaylist) {
534                    return ERROR_MALFORMED;
535                }
536
537                uint64_t length, offset;
538                err = parseByteRange(line, segmentRangeOffset, &length, &offset);
539
540                if (err == OK) {
541                    if (itemMeta == NULL) {
542                        itemMeta = new AMessage;
543                    }
544
545                    itemMeta->setInt64("range-offset", offset);
546                    itemMeta->setInt64("range-length", length);
547
548                    segmentRangeOffset = offset + length;
549                }
550            } else if (line.startsWith("#EXT-X-MEDIA")) {
551                err = parseMedia(line);
552            }
553
554            if (err != OK) {
555                return err;
556            }
557        }
558
559        if (!line.startsWith("#")) {
560            if (!mIsVariantPlaylist) {
561                int64_t durationUs;
562                if (itemMeta == NULL
563                        || !itemMeta->findInt64("durationUs", &durationUs)) {
564                    return ERROR_MALFORMED;
565                }
566            }
567
568            mItems.push();
569            Item *item = &mItems.editItemAt(mItems.size() - 1);
570
571            CHECK(MakeURL(mBaseURI.c_str(), line.c_str(), &item->mURI));
572
573            item->mMeta = itemMeta;
574
575            itemMeta.clear();
576        }
577
578        offset = offsetLF + 1;
579        ++lineNo;
580    }
581
582    return OK;
583}
584
585// static
586status_t M3UParser::parseMetaData(
587        const AString &line, sp<AMessage> *meta, const char *key) {
588    ssize_t colonPos = line.find(":");
589
590    if (colonPos < 0) {
591        return ERROR_MALFORMED;
592    }
593
594    int32_t x;
595    status_t err = ParseInt32(line.c_str() + colonPos + 1, &x);
596
597    if (err != OK) {
598        return err;
599    }
600
601    if (meta->get() == NULL) {
602        *meta = new AMessage;
603    }
604    (*meta)->setInt32(key, x);
605
606    return OK;
607}
608
609// static
610status_t M3UParser::parseMetaDataDuration(
611        const AString &line, sp<AMessage> *meta, const char *key) {
612    ssize_t colonPos = line.find(":");
613
614    if (colonPos < 0) {
615        return ERROR_MALFORMED;
616    }
617
618    double x;
619    status_t err = ParseDouble(line.c_str() + colonPos + 1, &x);
620
621    if (err != OK) {
622        return err;
623    }
624
625    if (meta->get() == NULL) {
626        *meta = new AMessage;
627    }
628    (*meta)->setInt64(key, (int64_t)(x * 1E6));
629
630    return OK;
631}
632
633// Find the next occurence of the character "what" at or after "offset",
634// but ignore occurences between quotation marks.
635// Return the index of the occurrence or -1 if not found.
636static ssize_t FindNextUnquoted(
637        const AString &line, char what, size_t offset) {
638    CHECK_NE((int)what, (int)'"');
639
640    bool quoted = false;
641    while (offset < line.size()) {
642        char c = line.c_str()[offset];
643
644        if (c == '"') {
645            quoted = !quoted;
646        } else if (c == what && !quoted) {
647            return offset;
648        }
649
650        ++offset;
651    }
652
653    return -1;
654}
655
656status_t M3UParser::parseStreamInf(
657        const AString &line, sp<AMessage> *meta) const {
658    ssize_t colonPos = line.find(":");
659
660    if (colonPos < 0) {
661        return ERROR_MALFORMED;
662    }
663
664    size_t offset = colonPos + 1;
665
666    while (offset < line.size()) {
667        ssize_t end = FindNextUnquoted(line, ',', offset);
668        if (end < 0) {
669            end = line.size();
670        }
671
672        AString attr(line, offset, end - offset);
673        attr.trim();
674
675        offset = end + 1;
676
677        ssize_t equalPos = attr.find("=");
678        if (equalPos < 0) {
679            continue;
680        }
681
682        AString key(attr, 0, equalPos);
683        key.trim();
684
685        AString val(attr, equalPos + 1, attr.size() - equalPos - 1);
686        val.trim();
687
688        ALOGV("key=%s value=%s", key.c_str(), val.c_str());
689
690        if (!strcasecmp("bandwidth", key.c_str())) {
691            const char *s = val.c_str();
692            char *end;
693            unsigned long x = strtoul(s, &end, 10);
694
695            if (end == s || *end != '\0') {
696                // malformed
697                continue;
698            }
699
700            if (meta->get() == NULL) {
701                *meta = new AMessage;
702            }
703            (*meta)->setInt32("bandwidth", x);
704        } else if (!strcasecmp("codecs", key.c_str())) {
705            if (!isQuotedString(val)) {
706                ALOGE("Expected quoted string for %s attribute, "
707                      "got '%s' instead.",
708                      key.c_str(), val.c_str());;
709
710                return ERROR_MALFORMED;
711            }
712
713            key.tolower();
714            const AString &codecs = unquoteString(val);
715            (*meta)->setString(key.c_str(), codecs.c_str());
716        } else if (!strcasecmp("audio", key.c_str())
717                || !strcasecmp("video", key.c_str())
718                || !strcasecmp("subtitles", key.c_str())) {
719            if (!isQuotedString(val)) {
720                ALOGE("Expected quoted string for %s attribute, "
721                      "got '%s' instead.",
722                      key.c_str(), val.c_str());
723
724                return ERROR_MALFORMED;
725            }
726
727            const AString &groupID = unquoteString(val);
728            ssize_t groupIndex = mMediaGroups.indexOfKey(groupID);
729
730            if (groupIndex < 0) {
731                ALOGE("Undefined media group '%s' referenced in stream info.",
732                      groupID.c_str());
733
734                return ERROR_MALFORMED;
735            }
736
737            key.tolower();
738            (*meta)->setString(key.c_str(), groupID.c_str());
739        }
740    }
741
742    return OK;
743}
744
745// static
746status_t M3UParser::parseCipherInfo(
747        const AString &line, sp<AMessage> *meta, const AString &baseURI) {
748    ssize_t colonPos = line.find(":");
749
750    if (colonPos < 0) {
751        return ERROR_MALFORMED;
752    }
753
754    size_t offset = colonPos + 1;
755
756    while (offset < line.size()) {
757        ssize_t end = FindNextUnquoted(line, ',', offset);
758        if (end < 0) {
759            end = line.size();
760        }
761
762        AString attr(line, offset, end - offset);
763        attr.trim();
764
765        offset = end + 1;
766
767        ssize_t equalPos = attr.find("=");
768        if (equalPos < 0) {
769            continue;
770        }
771
772        AString key(attr, 0, equalPos);
773        key.trim();
774
775        AString val(attr, equalPos + 1, attr.size() - equalPos - 1);
776        val.trim();
777
778        ALOGV("key=%s value=%s", key.c_str(), val.c_str());
779
780        key.tolower();
781
782        if (key == "method" || key == "uri" || key == "iv") {
783            if (meta->get() == NULL) {
784                *meta = new AMessage;
785            }
786
787            if (key == "uri") {
788                if (val.size() >= 2
789                        && val.c_str()[0] == '"'
790                        && val.c_str()[val.size() - 1] == '"') {
791                    // Remove surrounding quotes.
792                    AString tmp(val, 1, val.size() - 2);
793                    val = tmp;
794                }
795
796                AString absURI;
797                if (MakeURL(baseURI.c_str(), val.c_str(), &absURI)) {
798                    val = absURI;
799                } else {
800                    ALOGE("failed to make absolute url for <URL suppressed>.");
801                }
802            }
803
804            key.insert(AString("cipher-"), 0);
805
806            (*meta)->setString(key.c_str(), val.c_str(), val.size());
807        }
808    }
809
810    return OK;
811}
812
813// static
814status_t M3UParser::parseByteRange(
815        const AString &line, uint64_t curOffset,
816        uint64_t *length, uint64_t *offset) {
817    ssize_t colonPos = line.find(":");
818
819    if (colonPos < 0) {
820        return ERROR_MALFORMED;
821    }
822
823    ssize_t atPos = line.find("@", colonPos + 1);
824
825    AString lenStr;
826    if (atPos < 0) {
827        lenStr = AString(line, colonPos + 1, line.size() - colonPos - 1);
828    } else {
829        lenStr = AString(line, colonPos + 1, atPos - colonPos - 1);
830    }
831
832    lenStr.trim();
833
834    const char *s = lenStr.c_str();
835    char *end;
836    *length = strtoull(s, &end, 10);
837
838    if (s == end || *end != '\0') {
839        return ERROR_MALFORMED;
840    }
841
842    if (atPos >= 0) {
843        AString offStr = AString(line, atPos + 1, line.size() - atPos - 1);
844        offStr.trim();
845
846        const char *s = offStr.c_str();
847        *offset = strtoull(s, &end, 10);
848
849        if (s == end || *end != '\0') {
850            return ERROR_MALFORMED;
851        }
852    } else {
853        *offset = curOffset;
854    }
855
856    return OK;
857}
858
859status_t M3UParser::parseMedia(const AString &line) {
860    ssize_t colonPos = line.find(":");
861
862    if (colonPos < 0) {
863        return ERROR_MALFORMED;
864    }
865
866    bool haveGroupType = false;
867    MediaGroup::Type groupType = MediaGroup::TYPE_AUDIO;
868
869    bool haveGroupID = false;
870    AString groupID;
871
872    bool haveGroupLanguage = false;
873    AString groupLanguage;
874
875    bool haveGroupName = false;
876    AString groupName;
877
878    bool haveGroupAutoselect = false;
879    bool groupAutoselect = false;
880
881    bool haveGroupDefault = false;
882    bool groupDefault = false;
883
884    bool haveGroupForced = false;
885    bool groupForced = false;
886
887    bool haveGroupURI = false;
888    AString groupURI;
889
890    size_t offset = colonPos + 1;
891
892    while (offset < line.size()) {
893        ssize_t end = FindNextUnquoted(line, ',', offset);
894        if (end < 0) {
895            end = line.size();
896        }
897
898        AString attr(line, offset, end - offset);
899        attr.trim();
900
901        offset = end + 1;
902
903        ssize_t equalPos = attr.find("=");
904        if (equalPos < 0) {
905            continue;
906        }
907
908        AString key(attr, 0, equalPos);
909        key.trim();
910
911        AString val(attr, equalPos + 1, attr.size() - equalPos - 1);
912        val.trim();
913
914        ALOGV("key=%s value=%s", key.c_str(), val.c_str());
915
916        if (!strcasecmp("type", key.c_str())) {
917            if (!strcasecmp("subtitles", val.c_str())) {
918                groupType = MediaGroup::TYPE_SUBS;
919            } else if (!strcasecmp("audio", val.c_str())) {
920                groupType = MediaGroup::TYPE_AUDIO;
921            } else if (!strcasecmp("video", val.c_str())) {
922                groupType = MediaGroup::TYPE_VIDEO;
923            } else {
924                ALOGE("Invalid media group type '%s'", val.c_str());
925                return ERROR_MALFORMED;
926            }
927
928            haveGroupType = true;
929        } else if (!strcasecmp("group-id", key.c_str())) {
930            if (val.size() < 2
931                    || val.c_str()[0] != '"'
932                    || val.c_str()[val.size() - 1] != '"') {
933                ALOGE("Expected quoted string for GROUP-ID, got '%s' instead.",
934                      val.c_str());
935
936                return ERROR_MALFORMED;
937            }
938
939            groupID.setTo(val, 1, val.size() - 2);
940            haveGroupID = true;
941        } else if (!strcasecmp("language", key.c_str())) {
942            if (val.size() < 2
943                    || val.c_str()[0] != '"'
944                    || val.c_str()[val.size() - 1] != '"') {
945                ALOGE("Expected quoted string for LANGUAGE, got '%s' instead.",
946                      val.c_str());
947
948                return ERROR_MALFORMED;
949            }
950
951            groupLanguage.setTo(val, 1, val.size() - 2);
952            haveGroupLanguage = true;
953        } else if (!strcasecmp("name", key.c_str())) {
954            if (val.size() < 2
955                    || val.c_str()[0] != '"'
956                    || val.c_str()[val.size() - 1] != '"') {
957                ALOGE("Expected quoted string for NAME, got '%s' instead.",
958                      val.c_str());
959
960                return ERROR_MALFORMED;
961            }
962
963            groupName.setTo(val, 1, val.size() - 2);
964            haveGroupName = true;
965        } else if (!strcasecmp("autoselect", key.c_str())) {
966            groupAutoselect = false;
967            if (!strcasecmp("YES", val.c_str())) {
968                groupAutoselect = true;
969            } else if (!strcasecmp("NO", val.c_str())) {
970                groupAutoselect = false;
971            } else {
972                ALOGE("Expected YES or NO for AUTOSELECT attribute, "
973                      "got '%s' instead.",
974                      val.c_str());
975
976                return ERROR_MALFORMED;
977            }
978
979            haveGroupAutoselect = true;
980        } else if (!strcasecmp("default", key.c_str())) {
981            groupDefault = false;
982            if (!strcasecmp("YES", val.c_str())) {
983                groupDefault = true;
984            } else if (!strcasecmp("NO", val.c_str())) {
985                groupDefault = false;
986            } else {
987                ALOGE("Expected YES or NO for DEFAULT attribute, "
988                      "got '%s' instead.",
989                      val.c_str());
990
991                return ERROR_MALFORMED;
992            }
993
994            haveGroupDefault = true;
995        } else if (!strcasecmp("forced", key.c_str())) {
996            groupForced = false;
997            if (!strcasecmp("YES", val.c_str())) {
998                groupForced = true;
999            } else if (!strcasecmp("NO", val.c_str())) {
1000                groupForced = false;
1001            } else {
1002                ALOGE("Expected YES or NO for FORCED attribute, "
1003                      "got '%s' instead.",
1004                      val.c_str());
1005
1006                return ERROR_MALFORMED;
1007            }
1008
1009            haveGroupForced = true;
1010        } else if (!strcasecmp("uri", key.c_str())) {
1011            if (val.size() < 2
1012                    || val.c_str()[0] != '"'
1013                    || val.c_str()[val.size() - 1] != '"') {
1014                ALOGE("Expected quoted string for URI, got '%s' instead.",
1015                      val.c_str());
1016
1017                return ERROR_MALFORMED;
1018            }
1019
1020            AString tmp(val, 1, val.size() - 2);
1021
1022            if (!MakeURL(mBaseURI.c_str(), tmp.c_str(), &groupURI)) {
1023                ALOGI("Failed to make absolute URI from '%s'.", tmp.c_str());
1024            }
1025
1026            haveGroupURI = true;
1027        }
1028    }
1029
1030    if (!haveGroupType || !haveGroupID || !haveGroupName) {
1031        ALOGE("Incomplete EXT-X-MEDIA element.");
1032        return ERROR_MALFORMED;
1033    }
1034
1035    uint32_t flags = 0;
1036    if (haveGroupAutoselect && groupAutoselect) {
1037        flags |= MediaGroup::FLAG_AUTOSELECT;
1038    }
1039    if (haveGroupDefault && groupDefault) {
1040        flags |= MediaGroup::FLAG_DEFAULT;
1041    }
1042    if (haveGroupForced) {
1043        if (groupType != MediaGroup::TYPE_SUBS) {
1044            ALOGE("The FORCED attribute MUST not be present on anything "
1045                  "but SUBS media.");
1046
1047            return ERROR_MALFORMED;
1048        }
1049
1050        if (groupForced) {
1051            flags |= MediaGroup::FLAG_FORCED;
1052        }
1053    }
1054    if (haveGroupLanguage) {
1055        flags |= MediaGroup::FLAG_HAS_LANGUAGE;
1056    }
1057    if (haveGroupURI) {
1058        flags |= MediaGroup::FLAG_HAS_URI;
1059    }
1060
1061    ssize_t groupIndex = mMediaGroups.indexOfKey(groupID);
1062    sp<MediaGroup> group;
1063
1064    if (groupIndex < 0) {
1065        group = new MediaGroup(groupType);
1066        mMediaGroups.add(groupID, group);
1067    } else {
1068        group = mMediaGroups.valueAt(groupIndex);
1069
1070        if (group->type() != groupType) {
1071            ALOGE("Attempt to put media item under group of different type "
1072                  "(groupType = %d, item type = %d",
1073                  group->type(),
1074                  groupType);
1075
1076            return ERROR_MALFORMED;
1077        }
1078    }
1079
1080    return group->addMedia(
1081            groupName.c_str(),
1082            haveGroupURI ? groupURI.c_str() : NULL,
1083            haveGroupLanguage ? groupLanguage.c_str() : NULL,
1084            flags);
1085}
1086
1087// static
1088status_t M3UParser::ParseInt32(const char *s, int32_t *x) {
1089    char *end;
1090    long lval = strtol(s, &end, 10);
1091
1092    if (end == s || (*end != '\0' && *end != ',')) {
1093        return ERROR_MALFORMED;
1094    }
1095
1096    *x = (int32_t)lval;
1097
1098    return OK;
1099}
1100
1101// static
1102status_t M3UParser::ParseDouble(const char *s, double *x) {
1103    char *end;
1104    double dval = strtod(s, &end);
1105
1106    if (end == s || (*end != '\0' && *end != ',')) {
1107        return ERROR_MALFORMED;
1108    }
1109
1110    *x = dval;
1111
1112    return OK;
1113}
1114
1115// static
1116bool M3UParser::isQuotedString(const AString &str) {
1117    if (str.size() < 2
1118            || str.c_str()[0] != '"'
1119            || str.c_str()[str.size() - 1] != '"') {
1120        return false;
1121    }
1122    return true;
1123}
1124
1125// static
1126AString M3UParser::unquoteString(const AString &str) {
1127     if (!isQuotedString(str)) {
1128         return str;
1129     }
1130     return AString(str, 1, str.size() - 2);
1131}
1132
1133// static
1134bool M3UParser::codecIsType(const AString &codec, const char *type) {
1135    if (codec.size() < 4) {
1136        return false;
1137    }
1138    const char *c = codec.c_str();
1139    switch (FOURCC(c[0], c[1], c[2], c[3])) {
1140        // List extracted from http://www.mp4ra.org/codecs.html
1141        case 'ac-3':
1142        case 'alac':
1143        case 'dra1':
1144        case 'dtsc':
1145        case 'dtse':
1146        case 'dtsh':
1147        case 'dtsl':
1148        case 'ec-3':
1149        case 'enca':
1150        case 'g719':
1151        case 'g726':
1152        case 'm4ae':
1153        case 'mlpa':
1154        case 'mp4a':
1155        case 'raw ':
1156        case 'samr':
1157        case 'sawb':
1158        case 'sawp':
1159        case 'sevc':
1160        case 'sqcp':
1161        case 'ssmv':
1162        case 'twos':
1163        case 'agsm':
1164        case 'alaw':
1165        case 'dvi ':
1166        case 'fl32':
1167        case 'fl64':
1168        case 'ima4':
1169        case 'in24':
1170        case 'in32':
1171        case 'lpcm':
1172        case 'Qclp':
1173        case 'QDM2':
1174        case 'QDMC':
1175        case 'ulaw':
1176        case 'vdva':
1177            return !strcmp("audio", type);
1178
1179        case 'avc1':
1180        case 'avc2':
1181        case 'avcp':
1182        case 'drac':
1183        case 'encv':
1184        case 'mjp2':
1185        case 'mp4v':
1186        case 'mvc1':
1187        case 'mvc2':
1188        case 'resv':
1189        case 's263':
1190        case 'svc1':
1191        case 'vc-1':
1192        case 'CFHD':
1193        case 'civd':
1194        case 'DV10':
1195        case 'dvh5':
1196        case 'dvh6':
1197        case 'dvhp':
1198        case 'DVOO':
1199        case 'DVOR':
1200        case 'DVTV':
1201        case 'DVVT':
1202        case 'flic':
1203        case 'gif ':
1204        case 'h261':
1205        case 'h263':
1206        case 'HD10':
1207        case 'jpeg':
1208        case 'M105':
1209        case 'mjpa':
1210        case 'mjpb':
1211        case 'png ':
1212        case 'PNTG':
1213        case 'rle ':
1214        case 'rpza':
1215        case 'Shr0':
1216        case 'Shr1':
1217        case 'Shr2':
1218        case 'Shr3':
1219        case 'Shr4':
1220        case 'SVQ1':
1221        case 'SVQ3':
1222        case 'tga ':
1223        case 'tiff':
1224        case 'WRLE':
1225            return !strcmp("video", type);
1226
1227        default:
1228            return false;
1229    }
1230}
1231
1232}  // namespace android
1233