1 /* 2 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef HISTREAMER_HLS_TAGS_H 17 #define HISTREAMER_HLS_TAGS_H 18 19 #include <list> 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 namespace OHOS { 25 namespace Media { 26 namespace Plugin { 27 namespace HttpPlugin { 28 class Attribute { 29 public: 30 Attribute(std::string name, std::string value); 31 std::string GetName() const; 32 Attribute UnescapeQuotes() const; 33 uint64_t Decimal() const; 34 std::string QuotedString() const; 35 double FloatingPoint() const; 36 std::vector<uint8_t> HexSequence() const; 37 std::pair<std::size_t, std::size_t> GetByteRange() const; 38 std::pair<int, int> GetResolution() const; 39 private: 40 std::string name_; 41 std::string value_; 42 }; 43 44 enum struct HlsTagSection : uint8_t { 45 REGULAR, 46 SINGLE_VALUE, 47 ATTRIBUTES, 48 VALUES_LIST, 49 }; 50 51 enum struct HlsTag : uint32_t { 52 SECTION_REGULAR_START = static_cast<uint8_t>(HlsTagSection::REGULAR) << 16U, // regular tag 53 SECTION_SINGLE_VALUE_START = static_cast<uint8_t>(HlsTagSection::SINGLE_VALUE) << 16U, // single value tag 54 SECTION_ATTRIBUTES_START = static_cast<uint8_t>(HlsTagSection::ATTRIBUTES) << 16U, // attributes tag 55 SECTION_VALUES_LIST_START = static_cast<uint8_t>(HlsTagSection::VALUES_LIST) << 16U, // values list tag 56 57 EXTXDISCONTINUITY = SECTION_REGULAR_START, 58 EXTXENDLIST, 59 EXTXIFRAMESONLY, 60 61 URI = SECTION_SINGLE_VALUE_START, 62 EXTXVERSION, 63 EXTXBYTERANGE, 64 EXTXPROGRAMDATETIME, 65 EXTXTARGETDURATION, 66 EXTXMEDIASEQUENCE, 67 EXTXDISCONTINUITYSEQUENCE, 68 EXTXPLAYLISTTYPE, 69 70 EXTXKEY = SECTION_ATTRIBUTES_START, 71 EXTXMAP, 72 EXTXMEDIA, 73 EXTXSTART, 74 EXTXSTREAMINF, 75 EXTXIFRAMESTREAMINF, 76 EXTXSESSIONKEY, 77 78 EXTINF = SECTION_VALUES_LIST_START, 79 }; 80 81 class Tag { 82 public: 83 explicit Tag(HlsTag type); 84 virtual ~Tag() = default; 85 HlsTag GetType() const; 86 private: 87 HlsTag type_; 88 }; 89 90 class SingleValueTag : public Tag { 91 public: 92 SingleValueTag(HlsTag type, const std::string& v); 93 ~SingleValueTag() override = default; 94 const Attribute& GetValue() const; 95 private: 96 Attribute attr_; 97 }; 98 99 class AttributesTag : public Tag { 100 public: 101 AttributesTag(HlsTag type, const std::string& v); 102 ~AttributesTag() override = default; 103 std::shared_ptr<Attribute> GetAttributeByName(const char* name) const; 104 void AddAttribute(std::shared_ptr<Attribute>& attr); 105 protected: 106 std::list<std::shared_ptr<Attribute>> attributes; 107 private: 108 void ParseAttributes(const std::string& field); 109 std::string ParseAttributeName(std::istringstream& iss, std::ostringstream& oss) const; 110 std::string ParseAttributeValue(std::istringstream& iss, std::ostringstream& oss); 111 }; 112 113 class ValuesListTag : public AttributesTag { 114 public: 115 ValuesListTag(HlsTag type, const std::string& v); 116 ~ValuesListTag() override = default; 117 private: 118 void ParseValuesAttributes(const std::string& field); 119 }; 120 121 class TagFactory { 122 public: 123 static std::shared_ptr<Tag> CreateTagByName(const std::string& name, const std::string& value); 124 }; 125 126 std::list<std::shared_ptr<Tag>> ParseEntries(std::string& s); 127 } 128 } 129 } 130 } 131 132 #endif // HISTREAMER_HLS_TAGS_H 133