summaryrefslogtreecommitdiff
path: root/media/codec2/hal/aidl/ComponentInterface.cpp
blob: 8ae9fa8959047308dd1a983ce9cd920cbd1de772 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "Codec2-ComponentInterface"
#include <android-base/logging.h>

#include <android/binder_auto_utils.h>
#include <codec2/aidl/ComponentInterface.h>
#include <codec2/aidl/Configurable.h>

#include <utils/Timers.h>

#include <codec2/common/MultiAccessUnitHelper.h>

#include <C2Debug.h>
#include <C2PlatformSupport.h>

#include <chrono>
#include <thread>

namespace aidl {
namespace android {
namespace hardware {
namespace media {
namespace c2 {
namespace utils {

using ::ndk::ScopedAStatus;

namespace /* unnamed */ {

// Implementation of ConfigurableC2Intf based on C2ComponentInterface
struct CompIntf : public ConfigurableC2Intf {
    CompIntf(const std::shared_ptr<C2ComponentInterface>& intf,
        const std::shared_ptr<MultiAccessUnitInterface>& multiAccessUnitIntf):
        ConfigurableC2Intf{intf->getName(), intf->getId()},
        mIntf{intf}, mMultiAccessUnitIntf{multiAccessUnitIntf} {
    }

    virtual c2_status_t config(
            const std::vector<C2Param*>& params,
            c2_blocking_t mayBlock,
            std::vector<std::unique_ptr<C2SettingResult>>* const failures
            ) override {
        std::vector<C2Param*> paramsToIntf;
        std::vector<C2Param*> paramsToLargeFrameIntf;
        c2_status_t err = C2_OK;
        if (mMultiAccessUnitIntf == nullptr) {
            err = mIntf->config_vb(params, mayBlock, failures);
            return err;
        }
        for (auto &p : params) {
            if (mMultiAccessUnitIntf->isParamSupported(p->index())) {
                paramsToLargeFrameIntf.push_back(p);
            } else {
                paramsToIntf.push_back(p);
            }
        }
        c2_status_t err1 = C2_OK;
        if (paramsToIntf.size() > 0) {
            err1 = mIntf->config_vb(paramsToIntf, mayBlock, failures);
        }
        if (err1 != C2_OK) {
            LOG(ERROR) << "We have a failed config";
        }
        c2_status_t err2 = C2_OK;
        if (paramsToLargeFrameIntf.size() > 0) {
            err2 = mMultiAccessUnitIntf->config(
                    paramsToLargeFrameIntf, mayBlock, failures);
        }
        // TODO: correct failure vector
        return err1 != C2_OK ? err1 : err2;
    }

    virtual c2_status_t query(
            const std::vector<C2Param::Index>& indices,
            c2_blocking_t mayBlock,
            std::vector<std::unique_ptr<C2Param>>* const params
            ) const override {
        c2_status_t err = C2_OK;
        if (mMultiAccessUnitIntf == nullptr) {
            err = mIntf->query_vb({}, indices, mayBlock, params);
            return err;
        }
        std::vector<C2Param::Index> paramsToIntf;
        std::vector<C2Param::Index> paramsToLargeFrameIntf;
        for (auto &i : indices) {
            if (mMultiAccessUnitIntf->isParamSupported(i)) {
                paramsToLargeFrameIntf.push_back(i);
            } else {
                paramsToIntf.push_back(i);
            }
        }
        c2_status_t err1 = C2_OK;
        if (paramsToIntf.size() > 0) {
            err1 = mIntf->query_vb({}, paramsToIntf, mayBlock, params);
        }
        c2_status_t err2 = C2_OK;
        if (paramsToLargeFrameIntf.size() > 0) {
            err2 = mMultiAccessUnitIntf->query(
                    {}, paramsToLargeFrameIntf, mayBlock, params);
        }
        // TODO: correct failure vector
        return err1 != C2_OK ? err1 : err2;
    }

    virtual c2_status_t querySupportedParams(
            std::vector<std::shared_ptr<C2ParamDescriptor>>* const params
            ) const override {
        c2_status_t err = mIntf->querySupportedParams_nb(params);
        if (mMultiAccessUnitIntf != nullptr) {
            err =  mMultiAccessUnitIntf->querySupportedParams(params);
        }
        return err;
    }

    virtual c2_status_t querySupportedValues(
            std::vector<C2FieldSupportedValuesQuery>& fields,
            c2_blocking_t mayBlock) const override {
        if (mMultiAccessUnitIntf == nullptr) {
           return  mIntf->querySupportedValues_vb(fields, mayBlock);
        }
        std::vector<C2FieldSupportedValuesQuery> dup = fields;
        std::vector<C2FieldSupportedValuesQuery> queryArray[2];
        std::map<C2ParamField, std::pair<uint32_t, size_t>> queryMap;
        c2_status_t err = C2_OK;
        for (int i = 0 ; i < fields.size(); i++) {
            const C2ParamField &field = fields[i].field();
            uint32_t queryArrayIdx = 1;
            if (mMultiAccessUnitIntf->isValidField(fields[i].field())) {
                queryArrayIdx = 0;
            }
            queryMap[field] = std::make_pair(
                    queryArrayIdx, queryArray[queryArrayIdx].size());
            queryArray[queryArrayIdx].push_back(fields[i]);
        }
        if (queryArray[0].size() > 0) {
            err = mMultiAccessUnitIntf->querySupportedValues(queryArray[0], mayBlock);
        }
        if (queryArray[1].size() > 0) {
             err = mIntf->querySupportedValues_vb(queryArray[1], mayBlock);
        }
        for (int i = 0 ; i < dup.size(); i++) {
            auto it = queryMap.find(dup[i].field());
            if (it != queryMap.end()) {
                std::pair<uint32_t, size_t> queryid = it->second;
                fields[i] = queryArray[queryid.first][queryid.second];
            }
        }
        return err;
    }

protected:
    std::shared_ptr<C2ComponentInterface> mIntf;
    std::shared_ptr<MultiAccessUnitInterface> mMultiAccessUnitIntf;
};

} // unnamed namespace

// ComponentInterface
ComponentInterface::ComponentInterface(
        const std::shared_ptr<C2ComponentInterface>& intf,
        const std::shared_ptr<ParameterCache>& cache):ComponentInterface(intf, nullptr, cache) {
}

ComponentInterface::ComponentInterface(
        const std::shared_ptr<C2ComponentInterface>& intf,
        const std::shared_ptr<MultiAccessUnitInterface>& multiAccessUnitIntf,
        const std::shared_ptr<ParameterCache>& cache)
      : mInterface{intf},
        mConfigurable{SharedRefBase::make<CachedConfigurable>(
                std::make_unique<CompIntf>(intf, multiAccessUnitIntf))} {
    mInit = mConfigurable->init(cache);
}

c2_status_t ComponentInterface::status() const {
    return mInit;
}

ScopedAStatus ComponentInterface::getConfigurable(
        std::shared_ptr<IConfigurable> *configurable) {
    *configurable = mConfigurable;
    return ScopedAStatus::ok();
}

}  // namespace utils
}  // namespace c2
}  // namespace media
}  // namespace hardware
}  // namespace android
}  // namespace aidl