| 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/services/mojo_data_source_adapter.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/callback_helpers.h" |
| 12 #include "base/numerics/safe_conversions.h" |
| 13 #include "media/base/data_buffer.h" |
| 14 #include "media/mojo/common/media_type_converters.h" |
| 15 #include "media/mojo/common/mojo_data_buffer_converter.h" |
| 16 #include "mojo/public/cpp/system/data_pipe.h" |
| 17 |
| 18 namespace media { |
| 19 |
| 20 MojoDataSourceAdapter::MojoDataSourceAdapter( |
| 21 mojom::DataSourcePtr data_source, |
| 22 const base::Closure& data_source_ready_cb) |
| 23 : data_source_(std::move(data_source)), |
| 24 task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 25 weak_factory_(this) { |
| 26 DVLOG(1) << __func__; |
| 27 data_source_->Initialize(base::Bind(&MojoDataSourceAdapter::OnDataSourceReady, |
| 28 weak_factory_.GetWeakPtr(), |
| 29 data_source_ready_cb)); |
| 30 } |
| 31 |
| 32 MojoDataSourceAdapter::~MojoDataSourceAdapter() { |
| 33 DVLOG(1) << __func__; |
| 34 } |
| 35 |
| 36 void MojoDataSourceAdapter::Read(int64_t position, |
| 37 int size, |
| 38 uint8_t* data, |
| 39 const DataSource::ReadCB& read_cb) { |
| 40 DVLOG(3) << __func__; |
| 41 |
| 42 data_source_->Read(position, size, |
| 43 base::Bind(&MojoDataSourceAdapter::OnDataSourceRead, |
| 44 weak_factory_.GetWeakPtr(), data, read_cb)); |
| 45 } |
| 46 |
| 47 void MojoDataSourceAdapter::Stop() { |
| 48 data_source_->Stop(); |
| 49 } |
| 50 |
| 51 void MojoDataSourceAdapter::Abort() { |
| 52 data_source_->Abort(); |
| 53 } |
| 54 |
| 55 bool MojoDataSourceAdapter::GetSize(int64_t* size_out) { |
| 56 bool success = false; |
| 57 |
| 58 if (!task_runner_->BelongsToCurrentThread()) { |
| 59 std::unique_lock<std::mutex> lock(mutex_); |
| 60 task_runner_->PostTask( |
| 61 FROM_HERE, base::Bind(&MojoDataSourceAdapter::GetSizeInternal, |
| 62 weak_factory_.GetWeakPtr(), &success, size_out)); |
| 63 condition_.wait(lock); |
| 64 } else { |
| 65 MojoDataSourceAdapter::GetSizeInternal(&success, size_out); |
| 66 } |
| 67 |
| 68 return success; |
| 69 } |
| 70 |
| 71 bool MojoDataSourceAdapter::IsStreaming() { |
| 72 CHECK(task_runner_->BelongsToCurrentThread()); |
| 73 bool is_streaming = false; |
| 74 data_source_->IsStreaming(&is_streaming); |
| 75 return is_streaming; |
| 76 } |
| 77 |
| 78 void MojoDataSourceAdapter::SetBitrate(int bitrate) { |
| 79 data_source_->SetBitrate(bitrate); |
| 80 } |
| 81 |
| 82 void MojoDataSourceAdapter::OnDataSourceReady( |
| 83 const base::Closure& data_source_ready_cb, |
| 84 mojo::ScopedDataPipeConsumerHandle consumer_handle) { |
| 85 DVLOG(1) << __func__; |
| 86 DCHECK(consumer_handle.is_valid()); |
| 87 |
| 88 mojo_data_buffer_reader_.reset( |
| 89 new MojoDataBufferReader(std::move(consumer_handle))); |
| 90 |
| 91 data_source_ready_cb.Run(); |
| 92 } |
| 93 |
| 94 void MojoDataSourceAdapter::OnDataSourceRead(uint8_t* data, |
| 95 const DataSource::ReadCB& read_cb, |
| 96 mojom::DataBufferPtr buffer, |
| 97 int size) { |
| 98 DVLOG(3) << __func__; |
| 99 |
| 100 if (size == media::DataSource::kAborted || |
| 101 size == media::DataSource::kReadError) { |
| 102 read_cb.Run(size); |
| 103 return; |
| 104 } |
| 105 |
| 106 mojo_data_buffer_reader_->ReadDataBuffer( |
| 107 std::move(buffer), |
| 108 base::BindOnce(&MojoDataSourceAdapter::OnBufferRead, |
| 109 weak_factory_.GetWeakPtr(), data, read_cb, size)); |
| 110 } |
| 111 |
| 112 void MojoDataSourceAdapter::OnBufferRead( |
| 113 uint8_t* data, |
| 114 const DataSource::ReadCB& read_cb, |
| 115 int size, |
| 116 scoped_refptr<media::DataBuffer> buffer) { |
| 117 if (buffer) { |
| 118 CHECK(buffer->data_size() == size); |
| 119 memcpy(data, buffer->data(), buffer->data_size()); |
| 120 } |
| 121 |
| 122 read_cb.Run(size); |
| 123 } |
| 124 |
| 125 void MojoDataSourceAdapter::GetSizeInternal(bool* success, int64_t* size_out) { |
| 126 // Sync calls must be done from the bound thread. |
| 127 CHECK(task_runner_->BelongsToCurrentThread()); |
| 128 |
| 129 std::unique_lock<std::mutex> lock(mutex_); |
| 130 data_source_->GetSize(success, size_out); |
| 131 |
| 132 lock.unlock(); |
| 133 condition_.notify_one(); |
| 134 } |
| 135 |
| 136 } // namespace media |
| OLD | NEW |