summaryrefslogtreecommitdiff
path: root/chromium/media/mojo/common
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/media/mojo/common')
-rw-r--r--chromium/media/mojo/common/BUILD.gn20
-rw-r--r--chromium/media/mojo/common/audio_data_s16_converter.cc2
-rw-r--r--chromium/media/mojo/common/audio_data_s16_converter.h2
-rw-r--r--chromium/media/mojo/common/input_error_code_converter.cc2
-rw-r--r--chromium/media/mojo/common/input_error_code_converter.h2
-rw-r--r--chromium/media/mojo/common/media_type_converters.cc2
-rw-r--r--chromium/media/mojo/common/media_type_converters.h2
-rw-r--r--chromium/media/mojo/common/media_type_converters_unittest.cc2
-rw-r--r--chromium/media/mojo/common/mojo_data_pipe_read_write.cc2
-rw-r--r--chromium/media/mojo/common/mojo_data_pipe_read_write.h2
-rw-r--r--chromium/media/mojo/common/mojo_data_pipe_read_write_unittest.cc2
-rw-r--r--chromium/media/mojo/common/mojo_decoder_buffer_converter.cc2
-rw-r--r--chromium/media/mojo/common/mojo_decoder_buffer_converter.h2
-rw-r--r--chromium/media/mojo/common/mojo_decoder_buffer_converter_unittest.cc2
-rw-r--r--chromium/media/mojo/common/mojo_pipe_read_write_util.cc2
-rw-r--r--chromium/media/mojo/common/mojo_pipe_read_write_util.h2
-rw-r--r--chromium/media/mojo/common/mojo_shared_buffer_video_frame.cc288
-rw-r--r--chromium/media/mojo/common/mojo_shared_buffer_video_frame.h109
-rw-r--r--chromium/media/mojo/common/mojo_shared_buffer_video_frame_unittest.cc356
19 files changed, 16 insertions, 787 deletions
diff --git a/chromium/media/mojo/common/BUILD.gn b/chromium/media/mojo/common/BUILD.gn
index a570c550931..ae5303dc1d3 100644
--- a/chromium/media/mojo/common/BUILD.gn
+++ b/chromium/media/mojo/common/BUILD.gn
@@ -1,4 +1,4 @@
-# Copyright 2016 The Chromium Authors. All rights reserved.
+# Copyright 2016 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
@@ -19,7 +19,6 @@ source_set("common") {
]
deps = [
- ":mojo_shared_buffer_video_frame",
"//base",
"//gpu/ipc/common",
"//media",
@@ -31,22 +30,6 @@ source_set("common") {
]
}
-# This is a separate target so that //media/mojo/mojom can depend on it
-# without creating a cycle.
-source_set("mojo_shared_buffer_video_frame") {
- sources = [
- "mojo_shared_buffer_video_frame.cc",
- "mojo_shared_buffer_video_frame.h",
- ]
-
- deps = [
- "//base",
- "//media",
- "//mojo/public/cpp/system",
- "//ui/gfx/geometry",
- ]
-}
-
source_set("unit_tests") {
testonly = true
@@ -54,7 +37,6 @@ source_set("unit_tests") {
"media_type_converters_unittest.cc",
"mojo_data_pipe_read_write_unittest.cc",
"mojo_decoder_buffer_converter_unittest.cc",
- "mojo_shared_buffer_video_frame_unittest.cc",
]
deps = [
diff --git a/chromium/media/mojo/common/audio_data_s16_converter.cc b/chromium/media/mojo/common/audio_data_s16_converter.cc
index 7d1a07b284f..a9f2b5d4b13 100644
--- a/chromium/media/mojo/common/audio_data_s16_converter.cc
+++ b/chromium/media/mojo/common/audio_data_s16_converter.cc
@@ -1,4 +1,4 @@
-// Copyright 2021 The Chromium Authors. All rights reserved.
+// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
diff --git a/chromium/media/mojo/common/audio_data_s16_converter.h b/chromium/media/mojo/common/audio_data_s16_converter.h
index ba18c40605b..d5efb597832 100644
--- a/chromium/media/mojo/common/audio_data_s16_converter.h
+++ b/chromium/media/mojo/common/audio_data_s16_converter.h
@@ -1,4 +1,4 @@
-// Copyright 2021 The Chromium Authors. All rights reserved.
+// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
diff --git a/chromium/media/mojo/common/input_error_code_converter.cc b/chromium/media/mojo/common/input_error_code_converter.cc
index c740d65df63..63a956421fa 100644
--- a/chromium/media/mojo/common/input_error_code_converter.cc
+++ b/chromium/media/mojo/common/input_error_code_converter.cc
@@ -1,4 +1,4 @@
-// Copyright 2021 The Chromium Authors. All rights reserved.
+// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
diff --git a/chromium/media/mojo/common/input_error_code_converter.h b/chromium/media/mojo/common/input_error_code_converter.h
index cd6406f4c5e..99436e75934 100644
--- a/chromium/media/mojo/common/input_error_code_converter.h
+++ b/chromium/media/mojo/common/input_error_code_converter.h
@@ -1,4 +1,4 @@
-// Copyright 2021 The Chromium Authors. All rights reserved.
+// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
diff --git a/chromium/media/mojo/common/media_type_converters.cc b/chromium/media/mojo/common/media_type_converters.cc
index 93377483602..a5ac18beec0 100644
--- a/chromium/media/mojo/common/media_type_converters.cc
+++ b/chromium/media/mojo/common/media_type_converters.cc
@@ -1,4 +1,4 @@
-// Copyright 2014 The Chromium Authors. All rights reserved.
+// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
diff --git a/chromium/media/mojo/common/media_type_converters.h b/chromium/media/mojo/common/media_type_converters.h
index db71a2622c3..d94c1051970 100644
--- a/chromium/media/mojo/common/media_type_converters.h
+++ b/chromium/media/mojo/common/media_type_converters.h
@@ -1,4 +1,4 @@
-// Copyright 2014 The Chromium Authors. All rights reserved.
+// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
diff --git a/chromium/media/mojo/common/media_type_converters_unittest.cc b/chromium/media/mojo/common/media_type_converters_unittest.cc
index 89cfb3591d4..e1ab9298063 100644
--- a/chromium/media/mojo/common/media_type_converters_unittest.cc
+++ b/chromium/media/mojo/common/media_type_converters_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright 2014 The Chromium Authors. All rights reserved.
+// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
diff --git a/chromium/media/mojo/common/mojo_data_pipe_read_write.cc b/chromium/media/mojo/common/mojo_data_pipe_read_write.cc
index 10cdb3694a3..9db02bc7745 100644
--- a/chromium/media/mojo/common/mojo_data_pipe_read_write.cc
+++ b/chromium/media/mojo/common/mojo_data_pipe_read_write.cc
@@ -1,4 +1,4 @@
-// Copyright 2017 The Chromium Authors. All rights reserved.
+// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
diff --git a/chromium/media/mojo/common/mojo_data_pipe_read_write.h b/chromium/media/mojo/common/mojo_data_pipe_read_write.h
index 7c6f9d58d00..2cc975cd5a9 100644
--- a/chromium/media/mojo/common/mojo_data_pipe_read_write.h
+++ b/chromium/media/mojo/common/mojo_data_pipe_read_write.h
@@ -1,4 +1,4 @@
-// Copyright 2017 The Chromium Authors. All rights reserved.
+// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
diff --git a/chromium/media/mojo/common/mojo_data_pipe_read_write_unittest.cc b/chromium/media/mojo/common/mojo_data_pipe_read_write_unittest.cc
index f99cc6da2cf..e7dc4417e59 100644
--- a/chromium/media/mojo/common/mojo_data_pipe_read_write_unittest.cc
+++ b/chromium/media/mojo/common/mojo_data_pipe_read_write_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright 2017 The Chromium Authors. All rights reserved.
+// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
diff --git a/chromium/media/mojo/common/mojo_decoder_buffer_converter.cc b/chromium/media/mojo/common/mojo_decoder_buffer_converter.cc
index b15b859fb7a..c2541e3b0b5 100644
--- a/chromium/media/mojo/common/mojo_decoder_buffer_converter.cc
+++ b/chromium/media/mojo/common/mojo_decoder_buffer_converter.cc
@@ -1,4 +1,4 @@
-// Copyright 2016 The Chromium Authors. All rights reserved.
+// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
diff --git a/chromium/media/mojo/common/mojo_decoder_buffer_converter.h b/chromium/media/mojo/common/mojo_decoder_buffer_converter.h
index 46d3931a089..d09e78cf355 100644
--- a/chromium/media/mojo/common/mojo_decoder_buffer_converter.h
+++ b/chromium/media/mojo/common/mojo_decoder_buffer_converter.h
@@ -1,4 +1,4 @@
-// Copyright 2016 The Chromium Authors. All rights reserved.
+// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
diff --git a/chromium/media/mojo/common/mojo_decoder_buffer_converter_unittest.cc b/chromium/media/mojo/common/mojo_decoder_buffer_converter_unittest.cc
index 7259821aaac..938fc8fdc0f 100644
--- a/chromium/media/mojo/common/mojo_decoder_buffer_converter_unittest.cc
+++ b/chromium/media/mojo/common/mojo_decoder_buffer_converter_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright 2014 The Chromium Authors. All rights reserved.
+// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
diff --git a/chromium/media/mojo/common/mojo_pipe_read_write_util.cc b/chromium/media/mojo/common/mojo_pipe_read_write_util.cc
index 85b6fe9440d..2da6d7c9dcb 100644
--- a/chromium/media/mojo/common/mojo_pipe_read_write_util.cc
+++ b/chromium/media/mojo/common/mojo_pipe_read_write_util.cc
@@ -1,4 +1,4 @@
-// Copyright 2018 The Chromium Authors. All rights reserved.
+// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
diff --git a/chromium/media/mojo/common/mojo_pipe_read_write_util.h b/chromium/media/mojo/common/mojo_pipe_read_write_util.h
index 4d4f8726d0b..991a61f37e4 100644
--- a/chromium/media/mojo/common/mojo_pipe_read_write_util.h
+++ b/chromium/media/mojo/common/mojo_pipe_read_write_util.h
@@ -1,4 +1,4 @@
-// Copyright 2018 The Chromium Authors. All rights reserved.
+// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
diff --git a/chromium/media/mojo/common/mojo_shared_buffer_video_frame.cc b/chromium/media/mojo/common/mojo_shared_buffer_video_frame.cc
deleted file mode 100644
index c19582533b1..00000000000
--- a/chromium/media/mojo/common/mojo_shared_buffer_video_frame.cc
+++ /dev/null
@@ -1,288 +0,0 @@
-// Copyright 2016 The Chromium 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/mojo/common/mojo_shared_buffer_video_frame.h"
-
-#include <utility>
-#include <vector>
-
-#include "base/bind.h"
-#include "base/callback.h"
-#include "base/compiler_specific.h"
-#include "base/logging.h"
-#include "base/memory/shared_memory_mapping.h"
-#include "base/memory/unsafe_shared_memory_region.h"
-#include "base/numerics/safe_conversions.h"
-#include "base/numerics/safe_math.h"
-
-namespace media {
-
-// static
-scoped_refptr<MojoSharedBufferVideoFrame>
-MojoSharedBufferVideoFrame::CreateDefaultForTesting(
- const VideoPixelFormat format,
- const gfx::Size& dimensions,
- base::TimeDelta timestamp) {
- DCHECK(format == PIXEL_FORMAT_I420 || format == PIXEL_FORMAT_NV12);
- const gfx::Rect visible_rect(dimensions);
-
- // Since we're allocating memory for the new frame, pad the requested
- // size if necessary so that the requested size does line up on sample
- // boundaries. See related discussion in VideoFrame::CreateFrameInternal().
- const gfx::Size coded_size = DetermineAlignedSize(format, dimensions);
- if (!IsValidConfig(format, STORAGE_MOJO_SHARED_BUFFER, coded_size,
- visible_rect, dimensions)) {
- LOG(DFATAL) << __func__ << " Invalid config. "
- << ConfigToString(format, STORAGE_MOJO_SHARED_BUFFER,
- dimensions, visible_rect, dimensions);
- return nullptr;
- }
-
- // Allocate a shared memory buffer big enough to hold the desired frame.
- const size_t allocation_size = VideoFrame::AllocationSize(format, coded_size);
- auto region = base::UnsafeSharedMemoryRegion::Create(allocation_size);
- if (!region.IsValid()) {
- DLOG(ERROR) << __func__ << " Unable to allocate memory.";
- return nullptr;
- }
-
- // As both formats are 4:2:0, the U and V (or UV plane) have samples for each
- // 2x2 block.
- DCHECK((coded_size.width() % 2 == 0) && (coded_size.height() % 2 == 0));
-
- if (format == PIXEL_FORMAT_I420) {
- // Create and initialize the frame. As this is I420 format, the U and V
- // planes have samples for each 2x2 block. The memory is laid out as
- // follows:
- // - Yplane, full size (each element represents a 1x1 block)
- // - Uplane, quarter size (each element represents a 2x2 block)
- // - Vplane, quarter size (each element represents a 2x2 block)
- const uint32_t offsets[] = {
- 0 /* y_offset */, static_cast<uint32_t>(coded_size.GetArea()),
- static_cast<uint32_t>(coded_size.GetArea() * 5 / 4)};
- const int32_t strides[] = {coded_size.width(), coded_size.width() / 2,
- coded_size.width() / 2};
- return Create(format, coded_size, visible_rect, dimensions,
- std::move(region), offsets, strides, timestamp);
- } else {
- // |format| is PIXEL_FORMAT_NV12.
- // Create and initialize the frame. As this is NV12 format, the UV plane
- // has interleaved U & V samples for each 2x2 block. The memory is laid out
- // as follows:
- // - Yplane, full size (each element represents a 1x1 block)
- // - UVplane, full width, half height (each pair represents a 2x2 block)
- const uint32_t offsets[] = {0 /* y_offset */,
- static_cast<uint32_t>(coded_size.GetArea())};
- const int32_t strides[] = {coded_size.width(), coded_size.width()};
- return Create(format, coded_size, visible_rect, dimensions,
- std::move(region), offsets, strides, timestamp);
- }
-}
-
-scoped_refptr<MojoSharedBufferVideoFrame>
-MojoSharedBufferVideoFrame::CreateFromYUVFrame(VideoFrame& frame) {
- size_t num_planes = VideoFrame::NumPlanes(frame.format());
- DCHECK_LE(num_planes, 3u);
- DCHECK_GE(num_planes, 2u);
-
- std::vector<uint32_t> offsets(num_planes);
- std::vector<int32_t> strides(num_planes);
- std::vector<size_t> sizes(num_planes);
- size_t aggregate_size = 0;
- for (size_t i = 0; i < num_planes; ++i) {
- strides[i] = frame.stride(i);
- offsets[i] = aggregate_size;
- sizes[i] =
- VideoFrame::Rows(i, frame.format(), frame.coded_size().height()) *
- strides[i];
- aggregate_size += sizes[i];
- }
-
- auto region = base::UnsafeSharedMemoryRegion::Create(aggregate_size);
- if (!region.IsValid()) {
- DLOG(ERROR) << "Can't create new frame backing memory";
- return nullptr;
- }
- base::WritableSharedMemoryMapping dst_mapping = region.Map();
- if (!dst_mapping.IsValid()) {
- DLOG(ERROR) << "Can't create map frame backing memory";
- return nullptr;
- }
-
- // The data from |frame| may not be consecutive between planes. Copy data into
- // a shared memory buffer which is tightly packed. Padding inside each planes
- // are preserved.
- scoped_refptr<MojoSharedBufferVideoFrame> mojo_frame =
- MojoSharedBufferVideoFrame::Create(
- frame.format(), frame.coded_size(), frame.visible_rect(),
- frame.natural_size(), std::move(region), offsets, strides,
- frame.timestamp());
- CHECK(!!mojo_frame);
-
- // If the source memory region is a shared memory region we must map it too.
- base::WritableSharedMemoryMapping src_mapping;
- if (frame.storage_type() == VideoFrame::STORAGE_SHMEM) {
- if (!frame.shm_region()->IsValid()) {
- DLOG(ERROR) << "Invalid source shared memory region";
- return nullptr;
- }
- src_mapping = frame.shm_region()->Map();
- if (!src_mapping.IsValid()) {
- DLOG(ERROR) << "Can't map source shared memory region";
- return nullptr;
- }
- }
-
- // Copy plane data while mappings are in scope.
- for (size_t i = 0; i < num_planes; ++i) {
- memcpy(mojo_frame->shared_buffer_data() + offsets[i],
- static_cast<const void*>(frame.data(i)), sizes[i]);
- }
-
- // TODO(xingliu): Maybe also copy the alpha plane in
- // |MojoSharedBufferVideoFrame|. The alpha plane is ignored here, but
- // the |shared_memory| should contain the space for alpha plane.
-
- return mojo_frame;
-}
-
-// static
-scoped_refptr<MojoSharedBufferVideoFrame> MojoSharedBufferVideoFrame::Create(
- VideoPixelFormat format,
- const gfx::Size& coded_size,
- const gfx::Rect& visible_rect,
- const gfx::Size& natural_size,
- base::UnsafeSharedMemoryRegion region,
- base::span<const uint32_t> offsets,
- base::span<const int32_t> strides,
- base::TimeDelta timestamp) {
- if (!IsValidConfig(format, STORAGE_MOJO_SHARED_BUFFER, coded_size,
- visible_rect, natural_size)) {
- LOG(DFATAL) << __func__ << " Invalid config. "
- << ConfigToString(format, STORAGE_MOJO_SHARED_BUFFER,
- coded_size, visible_rect, natural_size);
- return nullptr;
- }
-
- // Validate that the format has the proper plane count and that it matches the
- // offsets/strides array sizes passed in.
- size_t num_planes = NumPlanes(format);
- if (num_planes != 3 && num_planes != 2) {
- DLOG(ERROR) << __func__ << " " << VideoPixelFormatToString(format)
- << " is not supported; only bi/tri-planar formats are allowed";
- return nullptr;
- }
- if (num_planes != offsets.size() || num_planes != strides.size()) {
- DLOG(ERROR) << __func__ << " offsets and strides length must match number "
- << "of planes";
- return nullptr;
- }
-
- // Validate that the offsets and strides fit in the buffer.
- //
- // We can rely on coded_size.GetArea() being relatively small (compared to the
- // range of an int) due to the IsValidConfig() check above.
- //
- // TODO(sandersd): Allow non-sequential formats.
- const size_t data_size = region.GetSize();
- std::vector<ColorPlaneLayout> planes(num_planes);
- for (size_t i = 0; i < num_planes; ++i) {
- if (strides[i] < 0) {
- DLOG(ERROR) << __func__ << " Invalid stride";
- return nullptr;
- }
-
- // Compute the number of bytes needed on each row.
- const size_t row_bytes = RowBytes(i, format, coded_size.width());
-
- // Safe given sizeof(size_t) >= sizeof(int32_t).
- size_t stride_size_t = strides[i];
- if (stride_size_t < row_bytes) {
- DLOG(ERROR) << __func__ << " Invalid stride";
- return nullptr;
- }
- const size_t rows = Rows(i, format, coded_size.height());
-
- // The last row only needs RowBytes() and not a full stride. This is to
- // avoid problems if the U and V data is interleaved (where |stride| is
- // double the number of bytes actually needed).
- base::CheckedNumeric<size_t> bound = base::CheckAdd(
- offsets[i], base::CheckMul(base::CheckSub(rows, 1), stride_size_t),
- row_bytes);
- if (!bound.IsValid() || bound.ValueOrDie() > data_size) {
- DLOG(ERROR) << __func__ << " Invalid offset";
- return nullptr;
- }
-
- planes[i].stride = strides[i];
- planes[i].offset = offsets[i];
- planes[i].size = i + 1 < num_planes ? offsets[i + 1] - offsets[i]
- : data_size - offsets.back();
- }
-
- auto layout =
- VideoFrameLayout::CreateWithPlanes(format, coded_size, std::move(planes));
- if (!layout) {
- return nullptr;
- }
- // Now allocate the frame and initialize it.
- scoped_refptr<MojoSharedBufferVideoFrame> frame(
- new MojoSharedBufferVideoFrame(*layout, visible_rect, natural_size,
- std::move(region), timestamp));
- if (!frame->Init(offsets)) {
- DLOG(ERROR) << __func__ << " MojoSharedBufferVideoFrame::Init failed.";
- return nullptr;
- }
-
- return frame;
-}
-
-MojoSharedBufferVideoFrame::MojoSharedBufferVideoFrame(
- const VideoFrameLayout& layout,
- const gfx::Rect& visible_rect,
- const gfx::Size& natural_size,
- base::UnsafeSharedMemoryRegion region,
- base::TimeDelta timestamp)
- : VideoFrame(layout,
- STORAGE_MOJO_SHARED_BUFFER,
- visible_rect,
- natural_size,
- timestamp),
- region_(std::move(region)) {
- DCHECK(region_.IsValid());
-}
-
-bool MojoSharedBufferVideoFrame::Init(base::span<const uint32_t> offsets) {
- DCHECK(!mapping_.IsValid());
- mapping_ = region_.Map();
- if (!mapping_.IsValid())
- return false;
- const size_t num_planes = NumPlanes(format());
- DCHECK_EQ(offsets.size(), num_planes);
- for (size_t i = 0; i < num_planes; ++i) {
- offsets_[i] = offsets[i];
- set_data(i, shared_buffer_data() + offsets[i]);
- }
- return true;
-}
-
-MojoSharedBufferVideoFrame::~MojoSharedBufferVideoFrame() {
- // Call |mojo_shared_buffer_done_cb_| to take ownership of
- // |shared_buffer_handle_|.
- if (mojo_shared_buffer_done_cb_) {
- std::move(mojo_shared_buffer_done_cb_).Run(std::move(region_));
- }
-}
-
-size_t MojoSharedBufferVideoFrame::PlaneOffset(size_t plane) const {
- DCHECK(IsValidPlane(format(), plane));
- return offsets_[plane];
-}
-
-void MojoSharedBufferVideoFrame::SetMojoSharedBufferDoneCB(
- MojoSharedBufferDoneCB mojo_shared_buffer_done_cb) {
- mojo_shared_buffer_done_cb_ = std::move(mojo_shared_buffer_done_cb);
-}
-
-} // namespace media
diff --git a/chromium/media/mojo/common/mojo_shared_buffer_video_frame.h b/chromium/media/mojo/common/mojo_shared_buffer_video_frame.h
deleted file mode 100644
index aa1a92f16ed..00000000000
--- a/chromium/media/mojo/common/mojo_shared_buffer_video_frame.h
+++ /dev/null
@@ -1,109 +0,0 @@
-// Copyright 2016 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef MEDIA_MOJO_COMMON_MOJO_SHARED_BUFFER_VIDEO_FRAME_H_
-#define MEDIA_MOJO_COMMON_MOJO_SHARED_BUFFER_VIDEO_FRAME_H_
-
-#include <stddef.h>
-#include <stdint.h>
-
-#include "base/callback_forward.h"
-#include "base/containers/span.h"
-#include "base/memory/ref_counted.h"
-#include "base/memory/shared_memory_mapping.h"
-#include "base/memory/unsafe_shared_memory_region.h"
-#include "media/base/video_frame.h"
-#include "media/base/video_frame_layout.h"
-#include "ui/gfx/geometry/rect.h"
-#include "ui/gfx/geometry/size.h"
-
-namespace media {
-
-// A derived class of media::VideoFrame holding a mojo::SharedBufferHandle
-// which is mapped on constructor and remains so for the lifetime of the
-// object. These frames are ref-counted.
-class MojoSharedBufferVideoFrame : public VideoFrame {
- public:
- // Callback called when this object is destructed. Ownership of the shared
- // memory is transferred to the callee.
- using MojoSharedBufferDoneCB =
- base::OnceCallback<void(base::UnsafeSharedMemoryRegion region)>;
-
- // Creates a new I420 or NV12 frame in shared memory with provided parameters
- // (coded_size() == natural_size() == visible_rect()), or returns nullptr.
- // Buffers for the frame are allocated but not initialized. The caller must
- // not make assumptions about the actual underlying sizes, but check the
- // returned VideoFrame instead. |format| must be either PIXEL_FORMAT_I420 or
- // PIXEL_FORMAT_NV12.
- static scoped_refptr<MojoSharedBufferVideoFrame> CreateDefaultForTesting(
- const VideoPixelFormat format,
- const gfx::Size& dimensions,
- base::TimeDelta timestamp);
-
- // Creates a YUV frame backed by shared memory from in-memory YUV frame.
- // Internally the data from in-memory YUV frame will be copied to a
- // consecutive block in shared memory. Will return null on failure.
- static scoped_refptr<MojoSharedBufferVideoFrame> CreateFromYUVFrame(
- VideoFrame& frame);
-
- // Creates a MojoSharedBufferVideoFrame that uses the memory in |handle|.
- // This will take ownership of |handle|, so the caller can no longer use it.
- // |mojo_shared_buffer_done_cb|, if not null, is called on destruction,
- // and is passed ownership of |handle|. |handle| must be writable. |offsets|
- // and |strides| should be in plane order.
- static scoped_refptr<MojoSharedBufferVideoFrame> Create(
- VideoPixelFormat format,
- const gfx::Size& coded_size,
- const gfx::Rect& visible_rect,
- const gfx::Size& natural_size,
- base::UnsafeSharedMemoryRegion region,
- base::span<const uint32_t> offsets,
- base::span<const int32_t> strides,
- base::TimeDelta timestamp);
-
- MojoSharedBufferVideoFrame(const MojoSharedBufferVideoFrame&) = delete;
- MojoSharedBufferVideoFrame& operator=(const MojoSharedBufferVideoFrame&) =
- delete;
-
- // Returns the offsets relative to the start of the shmem mapping for the
- // |plane| specified.
- size_t PlaneOffset(size_t plane) const;
-
- // Callers can `Duplicate()` the mapping to extend the lifetime of the region.
- const base::UnsafeSharedMemoryRegion& shmem_region() const { return region_; }
-
- // Sets the callback to be called to free the shmem region. If not null,
- // the callback is called when `this` is destroyed, and ownership of
- // `region_` is transferred to it.
- void SetMojoSharedBufferDoneCB(
- MojoSharedBufferDoneCB mojo_shared_buffer_done_cb);
-
- private:
- friend class MojoDecryptorService;
-
- MojoSharedBufferVideoFrame(const VideoFrameLayout& layout,
- const gfx::Rect& visible_rect,
- const gfx::Size& natural_size,
- base::UnsafeSharedMemoryRegion region,
- base::TimeDelta timestamp);
- ~MojoSharedBufferVideoFrame() override;
-
- // Initializes the MojoSharedBufferVideoFrame by creating a mapping onto
- // the shared memory, and then setting offsets as specified.
- bool Init(base::span<const uint32_t> offsets);
-
- uint8_t* shared_buffer_data() { return mapping_.GetMemoryAs<uint8_t>(); }
-
- // WritableSharedMemoryRegion has strict ownership and cannot be cloned. Since
- // the shared memory region may be reused and handed out to a producer
- // multiple times, this must use an UnsafeSharedMemoryRegion instead.
- base::UnsafeSharedMemoryRegion region_;
- base::WritableSharedMemoryMapping mapping_;
- size_t offsets_[kMaxPlanes];
- MojoSharedBufferDoneCB mojo_shared_buffer_done_cb_;
-};
-
-} // namespace media
-
-#endif // MEDIA_MOJO_COMMON_MOJO_SHARED_BUFFER_VIDEO_FRAME_H_
diff --git a/chromium/media/mojo/common/mojo_shared_buffer_video_frame_unittest.cc b/chromium/media/mojo/common/mojo_shared_buffer_video_frame_unittest.cc
deleted file mode 100644
index 18a70f3967b..00000000000
--- a/chromium/media/mojo/common/mojo_shared_buffer_video_frame_unittest.cc
+++ /dev/null
@@ -1,356 +0,0 @@
-// Copyright 2016 The Chromium 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/mojo/common/mojo_shared_buffer_video_frame.h"
-
-#include <stddef.h>
-#include <stdint.h>
-
-#include <string>
-
-#include "base/bind.h"
-#include "base/memory/ref_counted.h"
-#include "base/memory/unsafe_shared_memory_region.h"
-#include "base/test/bind.h"
-#include "base/time/time.h"
-#include "testing/gtest/include/gtest/gtest.h"
-#include "ui/gfx/geometry/rect.h"
-#include "ui/gfx/geometry/size.h"
-
-namespace media {
-
-TEST(MojoSharedBufferVideoFrameTest, CreateFrameWithSharedMemoryI420) {
- const int kWidth = 16;
- const int kHeight = 9;
- const base::TimeDelta kTimestamp = base::Microseconds(1337);
-
- // Create a MojoSharedBufferVideoFrame which will allocate enough space
- // to hold a 16x9 video frame.
- gfx::Size size(kWidth, kHeight);
- scoped_refptr<MojoSharedBufferVideoFrame> frame =
- MojoSharedBufferVideoFrame::CreateDefaultForTesting(PIXEL_FORMAT_I420,
- size, kTimestamp);
- ASSERT_TRUE(frame.get());
-
- // Verify that the correct frame was allocated.
- EXPECT_EQ(media::PIXEL_FORMAT_I420, frame->format());
-
- // The offsets should be set appropriately.
- EXPECT_EQ(frame->PlaneOffset(VideoFrame::kYPlane), 0u);
- EXPECT_GT(frame->PlaneOffset(VideoFrame::kUPlane), 0u);
- EXPECT_GT(frame->PlaneOffset(VideoFrame::kVPlane), 0u);
-
- // The strides should be set appropriately.
- EXPECT_EQ(frame->stride(VideoFrame::kYPlane), kWidth);
- EXPECT_EQ(frame->stride(VideoFrame::kUPlane), kWidth / 2);
- EXPECT_EQ(frame->stride(VideoFrame::kVPlane), kWidth / 2);
-
- // The data pointers for each plane should be set.
- EXPECT_TRUE(frame->data(VideoFrame::kYPlane));
- EXPECT_TRUE(frame->data(VideoFrame::kUPlane));
- EXPECT_TRUE(frame->data(VideoFrame::kVPlane));
-}
-
-TEST(MojoSharedBufferVideoFrameTest, CreateFrameWithSharedMemoryNV12) {
- const int kWidth = 16;
- const int kHeight = 9;
- const base::TimeDelta kTimestamp = base::Microseconds(1337);
-
- // Create a MojoSharedBufferVideoFrame which will allocate enough space
- // to hold a 16x9 video frame.
- gfx::Size size(kWidth, kHeight);
- scoped_refptr<MojoSharedBufferVideoFrame> frame =
- MojoSharedBufferVideoFrame::CreateDefaultForTesting(PIXEL_FORMAT_NV12,
- size, kTimestamp);
- ASSERT_TRUE(frame.get());
-
- // Verify that the correct frame was allocated.
- EXPECT_EQ(media::PIXEL_FORMAT_NV12, frame->format());
-
- // The offsets should be set appropriately.
- EXPECT_EQ(frame->PlaneOffset(VideoFrame::kYPlane), 0u);
- EXPECT_GT(frame->PlaneOffset(VideoFrame::kUVPlane), 0u);
-
- // The strides should be set appropriately.
- EXPECT_EQ(frame->stride(VideoFrame::kYPlane), kWidth);
- EXPECT_EQ(frame->stride(VideoFrame::kUVPlane), kWidth);
-
- // The data pointers for each plane should be set.
- EXPECT_TRUE(frame->data(VideoFrame::kYPlane));
- EXPECT_TRUE(frame->data(VideoFrame::kUVPlane));
-}
-
-TEST(MojoSharedBufferVideoFrameTest, CreateFrameAndPassSharedMemoryI420) {
- const int kWidth = 32;
- const int kHeight = 18;
- const base::TimeDelta kTimestamp = base::Microseconds(1338);
-
- // Some random values to use. Since we actually don't use the data inside the
- // frame, random values are fine (as long as the offsets are within the
- // memory size allocated).
- const VideoPixelFormat format = PIXEL_FORMAT_I420;
- const size_t y_offset = kWidth * 2;
- const size_t u_offset = kWidth * 3;
- const size_t v_offset = kWidth * 5;
- const int32_t y_stride = kWidth;
- const int32_t u_stride = kWidth - 1;
- const int32_t v_stride = kWidth - 2;
-
- // Allocate some shared memory.
- gfx::Size size(kWidth, kHeight);
- gfx::Rect visible_rect(size);
- size_t requested_size = VideoFrame::AllocationSize(format, size);
- ASSERT_LT(y_offset, requested_size);
- auto region = base::UnsafeSharedMemoryRegion::Create(requested_size);
- ASSERT_TRUE(region.IsValid());
-
- // Allocate frame.
- const uint32_t offsets[] = {y_offset, u_offset, v_offset};
- const int32_t strides[] = {y_stride, u_stride, v_stride};
- scoped_refptr<MojoSharedBufferVideoFrame> frame =
- MojoSharedBufferVideoFrame::Create(format, size, visible_rect, size,
- std::move(region), offsets, strides,
- kTimestamp);
- ASSERT_TRUE(frame.get());
- EXPECT_EQ(frame->format(), format);
-
- // The offsets should be set appropriately.
- EXPECT_EQ(frame->PlaneOffset(VideoFrame::kYPlane), y_offset);
- EXPECT_EQ(frame->PlaneOffset(VideoFrame::kUPlane), u_offset);
- EXPECT_EQ(frame->PlaneOffset(VideoFrame::kVPlane), v_offset);
-
- // The strides should be set appropriately.
- EXPECT_EQ(frame->stride(VideoFrame::kYPlane), y_stride);
- EXPECT_EQ(frame->stride(VideoFrame::kUPlane), u_stride);
- EXPECT_EQ(frame->stride(VideoFrame::kVPlane), v_stride);
-
- // The data pointers for each plane should be set.
- EXPECT_TRUE(frame->data(VideoFrame::kYPlane));
- EXPECT_TRUE(frame->data(VideoFrame::kUPlane));
- EXPECT_TRUE(frame->data(VideoFrame::kVPlane));
-}
-
-TEST(MojoSharedBufferVideoFrameTest, CreateFrameAndPassSharedMemoryNV12) {
- const int kWidth = 32;
- const int kHeight = 18;
- const base::TimeDelta kTimestamp = base::Microseconds(1338);
-
- // Some random values to use. Since we actually don't use the data inside the
- // frame, random values are fine (as long as the offsets are within the
- // memory size allocated).
- const VideoPixelFormat format = PIXEL_FORMAT_NV12;
- const size_t y_offset = kWidth * 2;
- const size_t uv_offset = kWidth * 3;
- const int32_t y_stride = kWidth;
- const int32_t uv_stride = kWidth + 1;
-
- // Allocate some shared memory.
- gfx::Size size(kWidth, kHeight);
- gfx::Rect visible_rect(size);
- size_t requested_size = VideoFrame::AllocationSize(format, size);
- ASSERT_LT(y_offset, requested_size);
- auto region = base::UnsafeSharedMemoryRegion::Create(requested_size);
- ASSERT_TRUE(region.IsValid());
-
- // Allocate frame.
- const uint32_t offsets[] = {y_offset, uv_offset};
- const int32_t strides[] = {y_stride, uv_stride};
- scoped_refptr<MojoSharedBufferVideoFrame> frame =
- MojoSharedBufferVideoFrame::Create(format, size, visible_rect, size,
- std::move(region), offsets, strides,
- kTimestamp);
- ASSERT_TRUE(frame.get());
- EXPECT_EQ(frame->format(), format);
-
- // The offsets should be set appropriately.
- EXPECT_EQ(frame->PlaneOffset(VideoFrame::kYPlane), y_offset);
- EXPECT_EQ(frame->PlaneOffset(VideoFrame::kUVPlane), uv_offset);
-
- // The strides should be set appropriately.
- EXPECT_EQ(frame->stride(VideoFrame::kYPlane), y_stride);
- EXPECT_EQ(frame->stride(VideoFrame::kUVPlane), uv_stride);
-
- // The data pointers for each plane should be set.
- EXPECT_TRUE(frame->data(VideoFrame::kYPlane));
- EXPECT_TRUE(frame->data(VideoFrame::kUVPlane));
-}
-
-TEST(MojoSharedBufferVideoFrameTest, CreateFrameOddWidth) {
- const int kWidth = 15;
- const int kHeight = 9;
- const base::TimeDelta kTimestamp = base::Microseconds(1337);
-
- VideoPixelFormat formats[] = {PIXEL_FORMAT_I420, PIXEL_FORMAT_NV12};
- for (auto format : formats) {
- // Create a MojoSharedBufferVideoFrame which will allocate enough space
- // to hold the video frame. Size should be adjusted.
- gfx::Size size(kWidth, kHeight);
- scoped_refptr<MojoSharedBufferVideoFrame> frame =
- MojoSharedBufferVideoFrame::CreateDefaultForTesting(format, size,
- kTimestamp);
- ASSERT_TRUE(frame.get());
-
- // Verify that the correct frame was allocated.
- EXPECT_EQ(format, frame->format());
-
- // The size should be >= 15x9.
- EXPECT_GE(frame->coded_size().width(), kWidth);
- EXPECT_GE(frame->coded_size().height(), kHeight);
- }
-}
-
-TEST(MojoSharedBufferVideoFrameTest, TestDestructionCallback) {
- const VideoPixelFormat format = PIXEL_FORMAT_I420;
- const int kWidth = 32;
- const int kHeight = 18;
- const base::TimeDelta kTimestamp = base::Microseconds(1338);
-
- // Allocate some shared memory.
- gfx::Size size(kWidth, kHeight);
- gfx::Rect visible_rect(size);
- size_t requested_size = VideoFrame::AllocationSize(format, size);
- auto region = base::UnsafeSharedMemoryRegion::Create(requested_size);
- ASSERT_TRUE(region.IsValid());
-
- const char kTestData[] = "reduce reuse recycle";
- {
- base::WritableSharedMemoryMapping mapping = region.Map();
- ASSERT_GT(mapping.size(), strlen(kTestData));
- // Note: deliberately using sizeof() to include the null terminator.
- memcpy(mapping.memory(), kTestData, sizeof(kTestData));
- }
-
- // Allocate frame.
- const uint32_t kOffsets[] = {0, 0, 0};
- const int32_t kStrides[] = {kWidth, kWidth, kWidth};
- scoped_refptr<MojoSharedBufferVideoFrame> frame =
- MojoSharedBufferVideoFrame::Create(format, size, visible_rect, size,
- std::move(region), kOffsets, kStrides,
- kTimestamp);
- ASSERT_TRUE(frame.get());
- EXPECT_EQ(frame->format(), format);
-
- // Set the destruction callback.
- bool callback_called = false;
- auto destruction_cb =
- base::BindLambdaForTesting([&](base::UnsafeSharedMemoryRegion region) {
- callback_called = true;
- ASSERT_EQ(requested_size, region.GetSize());
- // Unsafe regions are always mapped as writable.
- base::WritableSharedMemoryMapping mapping = region.Map();
- // Check that the test data that was written there is still there as a
- // proxy signal for checking that ownership of the shmem region has been
- // transferred.
- EXPECT_STREQ(kTestData, mapping.GetMemoryAs<char>());
- });
- frame->SetMojoSharedBufferDoneCB(std::move(destruction_cb));
- EXPECT_FALSE(callback_called);
-
- // Force destruction of |frame|.
- frame = nullptr;
- EXPECT_TRUE(callback_called);
-}
-
-TEST(MojoSharedBufferVideoFrameTest, InterleavedData) {
- const VideoPixelFormat format = PIXEL_FORMAT_I420;
- const int kWidth = 32;
- const int kHeight = 18;
- const base::TimeDelta kTimestamp = base::Microseconds(1338);
- gfx::Size size(kWidth, kHeight);
- gfx::Rect visible_rect(size);
-
- // Create interlaced UV data, which are each 1/4 the size of the Y data.
- const uint32_t y_offset = 0;
- const uint32_t u_offset =
- VideoFrame::PlaneSize(format, VideoFrame::kYPlane, size).GetArea();
- const uint32_t v_offset =
- u_offset + VideoFrame::RowBytes(VideoFrame::kUPlane, format, kWidth);
- const int32_t y_stride =
- VideoFrame::RowBytes(VideoFrame::kYPlane, format, kWidth);
- const int32_t u_stride = y_stride;
- const int32_t v_stride = y_stride;
-
- // Allocate some shared memory.
- size_t requested_size = VideoFrame::AllocationSize(format, size);
- auto region = base::UnsafeSharedMemoryRegion::Create(requested_size);
- ASSERT_TRUE(region.IsValid());
-
- // Allocate frame.
- const uint32_t kOffsets[] = {y_offset, u_offset, v_offset};
- const int32_t kStrides[] = {y_stride, u_stride, v_stride};
- scoped_refptr<MojoSharedBufferVideoFrame> frame =
- MojoSharedBufferVideoFrame::Create(format, size, visible_rect, size,
- std::move(region), kOffsets, kStrides,
- kTimestamp);
- ASSERT_TRUE(frame.get());
- EXPECT_EQ(frame->format(), format);
-
- // The offsets should be set appropriately.
- EXPECT_EQ(frame->PlaneOffset(VideoFrame::kYPlane), y_offset);
- EXPECT_EQ(frame->PlaneOffset(VideoFrame::kUPlane), u_offset);
- EXPECT_EQ(frame->PlaneOffset(VideoFrame::kVPlane), v_offset);
-
- // The strides should be set appropriately.
- EXPECT_EQ(frame->stride(VideoFrame::kYPlane), y_stride);
- EXPECT_EQ(frame->stride(VideoFrame::kUPlane), u_stride);
- EXPECT_EQ(frame->stride(VideoFrame::kVPlane), v_stride);
-
- // The data pointers for each plane should be set.
- EXPECT_TRUE(frame->data(VideoFrame::kYPlane));
- EXPECT_TRUE(frame->data(VideoFrame::kUPlane));
- EXPECT_TRUE(frame->data(VideoFrame::kVPlane));
-}
-
-TEST(MojoSharedBufferVideoFrameTest, I420FrameToMojoFrame) {
- std::vector<uint8_t> data = std::vector<uint8_t>(12, 1u);
- const auto pixel_format = VideoPixelFormat::PIXEL_FORMAT_I420;
- const auto size = gfx::Size(1, 1);
- const int32_t stride = 3;
-
- // The YUV frame only has 1 pixel. But each plane are not in consecutive
- // memory block, also stride is 3 bytes that contains 1 byte image data and 2
- // bytes padding.
- scoped_refptr<VideoFrame> frame = VideoFrame::WrapExternalYuvData(
- pixel_format, size, gfx::Rect(1, 1), size, stride, stride, stride,
- &data[0], &data[4], &data[8], base::TimeDelta());
- auto mojo_frame = MojoSharedBufferVideoFrame::CreateFromYUVFrame(*frame);
- EXPECT_TRUE(mojo_frame);
-
- const size_t y_stride = frame->stride(VideoFrame::kYPlane);
- const size_t u_stride = frame->stride(VideoFrame::kUPlane);
-
- // Verifies mapped size and offset.
- EXPECT_EQ(mojo_frame->shmem_region().GetSize(),
- static_cast<size_t>(3 * stride));
- EXPECT_EQ(mojo_frame->PlaneOffset(VideoFrame::kYPlane), 0u);
- EXPECT_EQ(mojo_frame->PlaneOffset(VideoFrame::kUPlane), y_stride);
- EXPECT_EQ(mojo_frame->PlaneOffset(VideoFrame::kVPlane), y_stride + u_stride);
-}
-
-TEST(MojoSharedBufferVideoFrameTest, NV12FrameToMojoFrame) {
- std::vector<uint8_t> data = std::vector<uint8_t>(12, 1u);
- const auto pixel_format = VideoPixelFormat::PIXEL_FORMAT_NV12;
- const auto size = gfx::Size(1, 1);
- const int32_t stride = 3;
-
- // The YUV frame only has 1 pixel. But each plane are not in consecutive
- // memory block, also stride is 3 bytes that contains 1 byte image data and 2
- // bytes padding in Y plane and 2 bytes image data and 1 byte padding in UV
- // plane.
- scoped_refptr<VideoFrame> frame = VideoFrame::WrapExternalYuvData(
- pixel_format, size, gfx::Rect(1, 1), size, stride, stride, stride,
- &data[0], &data[4], &data[4], base::TimeDelta());
- auto mojo_frame = MojoSharedBufferVideoFrame::CreateFromYUVFrame(*frame);
- EXPECT_TRUE(mojo_frame);
-
- const size_t y_stride = frame->stride(VideoFrame::kYPlane);
-
- // Verifies mapped size and offset.
- EXPECT_EQ(mojo_frame->shmem_region().GetSize(),
- static_cast<size_t>(2 * stride));
- EXPECT_EQ(mojo_frame->PlaneOffset(VideoFrame::kYPlane), 0u);
- EXPECT_EQ(mojo_frame->PlaneOffset(VideoFrame::kUVPlane), y_stride);
-}
-
-} // namespace media