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 // static | |
16 media::mojom::VideoFrameDataPtr StructTraits<media::mojom::VideoFrameDataView, | |
17 scoped_refptr<media::VideoFrame>>:: | |
18 data(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 // TODO(sandersd): Should we friend VideoFrame so that we can directly | |
43 // access the |mailbox_holder_| array? | |
44 std::vector<gpu::MailboxHolder> mailbox_holder( | |
45 media::VideoFrame::kMaxPlanes); | |
46 size_t num_planes = media::VideoFrame::NumPlanes(input->format()); | |
47 for (size_t i = 0; i < num_planes; i++) | |
48 mailbox_holder[i] = input->mailbox_holder(i); | |
49 return media::mojom::VideoFrameData::NewMailboxData( | |
50 media::mojom::MailboxVideoFrameData::New(std::move(mailbox_holder))); | |
51 } | |
52 | |
53 NOTREACHED() << "Unsupported VideoFrame conversion"; | |
54 return nullptr; | |
55 } | |
56 | |
57 // static | |
58 bool StructTraits<media::mojom::VideoFrameDataView, | |
59 scoped_refptr<media::VideoFrame>>:: | |
60 Read(media::mojom::VideoFrameDataView input, | |
61 scoped_refptr<media::VideoFrame>* output) { | |
62 // View of the |data| member of the input media::mojom::VideoFrame. | |
63 media::mojom::VideoFrameDataDataView data; | |
64 input.GetDataDataView(&data); | |
65 | |
66 DCHECK_EQ(input.end_of_stream(), data.is_null()); | |
67 if (input.end_of_stream()) { | |
68 *output = media::VideoFrame::CreateEOSFrame(); | |
69 return !!*output; | |
70 } | |
71 | |
72 media::VideoPixelFormat format; | |
73 if (!input.ReadFormat(&format)) | |
74 return false; | |
75 | |
76 gfx::Size coded_size; | |
77 if (!input.ReadCodedSize(&coded_size)) | |
78 return false; | |
79 | |
80 gfx::Rect visible_rect; | |
81 if (!input.ReadVisibleRect(&visible_rect)) | |
82 return false; | |
83 | |
84 // Coded size must contain the visible rect. | |
85 if (visible_rect.right() > coded_size.width() || | |
86 visible_rect.bottom() > coded_size.height()) { | |
87 return false; | |
88 } | |
89 | |
90 gfx::Size natural_size; | |
91 if (!input.ReadNaturalSize(&natural_size)) | |
92 return false; | |
93 | |
94 base::TimeDelta timestamp; | |
95 if (!input.ReadTimestamp(×tamp)) | |
96 return false; | |
97 | |
98 if (data.is_shared_buffer_data()) { | |
99 media::mojom::SharedBufferVideoFrameDataDataView shared_buffer_data; | |
100 data.GetSharedBufferDataDataView(&shared_buffer_data); | |
101 | |
102 // TODO(sandersd): Conversion from uint64_t to size_t could cause | |
103 // corruption. Platform-dependent types should be removed from the | |
104 // implementation (limiting to 32-bit offsets is fine). | |
105 *output = media::MojoSharedBufferVideoFrame::Create( | |
106 format, coded_size, visible_rect, natural_size, | |
107 shared_buffer_data.TakeFrameData(), | |
108 shared_buffer_data.frame_data_size(), shared_buffer_data.y_offset(), | |
109 shared_buffer_data.u_offset(), shared_buffer_data.v_offset(), | |
110 shared_buffer_data.y_stride(), shared_buffer_data.u_stride(), | |
111 shared_buffer_data.v_stride(), timestamp); | |
112 return !!*output; | |
113 } | |
114 | |
115 if (data.is_mailbox_data()) { | |
116 media::mojom::MailboxVideoFrameDataDataView mailbox_data; | |
117 data.GetMailboxDataDataView(&mailbox_data); | |
118 | |
119 std::vector<gpu::MailboxHolder> mailbox_holder; | |
120 if (!mailbox_data.ReadMailboxHolder(&mailbox_holder)) | |
121 return false; | |
122 | |
123 gpu::MailboxHolder mailbox_holder_array[media::VideoFrame::kMaxPlanes]; | |
124 for (size_t i = 0; i < media::VideoFrame::kMaxPlanes; i++) | |
125 mailbox_holder_array[i] = mailbox_holder[i]; | |
126 | |
127 *output = media::VideoFrame::WrapNativeTextures( | |
128 format, mailbox_holder_array, media::VideoFrame::ReleaseMailboxCB(), | |
129 coded_size, visible_rect, natural_size, timestamp); | |
130 return !!*output; | |
131 } | |
132 | |
133 // TODO(sandersd): Switch on the union tag to avoid this ugliness? | |
dcheng
2017/06/08 09:41:08
Hmm... this is OK for now, but maybe we can try us
sandersd (OOO until July 31)
2017/06/08 20:10:09
Lack of a type on the C++ side is the main problem
| |
134 NOTREACHED(); | |
135 return false; | |
136 } | |
137 | |
138 } // namespace mojo | |
OLD | NEW |