Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/host/desktop_session_agent.h" | 5 #include "remoting/host/desktop_session_agent.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "ipc/ipc_channel_proxy.h" | 8 #include "ipc/ipc_channel_proxy.h" |
| 9 #include "ipc/ipc_message.h" | 9 #include "ipc/ipc_message.h" |
| 10 #include "ipc/ipc_message_macros.h" | 10 #include "ipc/ipc_message_macros.h" |
| 11 #include "remoting/base/auto_thread_task_runner.h" | 11 #include "remoting/base/auto_thread_task_runner.h" |
| 12 #include "remoting/base/capture_data.h" | |
| 12 #include "remoting/host/chromoting_messages.h" | 13 #include "remoting/host/chromoting_messages.h" |
| 14 #include "remoting/proto/control.pb.h" | |
| 15 #include "third_party/skia/include/core/SkRegion.h" | |
| 13 | 16 |
| 14 namespace remoting { | 17 namespace remoting { |
| 15 | 18 |
| 16 DesktopSessionAgent::~DesktopSessionAgent() { | 19 DesktopSessionAgent::~DesktopSessionAgent() { |
| 17 DCHECK(caller_task_runner()->BelongsToCurrentThread()); | 20 DCHECK(!video_capturer_); |
| 18 } | 21 } |
| 19 | 22 |
| 20 bool DesktopSessionAgent::OnMessageReceived(const IPC::Message& message) { | 23 bool DesktopSessionAgent::OnMessageReceived(const IPC::Message& message) { |
| 21 DCHECK(caller_task_runner()->BelongsToCurrentThread()); | 24 DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 22 | 25 |
| 23 NOTIMPLEMENTED(); | 26 bool handled = true; |
| 24 return false; | 27 IPC_BEGIN_MESSAGE_MAP(DesktopSessionAgent, message) |
| 28 IPC_MESSAGE_HANDLER(ChromotingNetworkDesktopMsg_CaptureFrame, | |
| 29 OnCaptureFrame) | |
| 30 IPC_MESSAGE_HANDLER(ChromotingNetworkDesktopMsg_InvalidateRegion, | |
| 31 OnInvalidateRegion) | |
| 32 IPC_MESSAGE_HANDLER(ChromotingNetworkDesktopMsg_SharedBufferCreated, | |
| 33 OnSharedBufferCreated) | |
| 34 IPC_END_MESSAGE_MAP() | |
| 35 return handled; | |
| 25 } | 36 } |
| 26 | 37 |
| 27 void DesktopSessionAgent::OnChannelConnected(int32 peer_pid) { | 38 void DesktopSessionAgent::OnChannelConnected(int32 peer_pid) { |
| 28 DCHECK(caller_task_runner()->BelongsToCurrentThread()); | 39 DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 29 | 40 |
| 30 VLOG(1) << "IPC: desktop <- network (" << peer_pid << ")"; | 41 VLOG(1) << "IPC: desktop <- network (" << peer_pid << ")"; |
| 31 | |
| 32 NOTIMPLEMENTED(); | |
| 33 } | 42 } |
| 34 | 43 |
| 35 void DesktopSessionAgent::OnChannelError() { | 44 void DesktopSessionAgent::OnChannelError() { |
| 36 DCHECK(caller_task_runner()->BelongsToCurrentThread()); | 45 DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 37 | 46 |
| 38 // Make sure the channel is closed. | 47 // Make sure the channel is closed. |
| 39 network_channel_.reset(); | 48 network_channel_.reset(); |
| 40 | 49 |
| 41 // Notify the caller that |this| can be destroyed now. | 50 // Notify the caller that the channel has been disconnected. |
| 42 done_task_.Run(); | 51 disconnected_task_.Run(); |
| 43 } | 52 } |
| 44 | 53 |
| 45 bool DesktopSessionAgent::Start(const base::Closure& done_task, | 54 scoped_refptr<SharedBuffer> DesktopSessionAgent::CreateSharedBuffer( |
| 55 uint32 size) { | |
| 56 DCHECK(video_capture_task_runner()->BelongsToCurrentThread()); | |
| 57 | |
| 58 scoped_refptr<SharedBuffer> buffer = new SharedBuffer(size); | |
| 59 if (buffer->ptr() != NULL) { | |
| 60 buffer->set_id(next_shared_buffer_id_); | |
| 61 shared_buffers_.push_back(buffer); | |
| 62 | |
| 63 // |next_shared_buffer_id_| starts from 1 and incrementing it by 2 makes | |
| 64 // sure it is always odd and therefore zero is never used as a valid buffer | |
| 65 // ID. | |
| 66 // | |
| 67 // It is very unlikely (though theoretically possible) to allocate the same | |
| 68 // ID for two different buffers due to integer overflow. It should take | |
| 69 // about a year of allocating 100 new buffers every second. Practically | |
| 70 // speaking it never happens. | |
| 71 next_shared_buffer_id_ += 2; | |
|
Cris Neckar
2012/11/26 22:57:53
Nit: If this is already a time consuming operation
alexeypa (please no reviews)
2012/11/26 23:47:13
If we ever reallocate buffers we tend to reallocat
| |
| 72 | |
| 73 SendToNetwork(new ChromotingDesktopNetworkMsg_CreateSharedBuffer( | |
| 74 buffer->id(), buffer->handle(), buffer->size())); | |
| 75 } | |
| 76 | |
| 77 return buffer; | |
| 78 } | |
| 79 | |
| 80 void DesktopSessionAgent::ReleaseSharedBuffer( | |
| 81 scoped_refptr<SharedBuffer> buffer) { | |
| 82 DCHECK(video_capture_task_runner()->BelongsToCurrentThread()); | |
| 83 DCHECK(buffer->id() != 0); | |
| 84 | |
| 85 SendToNetwork( | |
| 86 new ChromotingDesktopNetworkMsg_ReleaseSharedBuffer(buffer->id())); | |
| 87 } | |
| 88 | |
| 89 void DesktopSessionAgent::OnCaptureCompleted( | |
| 90 scoped_refptr<CaptureData> capture_data) { | |
| 91 DCHECK(video_capture_task_runner()->BelongsToCurrentThread()); | |
| 92 | |
| 93 // Serialize CaptureData | |
| 94 SerializedCapturedData serialized_data; | |
| 95 serialized_data.shared_buffer_id = capture_data->shared_buffer()->id(); | |
| 96 serialized_data.dimensions = capture_data->size(); | |
| 97 serialized_data.pixel_format = capture_data->pixel_format(); | |
| 98 serialized_data.capture_time_ms = capture_data->capture_time_ms(); | |
| 99 serialized_data.client_sequence_number = | |
| 100 capture_data->client_sequence_number(); | |
| 101 serialized_data.dpi = capture_data->dpi(); | |
| 102 for (SkRegion::Iterator i(capture_data->dirty_region()); !i.done(); i.next()) | |
| 103 serialized_data.dirty_region.push_back(i.rect()); | |
| 104 | |
| 105 SendToNetwork( | |
| 106 new ChromotingDesktopNetworkMsg_CaptureCompleted(serialized_data)); | |
| 107 } | |
| 108 | |
| 109 void DesktopSessionAgent::OnCursorShapeChanged( | |
| 110 scoped_ptr<protocol::CursorShapeInfo> cursor_shape) { | |
| 111 DCHECK(video_capture_task_runner()->BelongsToCurrentThread()); | |
| 112 | |
| 113 NOTIMPLEMENTED(); | |
| 114 } | |
| 115 | |
| 116 bool DesktopSessionAgent::Start(const base::Closure& disconnected_task, | |
| 46 IPC::PlatformFileForTransit* desktop_pipe_out) { | 117 IPC::PlatformFileForTransit* desktop_pipe_out) { |
| 47 DCHECK(caller_task_runner()->BelongsToCurrentThread()); | 118 DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 48 | 119 |
| 49 done_task_ = done_task; | 120 disconnected_task_ = disconnected_task; |
| 50 return DoCreateNetworkChannel(desktop_pipe_out, &network_channel_); | 121 if (!CreateChannelForNetworkProcess(desktop_pipe_out, &network_channel_)) |
| 122 return false; | |
| 123 | |
| 124 // Start the video capturer. | |
| 125 video_capture_task_runner()->PostTask( | |
| 126 FROM_HERE, base::Bind(&DesktopSessionAgent::StartVideoCapturer, this)); | |
| 127 return true; | |
| 128 } | |
| 129 | |
| 130 void DesktopSessionAgent::Stop() { | |
| 131 DCHECK(caller_task_runner()->BelongsToCurrentThread()); | |
| 132 | |
| 133 // Stop the video capturer. | |
| 134 video_capture_task_runner()->PostTask( | |
| 135 FROM_HERE, base::Bind(&DesktopSessionAgent::StopVideoCapturer, this)); | |
| 136 } | |
| 137 | |
| 138 void DesktopSessionAgent::OnCaptureFrame() { | |
| 139 if (!video_capture_task_runner()->BelongsToCurrentThread()) { | |
| 140 video_capture_task_runner()->PostTask( | |
| 141 FROM_HERE, | |
| 142 base::Bind(&DesktopSessionAgent::OnCaptureFrame, this)); | |
| 143 return; | |
| 144 } | |
| 145 | |
| 146 // VideoFrameCapturer supports a very few (currently 2) outstanding capture | |
| 147 // requests. The requests are serialized on |video_capture_task_runner()| task | |
| 148 // runner. If the client issues more requests, pixel data in captured farmes | |
| 149 // will likely be corrupted but stability of VideoFrameCapturer will not be | |
| 150 // affected. | |
| 151 video_capturer_->CaptureInvalidRegion(); | |
| 152 } | |
| 153 | |
| 154 void DesktopSessionAgent::OnInvalidateRegion( | |
| 155 const std::vector<SkIRect>& invalid_rects) { | |
| 156 if (!video_capture_task_runner()->BelongsToCurrentThread()) { | |
| 157 video_capture_task_runner()->PostTask( | |
| 158 FROM_HERE, | |
| 159 base::Bind(&DesktopSessionAgent::OnInvalidateRegion, this, | |
| 160 invalid_rects)); | |
| 161 return; | |
| 162 } | |
| 163 | |
| 164 SkIRect bounds = SkIRect::MakeSize(video_capturer_->size_most_recent()); | |
| 165 | |
| 166 // Convert |invalid_rects| into a region. | |
| 167 SkRegion invalid_region; | |
| 168 for (std::vector<SkIRect>::const_iterator i = invalid_rects.begin(); | |
| 169 i != invalid_rects.end(); ++i) { | |
| 170 // Validate each rectange and clip it to the frame bounds. If the rectangle | |
| 171 // is not valid it is ignored. | |
| 172 SkIRect rect; | |
| 173 if (rect.intersect(*i, bounds)) { | |
| 174 invalid_region.op(rect, SkRegion::kUnion_Op); | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 video_capturer_->InvalidateRegion(invalid_region); | |
| 179 } | |
| 180 | |
| 181 void DesktopSessionAgent::OnSharedBufferCreated(int id) { | |
| 182 if (!video_capture_task_runner()->BelongsToCurrentThread()) { | |
| 183 video_capture_task_runner()->PostTask( | |
| 184 FROM_HERE, | |
| 185 base::Bind(&DesktopSessionAgent::OnSharedBufferCreated, this, id)); | |
| 186 return; | |
| 187 } | |
| 188 | |
| 189 // Drop the cached reference to the buffer. | |
| 190 SharedBuffers::iterator i = shared_buffers_.begin(); | |
| 191 for (; i != shared_buffers_.end(); ++i) { | |
| 192 if ((*i)->id() == id) { | |
| 193 shared_buffers_.erase(i); | |
| 194 break; | |
| 195 } | |
| 196 } | |
| 197 } | |
| 198 | |
| 199 void DesktopSessionAgent::SendToNetwork(IPC::Message* message) { | |
| 200 if (!caller_task_runner()->BelongsToCurrentThread()) { | |
| 201 caller_task_runner()->PostTask( | |
| 202 FROM_HERE, | |
| 203 base::Bind(&DesktopSessionAgent::SendToNetwork, this, message)); | |
| 204 return; | |
| 205 } | |
| 206 | |
| 207 if (network_channel_) { | |
| 208 network_channel_->Send(message); | |
| 209 } else { | |
| 210 delete message; | |
| 211 } | |
| 212 } | |
| 213 | |
| 214 void DesktopSessionAgent::StartVideoCapturer() { | |
| 215 DCHECK(video_capture_task_runner()->BelongsToCurrentThread()); | |
| 216 | |
| 217 video_capturer_ = VideoFrameCapturer::CreateWithFactory(this); | |
| 218 video_capturer_->Start(this); | |
| 219 } | |
| 220 | |
| 221 void DesktopSessionAgent::StopVideoCapturer() { | |
| 222 DCHECK(video_capture_task_runner()->BelongsToCurrentThread()); | |
| 223 | |
| 224 if (video_capturer_) { | |
| 225 video_capturer_->Stop(); | |
| 226 video_capturer_.reset(); | |
| 227 } | |
| 228 | |
| 229 // Free any shared buffers left. | |
| 230 shared_buffers_.clear(); | |
| 51 } | 231 } |
| 52 | 232 |
| 53 DesktopSessionAgent::DesktopSessionAgent( | 233 DesktopSessionAgent::DesktopSessionAgent( |
| 54 scoped_refptr<AutoThreadTaskRunner> caller_task_runner, | 234 scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 55 scoped_refptr<AutoThreadTaskRunner> io_task_runner) | 235 scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 236 scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner) | |
| 56 : caller_task_runner_(caller_task_runner), | 237 : caller_task_runner_(caller_task_runner), |
| 57 io_task_runner_(io_task_runner) { | 238 io_task_runner_(io_task_runner), |
| 239 video_capture_task_runner_(video_capture_task_runner), | |
| 240 next_shared_buffer_id_(1) { | |
| 58 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 241 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 59 } | 242 } |
| 60 | 243 |
| 61 } // namespace remoting | 244 } // namespace remoting |
| OLD | NEW |