1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 OHOS_SHARING_FRAME_H
17 #define OHOS_SHARING_FRAME_H
18 
19 #include <list>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 #include "common/const_def.h"
25 #include "utils/data_buffer.h"
26 
27 namespace OHOS {
28 namespace Sharing {
29 class CodecInfo {
30 public:
31     using Ptr = std::shared_ptr<CodecInfo>;
32 
33     CodecInfo() = default;
34     virtual ~CodecInfo() = default;
35 
36     TrackType GetTrackType();
37     virtual CodecId GetCodecId() = 0;
38 };
39 
40 class Frame : public DataBuffer,
41               public CodecInfo {
42 public:
43     using Ptr = std::shared_ptr<Frame>;
44 
45     Frame() = default;
Frame(DataBuffer &&dataBuffer)46     explicit Frame(DataBuffer &&dataBuffer) : DataBuffer(std::move(dataBuffer)) {}
47 
48     ~Frame() override {};
49 
50     virtual uint32_t Dts() = 0;
51 
Pts()52     virtual uint32_t Pts()
53     {
54         return Dts();
55     }
56 
57     virtual bool KeyFrame() = 0;
58     virtual bool ConfigFrame() = 0;
59     virtual size_t PrefixSize() = 0;
60 
CacheAble()61     virtual bool CacheAble()
62     {
63         return true;
64     }
65 
DropAble()66     virtual bool DropAble()
67     {
68         return false;
69     }
70 
DecodeAble()71     virtual bool DecodeAble()
72     {
73         if (GetTrackType() != TRACK_VIDEO) {
74             return true;
75         }
76 
77         return !ConfigFrame();
78     }
79 };
80 
81 class FrameImpl : public Frame {
82 public:
83     using Ptr = std::shared_ptr<FrameImpl>;
84 
Create()85     static std::shared_ptr<FrameImpl> Create()
86     {
87         return std::make_shared<FrameImpl>();
88     }
89 
FrameImpl(DataBuffer &&dataBuffer)90     explicit FrameImpl(DataBuffer &&dataBuffer) : Frame(std::move(dataBuffer)) {}
91 
CreateFrom(DataBuffer &&dataBuffer)92     static std::shared_ptr<FrameImpl> CreateFrom(DataBuffer &&dataBuffer)
93     {
94         return std::make_shared<FrameImpl>(std::move(dataBuffer));
95     }
96 
97     uint32_t Dts() override
98     {
99         return dts_;
100     }
101 
102     uint32_t Pts() override
103     {
104         return pts_ ? pts_ : dts_;
105     }
106 
107     size_t PrefixSize() override
108     {
109         return prefixSize_;
110     }
111 
112     CodecId GetCodecId() override
113     {
114         return codecId_;
115     }
116 
117     bool KeyFrame() override
118     {
119         return false;
120     }
121 
122     bool ConfigFrame() override
123     {
124         return false;
125     }
126 
127     FrameImpl() = default;
128 
129 public:
130     uint32_t dts_ = 0;
131     uint32_t pts_ = 0;
132 
133     size_t prefixSize_ = 0;
134     CodecId codecId_ = CODEC_NONE;
135 };
136 } // namespace Sharing
137 } // namespace OHOS
138 #endif