0

Use base::span for MojoDataPipeWriter

Change-Id: Ifb32ce89bf9ac1260a898c81bc5a8adf541ddc98
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6271204
Reviewed-by: Nico Weber <thakis@chromium.org>
Reviewed-by: Łukasz Anforowicz <lukasza@chromium.org>
Commit-Queue: Feras Aldahlawi <feras@chromium.org>
Reviewed-by: John Rummell <jrummell@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1423434}
This commit is contained in:
Feras Aldahlawi
2025-02-21 14:57:36 -08:00
committed by Chromium LUCI CQ
parent 985cfbef0d
commit aa7de48e92
7 changed files with 26 additions and 38 deletions

@ -61,7 +61,7 @@ class DecoderBufferProviderImplTest : public testing::Test {
}
void WriteBufferData() {
writer_->Write(kSerializedData.data(), kSerializedData.size(),
writer_->Write(kSerializedData,
base::BindOnce(&DecoderBufferProviderImplTest::OnWriteDone,
base::Unretained(this)));
}

@ -115,9 +115,8 @@ class MojoSenderWrapper {
is_frame_in_flight_ = true;
data_pipe_writer_.Write(
buffer->data(), buffer->size(),
base::BindOnce(&MojoSenderWrapper::OnPipeWriteComplete,
weak_factory_.GetWeakPtr()));
*buffer, base::BindOnce(&MojoSenderWrapper::OnPipeWriteComplete,
weak_factory_.GetWeakPtr()));
stream_sender_->SendFrame(
media::mojom::DecoderBuffer::From(*buffer),
base::BindOnce(&MojoSenderWrapper::OnFrameReadComplete,

@ -53,7 +53,7 @@ class DecoderBufferReaderTest : public testing::Test {
}
void WriteBufferData() {
writer_->Write(serialized_data_.data(), serialized_data_.size(),
writer_->Write(serialized_data_,
base::BindOnce(&DecoderBufferReaderTest::OnWriteDone,
base::Unretained(this)));
}

@ -166,18 +166,16 @@ MojoDataPipeWriter::~MojoDataPipeWriter() {
DVLOG(1) << __func__;
}
void MojoDataPipeWriter::Write(const uint8_t* buffer,
uint32_t buffer_size,
void MojoDataPipeWriter::Write(base::span<const uint8_t> buffer,
DoneCB done_cb) {
DVLOG(3) << __func__;
// Write() can not be called when another writing request is in process.
DCHECK(current_buffer_.empty());
DCHECK(done_cb);
if (!buffer_size) {
if (buffer.empty()) {
std::move(done_cb).Run(true);
return;
}
DCHECK(buffer);
// Cannot write if the pipe is already closed.
if (!producer_handle_.is_valid()) {
@ -187,9 +185,7 @@ void MojoDataPipeWriter::Write(const uint8_t* buffer,
return;
}
// TODO(lukasza): Take `span` instead of `buffer` + `buffer_size`.
current_buffer_ =
UNSAFE_TODO(base::span<const uint8_t>(buffer, size_t{buffer_size}));
current_buffer_ = buffer;
done_cb_ = std::move(done_cb);
// Try writing data immediately to reduce latency.
TryWriteData(MOJO_RESULT_OK);

@ -76,13 +76,13 @@ class MojoDataPipeWriter {
~MojoDataPipeWriter();
using DoneCB = base::OnceCallback<void(bool)>;
// Writes |num_bytes| data from |buffer| into the mojo data pipe. When the
// Writes data from |buffer| into the mojo data pipe. When the
// operation completes, |done_cb| is called and indicates whether the writing
// succeeded. This is not allowed to be called when another writing is
// ongoing. |buffer| needs to be valid for reading during the entire writing
// process. |done_cb| will be called immediately if |num_bytes| is zero or
// the data pipe is closed without doing anything.
void Write(const uint8_t* buffer, uint32_t num_bytes, DoneCB done_cb);
// process. |done_cb| will be called immediately if the size of |bytes| is
// zero or the data pipe is closed without doing anything.
void Write(base::span<const uint8_t> buffer, DoneCB done_cb);
bool IsPipeValid() const;

@ -40,8 +40,7 @@ class MojoDataPipeReadWrite {
reader_ = std::make_unique<MojoDataPipeReader>(std::move(consumer_handle));
}
void WriteAndRead(const uint8_t* buffer,
uint32_t buffer_size,
void WriteAndRead(base::span<const uint8_t> buffer,
bool discard_data = false) {
base::RunLoop run_loop;
base::MockCallback<MojoDataPipeWriter::DoneCB> mock_write_cb;
@ -51,16 +50,17 @@ class MojoDataPipeReadWrite {
EXPECT_CALL(mock_write_cb, Run(true)).Times(1);
EXPECT_CALL(mock_read_cb, Run(true)).Times(1);
writer_->Write(buffer, buffer_size, mock_write_cb.Get());
writer_->Write(buffer, mock_write_cb.Get());
EXPECT_TRUE(read_buffer_.empty());
if (discard_data) {
reader_->Read(nullptr, buffer_size, mock_read_cb.Get());
reader_->Read(nullptr, buffer.size(), mock_read_cb.Get());
run_loop.RunUntilIdle();
} else {
read_buffer_.resize(buffer_size);
reader_->Read(read_buffer_.data(), buffer_size, mock_read_cb.Get());
read_buffer_.resize(buffer.size());
reader_->Read(read_buffer_.data(), buffer.size(), mock_read_cb.Get());
run_loop.RunUntilIdle();
EXPECT_EQ(0, std::memcmp(buffer, read_buffer_.data(), buffer_size));
EXPECT_EQ(0,
std::memcmp(buffer.data(), read_buffer_.data(), buffer.size()));
read_buffer_.clear();
}
}
@ -76,8 +76,7 @@ TEST(MojoDataPipeReadWriteTest, Normal) {
base::test::SingleThreadTaskEnvironment task_environment;
std::string kData = "hello, world";
MojoDataPipeReadWrite pipe_read_write_;
pipe_read_write_.WriteAndRead(reinterpret_cast<const uint8_t*>(kData.data()),
kData.size());
pipe_read_write_.WriteAndRead(base::as_byte_span(kData));
}
TEST(MojoDataPipeReadWriteTest, SequentialReading) {
@ -85,18 +84,15 @@ TEST(MojoDataPipeReadWriteTest, SequentialReading) {
std::string kData1 = "hello, world";
std::string kData2 = "Bye!";
MojoDataPipeReadWrite pipe_read_write_;
pipe_read_write_.WriteAndRead(reinterpret_cast<const uint8_t*>(kData1.data()),
kData1.size());
pipe_read_write_.WriteAndRead(reinterpret_cast<const uint8_t*>(kData2.data()),
kData2.size());
pipe_read_write_.WriteAndRead(base::as_byte_span(kData1));
pipe_read_write_.WriteAndRead(base::as_byte_span(kData2));
}
TEST(MojoDataPipeReadWriteTest, LongerThanCapacity) {
base::test::SingleThreadTaskEnvironment task_environment;
std::string kData = "hello, world, hello, world, hello, world";
MojoDataPipeReadWrite pipe_read_write_(10);
pipe_read_write_.WriteAndRead(reinterpret_cast<const uint8_t*>(kData.data()),
kData.size());
pipe_read_write_.WriteAndRead(base::as_byte_span(kData));
}
TEST(MojoDataPipeReadWriteTest, DiscardDataInPipe) {
@ -104,10 +100,8 @@ TEST(MojoDataPipeReadWriteTest, DiscardDataInPipe) {
std::string kData1 = "to be discarded";
std::string kData2 = "hello, world, hello, world, hello, world";
MojoDataPipeReadWrite pipe_read_write_(10);
pipe_read_write_.WriteAndRead(reinterpret_cast<const uint8_t*>(kData1.data()),
kData1.size(), true);
pipe_read_write_.WriteAndRead(reinterpret_cast<const uint8_t*>(kData2.data()),
kData2.size());
pipe_read_write_.WriteAndRead(base::as_byte_span(kData1), true);
pipe_read_write_.WriteAndRead(base::as_byte_span(kData2));
}
} // namespace media

@ -348,9 +348,8 @@ void DemuxerStreamAdapter::WriteFrame() {
if (!pending_frame_->end_of_stream()) {
data_pipe_writer_.Write(
pending_frame_->data(), pending_frame_->size(),
base::BindOnce(&DemuxerStreamAdapter::OnFrameWritten,
base::Unretained(this)));
*pending_frame_, base::BindOnce(&DemuxerStreamAdapter::OnFrameWritten,
base::Unretained(this)));
} else {
DemuxerStreamAdapter::OnFrameWritten(true);
}