aboutsummaryrefslogtreecommitdiff
path: root/components/V4L2ComponentStore.cpp
blob: feb579969bbf21ca0fe4e43fd475a679b66c7333 (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
207
208
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//#define LOG_NDEBUG 0
#define LOG_TAG "V4L2ComponentStore"

#include <v4l2_codec2/components/V4L2ComponentStore.h>

#include <stdint.h>

#include <memory>
#include <mutex>

#include <C2.h>
#include <C2Config.h>
#include <log/log.h>
#include <media/stagefright/foundation/MediaDefs.h>

#include <v4l2_codec2/common/V4L2ComponentCommon.h>
#include <v4l2_codec2/components/V4L2ComponentFactory.h>

namespace android {
namespace {
const uint32_t kComponentRank = 0x80;

std::string getMediaTypeFromComponentName(const std::string& name) {
    if (name == V4L2ComponentName::kH264Decoder || name == V4L2ComponentName::kH264SecureDecoder ||
        name == V4L2ComponentName::kH264Encoder) {
        return MEDIA_MIMETYPE_VIDEO_AVC;
    }
    if (name == V4L2ComponentName::kVP8Decoder || name == V4L2ComponentName::kVP8SecureDecoder ||
        name == V4L2ComponentName::kVP8Encoder) {
        return MEDIA_MIMETYPE_VIDEO_VP8;
    }
    if (name == V4L2ComponentName::kVP9Decoder || name == V4L2ComponentName::kVP9SecureDecoder ||
        name == V4L2ComponentName::kVP9Encoder) {
        return MEDIA_MIMETYPE_VIDEO_VP9;
    }
    if (name == V4L2ComponentName::kHEVCDecoder || name == V4L2ComponentName::kHEVCSecureDecoder) {
        return MEDIA_MIMETYPE_VIDEO_HEVC;
    }
    return "";
}

}  // namespace

// static
std::shared_ptr<C2ComponentStore> V4L2ComponentStore::Create() {
    ALOGV("%s()", __func__);

    static std::mutex mutex;
    static std::weak_ptr<C2ComponentStore> platformStore;

    std::lock_guard<std::mutex> lock(mutex);
    std::shared_ptr<C2ComponentStore> store = platformStore.lock();
    if (store != nullptr) return store;

    store = std::shared_ptr<C2ComponentStore>(new V4L2ComponentStore());
    platformStore = store;
    return store;
}

V4L2ComponentStore::V4L2ComponentStore() : mReflector(std::make_shared<C2ReflectorHelper>()) {
    ALOGV("%s()", __func__);
}

V4L2ComponentStore::~V4L2ComponentStore() {
    ALOGV("%s()", __func__);

    std::lock_guard<std::mutex> lock(mCachedFactoriesLock);
    mCachedFactories.clear();
}

C2String V4L2ComponentStore::getName() const {
    return "android.componentStore.v4l2";
}

c2_status_t V4L2ComponentStore::createComponent(C2String name,
                                                std::shared_ptr<C2Component>* const component) {
    ALOGV("%s(%s)", __func__, name.c_str());

    if (!V4L2ComponentName::isValid(name.c_str())) {
        ALOGI("%s(): Invalid component name: %s", __func__, name.c_str());
        return C2_NOT_FOUND;
    }

    auto factory = GetFactory(name);
    if (factory == nullptr) return C2_CORRUPTED;

    component->reset();
    return factory->createComponent(0, component);
}

c2_status_t V4L2ComponentStore::createInterface(
        C2String name, std::shared_ptr<C2ComponentInterface>* const interface) {
    ALOGV("%s(%s)", __func__, name.c_str());

    if (!V4L2ComponentName::isValid(name.c_str())) {
        ALOGI("%s(): Invalid component name: %s", __func__, name.c_str());
        return C2_NOT_FOUND;
    }

    auto factory = GetFactory(name);
    if (factory == nullptr) return C2_CORRUPTED;

    interface->reset();
    return factory->createInterface(0, interface);
}

std::vector<std::shared_ptr<const C2Component::Traits>> V4L2ComponentStore::listComponents() {
    ALOGV("%s()", __func__);

    std::vector<std::shared_ptr<const C2Component::Traits>> ret;
    ret.push_back(GetTraits(V4L2ComponentName::kH264Encoder));
    ret.push_back(GetTraits(V4L2ComponentName::kH264Decoder));
    ret.push_back(GetTraits(V4L2ComponentName::kH264SecureDecoder));
    ret.push_back(GetTraits(V4L2ComponentName::kVP8Encoder));
    ret.push_back(GetTraits(V4L2ComponentName::kVP8Decoder));
    ret.push_back(GetTraits(V4L2ComponentName::kVP8SecureDecoder));
    ret.push_back(GetTraits(V4L2ComponentName::kVP9Encoder));
    ret.push_back(GetTraits(V4L2ComponentName::kVP9Decoder));
    ret.push_back(GetTraits(V4L2ComponentName::kVP9SecureDecoder));
    ret.push_back(GetTraits(V4L2ComponentName::kHEVCDecoder));
    ret.push_back(GetTraits(V4L2ComponentName::kHEVCSecureDecoder));
    return ret;
}

std::shared_ptr<C2ParamReflector> V4L2ComponentStore::getParamReflector() const {
    return mReflector;
}

c2_status_t V4L2ComponentStore::copyBuffer(std::shared_ptr<C2GraphicBuffer> /* src */,
                                           std::shared_ptr<C2GraphicBuffer> /* dst */) {
    return C2_OMITTED;
}

c2_status_t V4L2ComponentStore::querySupportedParams_nb(
        std::vector<std::shared_ptr<C2ParamDescriptor>>* const /* params */) const {
    return C2_OK;
}

c2_status_t V4L2ComponentStore::query_sm(
        const std::vector<C2Param*>& stackParams,
        const std::vector<C2Param::Index>& heapParamIndices,
        std::vector<std::unique_ptr<C2Param>>* const /* heapParams */) const {
    // There are no supported config params.
    return stackParams.empty() && heapParamIndices.empty() ? C2_OK : C2_BAD_INDEX;
}

c2_status_t V4L2ComponentStore::config_sm(
        const std::vector<C2Param*>& params,
        std::vector<std::unique_ptr<C2SettingResult>>* const /* failures */) {
    // There are no supported config params.
    return params.empty() ? C2_OK : C2_BAD_INDEX;
}

c2_status_t V4L2ComponentStore::querySupportedValues_sm(
        std::vector<C2FieldSupportedValuesQuery>& fields) const {
    // There are no supported config params.
    return fields.empty() ? C2_OK : C2_BAD_INDEX;
}

::C2ComponentFactory* V4L2ComponentStore::GetFactory(const C2String& name) {
    ALOGV("%s(%s)", __func__, name.c_str());
    ALOG_ASSERT(V4L2ComponentName::isValid(name.c_str()));

    std::lock_guard<std::mutex> lock(mCachedFactoriesLock);
    const auto it = mCachedFactories.find(name);
    if (it != mCachedFactories.end()) return it->second.get();

    std::unique_ptr<::C2ComponentFactory> factory = V4L2ComponentFactory::create(
            name, std::static_pointer_cast<C2ReflectorHelper>(getParamReflector()));
    if (factory == nullptr) {
        ALOGE("Failed to create factory for %s", name.c_str());
        return nullptr;
    }

    auto ret = factory.get();
    mCachedFactories.emplace(name, std::move(factory));
    return ret;
}

std::shared_ptr<const C2Component::Traits> V4L2ComponentStore::GetTraits(const C2String& name) {
    ALOGV("%s(%s)", __func__, name.c_str());

    if (!V4L2ComponentName::isValid(name.c_str())) {
        ALOGE("Invalid component name: %s", name.c_str());
        return nullptr;
    }

    std::lock_guard<std::mutex> lock(mCachedTraitsLock);
    auto it = mCachedTraits.find(name);
    if (it != mCachedTraits.end()) return it->second;

    auto traits = std::make_shared<C2Component::Traits>();
    traits->name = name;
    traits->domain = C2Component::DOMAIN_VIDEO;
    traits->rank = kComponentRank;
    traits->mediaType = getMediaTypeFromComponentName(name);
    traits->kind = V4L2ComponentName::isEncoder(name.c_str()) ? C2Component::KIND_ENCODER
                                                              : C2Component::KIND_DECODER;

    mCachedTraits.emplace(name, traits);
    return traits;
}

}  // namespace android