1 /*
2  * Copyright (C) 2019 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 // Helper classes and functions to be used by provider and listener.
18 #ifndef ANDROID_MEDIA_ECO_UTILS_H_
19 #define ANDROID_MEDIA_ECO_UTILS_H_
20 
21 #include <cutils/atomic.h>
22 #include <utils/Errors.h>
23 
24 #include "ECOData.h"
25 #include "ECODataKey.h"
26 #include "ECOServiceConstants.h"
27 
28 namespace android {
29 namespace media {
30 namespace eco {
31 
32 #define RETURN_STATUS_IF_ERROR(expr)  \
33     {                                 \
34         status_t _errorCode = (expr); \
35         if (_errorCode != NO_ERROR) { \
36             return _errorCode;        \
37         }                             \
38     }
39 
40 // Helper structure to hold encoder config. This is used by EncoderProvider to provide stats to
41 // ECOService or by ECOService to provide info to the listener. Providers could implement their
42 // own more complex encoder config as long as ECOService supports parsing them.
43 struct SimpleEncoderConfig {
44     // Encoder name as defined in device/google/[device]/media_codecs.xml.
45     std::string mEncoderName;
46 
47     // Codec Type as defined in ECOServiceConstants.h. -1 means unavailable.
48     int32_t mCodecType;
49 
50     // Codec profile as defined in ECOServiceConstants.h. -1 means unavailable.
51     int32_t mProfile;
52 
53     // Codec level as defined in ECOServiceConstants.h. -1 means unavailable.
54     int32_t mLevel;
55 
56     // Target bitrate in bits per second. -1 means unavailable.
57     int32_t mTargetBitrate;
58 
59     // Key frame interval in frames. -1 means unavailable.
60     int32_t mKeyFrameIntervalFrames;
61 
62     // Frame rate in frames per second. -1 means unavailable.
63     float mFrameRateFps;
64 
SimpleEncoderConfigSimpleEncoderConfig65     SimpleEncoderConfig()
66           : mEncoderName(""),
67             mCodecType(-1),
68             mProfile(-1),
69             mLevel(-1),
70             mTargetBitrate(-1),
71             mKeyFrameIntervalFrames(-1),
72             mFrameRateFps(-1) {}
73 
SimpleEncoderConfigSimpleEncoderConfig74     SimpleEncoderConfig(const std::string& name, int32_t codecType, int32_t profile, int32_t level,
75                         int32_t bitrate, int32_t kfi, float framerateFps)
76           : mEncoderName(name),
77             mCodecType(codecType),
78             mProfile(profile),
79             mLevel(level),
80             mTargetBitrate(bitrate),
81             mKeyFrameIntervalFrames(kfi),
82             mFrameRateFps(framerateFps) {}
83 
84     // Convert this SimpleEncoderConfig to ECOData with dataType.
85     ECOData toEcoData(ECOData::ECODatatype dataType);
86 };
87 
88 // Helper structure for
89 struct SimpleEncodedFrameData {
90     // Frame sequence number starts from 0.
91     int32_t mFrameNum;
92 
93     // Frame type as defined in ECOServiceConstants.h. -1 means unavailable.
94     int8_t mFrameType;
95 
96     // Frame presentation timestamp in us. -1 means unavailable.
97     int64_t mFramePtsUs;
98 
99     // Avg quantization parameter of the frame. -1 means unavailable.
100     int32_t mAvgQp;
101 
102     // Frame size in bytes. -1 means unavailable.
103     int32_t mFrameSizeBytes;
104 
SimpleEncodedFrameDataSimpleEncodedFrameData105     SimpleEncodedFrameData()
106           : mFrameNum(-1),
107             mFrameType(FrameTypeUnknown),
108             mFramePtsUs(-1),
109             mAvgQp(-1),
110             mFrameSizeBytes(-1) {}
111 
SimpleEncodedFrameDataSimpleEncodedFrameData112     SimpleEncodedFrameData(int32_t frameNum, int8_t frameType, int64_t ptsUs, int32_t qp,
113                            int32_t sizeBytes)
114           : mFrameNum(frameNum),
115             mFrameType(frameType),
116             mFramePtsUs(ptsUs),
117             mAvgQp(qp),
118             mFrameSizeBytes(sizeBytes) {}
119 
120     // Convert this SimpleEncoderConfig to ECOData with dataType.
121     ECOData toEcoData(ECOData::ECODatatype dataType);
122 };
123 
124 bool copyKeyValue(const ECOData& src, ECOData* dst);
125 
126 }  // namespace eco
127 }  // namespace media
128 }  // namespace android
129 
130 #endif  // ANDROID_MEDIA_ECO_UTILS_H_
131