OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 "media/mojo/interfaces/video_frame_struct_traits.h" | |
6 | |
7 #include <utility> | |
8 #include <vector> | |
9 | |
10 #include "base/logging.h" | |
11 #include "media/mojo/common/mojo_shared_buffer_video_frame.h" | |
12 | |
13 namespace mojo { | |
14 | |
15 namespace { | |
16 | |
17 media::mojom::VideoFrameDataPtr MakeVideoFrameData( | |
18 const scoped_refptr<media::VideoFrame>& input) { | |
19 // EOS frames contain no data. | |
20 if (input->metadata()->IsTrue(media::VideoFrameMetadata::END_OF_STREAM)) | |
21 return nullptr; | |
22 | |
23 if (input->storage_type() == media::VideoFrame::STORAGE_MOJO_SHARED_BUFFER) { | |
24 media::MojoSharedBufferVideoFrame* mojo_frame = | |
25 static_cast<media::MojoSharedBufferVideoFrame*>(input.get()); | |
26 | |
27 mojo::ScopedSharedBufferHandle dup = mojo_frame->Handle().Clone(); | |
28 DCHECK(dup.is_valid()); | |
29 | |
30 return media::mojom::VideoFrameData::NewSharedBufferData( | |
31 media::mojom::SharedBufferVideoFrameData::New( | |
32 std::move(dup), mojo_frame->MappedSize(), | |
33 mojo_frame->stride(media::VideoFrame::kYPlane), | |
34 mojo_frame->stride(media::VideoFrame::kUPlane), | |
35 mojo_frame->stride(media::VideoFrame::kVPlane), | |
36 mojo_frame->PlaneOffset(media::VideoFrame::kYPlane), | |
37 mojo_frame->PlaneOffset(media::VideoFrame::kUPlane), | |
38 mojo_frame->PlaneOffset(media::VideoFrame::kVPlane))); | |
39 } | |
40 | |
41 if (input->HasTextures()) { | |
42 std::vector<gpu::MailboxHolder> mailbox_holder( | |
43 media::VideoFrame::kMaxPlanes); | |
44 size_t num_planes = media::VideoFrame::NumPlanes(input->format()); | |
45 for (size_t i = 0; i < num_planes; i++) | |
46 mailbox_holder[i] = input->mailbox_holder(i); | |
47 return media::mojom::VideoFrameData::NewMailboxData( | |
48 media::mojom::MailboxVideoFrameData::New(std::move(mailbox_holder))); | |
49 } | |
50 | |
51 NOTREACHED() << "Unsupported VideoFrame conversion"; | |
dcheng
2017/06/09 21:52:23
As mentioned, there shouldn't be any CHECKs/DCHECK
sandersd (OOO until July 31)
2017/06/09 22:07:01
I disagree here; we really don't support serializi
dcheng
2017/06/10 00:39:32
OK, I got confused and missed that this was for se
| |
52 return nullptr; | |
53 } | |
54 | |
55 } // namespace | |
56 | |
57 // static | |
58 void* StructTraits<media::mojom::VideoFrameDataView, | |
59 scoped_refptr<media::VideoFrame>>:: | |
60 SetUpContext(const scoped_refptr<media::VideoFrame>& input) { | |
61 return new media::mojom::VideoFrameDataPtr(MakeVideoFrameData(input)); | |
62 } | |
63 | |
64 // static | |
65 void StructTraits<media::mojom::VideoFrameDataView, | |
66 scoped_refptr<media::VideoFrame>>:: | |
67 TearDownContext(const scoped_refptr<media::VideoFrame>& input, | |
68 void* context) { | |
69 delete static_cast<media::mojom::VideoFrameDataPtr*>(context); | |
70 } | |
71 | |
72 // static | |
73 media::mojom::VideoFrameDataPtr& | |
74 StructTraits<media::mojom::VideoFrameDataView, | |
75 scoped_refptr<media::VideoFrame>>:: | |
76 data(const scoped_refptr<media::VideoFrame>& input, void* context) { | |
77 return *static_cast<media::mojom::VideoFrameDataPtr*>(context); | |
78 } | |
79 | |
80 // static | |
81 bool StructTraits<media::mojom::VideoFrameDataView, | |
82 scoped_refptr<media::VideoFrame>>:: | |
83 Read(media::mojom::VideoFrameDataView input, | |
84 scoped_refptr<media::VideoFrame>* output) { | |
85 // View of the |data| member of the input media::mojom::VideoFrame. | |
86 media::mojom::VideoFrameDataDataView data; | |
87 input.GetDataDataView(&data); | |
88 | |
89 DCHECK_EQ(input.end_of_stream(), data.is_null()); | |
dcheng
2017/06/09 21:52:23
Ditto here: this needs to be handled gracefully (w
sandersd (OOO until July 31)
2017/06/09 22:07:01
Yes, in this case we just trust |end_of_stream|, w
dcheng
2017/06/10 00:39:32
But this is deserialization, and a bad process can
sandersd (OOO until July 31)
2017/06/10 01:18:21
Fuzzing false-positives is a very convincing answe
| |
90 if (input.end_of_stream()) { | |
91 *output = media::VideoFrame::CreateEOSFrame(); | |
92 return !!*output; | |
93 } | |
94 | |
95 media::VideoPixelFormat format; | |
96 if (!input.ReadFormat(&format)) | |
97 return false; | |
98 | |
99 gfx::Size coded_size; | |
100 if (!input.ReadCodedSize(&coded_size)) | |
101 return false; | |
102 | |
103 gfx::Rect visible_rect; | |
104 if (!input.ReadVisibleRect(&visible_rect)) | |
105 return false; | |
106 | |
107 // Coded size must contain the visible rect. | |
108 if (visible_rect.right() > coded_size.width() || | |
109 visible_rect.bottom() > coded_size.height()) { | |
110 return false; | |
111 } | |
112 | |
113 gfx::Size natural_size; | |
114 if (!input.ReadNaturalSize(&natural_size)) | |
115 return false; | |
116 | |
117 base::TimeDelta timestamp; | |
118 if (!input.ReadTimestamp(×tamp)) | |
119 return false; | |
120 | |
121 if (data.is_shared_buffer_data()) { | |
122 media::mojom::SharedBufferVideoFrameDataDataView shared_buffer_data; | |
123 data.GetSharedBufferDataDataView(&shared_buffer_data); | |
124 | |
125 // TODO(sandersd): Conversion from uint64_t to size_t could cause | |
126 // corruption. Platform-dependent types should be removed from the | |
127 // implementation (limiting to 32-bit offsets is fine). | |
128 *output = media::MojoSharedBufferVideoFrame::Create( | |
129 format, coded_size, visible_rect, natural_size, | |
130 shared_buffer_data.TakeFrameData(), | |
131 shared_buffer_data.frame_data_size(), shared_buffer_data.y_offset(), | |
132 shared_buffer_data.u_offset(), shared_buffer_data.v_offset(), | |
133 shared_buffer_data.y_stride(), shared_buffer_data.u_stride(), | |
134 shared_buffer_data.v_stride(), timestamp); | |
135 return !!*output; | |
136 } | |
137 | |
138 if (data.is_mailbox_data()) { | |
139 media::mojom::MailboxVideoFrameDataDataView mailbox_data; | |
140 data.GetMailboxDataDataView(&mailbox_data); | |
141 | |
142 std::vector<gpu::MailboxHolder> mailbox_holder; | |
143 if (!mailbox_data.ReadMailboxHolder(&mailbox_holder)) | |
144 return false; | |
145 | |
146 gpu::MailboxHolder mailbox_holder_array[media::VideoFrame::kMaxPlanes]; | |
147 for (size_t i = 0; i < media::VideoFrame::kMaxPlanes; i++) | |
148 mailbox_holder_array[i] = mailbox_holder[i]; | |
149 | |
150 *output = media::VideoFrame::WrapNativeTextures( | |
151 format, mailbox_holder_array, media::VideoFrame::ReleaseMailboxCB(), | |
152 coded_size, visible_rect, natural_size, timestamp); | |
153 return !!*output; | |
154 } | |
155 | |
156 // TODO(sandersd): Switch on the union tag to avoid this ugliness? | |
157 NOTREACHED(); | |
158 return false; | |
159 } | |
160 | |
161 } // namespace mojo | |
OLD | NEW |