1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/mirroring/service/fake_video_capture_host.h"
6
7 #include "base/memory/read_only_shared_memory_region.h"
8 #include "media/base/video_frame.h"
9 #include "media/capture/mojom/video_capture_types.mojom.h"
10
11 namespace mirroring {
12
FakeVideoCaptureHost(mojo::PendingReceiver<media::mojom::VideoCaptureHost> receiver)13 FakeVideoCaptureHost::FakeVideoCaptureHost(
14 mojo::PendingReceiver<media::mojom::VideoCaptureHost> receiver)
15 : receiver_(this, std::move(receiver)) {}
~FakeVideoCaptureHost()16 FakeVideoCaptureHost::~FakeVideoCaptureHost() {}
17
Start(const base::UnguessableToken & device_id,const base::UnguessableToken & session_id,const media::VideoCaptureParams & params,mojo::PendingRemote<media::mojom::VideoCaptureObserver> observer)18 void FakeVideoCaptureHost::Start(
19 const base::UnguessableToken& device_id,
20 const base::UnguessableToken& session_id,
21 const media::VideoCaptureParams& params,
22 mojo::PendingRemote<media::mojom::VideoCaptureObserver> observer) {
23 ASSERT_TRUE(observer);
24 observer_.Bind(std::move(observer));
25 observer_->OnStateChanged(media::mojom::VideoCaptureState::STARTED);
26 }
27
Stop(const base::UnguessableToken & device_id)28 void FakeVideoCaptureHost::Stop(const base::UnguessableToken& device_id) {
29 if (!observer_)
30 return;
31
32 observer_->OnStateChanged(media::mojom::VideoCaptureState::ENDED);
33 observer_.reset();
34 OnStopped();
35 }
36
SendOneFrame(const gfx::Size & size,base::TimeTicks capture_time)37 void FakeVideoCaptureHost::SendOneFrame(const gfx::Size& size,
38 base::TimeTicks capture_time) {
39 if (!observer_)
40 return;
41
42 auto shmem = base::ReadOnlySharedMemoryRegion::Create(5000);
43 memset(shmem.mapping.memory(), 125, 5000);
44 observer_->OnNewBuffer(
45 0, media::mojom::VideoBufferHandle::NewReadOnlyShmemRegion(
46 std::move(shmem.region)));
47 media::VideoFrameMetadata metadata;
48 metadata.frame_rate = 30;
49 metadata.reference_time = capture_time;
50 observer_->OnBufferReady(
51 0, media::mojom::VideoFrameInfo::New(
52 base::TimeDelta(), metadata, media::PIXEL_FORMAT_I420, size,
53 gfx::Rect(size), gfx::ColorSpace::CreateREC709(), nullptr));
54 }
55
56 } // namespace mirroring
57