1 /*
2  * Copyright (C) 2021 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 CODEC_CAPABILITY_H
17 #define CODEC_CAPABILITY_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <vector>
22 #include "av_common.h"
23 #include "nocopyable.h"
24 
25 namespace OHOS {
26 namespace Media {
27 constexpr size_t ENCODER_CAPABILITY_DATA_SIZE_LIMIT = 100; // 100 size limit
28 class CodecRange {
29 public:
30     int32_t minVal;
31     int32_t maxVal;
CodecRange()32     CodecRange() : minVal(0), maxVal(0) {}
CodecRange(const int32_t & min,const int32_t & max)33     CodecRange(const int32_t &min, const int32_t &max)
34     {
35         if (min <= max) {
36             this->minVal = min;
37             this->maxVal = max;
38         } else {
39             this->minVal = 0;
40             this->maxVal = 0;
41         }
42     }
43 
Create(const int32_t & min,const int32_t & max)44     CodecRange Create(const int32_t &min, const int32_t &max)
45     {
46         return CodecRange(min, max);
47     }
48 
Marshalling(Parcel & parcel)49     bool Marshalling(Parcel &parcel) const
50     {
51         return parcel.WriteInt32(minVal)
52             && parcel.WriteInt32(maxVal);
53     }
54 
Unmarshalling(Parcel & parcel)55     void Unmarshalling(Parcel &parcel)
56     {
57         minVal = parcel.ReadInt32();
58         maxVal = parcel.ReadInt32();
59     }
60 };
61 
62 class EncoderCapabilityData {
63 public:
64     std::string mimeType = "";
65     std::string type = "";
66     CodecRange bitrate;
67     CodecRange frameRate;
68     CodecRange width;
69     CodecRange height;
70     CodecRange channels;
71     std::vector<int32_t> sampleRate;
72 
Marshalling(Parcel & parcel)73     bool Marshalling(Parcel &parcel) const
74     {
75         if (!parcel.WriteString(mimeType)) {
76             return false;
77         }
78         if (!parcel.WriteString(type)) {
79             return false;
80         }
81         if (!(bitrate.Marshalling(parcel) && frameRate.Marshalling(parcel)
82             && width.Marshalling(parcel) && height.Marshalling(parcel)
83             && channels.Marshalling(parcel))) {
84             return false;
85         }
86         size_t size = sampleRate.size();
87         if (!parcel.WriteUint64(size)) {
88             return false;
89         }
90         for (const auto &i : sampleRate) {
91             if (!parcel.WriteInt32(i)) {
92                 return false;
93             }
94         }
95         return true;
96     }
97 
Unmarshalling(Parcel & parcel)98     void Unmarshalling(Parcel &parcel)
99     {
100         mimeType = parcel.ReadString();
101         type = parcel.ReadString();
102         bitrate.Unmarshalling(parcel);
103         frameRate.Unmarshalling(parcel);
104         width.Unmarshalling(parcel);
105         height.Unmarshalling(parcel);
106         channels.Unmarshalling(parcel);
107         size_t size = parcel.ReadUint64();
108         // it may change in the future, Restricted by security requirements
109         if (size > ENCODER_CAPABILITY_DATA_SIZE_LIMIT) {
110             return;
111         }
112         for (size_t i = 0; i < size; i++) {
113             sampleRate.push_back(parcel.ReadInt32());
114         }
115     }
116 };
117 } // namespace Media
118 } // namespace OHOS
119 #endif // CODEC_CAPABILITY_H