OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "services/video_capture/public/cpp/receiver_media_to_mojo_adapter.h" |
| 6 |
| 7 #include "media/capture/video/shared_memory_buffer_tracker.h" |
| 8 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 9 #include "mojo/public/cpp/system/platform_handle.h" |
| 10 |
| 11 namespace { |
| 12 |
| 13 class MojoBufferHandleProvider |
| 14 : public media::VideoCaptureDevice::Client::Buffer::HandleProvider { |
| 15 public: |
| 16 MojoBufferHandleProvider(mojo::ScopedSharedBufferHandle buffer_handle) |
| 17 : buffer_handle_(std::move(buffer_handle)) {} |
| 18 |
| 19 // Implementation of Buffer::HandleProvider: |
| 20 mojo::ScopedSharedBufferHandle GetHandleForInterProcessTransit() override { |
| 21 return buffer_handle_->Clone(); |
| 22 } |
| 23 base::SharedMemoryHandle GetNonOwnedSharedMemoryHandleForLegacyIPC() |
| 24 override { |
| 25 LazyUnwrapHandleAndMapMemory(); |
| 26 return shared_memory_->handle(); |
| 27 } |
| 28 std::unique_ptr<media::VideoCaptureBufferHandle> GetHandleForInProcessAccess() |
| 29 override { |
| 30 LazyUnwrapHandleAndMapMemory(); |
| 31 return base::MakeUnique<media::SharedMemoryBufferHandle>( |
| 32 &shared_memory_.value(), memory_size_); |
| 33 } |
| 34 |
| 35 private: |
| 36 void LazyUnwrapHandleAndMapMemory() { |
| 37 if (shared_memory_.has_value()) |
| 38 return; // already done before |
| 39 |
| 40 base::SharedMemoryHandle memory_handle; |
| 41 const MojoResult result = |
| 42 mojo::UnwrapSharedMemoryHandle(buffer_handle_->Clone(), &memory_handle, |
| 43 &memory_size_, &read_only_flag_); |
| 44 DCHECK_EQ(MOJO_RESULT_OK, result); |
| 45 DCHECK_GT(memory_size_, 0u); |
| 46 |
| 47 shared_memory_.emplace(memory_handle, read_only_flag_); |
| 48 if (!shared_memory_->Map(memory_size_)) { |
| 49 DLOG(ERROR) << "LazyUnwrapHandleAndMapMemory: Map failed."; |
| 50 } |
| 51 } |
| 52 |
| 53 mojo::ScopedSharedBufferHandle buffer_handle_; |
| 54 base::Optional<base::SharedMemory> shared_memory_; |
| 55 size_t memory_size_ = 0; |
| 56 bool read_only_flag_ = false; |
| 57 }; |
| 58 |
| 59 class ScopedAccessPermissionMojoToMediaAdapter |
| 60 : public media::VideoCaptureDevice::Client::Buffer::ScopedAccessPermission { |
| 61 public: |
| 62 ScopedAccessPermissionMojoToMediaAdapter( |
| 63 video_capture::mojom::ScopedAccessPermissionPtr access_permission) |
| 64 : access_permission_(std::move(access_permission)) {} |
| 65 |
| 66 private: |
| 67 video_capture::mojom::ScopedAccessPermissionPtr access_permission_; |
| 68 }; |
| 69 |
| 70 } // anonymous namespace |
| 71 |
| 72 namespace video_capture { |
| 73 |
| 74 ReceiverMediaToMojoAdapter::ReceiverMediaToMojoAdapter( |
| 75 std::unique_ptr<media::VideoFrameReceiver> receiver) |
| 76 : receiver_(std::move(receiver)) {} |
| 77 |
| 78 ReceiverMediaToMojoAdapter::~ReceiverMediaToMojoAdapter() = default; |
| 79 |
| 80 void ReceiverMediaToMojoAdapter::OnNewBufferHandle( |
| 81 int32_t buffer_id, |
| 82 mojo::ScopedSharedBufferHandle buffer_handle) { |
| 83 receiver_->OnNewBufferHandle( |
| 84 buffer_id, |
| 85 base::MakeUnique<MojoBufferHandleProvider>(std::move(buffer_handle))); |
| 86 } |
| 87 |
| 88 void ReceiverMediaToMojoAdapter::OnFrameReadyInBuffer( |
| 89 int32_t buffer_id, |
| 90 int32_t frame_feedback_id, |
| 91 mojom::ScopedAccessPermissionPtr access_permission, |
| 92 media::mojom::VideoFrameInfoPtr frame_info) { |
| 93 receiver_->OnFrameReadyInBuffer( |
| 94 buffer_id, frame_feedback_id, |
| 95 base::MakeUnique<ScopedAccessPermissionMojoToMediaAdapter>( |
| 96 std::move(access_permission)), |
| 97 std::move(frame_info)); |
| 98 } |
| 99 |
| 100 void ReceiverMediaToMojoAdapter::OnBufferRetired(int32_t buffer_id) { |
| 101 receiver_->OnBufferRetired(buffer_id); |
| 102 } |
| 103 |
| 104 void ReceiverMediaToMojoAdapter::OnError() { |
| 105 receiver_->OnError(); |
| 106 } |
| 107 |
| 108 void ReceiverMediaToMojoAdapter::OnLog(const std::string& message) { |
| 109 receiver_->OnLog(message); |
| 110 } |
| 111 |
| 112 } // namespace video_capture |
OLD | NEW |