[camera] Source code refactoring (#31)

Changes:
* Source code refactoring
* Add temporary code for camera image stream APIs
This commit is contained in:
Hidenori Matsubayashi
2021-08-17 17:49:36 +09:00
committed by GitHub
parent 736eb878ee
commit 34feab80cc
11 changed files with 173 additions and 71 deletions
@@ -0,0 +1,72 @@
// Copyright 2021 Sony Group Corporation. All rights reserved.
// Copyright 2013 The Flutter 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 "channels/event_channel_image_stream.h"
#include <flutter/event_stream_handler_functions.h>
#include <flutter/standard_method_codec.h>
#include <vector>
namespace {
constexpr char kChannelName[] = "plugins.flutter.io/camera/imageStream";
// See: [getFormat()] in
// https://developer.android.com/reference/android/media/Image
constexpr int32_t kImageFormatRGBA8888 = 4;
}; // namespace
EventChannelImageStream::EventChannelImageStream(
flutter::PluginRegistrar* registrar) {
auto event_channel =
std::make_unique<flutter::EventChannel<flutter::EncodableValue>>(
registrar->messenger(), kChannelName,
&flutter::StandardMethodCodec::GetInstance());
auto event_channel_handler = std::make_unique<
flutter::StreamHandlerFunctions<flutter::EncodableValue>>(
[this](
const flutter::EncodableValue* arguments,
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&& events)
-> std::unique_ptr<
flutter::StreamHandlerError<flutter::EncodableValue>> {
event_sink_ = std::move(events);
return nullptr;
},
[this](const flutter::EncodableValue* arguments)
-> std::unique_ptr<
flutter::StreamHandlerError<flutter::EncodableValue>> {
event_sink_ = nullptr;
return nullptr;
});
event_channel->SetStreamHandler(std::move(event_channel_handler));
}
// See: [setImageStreamImageAvailableListener] in
// flutter/plugins/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/Camera.java
void EventChannelImageStream::Send(const int32_t& width, const int32_t& height,
const uint8_t* pixels) {
const uint32_t len = width * 4 * height;
std::vector<uint8_t> bytes(pixels, pixels + len);
flutter::EncodableList planes;
flutter::EncodableMap plane = {
{flutter::EncodableValue("bytesPerRow"), flutter::EncodableValue(width)},
{flutter::EncodableValue("bytesPerPixel"), flutter::EncodableValue(4)},
{flutter::EncodableValue("bytes"), flutter::EncodableValue(bytes)},
};
flutter::EncodableValue plane_value(plane);
planes.push_back(plane_value);
flutter::EncodableMap encodables = {
{flutter::EncodableValue("width"), flutter::EncodableValue(width)},
{flutter::EncodableValue("height"), flutter::EncodableValue(height)},
{flutter::EncodableValue("format"),
flutter::EncodableValue(kImageFormatRGBA8888)},
{flutter::EncodableValue("planes"), flutter::EncodableValue(planes)}};
flutter::EncodableValue event(encodables);
event_sink_->Success(event);
}
@@ -0,0 +1,27 @@
// Copyright 2021 Sony Group Corporation. All rights reserved.
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef PACKAGES_CAMERA_CAMERA_ELINUX_CAMERA_CHANNELS_EVENT_CHANNEL_IMAGE_STREAM_H_
#define PACKAGES_CAMERA_CAMERA_ELINUX_CAMERA_CHANNELS_EVENT_CHANNEL_IMAGE_STREAM_H_
#include <flutter/encodable_value.h>
#include <flutter/event_channel.h>
#include <flutter/plugin_registrar.h>
#include <string>
class EventChannelImageStream {
public:
EventChannelImageStream(flutter::PluginRegistrar* registrar);
~EventChannelImageStream() = default;
void Send(const int32_t& width, const int32_t& height, const uint8_t* pixels);
private:
std::unique_ptr<flutter::EventChannel<flutter::EncodableValue>> channel_;
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>> event_sink_;
};
#endif // PACKAGES_CAMERA_CAMERA_ELINUX_CAMERA_CHANNELS_EVENT_CHANNEL_IMAGE_STREAM_H_
@@ -0,0 +1,33 @@
// Copyright 2021 Sony Group Corporation. All rights reserved.
// Copyright 2013 The Flutter 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 "channels/method_channel_camera.h"
#include <flutter/standard_method_codec.h>
namespace {
constexpr char kChannelName[] = "flutter.io/cameraPlugin/camera";
constexpr char kChannelMethodInitialized[] = "initialized";
}; // namespace
MethodChannelCamera::MethodChannelCamera(flutter::PluginRegistrar* registrar,
int64_t camera_id) {
std::string channel_name = kChannelName + std::to_string(camera_id);
channel_ = std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
registrar->messenger(), channel_name.c_str(),
&flutter::StandardMethodCodec::GetInstance());
}
void MethodChannelCamera::SendInitializedEvent(
CameraInitializedEvent& message) {
auto value = std::make_unique<flutter::EncodableValue>(message.ToMap());
Send(kChannelMethodInitialized, std::move(value));
}
void MethodChannelCamera::Send(
const std::string& method,
std::unique_ptr<flutter::EncodableValue>&& arguments) {
channel_->InvokeMethod(method, std::move(arguments));
}
@@ -0,0 +1,31 @@
// Copyright 2021 Sony Group Corporation. All rights reserved.
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef PACKAGES_CAMERA_CAMERA_ELINUX_CAMERA_CHANNELS_METHOD_CHANNEL_CAMERA_H_
#define PACKAGES_CAMERA_CAMERA_ELINUX_CAMERA_CHANNELS_METHOD_CHANNEL_CAMERA_H_
#include <flutter/encodable_value.h>
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar.h>
#include <string>
#include "events/camera_initialized_event.h"
class MethodChannelCamera {
public:
MethodChannelCamera(flutter::PluginRegistrar* registrar, int64_t camera_id);
~MethodChannelCamera() = default;
void SendInitializedEvent(CameraInitializedEvent& message);
private:
void Send(const std::string& method,
std::unique_ptr<flutter::EncodableValue>&& arguments);
std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> channel_;
};
#endif // PACKAGES_CAMERA_CAMERA_ELINUX_CAMERA_CHANNELS_METHOD_CHANNEL_CAMERA_H_
@@ -0,0 +1,37 @@
// Copyright 2021 Sony Group Corporation. All rights reserved.
// Copyright 2013 The Flutter 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 "channels/method_channel_device.h"
#include <flutter/standard_method_codec.h>
namespace {
constexpr char kChannelName[] = "flutter.io/cameraPlugin/device";
constexpr char kChannelMethodInitialized[] = "initialized";
constexpr char kOrientation[] = "orientation";
}; // namespace
MethodChannelDevice::MethodChannelDevice(flutter::PluginRegistrar* registrar) {
channel_ = std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
registrar->messenger(), kChannelName,
&flutter::StandardMethodCodec::GetInstance());
}
void MethodChannelDevice::SendDeviceOrientationChangeEvent(
const DeviceOrientation& orientation) {
flutter::EncodableMap mp;
mp[flutter::EncodableValue(kOrientation)] =
flutter::EncodableValue(SerializeDeviceOrientation(orientation));
auto value = std::make_unique<flutter::EncodableValue>(mp);
Send(kChannelMethodInitialized, std::move(value));
}
void MethodChannelDevice::Send(
const std::string& method,
std::unique_ptr<flutter::EncodableValue>&& arguments) {
channel_->InvokeMethod(method, std::move(arguments));
}
@@ -0,0 +1,31 @@
// Copyright 2021 Sony Group Corporation. All rights reserved.
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef PACKAGES_CAMERA_CAMERA_ELINUX_CAMERA_CHANNELS_METHOD_CHANNEL_DEVICE_H_
#define PACKAGES_CAMERA_CAMERA_ELINUX_CAMERA_CHANNELS_METHOD_CHANNEL_DEVICE_H_
#include <flutter/encodable_value.h>
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar.h>
#include <string>
#include "types/orientation.h"
class MethodChannelDevice {
public:
MethodChannelDevice(flutter::PluginRegistrar* registrar);
~MethodChannelDevice() = default;
void SendDeviceOrientationChangeEvent(const DeviceOrientation& orientation);
private:
void Send(const std::string& method,
std::unique_ptr<flutter::EncodableValue>&& arguments);
std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> channel_;
};
#endif // PACKAGES_CAMERA_CAMERA_ELINUX_CAMERA_CHANNELS_METHOD_CHANNEL_DEVICE_H_