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_M3U8_H
17 #define HISTREAMER_M3U8_H
18 
19 #include <memory>
20 #include <string>
21 #include <list>
22 #include <unordered_map>
23 #include <functional>
24 #include "hls_tags.h"
25 
26 namespace OHOS {
27 namespace Media {
28 namespace Plugin {
29 namespace HttpPlugin {
30 enum class M3U8MediaType : int32_t {
31     M3U8_MEDIA_TYPE_INVALID = -1,
32     M3U8_MEDIA_TYPE_AUDIO,
33     M3U8_MEDIA_TYPE_VIDEO,
34     M3U8_MEDIA_TYPE_SUBTITLES,
35     M3U8_MEDIA_TYPE_CLOSED_CAPTIONS,
36     M3U8_N_MEDIA_TYPES,
37 };
38 
39 struct M3U8InitFile {
40     std::string uri;
41     int offset;
42     int size;
43 };
44 
45 struct M3U8Fragment {
46     M3U8Fragment(std::string uri, std::string title, double duration, int sequence, bool discont);
47     std::string uri_;
48     std::string title_;
49     double duration_;
50     int64_t sequence_;
51     bool discont_ {false};
52     std::string key_ {};
53     int iv_[16] {0};
54     int offset_ {-1};
55     int size_ {0};
56 };
57 
58 struct M3U8Info {
59     std::string uri;
60     std::string title;
61     double duration = 0;
62     bool discontinuity = false;
63     bool bVod;
64 };
65 
66 struct M3U8 {
67     M3U8(std::string uri, std::string name);
68     void InitTagUpdatersMap();
69     bool Update(std::string& playList);
70     void UpdateFromTags(std::list<std::shared_ptr<Tag>>& tags);
71     void GetExtInf(const std::shared_ptr<Tag>& tag, double& duration, std::string& title) const;
72     double GetDuration() const;
73     bool IsLive() const;
74 
75     std::string uri_;
76     std::string name_;
77     std::unordered_map<HlsTag, std::function<void(std::shared_ptr<Tag>&, M3U8Info&)>> tagUpdatersMap_;
78 
79     double targetDuration_ {0.0};
80     bool bLive_ {};
81     std::list<std::shared_ptr<M3U8Fragment>> files_;
82     uint64_t sequence_ {1}; // default 1
83     int discontSequence_ {0};
84     std::string playList_;
85 };
86 
87 struct M3U8Media {
88     M3U8MediaType type_;
89     std::string groupID_;
90     std::string name_;
91     std::string lang_;
92     std::string uri_;
93     bool isDefault_;
94     bool autoSelect_;
95     bool forced_;
96     std::shared_ptr<M3U8> m3u8_;
97 };
98 
99 struct M3U8VariantStream {
100     M3U8VariantStream(std::string name, std::string uri, std::shared_ptr<M3U8> m3u8);
101     std::string name_;
102     std::string uri_;
103     std::string codecs_;
104     uint64_t bandWidth_ {};
105     int programID_ {};
106     int width_ {};
107     int height_ {};
108     bool iframe_ {false};
109     std::shared_ptr<M3U8> m3u8_;
110     std::list<M3U8Media> media_;
111 };
112 
113 struct M3U8MasterPlaylist {
114     M3U8MasterPlaylist(std::string& playList, const std::string& uri);
115     void UpdateMediaPlaylist();
116     void UpdateMasterPlaylist();
117     std::list<std::shared_ptr<M3U8VariantStream>> variants_;
118     std::shared_ptr<M3U8VariantStream> defaultVariant_;
119     std::string uri_;
120     bool isSimple_ {false};
121     std::string playList_;
122     double duration_ {0};
123     bool bLive_ {};
124 };
125 }
126 }
127 }
128 }
129 #endif