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