blob: 728804dfaea0ee6ab45eb8afcad04c8d347092a5 [file] [log] [blame]
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <fuchsia/mediacodec/cpp/fidl.h>
#include <lib/fidl/cpp/binding.h>
#include <lib/sys/cpp/component_context.h>
#include "codec_factory_app.h"
#include "codec_factory_policy.h"
// There's an instance of CodecFactoryImpl per interface instance, to allow the
// implementation of this class to be stateful. In particular, the state set up
// by AttachLifetimeTracking() calls applies to the next
// create.
class CodecFactoryImpl : public fuchsia::mediacodec::CodecFactory {
static void CreateSelfOwned(CodecFactoryApp* app, sys::ComponentContext* component_context,
fidl::InterfaceRequest<fuchsia::mediacodec::CodecFactory> request);
// See .fidl file comments.
void CreateDecoder(fuchsia::mediacodec::CreateDecoder_Params params,
fidl::InterfaceRequest<fuchsia::media::StreamProcessor> decoder) override;
void CreateEncoder(
fuchsia::mediacodec::CreateEncoder_Params encoder_params,
fidl::InterfaceRequest<fuchsia::media::StreamProcessor> encoder_request) override;
void AttachLifetimeTracking(zx::eventpair codec_end) override;
CodecFactoryImpl(CodecFactoryApp* app, sys::ComponentContext* component_context,
fidl::InterfaceRequest<fuchsia::mediacodec::CodecFactory> request);
void OwnSelf(std::unique_ptr<CodecFactoryImpl> self);
void AttachLifetimeTrackingEventpairDownstream(
const fuchsia::mediacodec::CodecFactoryPtr* factory);
bool AdmitHwDecoder(const fuchsia::mediacodec::CreateDecoder_Params& params);
bool AdmitHwEncoder(const fuchsia::mediacodec::CreateEncoder_Params& params);
// We don't have a lock_ in here - we rely on FIDL message dispatch being
// one-at-a-time.
// This class doesn't own these pointers - the creator of CodecFactoryImpl
// must ensure these outlast this instance of CodecFactoryImpl.
CodecFactoryApp* app_ = nullptr;
sys::ComponentContext* component_context_ = nullptr;
// This is only holding the underlying channel between construction and
// OwnSelf(), at which point the channel moves into the binding.
fidl::InterfaceRequest<fuchsia::mediacodec::CodecFactory> request_temp_;
// The CodecFactoryImpl is essentially self-owned via this member. If we need
// to self-destruct we can reset this binding_ unique_ptr which will delete
// the binding which will delete the CodecFactoryImpl owned by binding_.
// Similarly if the channel closes, the binding will drop the unique_ptr to
// the CodecFactory which will delete the factory and binding.
// TODO(dustingreen): Cover both cases mentioned above. May have to punt a
// stage of deletion to the async::Loop perhaps if this doesn't work.
using BindingType =
fidl::Binding<fuchsia::mediacodec::CodecFactory, std::unique_ptr<CodecFactoryImpl>>;
std::unique_ptr<BindingType> binding_;
std::vector<zx::eventpair> lifetime_tracking_;