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 struct VideoFrameStructTraitsContext { | |
18 // Tracked separately because |data| can be null. | |
19 bool has_data = false; | |
20 media::mojom::VideoFrameDataPtr data; | |
21 }; | |
22 | |
23 media::mojom::VideoFrameDataPtr MakeVideoFrameData( | |
24 const scoped_refptr<media::VideoFrame>& input) { | |
25 // EOS frames contain no data. | |
26 if (input->metadata()->IsTrue(media::VideoFrameMetadata::END_OF_STREAM)) | |
27 return nullptr; | |
28 | |
29 if (input->storage_type() == media::VideoFrame::STORAGE_MOJO_SHARED_BUFFER) { | |
30 media::MojoSharedBufferVideoFrame* mojo_frame = | |
31 static_cast<media::MojoSharedBufferVideoFrame*>(input.get()); | |
32 | |
33 mojo::ScopedSharedBufferHandle dup = mojo_frame->Handle().Clone(); | |
34 DCHECK(dup.is_valid()); | |
35 | |
36 return media::mojom::VideoFrameData::NewSharedBufferData( | |
37 media::mojom::SharedBufferVideoFrameData::New( | |
38 std::move(dup), mojo_frame->MappedSize(), | |
39 mojo_frame->stride(media::VideoFrame::kYPlane), | |
40 mojo_frame->stride(media::VideoFrame::kUPlane), | |
41 mojo_frame->stride(media::VideoFrame::kVPlane), | |
42 mojo_frame->PlaneOffset(media::VideoFrame::kYPlane), | |
43 mojo_frame->PlaneOffset(media::VideoFrame::kUPlane), | |
44 mojo_frame->PlaneOffset(media::VideoFrame::kVPlane))); | |
45 } | |
46 | |
47 if (input->HasTextures()) { | |
48 std::vector<gpu::MailboxHolder> mailbox_holder( | |
49 media::VideoFrame::kMaxPlanes); | |
50 size_t num_planes = media::VideoFrame::NumPlanes(input->format()); | |
51 for (size_t i = 0; i < num_planes; i++) | |
52 mailbox_holder[i] = input->mailbox_holder(i); | |
53 return media::mojom::VideoFrameData::NewMailboxData( | |
54 media::mojom::MailboxVideoFrameData::New(std::move(mailbox_holder))); | |
55 } | |
56 | |
57 NOTREACHED() << "Unsupported VideoFrame conversion"; | |
58 return nullptr; | |
59 } | |
60 | |
61 } // namespace | |
62 | |
63 // static | |
64 void* StructTraits<media::mojom::VideoFrameDataView, | |
65 scoped_refptr<media::VideoFrame>>:: | |
66 SetUpContext(const scoped_refptr<media::VideoFrame>& input) { | |
67 return new VideoFrameStructTraitsContext(); | |
68 } | |
69 | |
70 // static | |
71 void StructTraits<media::mojom::VideoFrameDataView, | |
72 scoped_refptr<media::VideoFrame>>:: | |
73 TearDownContext(const scoped_refptr<media::VideoFrame>& input, | |
74 void* context) { | |
75 delete static_cast<VideoFrameStructTraitsContext*>(context); | |
76 } | |
77 | |
78 // static | |
79 media::mojom::VideoFrameDataPtr& | |
80 StructTraits<media::mojom::VideoFrameDataView, | |
81 scoped_refptr<media::VideoFrame>>:: | |
82 data(const scoped_refptr<media::VideoFrame>& input, void* context) { | |
83 VideoFrameStructTraitsContext* ctx = | |
84 static_cast<VideoFrameStructTraitsContext*>(context); | |
85 | |
86 if (!ctx->has_data) { | |
dcheng
2017/06/08 20:43:15
Rather than using has_data, just call MakeVideoFra
| |
87 ctx->data = MakeVideoFrameData(input); | |
88 ctx->has_data = true; | |
89 } | |
90 | |
91 return ctx->data; | |
92 } | |
93 | |
94 // static | |
95 bool StructTraits<media::mojom::VideoFrameDataView, | |
96 scoped_refptr<media::VideoFrame>>:: | |
97 Read(media::mojom::VideoFrameDataView input, | |
98 scoped_refptr<media::VideoFrame>* output) { | |
99 // View of the |data| member of the input media::mojom::VideoFrame. | |
100 media::mojom::VideoFrameDataDataView data; | |
101 input.GetDataDataView(&data); | |
102 | |
103 DCHECK_EQ(input.end_of_stream(), data.is_null()); | |
104 if (input.end_of_stream()) { | |
105 *output = media::VideoFrame::CreateEOSFrame(); | |
106 return !!*output; | |
107 } | |
108 | |
109 media::VideoPixelFormat format; | |
110 if (!input.ReadFormat(&format)) | |
111 return false; | |
112 | |
113 gfx::Size coded_size; | |
114 if (!input.ReadCodedSize(&coded_size)) | |
115 return false; | |
116 | |
117 gfx::Rect visible_rect; | |
118 if (!input.ReadVisibleRect(&visible_rect)) | |
119 return false; | |
120 | |
121 // Coded size must contain the visible rect. | |
122 if (visible_rect.right() > coded_size.width() || | |
123 visible_rect.bottom() > coded_size.height()) { | |
124 return false; | |
125 } | |
126 | |
127 gfx::Size natural_size; | |
128 if (!input.ReadNaturalSize(&natural_size)) | |
129 return false; | |
130 | |
131 base::TimeDelta timestamp; | |
132 if (!input.ReadTimestamp(×tamp)) | |
133 return false; | |
134 | |
135 if (data.is_shared_buffer_data()) { | |
136 media::mojom::SharedBufferVideoFrameDataDataView shared_buffer_data; | |
137 data.GetSharedBufferDataDataView(&shared_buffer_data); | |
138 | |
139 // TODO(sandersd): Conversion from uint64_t to size_t could cause | |
140 // corruption. Platform-dependent types should be removed from the | |
141 // implementation (limiting to 32-bit offsets is fine). | |
142 *output = media::MojoSharedBufferVideoFrame::Create( | |
143 format, coded_size, visible_rect, natural_size, | |
144 shared_buffer_data.TakeFrameData(), | |
145 shared_buffer_data.frame_data_size(), shared_buffer_data.y_offset(), | |
146 shared_buffer_data.u_offset(), shared_buffer_data.v_offset(), | |
147 shared_buffer_data.y_stride(), shared_buffer_data.u_stride(), | |
148 shared_buffer_data.v_stride(), timestamp); | |
149 return !!*output; | |
150 } | |
151 | |
152 if (data.is_mailbox_data()) { | |
153 media::mojom::MailboxVideoFrameDataDataView mailbox_data; | |
154 data.GetMailboxDataDataView(&mailbox_data); | |
155 | |
156 std::vector<gpu::MailboxHolder> mailbox_holder; | |
157 if (!mailbox_data.ReadMailboxHolder(&mailbox_holder)) | |
158 return false; | |
159 | |
160 gpu::MailboxHolder mailbox_holder_array[media::VideoFrame::kMaxPlanes]; | |
161 for (size_t i = 0; i < media::VideoFrame::kMaxPlanes; i++) | |
162 mailbox_holder_array[i] = mailbox_holder[i]; | |
163 | |
164 *output = media::VideoFrame::WrapNativeTextures( | |
165 format, mailbox_holder_array, media::VideoFrame::ReleaseMailboxCB(), | |
166 coded_size, visible_rect, natural_size, timestamp); | |
167 return !!*output; | |
168 } | |
169 | |
170 // TODO(sandersd): Switch on the union tag to avoid this ugliness? | |
171 NOTREACHED(); | |
172 return false; | |
173 } | |
174 | |
175 } // namespace mojo | |
OLD | NEW |