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" | |
| 13 | 15 |
| 14 namespace remoting { | 16 namespace remoting { |
| 15 | 17 |
| 16 DesktopSessionAgent::~DesktopSessionAgent() { | 18 DesktopSessionAgent::~DesktopSessionAgent() { |
| 17 DCHECK(caller_task_runner()->BelongsToCurrentThread()); | 19 DCHECK(!video_capturer_); |
| 18 } | 20 } |
| 19 | 21 |
| 20 bool DesktopSessionAgent::OnMessageReceived(const IPC::Message& message) { | 22 bool DesktopSessionAgent::OnMessageReceived(const IPC::Message& message) { |
| 21 DCHECK(caller_task_runner()->BelongsToCurrentThread()); | 23 DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 22 | 24 |
| 23 NOTIMPLEMENTED(); | 25 bool handled = true; |
| 24 return false; | 26 IPC_BEGIN_MESSAGE_MAP(DesktopSessionAgent, message) |
| 27 IPC_MESSAGE_HANDLER(ChromotingNetworkDesktopMsg_CaptureFrame, | |
| 28 OnCaptureFrame) | |
| 29 IPC_MESSAGE_HANDLER(ChromotingNetworkDesktopMsg_InvalidateRegion, | |
| 30 OnInvalidateRegion) | |
| 31 IPC_MESSAGE_HANDLER(ChromotingNetworkDesktopMsg_SharedBufferCreated, | |
| 32 OnSharedBufferCreated) | |
| 33 IPC_END_MESSAGE_MAP() | |
| 34 return handled; | |
| 25 } | 35 } |
| 26 | 36 |
| 27 void DesktopSessionAgent::OnChannelConnected(int32 peer_pid) { | 37 void DesktopSessionAgent::OnChannelConnected(int32 peer_pid) { |
| 28 DCHECK(caller_task_runner()->BelongsToCurrentThread()); | 38 DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 29 | 39 |
| 30 VLOG(1) << "IPC: desktop <- network (" << peer_pid << ")"; | 40 VLOG(1) << "IPC: desktop <- network (" << peer_pid << ")"; |
| 31 | |
| 32 NOTIMPLEMENTED(); | |
| 33 } | 41 } |
| 34 | 42 |
| 35 void DesktopSessionAgent::OnChannelError() { | 43 void DesktopSessionAgent::OnChannelError() { |
| 36 DCHECK(caller_task_runner()->BelongsToCurrentThread()); | 44 DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 37 | 45 |
| 38 // Make sure the channel is closed. | 46 // Make sure the channel is closed. |
| 39 network_channel_.reset(); | 47 network_channel_.reset(); |
| 40 | 48 |
| 41 // Notify the caller that |this| can be destroyed now. | 49 // Notify the caller that the channel has been disconnected. |
| 42 done_task_.Run(); | 50 disconnected_task_.Run(); |
| 43 } | 51 } |
| 44 | 52 |
| 45 bool DesktopSessionAgent::Start(const base::Closure& done_task, | 53 scoped_refptr<SharedBuffer> DesktopSessionAgent::CreateSharedBuffer( |
| 54 uint32 size) { | |
| 55 DCHECK(video_capture_task_runner()->BelongsToCurrentThread()); | |
| 56 | |
| 57 scoped_refptr<SharedBuffer> buffer = new SharedBuffer(size); | |
| 58 if (buffer->ptr() != NULL) { | |
| 59 // Use buffer's address as its ID. | |
| 60 buffer->set_id(reinterpret_cast<intptr_t>(buffer.get())); | |
|
Cris Neckar
2012/11/20 22:23:49
In practice this may not be an issue but it is gen
alexeypa (please no reviews)
2012/11/20 22:56:03
There are two processes: desktop and network. The
Cris Neckar
2012/11/21 01:00:54
Gotcha, if it's doable can you use something other
alexeypa (please no reviews)
2012/11/22 00:59:24
Done.
| |
| 61 shared_buffers_.push_back(buffer); | |
| 62 | |
| 63 SendToNetwork(new ChromotingDesktopNetworkMsg_CreateSharedBuffer( | |
| 64 buffer->id(), buffer->handle(), buffer->size())); | |
| 65 } | |
| 66 | |
| 67 return buffer; | |
| 68 } | |
| 69 | |
| 70 void DesktopSessionAgent::ReleaseSharedBuffer( | |
| 71 scoped_refptr<SharedBuffer> buffer) { | |
| 72 DCHECK(video_capture_task_runner()->BelongsToCurrentThread()); | |
| 73 DCHECK(buffer->id() != 0); | |
| 74 | |
| 75 SendToNetwork( | |
| 76 new ChromotingDesktopNetworkMsg_ReleaseSharedBuffer(buffer->id())); | |
| 77 } | |
| 78 | |
| 79 void DesktopSessionAgent::OnCaptureCompleted( | |
| 80 scoped_refptr<CaptureData> capture_data) { | |
| 81 DCHECK(video_capture_task_runner()->BelongsToCurrentThread()); | |
| 82 | |
| 83 // Serialize CaptureData | |
| 84 SerializedCapturedData serialized_data; | |
| 85 serialized_data.shared_buffer_id = capture_data->shared_buffer()->id(); | |
| 86 serialized_data.dimensions = capture_data->size(); | |
| 87 serialized_data.pixel_format = capture_data->pixel_format(); | |
| 88 serialized_data.capture_time_ms = capture_data->capture_time_ms(); | |
| 89 serialized_data.client_sequence_number = | |
| 90 capture_data->client_sequence_number(); | |
| 91 serialized_data.dpi = capture_data->dpi(); | |
| 92 for (SkRegion::Iterator i(capture_data->dirty_region()); !i.done(); i.next()) | |
| 93 serialized_data.dirty_region.push_back(i.rect()); | |
| 94 | |
| 95 SendToNetwork( | |
| 96 new ChromotingDesktopNetworkMsg_CaptureCompleted(serialized_data)); | |
| 97 } | |
| 98 | |
| 99 void DesktopSessionAgent::OnCursorShapeChanged( | |
| 100 scoped_ptr<protocol::CursorShapeInfo> cursor_shape) { | |
| 101 DCHECK(video_capture_task_runner()->BelongsToCurrentThread()); | |
| 102 | |
| 103 NOTIMPLEMENTED(); | |
| 104 } | |
| 105 | |
| 106 bool DesktopSessionAgent::Start(const base::Closure& disconnected_task, | |
| 46 IPC::PlatformFileForTransit* desktop_pipe_out) { | 107 IPC::PlatformFileForTransit* desktop_pipe_out) { |
| 47 DCHECK(caller_task_runner()->BelongsToCurrentThread()); | 108 DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 48 | 109 |
| 49 done_task_ = done_task; | 110 disconnected_task_ = disconnected_task; |
| 50 return DoCreateNetworkChannel(desktop_pipe_out, &network_channel_); | 111 if (!CreateChannelForNetworkProcess(desktop_pipe_out, &network_channel_)) |
| 112 return false; | |
| 113 | |
| 114 // Start the video capturer. | |
| 115 video_capture_task_runner()->PostTask( | |
| 116 FROM_HERE, base::Bind(&DesktopSessionAgent::StartVideoCapturer, this)); | |
| 117 return true; | |
| 118 } | |
| 119 | |
| 120 void DesktopSessionAgent::Stop() { | |
| 121 DCHECK(caller_task_runner()->BelongsToCurrentThread()); | |
| 122 | |
| 123 // Stop the video capturer. | |
| 124 video_capture_task_runner()->PostTask( | |
| 125 FROM_HERE, base::Bind(&DesktopSessionAgent::StopVideoCapturer, this)); | |
| 126 } | |
| 127 | |
| 128 void DesktopSessionAgent::InvalidateRegion( | |
| 129 scoped_ptr<SkRegion> invalid_region) { | |
| 130 DCHECK(video_capture_task_runner()->BelongsToCurrentThread()); | |
| 131 | |
| 132 video_capturer_->InvalidateRegion(*invalid_region); | |
| 133 } | |
| 134 | |
| 135 void DesktopSessionAgent::OnCaptureFrame() { | |
| 136 if (!video_capture_task_runner()->BelongsToCurrentThread()) { | |
| 137 video_capture_task_runner()->PostTask( | |
| 138 FROM_HERE, | |
| 139 base::Bind(&DesktopSessionAgent::OnCaptureFrame, this)); | |
| 140 return; | |
| 141 } | |
| 142 | |
| 143 video_capturer_->CaptureInvalidRegion(); | |
| 144 } | |
| 145 | |
| 146 void DesktopSessionAgent::OnInvalidateRegion( | |
| 147 const std::vector<SkIRect>& invalid_rects) { | |
|
aedla
2012/11/21 21:53:54
I can't find any check that invalid_rects has sane
alexeypa (please no reviews)
2012/11/22 00:59:24
Done. The list of rects is sanitized now.
| |
| 148 DCHECK(caller_task_runner()->BelongsToCurrentThread()); | |
| 149 | |
| 150 scoped_ptr<SkRegion> invalid_region(new SkRegion()); | |
| 151 invalid_region->setRects(&invalid_rects[0], invalid_rects.size()); | |
| 152 | |
| 153 video_capture_task_runner()->PostTask( | |
| 154 FROM_HERE, base::Bind(&DesktopSessionAgent::InvalidateRegion, this, | |
| 155 base::Passed(&invalid_region))); | |
| 156 } | |
| 157 | |
| 158 void DesktopSessionAgent::OnSharedBufferCreated(intptr_t id) { | |
| 159 if (!video_capture_task_runner()->BelongsToCurrentThread()) { | |
| 160 video_capture_task_runner()->PostTask( | |
| 161 FROM_HERE, | |
| 162 base::Bind(&DesktopSessionAgent::OnSharedBufferCreated, this, id)); | |
| 163 return; | |
| 164 } | |
| 165 | |
| 166 // Drop the cached reference to the buffer. | |
| 167 SharedBuffers::iterator i = shared_buffers_.begin(); | |
| 168 for (; i != shared_buffers_.end(); ++i) { | |
| 169 if ((*i)->id() == id) { | |
| 170 shared_buffers_.erase(i); | |
| 171 break; | |
| 172 } | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 void DesktopSessionAgent::SendToNetwork(IPC::Message* message) { | |
| 177 if (!caller_task_runner()->BelongsToCurrentThread()) { | |
| 178 caller_task_runner()->PostTask( | |
| 179 FROM_HERE, | |
| 180 base::Bind(&DesktopSessionAgent::SendToNetwork, this, message)); | |
| 181 return; | |
| 182 } | |
| 183 | |
| 184 if (network_channel_) { | |
| 185 network_channel_->Send(message); | |
| 186 } else { | |
| 187 delete message; | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 void DesktopSessionAgent::StartVideoCapturer() { | |
| 192 DCHECK(video_capture_task_runner()->BelongsToCurrentThread()); | |
| 193 | |
| 194 video_capturer_ = VideoFrameCapturer::CreateWithFactory(this); | |
| 195 video_capturer_->Start(this); | |
| 196 } | |
| 197 | |
| 198 void DesktopSessionAgent::StopVideoCapturer() { | |
| 199 DCHECK(video_capture_task_runner()->BelongsToCurrentThread()); | |
| 200 | |
| 201 if (video_capturer_) { | |
| 202 video_capturer_->Stop(); | |
| 203 video_capturer_.reset(); | |
| 204 } | |
| 205 | |
| 206 // Free any shared buffers left. | |
| 207 shared_buffers_.clear(); | |
| 51 } | 208 } |
| 52 | 209 |
| 53 DesktopSessionAgent::DesktopSessionAgent( | 210 DesktopSessionAgent::DesktopSessionAgent( |
| 54 scoped_refptr<AutoThreadTaskRunner> caller_task_runner, | 211 scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 55 scoped_refptr<AutoThreadTaskRunner> io_task_runner) | 212 scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 213 scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner) | |
| 56 : caller_task_runner_(caller_task_runner), | 214 : caller_task_runner_(caller_task_runner), |
| 57 io_task_runner_(io_task_runner) { | 215 io_task_runner_(io_task_runner), |
| 216 video_capture_task_runner_(video_capture_task_runner) { | |
| 58 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 217 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 59 } | 218 } |
| 60 | 219 |
| 61 } // namespace remoting | 220 } // namespace remoting |
| OLD | NEW |