blob: 8be2bce61b170a973b649d3143da941cea5d8192 [file] [log] [blame]
// Copyright 2018 The Chromium OS 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 "media_perception/video_capture_service_client.h"
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include "media_perception/mojo_connector.h"
#include "media_perception/producer_impl.h"
#include "media_perception/video_frame_handler_impl.h"
#include "mojom/media_perception_service.mojom.h"
namespace mri {
// Implementation of the VideoCaptureServiceClient interface for interacting
// with the Chrome VideoCaptureService using google3 code.
class VideoCaptureServiceClientImpl : public VideoCaptureServiceClient {
VideoCaptureServiceClientImpl() : mojo_connector_(nullptr) {}
// Set the global mojo connector object for use with talking to the video
// capture service.
void SetMojoConnector(MojoConnector* mojo_connector);
// VideoCaptureServiceClient overrides:
bool Connect() override;
bool IsConnected() override;
void GetDevices(const GetDevicesCallback& callback) override;
void OpenDevice(const std::string& device_id,
bool force_reopen_with_settings,
const SerializedVideoStreamParams& capture_format,
const OpenDeviceCallback& callback) override;
bool IsVideoCaptureStartedForDevice(
const std::string& device_id,
SerializedVideoStreamParams* capture_format) override;
int AddFrameHandler(const std::string& device_id,
FrameHandler handler) override;
bool RemoveFrameHandler(const std::string& device_id,
int frame_handler_id) override;
void CreateVirtualDevice(const SerializedVideoDevice& video_device,
const VirtualDeviceCallback& callback) override;
void PushFrameToVirtualDevice(const std::string& device_id,
uint64_t timestamp_in_microseconds,
std::unique_ptr<const uint8_t[]> data,
int data_size,
RawPixelFormat pixel_format,
int frame_width,
int frame_height) override;
void CloseVirtualDevice(const std::string& device_id) override;
void OnOpenDeviceCallback(const OpenDeviceCallback& callback,
std::string device_id,
CreatePushSubscriptionResultCode code,
SerializedVideoStreamParams params);
MojoConnector* mojo_connector_;
// Stores a map of device ids to video_frame_handlers for receiving frame for
// the correct mojo object associated with an open device.
std::map<std::string /*device_id*/, std::shared_ptr<VideoFrameHandlerImpl>>
// Guards against concurrent changes to
// |device_id_to_video_frame_handler_map_|.
mutable std::mutex device_id_to_video_frame_handler_map_lock_;
// Stores a map of device ids to producers for pushing frames to the correct
// mojo object when PushFrameToVirtualDevice is called.
// ProducerImpl objects provide an interface for buffer info updates of an
// associated virtual device.
std::map<std::string /*device_id*/, std::shared_ptr<ProducerImpl>>
// Guards against concurrent changes to |device_id_to_producer_map_|.
// TODO( Remove use of locks if possible.
mutable std::mutex device_id_to_producer_map_lock_;
} // namespace mri