| 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 #ifndef MEDIA_MOJO_COMMON_MOJO_DATA_BUFFER_CONVERTER_ |
| 6 #define MEDIA_MOJO_COMMON_MOJO_DATA_BUFFER_CONVERTER_ |
| 7 |
| 8 #include "base/macros.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "media/mojo/interfaces/media_types.mojom.h" |
| 11 #include "mojo/public/cpp/system/data_pipe.h" |
| 12 #include "mojo/public/cpp/system/watcher.h" |
| 13 |
| 14 namespace media { |
| 15 |
| 16 class DataBuffer; |
| 17 |
| 18 // A helper class that converts mojom::DataBuffer to media::DataBuffer. |
| 19 // The data part of the DataBuffer is read from a DataPipe. |
| 20 class MojoDataBufferReader { |
| 21 public: |
| 22 using ReadCB = base::OnceCallback<void(scoped_refptr<DataBuffer>)>; |
| 23 |
| 24 // Creates a MojoDataBufferReader of |type| and set the |producer_handle|. |
| 25 static std::unique_ptr<MojoDataBufferReader> Create( |
| 26 uint32_t capacity_num_bytes, |
| 27 mojo::ScopedDataPipeProducerHandle* producer_handle); |
| 28 |
| 29 // Hold the consumer handle to read DataBuffer data. |
| 30 explicit MojoDataBufferReader( |
| 31 mojo::ScopedDataPipeConsumerHandle consumer_handle); |
| 32 |
| 33 ~MojoDataBufferReader(); |
| 34 |
| 35 // Converts |buffer| into a DataBuffer (read data from DataPipe if needed). |
| 36 // |read_cb| is called with the result DataBuffer. |
| 37 // Reports a null DataBuffer in case of an error. |
| 38 void ReadDataBuffer(mojom::DataBufferPtr buffer, ReadCB read_cb); |
| 39 |
| 40 private: |
| 41 void OnPipeError(MojoResult result); |
| 42 void OnPipeReadable(MojoResult result); |
| 43 void ReadDataBufferData(); |
| 44 |
| 45 // For reading the data section of a DataBuffer. |
| 46 mojo::ScopedDataPipeConsumerHandle consumer_handle_; |
| 47 mojo::Watcher pipe_watcher_; |
| 48 |
| 49 // Only valid during pending read. |
| 50 ReadCB read_cb_; |
| 51 scoped_refptr<DataBuffer> media_buffer_; |
| 52 uint32_t bytes_read_; |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(MojoDataBufferReader); |
| 55 }; |
| 56 |
| 57 // A helper class that converts media::DataBuffer to mojom::DataBuffer. |
| 58 // The data part of the DataBuffer is written into a DataPipe. |
| 59 class MojoDataBufferWriter { |
| 60 public: |
| 61 // Creates a MojoDataBufferWriter of |type| and set the |consumer_handle|. |
| 62 static std::unique_ptr<MojoDataBufferWriter> Create( |
| 63 uint32_t capacity_num_bytes, |
| 64 mojo::ScopedDataPipeConsumerHandle* consumer_handle); |
| 65 |
| 66 // Hold the producer handle to write DataBuffer data. |
| 67 explicit MojoDataBufferWriter( |
| 68 mojo::ScopedDataPipeProducerHandle producer_handle); |
| 69 |
| 70 ~MojoDataBufferWriter(); |
| 71 |
| 72 // Converts a DataBuffer into mojo DataBuffer. |
| 73 // DataBuffer data is asynchronously written into DataPipe if needed. |
| 74 // Returns null if conversion failed or if the data pipe is already closed. |
| 75 mojom::DataBufferPtr WriteDataBuffer( |
| 76 const scoped_refptr<DataBuffer>& media_buffer); |
| 77 |
| 78 private: |
| 79 void OnPipeError(MojoResult result); |
| 80 void OnPipeWritable(MojoResult result); |
| 81 MojoResult WriteDataBufferData(); |
| 82 |
| 83 // For writing the data section of DataBuffer into DataPipe. |
| 84 mojo::ScopedDataPipeProducerHandle producer_handle_; |
| 85 mojo::Watcher pipe_watcher_; |
| 86 |
| 87 // Only valid when data is being written to the pipe. |
| 88 scoped_refptr<DataBuffer> media_buffer_; |
| 89 uint32_t bytes_written_; |
| 90 |
| 91 DISALLOW_COPY_AND_ASSIGN(MojoDataBufferWriter); |
| 92 }; |
| 93 |
| 94 } // namespace media |
| 95 |
| 96 #endif // MEDIA_MOJO_COMMON_MOJO_DATA_BUFFER_CONVERTER_ |
| OLD | NEW |