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_.get(), memory_size_); | |
33 } | |
34 | |
35 private: | |
36 void LazyUnwrapHandleAndMapMemory() { | |
37 if (shared_memory_ != nullptr) | |
38 return; // already done before | |
emircan
2017/04/19 18:16:48
nit: You can also use base::Optional for this and
chfremer
2017/04/19 23:03:37
Done.
| |
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_ = | |
48 base::MakeUnique<base::SharedMemory>(memory_handle, read_only_flag_); | |
49 if (!shared_memory_->Map(memory_size_)) { | |
50 DLOG(ERROR) << "LazyUnwrapHandleAndMapMemory: Map failed."; | |
51 } | |
52 } | |
53 | |
54 mojo::ScopedSharedBufferHandle buffer_handle_; | |
55 std::unique_ptr<base::SharedMemory> shared_memory_; | |
56 size_t memory_size_ = 0; | |
57 bool read_only_flag_ = false; | |
58 }; | |
59 | |
60 class ScopedAccessPermissionMojoToMediaAdapter | |
61 : public media::VideoCaptureDevice::Client::Buffer::ScopedAccessPermission { | |
62 public: | |
63 ScopedAccessPermissionMojoToMediaAdapter( | |
64 video_capture::mojom::ScopedAccessPermissionPtr access_permission) | |
65 : access_permission_(std::move(access_permission)) {} | |
66 | |
67 private: | |
68 video_capture::mojom::ScopedAccessPermissionPtr access_permission_; | |
69 }; | |
70 | |
71 } // anonymous namespace | |
72 | |
73 namespace video_capture { | |
74 | |
75 ReceiverMediaToMojoAdapter::ReceiverMediaToMojoAdapter( | |
76 std::unique_ptr<media::VideoFrameReceiver> receiver) | |
77 : receiver_(std::move(receiver)) {} | |
78 | |
79 ReceiverMediaToMojoAdapter::~ReceiverMediaToMojoAdapter() = default; | |
80 | |
81 void ReceiverMediaToMojoAdapter::OnNewBufferHandle( | |
82 int32_t buffer_id, | |
83 mojo::ScopedSharedBufferHandle buffer_handle) { | |
84 receiver_->OnNewBufferHandle( | |
85 buffer_id, | |
86 base::MakeUnique<MojoBufferHandleProvider>(std::move(buffer_handle))); | |
87 } | |
88 | |
89 void ReceiverMediaToMojoAdapter::OnFrameReadyInBuffer( | |
90 int32_t buffer_id, | |
91 int32_t frame_feedback_id, | |
92 mojom::ScopedAccessPermissionPtr access_permission, | |
93 media::mojom::VideoFrameInfoPtr frame_info) { | |
94 receiver_->OnFrameReadyInBuffer( | |
95 buffer_id, frame_feedback_id, | |
96 base::MakeUnique<ScopedAccessPermissionMojoToMediaAdapter>( | |
97 std::move(access_permission)), | |
98 std::move(frame_info)); | |
99 } | |
100 | |
101 void ReceiverMediaToMojoAdapter::OnBufferRetired(int32_t buffer_id) { | |
102 receiver_->OnBufferRetired(buffer_id); | |
103 } | |
104 | |
105 void ReceiverMediaToMojoAdapter::OnError() { | |
106 receiver_->OnError(); | |
107 } | |
108 | |
109 void ReceiverMediaToMojoAdapter::OnLog(const std::string& message) { | |
110 receiver_->OnLog(message); | |
111 } | |
112 | |
113 } // namespace video_capture | |
OLD | NEW |