-
Notifications
You must be signed in to change notification settings - Fork 1
/
video_frame_handler_impl.cc
150 lines (127 loc) · 5.11 KB
/
video_frame_handler_impl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// 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_frame_handler_impl.h"
#include <utility>
#include <base/logging.h>
#include <mojo/public/cpp/system/handle.h>
#include <mojo/public/cpp/system/platform_handle.h>
#include "mojom/scoped_access_permission.mojom.h"
#include "mojom/video_capture_types.mojom.h"
namespace mri {
bool VideoFrameHandlerImpl::HasValidCaptureFormat() {
return capture_format_.width_in_pixels() > 0 &&
capture_format_.height_in_pixels() > 0;
}
void VideoFrameHandlerImpl::SetCaptureFormat(const VideoStreamParams& params) {
capture_format_ = params;
}
bool VideoFrameHandlerImpl::CaptureFormatsMatch(
const VideoStreamParams& params) {
return capture_format_.width_in_pixels() == params.width_in_pixels() &&
capture_format_.height_in_pixels() == params.height_in_pixels() &&
capture_format_.frame_rate_in_frames_per_second() ==
params.frame_rate_in_frames_per_second();
}
VideoStreamParams VideoFrameHandlerImpl::GetCaptureFormat() {
return capture_format_;
}
int VideoFrameHandlerImpl::GetFrameHandlerCount() {
return frame_handler_map_.size();
}
int VideoFrameHandlerImpl::AddFrameHandler(
VideoCaptureServiceClient::FrameHandler frame_handler) {
frame_handler_id_counter_++;
frame_handler_map_.insert(
std::make_pair(frame_handler_id_counter_, std::move(frame_handler)));
return frame_handler_id_counter_;
}
bool VideoFrameHandlerImpl::RemoveFrameHandler(int frame_handler_id) {
std::map<int, VideoCaptureServiceClient::FrameHandler>::iterator it =
frame_handler_map_.find(frame_handler_id);
if (it == frame_handler_map_.end()) {
return false;
}
frame_handler_map_.erase(frame_handler_id);
return true;
}
mojo::PendingRemote<video_capture::mojom::VideoFrameHandler>
VideoFrameHandlerImpl::CreateInterfacePendingRemote() {
mojo::PendingRemote<video_capture::mojom::VideoFrameHandler> handler;
receiver_.Bind(handler.InitWithNewPipeAndPassReceiver());
return handler;
}
void VideoFrameHandlerImpl::OnNewBuffer(
int32_t buffer_id, media::mojom::VideoBufferHandlePtr buffer_handle) {
LOG(INFO) << "On new buffer";
CHECK(buffer_handle->is_shared_memory_via_raw_file_descriptor());
base::ScopedPlatformFile platform_file;
MojoResult mojo_result = mojo::UnwrapPlatformFile(
std::move(buffer_handle->get_shared_memory_via_raw_file_descriptor()
->file_descriptor_handle),
&platform_file);
if (mojo_result != MOJO_RESULT_OK) {
LOG(ERROR) << "Failed to unwrap handle: " << mojo_result;
return;
}
base::UnsafeSharedMemoryRegion shm_region =
base::UnsafeSharedMemoryRegion::Deserialize(
base::subtle::PlatformSharedMemoryRegion::Take(
base::ScopedFD(std::move(platform_file)),
base::subtle::PlatformSharedMemoryRegion::Mode::kUnsafe,
buffer_handle->get_shared_memory_via_raw_file_descriptor()
->shared_memory_size_in_bytes,
base::UnguessableToken::Create()));
if (!shm_region.IsValid()) {
LOG(ERROR) << "Failed to unwrap handle to valid shared memory region.";
return;
}
base::WritableSharedMemoryMapping shm_mapping = shm_region.Map();
if (!shm_mapping.IsValid()) {
LOG(ERROR) << "Failed to map shared memory region.";
return;
}
incoming_buffer_id_to_buffer_map_.insert(
std::make_pair(buffer_id, std::move(shm_mapping)));
}
void VideoFrameHandlerImpl::OnFrameReadyInBuffer(
int32_t buffer_id,
int32_t frame_feedback_id,
mojo::PendingRemote<video_capture::mojom::ScopedAccessPermission>
permission,
media::mojom::VideoFrameInfoPtr frame_info) {
base::WritableSharedMemoryMapping* incoming_buffer =
&incoming_buffer_id_to_buffer_map_.at(buffer_id);
// Loop through all the registered frame handlers and push a frame out.
for (auto& entry : frame_handler_map_) {
entry.second(frame_info->timestamp->microseconds,
incoming_buffer->GetMemoryAs<const uint8_t>(),
incoming_buffer->size(), capture_format_.width_in_pixels(),
capture_format_.height_in_pixels());
}
}
void VideoFrameHandlerImpl::OnFrameDropped(
::media::mojom::VideoCaptureFrameDropReason reason) {
LOG(WARNING) << "Got call to OnFrameDropped: " << reason;
}
void VideoFrameHandlerImpl::OnBufferRetired(int32_t buffer_id) {
incoming_buffer_id_to_buffer_map_.erase(buffer_id);
}
// The following methods are not needed to be implementated, as far as we know
// now.
void VideoFrameHandlerImpl::OnError(::media::mojom::VideoCaptureError error) {
LOG(ERROR) << "Got call to OnError: " << error;
}
void VideoFrameHandlerImpl::OnLog(const std::string& message) {
LOG(INFO) << "Got call to OnLog: " << message;
}
void VideoFrameHandlerImpl::OnStarted() {
LOG(INFO) << "Got call to OnStarted";
}
void VideoFrameHandlerImpl::OnStartedUsingGpuDecode() {
LOG(INFO) << "Got call on OnStartedUsingGpuDecode";
}
void VideoFrameHandlerImpl::OnStopped() {
LOG(INFO) << "Got call to OnStopped";
}
} // namespace mri