blob: bf1475aaef85254664a2087a2a1b39f09554cf64 [file] [log] [blame]
// Copyright 2017 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.
#ifndef MEDIA_CAPTURE_VIDEO_CHROMEOS_CAMERA_HAL_DISPATCHER_IMPL_H_
#define MEDIA_CAPTURE_VIDEO_CHROMEOS_CAMERA_HAL_DISPATCHER_IMPL_H_
#include <memory>
#include <set>
#include "base/containers/unique_ptr_adapters.h"
#include "base/files/scoped_file.h"
#include "base/memory/singleton.h"
#include "base/threading/thread.h"
#include "components/chromeos_camera/common/jpeg_encode_accelerator.mojom.h"
#include "components/chromeos_camera/common/mjpeg_decode_accelerator.mojom.h"
#include "media/capture/capture_export.h"
#include "media/capture/video/chromeos/mojom/cros_camera_service.mojom.h"
#include "media/capture/video/chromeos/video_capture_device_factory_chromeos.h"
#include "media/capture/video/video_capture_device_factory.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/platform/platform_channel_server_endpoint.h"
namespace base {
class SingleThreadTaskRunner;
class WaitableEvent;
} // namespace base
namespace media {
using MojoJpegEncodeAcceleratorFactoryCB = base::RepeatingCallback<void(
mojo::PendingReceiver<chromeos_camera::mojom::JpegEncodeAccelerator>)>;
class CAPTURE_EXPORT CameraClientObserver {
public:
virtual ~CameraClientObserver();
virtual void OnChannelCreated(
mojo::PendingRemote<cros::mojom::CameraModule> camera_module) = 0;
};
// The CameraHalDispatcherImpl hosts and waits on the unix domain socket
// /var/run/camera3.sock. CameraHalServer and CameraHalClients connect to the
// unix domain socket to create the initial Mojo connections with the
// CameraHalDisptcherImpl, and CameraHalDispatcherImpl then creates and
// dispaches the Mojo channels between CameraHalServer and CameraHalClients to
// establish direct Mojo connections between the CameraHalServer and the
// CameraHalClients.
//
// For general documentation about the CameraHalDispater Mojo interface see the
// comments in mojo/cros_camera_service.mojom.
class CAPTURE_EXPORT CameraHalDispatcherImpl final
: public cros::mojom::CameraHalDispatcher,
public base::trace_event::TraceLog::EnabledStateObserver {
public:
static CameraHalDispatcherImpl* GetInstance();
bool Start(MojoMjpegDecodeAcceleratorFactoryCB jda_factory,
MojoJpegEncodeAcceleratorFactoryCB jea_factory);
void AddClientObserver(std::unique_ptr<CameraClientObserver> observer);
bool IsStarted();
// CameraHalDispatcher implementations.
void RegisterServer(
mojo::PendingRemote<cros::mojom::CameraHalServer> server) final;
void RegisterClient(
mojo::PendingRemote<cros::mojom::CameraHalClient> client) final;
void GetJpegDecodeAccelerator(
mojo::PendingReceiver<chromeos_camera::mojom::MjpegDecodeAccelerator>
jda_receiver) final;
void GetJpegEncodeAccelerator(
mojo::PendingReceiver<chromeos_camera::mojom::JpegEncodeAccelerator>
jea_receiver) final;
// base::trace_event::TraceLog::EnabledStateObserver implementation.
void OnTraceLogEnabled() final;
void OnTraceLogDisabled() final;
private:
friend struct base::DefaultSingletonTraits<CameraHalDispatcherImpl>;
// Allow the test to construct the class directly.
friend class CameraHalDispatcherImplTest;
CameraHalDispatcherImpl();
~CameraHalDispatcherImpl() final;
bool StartThreads();
// Creates the unix domain socket for the camera client processes and the
// camera HALv3 adapter process to connect.
void CreateSocket(base::WaitableEvent* started);
// Waits for incoming connections (from HAL process or from client processes).
// Runs on |blocking_io_thread_|.
void StartServiceLoop(base::ScopedFD socket_fd, base::WaitableEvent* started);
void RegisterClientOnProxyThread(
mojo::PendingRemote<cros::mojom::CameraHalClient> client);
void AddClientObserverOnProxyThread(
std::unique_ptr<CameraClientObserver> observer);
void EstablishMojoChannel(CameraClientObserver* client_observer);
// Handler for incoming Mojo connection on the unix domain socket.
void OnPeerConnected(mojo::ScopedMessagePipeHandle message_pipe);
// Mojo connection error handlers.
void OnCameraHalServerConnectionError();
void OnCameraHalClientConnectionError(CameraClientObserver* client);
void StopOnProxyThread();
void OnTraceLogEnabledOnProxyThread();
void OnTraceLogDisabledOnProxyThread();
base::ScopedFD proxy_fd_;
base::ScopedFD cancel_pipe_;
base::Thread proxy_thread_;
base::Thread blocking_io_thread_;
scoped_refptr<base::SingleThreadTaskRunner> proxy_task_runner_;
scoped_refptr<base::SingleThreadTaskRunner> blocking_io_task_runner_;
mojo::ReceiverSet<cros::mojom::CameraHalDispatcher> receiver_set_;
mojo::Remote<cros::mojom::CameraHalServer> camera_hal_server_;
std::set<std::unique_ptr<CameraClientObserver>, base::UniquePtrComparator>
client_observers_;
MojoMjpegDecodeAcceleratorFactoryCB jda_factory_;
MojoJpegEncodeAcceleratorFactoryCB jea_factory_;
DISALLOW_COPY_AND_ASSIGN(CameraHalDispatcherImpl);
};
} // namespace media
#endif // MEDIA_CAPTURE_VIDEO_CHROMEOS_CAMERA_HAL_DISPATCHER_IMPL_H_