Chromium Code Reviews| Index: media/mojo/services/media_type_converters.cc |
| diff --git a/media/mojo/services/media_type_converters.cc b/media/mojo/services/media_type_converters.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0841f1308efb8f078d3b05a807d70c526aa1ebda |
| --- /dev/null |
| +++ b/media/mojo/services/media_type_converters.cc |
| @@ -0,0 +1,100 @@ |
| +// Copyright 2014 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/services/media_type_converters.h" |
| + |
| +#include "base/macros.h" |
| +#include "media/base/buffering_state.h" |
| +#include "media/base/decoder_buffer.h" |
| +#include "mojo/public/cpp/system/data_pipe.h" |
| + |
| +namespace mojo { |
| + |
| +#define ASSERT_ENUM_VALUES_EQUAL(value) \ |
| + COMPILE_ASSERT(media::BUFFERING_##value == \ |
| + static_cast<media::BufferingState>(BUFFERING_STATE_##value), \ |
| + value##_enum_value_matches) |
| + |
| +ASSERT_ENUM_VALUES_EQUAL(HAVE_NOTHING); |
| +ASSERT_ENUM_VALUES_EQUAL(HAVE_ENOUGH); |
| + |
| +MediaDecoderBufferPtr TypeConverter<MediaDecoderBufferPtr, |
| + scoped_refptr<media::DecoderBuffer> >::ConvertFrom( |
| + const scoped_refptr<media::DecoderBuffer> input) { |
| + MediaDecoderBufferPtr mojo_buffer(MediaDecoderBuffer::New()); |
| + mojo_buffer->timestamp = input->timestamp().ToInternalValue(); |
|
xhwang
2014/09/03 00:09:08
Here and below, s/ToInternalValue/InMicroseconds/
tim (not reviewing)
2014/09/03 00:43:50
Ah, good call. Done.
|
| + mojo_buffer->duration = input->duration().ToInternalValue(); |
| + mojo_buffer->data_size = input->data_size(); |
| + mojo_buffer->side_data_size = input->side_data_size(); |
| + mojo_buffer->front_discard_usec = |
| + input->discard_padding().first.ToInternalValue(); |
| + mojo_buffer->back_discard_usec = |
| + input->discard_padding().second.ToInternalValue(); |
| + mojo_buffer->splice_timestamp = input->splice_timestamp().ToInternalValue(); |
| + |
| + // TODO(tim): Assuming this is small so allowing extra copies. |
| + std::vector<uint8> side_data(input->side_data(), |
| + input->side_data() + input->side_data_size()); |
| + mojo_buffer->side_data.Swap(&side_data); |
| + |
| + MojoCreateDataPipeOptions options; |
| + options.struct_size = sizeof(MojoCreateDataPipeOptions); |
| + options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
| + options.element_num_bytes = 1; |
| + options.capacity_num_bytes = input->data_size(); |
| + DataPipe data_pipe(options); |
| + mojo_buffer->data = data_pipe.consumer_handle.Pass(); |
| + |
| + uint32_t num_bytes = input->data_size(); |
| + // TODO(tim): ALL_OR_NONE isn't really appropriate. Check success? |
| + // If fails, we'd still return the buffer, but we'd need to HandleWatch |
| + // to fill the pipe at a later time, which means the de-marshalling code |
| + // needs to wait for a readable pipe (which it currently doesn't). |
| + WriteDataRaw(data_pipe.producer_handle.get(), |
| + input->data(), |
| + &num_bytes, |
| + MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); |
| + return mojo_buffer.Pass(); |
| +} |
| + |
| +scoped_refptr<media::DecoderBuffer> TypeConverter<MediaDecoderBufferPtr, |
| + scoped_refptr<media::DecoderBuffer> >::ConvertTo( |
| + const MediaDecoderBufferPtr& input) { |
| + uint32_t num_bytes = 0; |
| + // TODO(tim): We're assuming that because we always write to the pipe above |
| + // before sending the MediaDecoderBuffer that the pipe is readable when |
| + // we get here. |
| + ReadDataRaw(input->data.get(), NULL, &num_bytes, MOJO_READ_DATA_FLAG_QUERY); |
| + CHECK_EQ(num_bytes, input->data_size) << "Pipe error converting buffer"; |
| + |
| + scoped_ptr<uint8[]> data(new uint8[num_bytes]); // Uninitialized. |
| + ReadDataRaw(input->data.get(), data.get(), &num_bytes, |
| + MOJO_READ_DATA_FLAG_ALL_OR_NONE); |
| + CHECK_EQ(num_bytes, input->data_size) << "Pipe error converting buffer"; |
| + |
| + // TODO(tim): We can't create a media::DecoderBuffer that has side_data |
| + // without copying data because it wants to ensure alignment. Could we |
| + // read directly into a pre-padded DecoderBuffer? |
| + scoped_refptr<media::DecoderBuffer> buffer; |
| + if (input->side_data_size) { |
| + buffer = media::DecoderBuffer::CopyFrom(data.get(), |
| + num_bytes, |
| + input->side_data.storage().data(), |
| + input->side_data_size); |
| + } else { |
| + buffer = media::DecoderBuffer::CopyFrom(data.get(), num_bytes); |
| + } |
| + |
| + buffer->set_timestamp(base::TimeDelta::FromInternalValue(input->timestamp)); |
|
xhwang
2014/09/03 00:09:08
ditto: FromMicroseconds()
tim (not reviewing)
2014/09/03 00:43:50
Done.
|
| + buffer->set_duration(base::TimeDelta::FromInternalValue(input->duration)); |
| + media::DecoderBuffer::DiscardPadding discard_padding( |
| + base::TimeDelta::FromInternalValue(input->front_discard_usec), |
| + base::TimeDelta::FromInternalValue(input->back_discard_usec)); |
| + buffer->set_discard_padding(discard_padding); |
| + buffer->set_splice_timestamp( |
| + base::TimeDelta::FromInternalValue(input->splice_timestamp)); |
| + return buffer; |
| +} |
| + |
| +} // namespace mojo |