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 // TODO(sandersd): Why are all of these *_struct_traits.h includes required, but |
| 8 // no additional deps are? Why are they not required if the code is in |
| 9 // video_frame_struct_traits.h? |
| 10 #include "base/logging.h" |
| 11 #include "gpu/ipc/common/mailbox_holder_struct_traits.h" |
| 12 #include "media/base/ipc/media_param_traits_macros.h" |
| 13 #include "media/mojo/common/mojo_shared_buffer_video_frame.h" |
| 14 #include "mojo/common/common_custom_types_struct_traits.h" |
| 15 #include "ui/gfx/geometry/mojo/geometry_struct_traits.h" |
| 16 #include "ui/gfx/mojo/transform_struct_traits.h" |
| 17 |
| 18 namespace mojo { |
| 19 |
| 20 // static |
| 21 media::mojom::VideoFrameDataPtr StructTraits<media::mojom::VideoFrameDataView, |
| 22 scoped_refptr<media::VideoFrame>>:: |
| 23 data(const scoped_refptr<media::VideoFrame>& input) { |
| 24 // EOS frames contain no data. |
| 25 if (input->metadata()->IsTrue(media::VideoFrameMetadata::END_OF_STREAM)) |
| 26 return nullptr; |
| 27 |
| 28 if (input->storage_type() == media::VideoFrame::STORAGE_MOJO_SHARED_BUFFER) { |
| 29 media::MojoSharedBufferVideoFrame* mojo_frame = |
| 30 static_cast<media::MojoSharedBufferVideoFrame*>(input.get()); |
| 31 |
| 32 mojo::ScopedSharedBufferHandle dup = mojo_frame->Handle().Clone(); |
| 33 DCHECK(dup.is_valid()); |
| 34 |
| 35 return media::mojom::VideoFrameData::NewSharedBufferData( |
| 36 media::mojom::SharedBufferVideoFrameData::New( |
| 37 std::move(dup), mojo_frame->MappedSize(), |
| 38 mojo_frame->stride(media::VideoFrame::kYPlane), |
| 39 mojo_frame->stride(media::VideoFrame::kUPlane), |
| 40 mojo_frame->stride(media::VideoFrame::kVPlane), |
| 41 mojo_frame->PlaneOffset(media::VideoFrame::kYPlane), |
| 42 mojo_frame->PlaneOffset(media::VideoFrame::kUPlane), |
| 43 mojo_frame->PlaneOffset(media::VideoFrame::kVPlane))); |
| 44 } |
| 45 |
| 46 if (input->HasTextures()) { |
| 47 // TODO(sandersd): Should we friend VideoFrame so that we can directly |
| 48 // access the |mailbox_holders_| array? |
| 49 std::vector<gpu::MailboxHolder> mailbox_holder( |
| 50 media::VideoFrame::kMaxPlanes); |
| 51 for (size_t i = 0; i < media::VideoFrame::kMaxPlanes; i++) |
| 52 mailbox_holder.push_back(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 // static |
| 62 bool StructTraits<media::mojom::VideoFrameDataView, |
| 63 scoped_refptr<media::VideoFrame>>:: |
| 64 Read(media::mojom::VideoFrameDataView input, |
| 65 scoped_refptr<media::VideoFrame>* output) { |
| 66 // View of the |data| member of the input media::mojom::VideoFrame. |
| 67 media::mojom::VideoFrameDataDataView data; |
| 68 input.GetDataDataView(&data); |
| 69 |
| 70 DCHECK_EQ(input.end_of_stream(), data.is_null()); |
| 71 if (input.end_of_stream()) { |
| 72 *output = media::VideoFrame::CreateEOSFrame(); |
| 73 return !!*output; |
| 74 } |
| 75 |
| 76 media::VideoPixelFormat format; |
| 77 if (!input.ReadFormat(&format)) |
| 78 return false; |
| 79 |
| 80 gfx::Size coded_size; |
| 81 if (!input.ReadCodedSize(&coded_size)) |
| 82 return false; |
| 83 |
| 84 gfx::Rect visible_rect; |
| 85 if (!input.ReadVisibleRect(&visible_rect)) |
| 86 return false; |
| 87 |
| 88 // Coded size must contain the visible rect. |
| 89 if (visible_rect.right() > coded_size.width() || |
| 90 visible_rect.bottom() > coded_size.height()) { |
| 91 return false; |
| 92 } |
| 93 |
| 94 gfx::Size natural_size; |
| 95 if (!input.ReadNaturalSize(&natural_size)) |
| 96 return false; |
| 97 |
| 98 base::TimeDelta timestamp; |
| 99 if (!input.ReadTimestamp(×tamp)) |
| 100 return false; |
| 101 |
| 102 if (data.is_shared_buffer_data()) { |
| 103 media::mojom::SharedBufferVideoFrameDataDataView shared_buffer_data; |
| 104 data.GetSharedBufferDataDataView(&shared_buffer_data); |
| 105 |
| 106 // TODO(sandersd): Conversion from uint64_t to size_t could cause |
| 107 // corruption. Platform-dependent types should be removed from the |
| 108 // implementation (limiting to 32-bit offsets is fine; if we ever need a |
| 109 // >4GiB VideoFrame I will eat my hat). |
| 110 *output = media::MojoSharedBufferVideoFrame::Create( |
| 111 format, coded_size, visible_rect, natural_size, |
| 112 shared_buffer_data.TakeFrameData(), |
| 113 shared_buffer_data.frame_data_size(), shared_buffer_data.y_offset(), |
| 114 shared_buffer_data.u_offset(), shared_buffer_data.v_offset(), |
| 115 shared_buffer_data.y_stride(), shared_buffer_data.u_stride(), |
| 116 shared_buffer_data.v_stride(), timestamp); |
| 117 return !!*output; |
| 118 } |
| 119 |
| 120 if (data.is_mailbox_data()) { |
| 121 media::mojom::MailboxVideoFrameDataDataView mailbox_data; |
| 122 data.GetMailboxDataDataView(&mailbox_data); |
| 123 |
| 124 std::vector<gpu::MailboxHolder> mailbox_holder; |
| 125 if (!mailbox_data.ReadMailboxHolder(&mailbox_holder)) |
| 126 return false; |
| 127 |
| 128 DCHECK_EQ(mailbox_holder.size(), media::VideoFrame::kMaxPlanes); |
| 129 gpu::MailboxHolder mailbox_holder_array[media::VideoFrame::kMaxPlanes]; |
| 130 for (size_t i = 0; i < media::VideoFrame::kMaxPlanes; i++) |
| 131 mailbox_holder_array[i] = mailbox_holder[i]; |
| 132 |
| 133 *output = media::VideoFrame::WrapNativeTextures( |
| 134 format, mailbox_holder_array, media::VideoFrame::ReleaseMailboxCB(), |
| 135 coded_size, visible_rect, natural_size, timestamp); |
| 136 return !!*output; |
| 137 } |
| 138 |
| 139 // TODO(sandersd): Switch on the union tag to avoid this ugliness? |
| 140 NOTREACHED(); |
| 141 return false; |
| 142 } |
| 143 |
| 144 } // namespace mojo |
OLD | NEW |