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 <media/mediaplayer.h> 25 #include <utils/Vector.h> 26 27 namespace android { 28 29 struct M3UParser : public RefBase { 30 M3UParser(const char *baseURI, const void *data, size_t size); 31 32 status_t initCheck() const; 33 34 bool isExtM3U() const; 35 bool isVariantPlaylist() const; 36 bool isComplete() const; 37 bool isEvent() const; 38 size_t getDiscontinuitySeq() const; 39 int64_t getTargetDuration() const; 40 int32_t getFirstSeqNumber() const; 41 void getSeqNumberRange(int32_t *firstSeq, int32_t *lastSeq) const; 42 43 sp<AMessage> meta(); 44 45 size_t size(); 46 bool itemAt(size_t index, AString *uri, sp<AMessage> *meta = NULL); 47 48 void pickRandomMediaItems(); 49 status_t selectTrack(size_t index, bool select); 50 size_t getTrackCount() const; 51 sp<AMessage> getTrackInfo(size_t index) const; 52 ssize_t getSelectedIndex() const; 53 ssize_t getSelectedTrack(media_track_type /* type */) const; 54 55 bool getTypeURI(size_t index, const char *key, AString *uri) const; 56 bool hasType(size_t index, const char *key) const; 57 58 AString getFullCipherUri(const AString &partial); 59 60 protected: 61 virtual ~M3UParser(); 62 63 private: 64 struct MediaGroup; 65 66 struct Item { 67 AString mURI; 68 sp<AMessage> mMeta; 69 AString makeURL(const char *baseURL) const; 70 }; 71 72 status_t mInitCheck; 73 74 AString mBaseURI; 75 bool mIsExtM3U; 76 bool mIsVariantPlaylist; 77 bool mIsComplete; 78 bool mIsEvent; 79 int32_t mFirstSeqNumber; 80 int32_t mLastSeqNumber; 81 int64_t mTargetDurationUs; 82 size_t mDiscontinuitySeq; 83 int32_t mDiscontinuityCount; 84 85 sp<AMessage> mMeta; 86 Vector<Item> mItems; 87 ssize_t mSelectedIndex; 88 89 // Media groups keyed by group ID. 90 KeyedVector<AString, sp<MediaGroup> > mMediaGroups; 91 92 status_t parse(const void *data, size_t size); 93 94 static status_t parseMetaData( 95 const AString &line, sp<AMessage> *meta, const char *key); 96 97 static status_t parseMetaDataDuration( 98 const AString &line, sp<AMessage> *meta, const char *key); 99 100 status_t parseStreamInf( 101 const AString &line, sp<AMessage> *meta) const; 102 103 static status_t parseCipherInfo( 104 const AString &line, sp<AMessage> *meta); 105 106 static status_t parseByteRange( 107 const AString &line, uint64_t curOffset, 108 uint64_t *length, uint64_t *offset); 109 110 status_t parseMedia(const AString &line); 111 112 static status_t parseDiscontinuitySequence(const AString &line, size_t *seq); 113 114 static status_t ParseInt32(const char *s, int32_t *x); 115 static status_t ParseDouble(const char *s, double *x); 116 117 static bool isQuotedString(const AString &str); 118 static AString unquoteString(const AString &str); 119 static bool codecIsType(const AString &codec, const char *type); 120 121 DISALLOW_EVIL_CONSTRUCTORS(M3UParser); 122 }; 123 124 } // namespace android 125 126 #endif // M3U_PARSER_H_ 127