OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "content/renderer/pepper/pepper_video_source_host.h" | 5 #include "content/renderer/pepper/pepper_video_source_host.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/safe_numerics.h" | 8 #include "base/safe_numerics.h" |
9 #include "content/public/renderer/renderer_ppapi_host.h" | 9 #include "content/public/renderer/renderer_ppapi_host.h" |
10 #include "content/renderer/render_thread_impl.h" | 10 #include "content/renderer/render_thread_impl.h" |
11 #include "ppapi/c/pp_errors.h" | 11 #include "ppapi/c/pp_errors.h" |
12 #include "ppapi/host/dispatch_host_message.h" | 12 #include "ppapi/host/dispatch_host_message.h" |
13 #include "ppapi/host/ppapi_host.h" | 13 #include "ppapi/host/ppapi_host.h" |
14 #include "ppapi/proxy/ppapi_messages.h" | 14 #include "ppapi/proxy/ppapi_messages.h" |
15 #include "ppapi/proxy/ppb_image_data_proxy.h" | 15 #include "ppapi/proxy/ppb_image_data_proxy.h" |
16 #include "ppapi/shared_impl/scoped_pp_resource.h" | 16 #include "ppapi/shared_impl/scoped_pp_resource.h" |
17 #include "ppapi/thunk/enter.h" | 17 #include "ppapi/thunk/enter.h" |
18 #include "ppapi/thunk/ppb_image_data_api.h" | 18 #include "ppapi/thunk/ppb_image_data_api.h" |
19 #include "third_party/libjingle/source/talk/media/base/videocommon.h" | 19 #include "third_party/libjingle/source/talk/media/base/videocommon.h" |
20 #include "third_party/libjingle/source/talk/media/base/videoframe.h" | 20 #include "third_party/libjingle/source/talk/media/base/videoframe.h" |
21 #include "third_party/skia/include/core/SkBitmap.h" | 21 #include "third_party/skia/include/core/SkBitmap.h" |
22 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" | 22 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" |
23 | 23 |
24 using ppapi::host::HostMessageContext; | 24 using ppapi::host::HostMessageContext; |
25 using ppapi::host::ReplyMessageContext; | 25 using ppapi::host::ReplyMessageContext; |
26 | 26 |
27 namespace content { | 27 namespace content { |
28 | 28 |
| 29 PepperVideoSourceHost::FrameReceiver::FrameReceiver( |
| 30 const base::WeakPtr<PepperVideoSourceHost>& host) |
| 31 : host_(host), |
| 32 main_message_loop_proxy_(base::MessageLoopProxy::current()) { |
| 33 } |
| 34 |
| 35 PepperVideoSourceHost::FrameReceiver::~FrameReceiver() { |
| 36 } |
| 37 |
| 38 bool PepperVideoSourceHost::FrameReceiver::GotFrame( |
| 39 cricket::VideoFrame* frame) { |
| 40 // It's not safe to access the host from this thread, so post a task to our |
| 41 // main thread to transfer the new frame. |
| 42 main_message_loop_proxy_->PostTask( |
| 43 FROM_HERE, |
| 44 base::Bind(&FrameReceiver::OnGotFrame, |
| 45 this, |
| 46 base::Passed(scoped_ptr<cricket::VideoFrame>(frame)))); |
| 47 |
| 48 return true; |
| 49 } |
| 50 |
| 51 void PepperVideoSourceHost::FrameReceiver::OnGotFrame( |
| 52 scoped_ptr<cricket::VideoFrame> frame) { |
| 53 if (host_) { |
| 54 // Take ownership of the new frame, and possibly delete any unsent one. |
| 55 host_->last_frame_.swap(frame); |
| 56 |
| 57 if (host_->get_frame_pending_) |
| 58 host_->SendGetFrameReply(); |
| 59 } |
| 60 } |
| 61 |
29 PepperVideoSourceHost::PepperVideoSourceHost( | 62 PepperVideoSourceHost::PepperVideoSourceHost( |
30 RendererPpapiHost* host, | 63 RendererPpapiHost* host, |
31 PP_Instance instance, | 64 PP_Instance instance, |
32 PP_Resource resource) | 65 PP_Resource resource) |
33 : ResourceHost(host->GetPpapiHost(), instance, resource), | 66 : ResourceHost(host->GetPpapiHost(), instance, resource), |
34 renderer_ppapi_host_(host), | 67 renderer_ppapi_host_(host), |
35 weak_factory_(this), | 68 weak_factory_(this), |
36 main_message_loop_proxy_(base::MessageLoopProxy::current()), | |
37 source_handler_(new content::VideoSourceHandler(NULL)), | 69 source_handler_(new content::VideoSourceHandler(NULL)), |
| 70 frame_receiver_(new FrameReceiver(weak_factory_.GetWeakPtr())), |
38 get_frame_pending_(false) { | 71 get_frame_pending_(false) { |
39 } | 72 } |
40 | 73 |
41 PepperVideoSourceHost::~PepperVideoSourceHost() { | 74 PepperVideoSourceHost::~PepperVideoSourceHost() { |
42 Close(); | 75 Close(); |
43 } | 76 } |
44 | 77 |
45 int32_t PepperVideoSourceHost::OnResourceMessageReceived( | 78 int32_t PepperVideoSourceHost::OnResourceMessageReceived( |
46 const IPC::Message& msg, | 79 const IPC::Message& msg, |
47 HostMessageContext* context) { | 80 HostMessageContext* context) { |
48 IPC_BEGIN_MESSAGE_MAP(PepperVideoSourceHost, msg) | 81 IPC_BEGIN_MESSAGE_MAP(PepperVideoSourceHost, msg) |
49 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoSource_Open, | 82 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoSource_Open, |
50 OnHostMsgOpen) | 83 OnHostMsgOpen) |
51 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_GetFrame, | 84 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_GetFrame, |
52 OnHostMsgGetFrame) | 85 OnHostMsgGetFrame) |
53 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_Close, | 86 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_Close, |
54 OnHostMsgClose) | 87 OnHostMsgClose) |
55 IPC_END_MESSAGE_MAP() | 88 IPC_END_MESSAGE_MAP() |
56 return PP_ERROR_FAILED; | 89 return PP_ERROR_FAILED; |
57 } | 90 } |
58 | 91 |
59 bool PepperVideoSourceHost::GotFrame(cricket::VideoFrame* frame) { | |
60 // It's not safe to access this on another thread, so post a task to our | |
61 // main thread to transfer the new frame. | |
62 main_message_loop_proxy_->PostTask( | |
63 FROM_HERE, | |
64 base::Bind(&PepperVideoSourceHost::OnGotFrame, | |
65 weak_factory_.GetWeakPtr(), | |
66 base::Passed(scoped_ptr<cricket::VideoFrame>(frame)))); | |
67 | |
68 return true; | |
69 } | |
70 | |
71 void PepperVideoSourceHost::OnGotFrame(scoped_ptr<cricket::VideoFrame> frame) { | |
72 // Take ownership of the new frame, and possibly delete any unsent one. | |
73 last_frame_.swap(frame); | |
74 | |
75 if (get_frame_pending_) { | |
76 ppapi::HostResource image_data_resource; | |
77 PP_TimeTicks timestamp = 0; | |
78 int32_t result = ConvertFrame(&image_data_resource, ×tamp); | |
79 SendFrame(image_data_resource, timestamp, result); | |
80 } | |
81 } | |
82 | |
83 int32_t PepperVideoSourceHost::OnHostMsgOpen(HostMessageContext* context, | 92 int32_t PepperVideoSourceHost::OnHostMsgOpen(HostMessageContext* context, |
84 const std::string& stream_url) { | 93 const std::string& stream_url) { |
85 GURL gurl(stream_url); | 94 GURL gurl(stream_url); |
86 if (!gurl.is_valid()) | 95 if (!gurl.is_valid()) |
87 return PP_ERROR_BADARGUMENT; | 96 return PP_ERROR_BADARGUMENT; |
88 | 97 |
89 if (!source_handler_->Open(gurl.spec(), this)) | 98 if (!source_handler_->Open(gurl.spec(), frame_receiver_.get())) |
90 return PP_ERROR_BADARGUMENT; | 99 return PP_ERROR_BADARGUMENT; |
91 | 100 |
92 stream_url_ = gurl.spec(); | 101 stream_url_ = gurl.spec(); |
93 | 102 |
94 ReplyMessageContext reply_context = context->MakeReplyMessageContext(); | 103 ReplyMessageContext reply_context = context->MakeReplyMessageContext(); |
95 reply_context.params.set_result(PP_OK); | 104 reply_context.params.set_result(PP_OK); |
96 host()->SendReply(reply_context, PpapiPluginMsg_VideoSource_OpenReply()); | 105 host()->SendReply(reply_context, PpapiPluginMsg_VideoSource_OpenReply()); |
97 return PP_OK_COMPLETIONPENDING; | 106 return PP_OK_COMPLETIONPENDING; |
98 } | 107 } |
99 | 108 |
100 int32_t PepperVideoSourceHost::OnHostMsgGetFrame( | 109 int32_t PepperVideoSourceHost::OnHostMsgGetFrame( |
101 HostMessageContext* context) { | 110 HostMessageContext* context) { |
102 if (!source_handler_.get()) | 111 if (!source_handler_.get()) |
103 return PP_ERROR_FAILED; | 112 return PP_ERROR_FAILED; |
104 if (get_frame_pending_) | 113 if (get_frame_pending_) |
105 return PP_ERROR_INPROGRESS; | 114 return PP_ERROR_INPROGRESS; |
106 | 115 |
107 reply_context_ = context->MakeReplyMessageContext(); | 116 reply_context_ = context->MakeReplyMessageContext(); |
108 get_frame_pending_ = true; | 117 get_frame_pending_ = true; |
109 | 118 |
110 // If a frame is ready, try to convert it and reply. | 119 // If a frame is ready, try to convert it and send the reply. |
111 if (last_frame_.get()) { | 120 if (last_frame_.get()) |
112 ppapi::HostResource image_data_resource; | 121 SendGetFrameReply(); |
113 PP_TimeTicks timestamp = 0; | |
114 int32_t result = ConvertFrame(&image_data_resource, ×tamp); | |
115 if (result == PP_OK) { | |
116 SendFrame(image_data_resource, timestamp, result); | |
117 } else { | |
118 reply_context_ = ppapi::host::ReplyMessageContext(); | |
119 return result; | |
120 } | |
121 } | |
122 | 122 |
123 return PP_OK_COMPLETIONPENDING; | 123 return PP_OK_COMPLETIONPENDING; |
124 } | 124 } |
125 | 125 |
126 int32_t PepperVideoSourceHost::OnHostMsgClose(HostMessageContext* context) { | 126 int32_t PepperVideoSourceHost::OnHostMsgClose(HostMessageContext* context) { |
127 Close(); | 127 Close(); |
128 return PP_OK; | 128 return PP_OK; |
129 } | 129 } |
130 | 130 |
131 int32_t PepperVideoSourceHost::ConvertFrame( | 131 void PepperVideoSourceHost::SendGetFrameReply() { |
132 ppapi::HostResource* image_data_resource, | 132 DCHECK(get_frame_pending_); |
133 PP_TimeTicks* timestamp) { | 133 get_frame_pending_ = false; |
| 134 |
134 DCHECK(last_frame_.get()); | 135 DCHECK(last_frame_.get()); |
135 scoped_ptr<cricket::VideoFrame> frame(last_frame_.release()); | 136 scoped_ptr<cricket::VideoFrame> frame(last_frame_.release()); |
136 | 137 |
137 int32_t width = base::checked_numeric_cast<int32_t>(frame->GetWidth()); | 138 int32_t width = base::checked_numeric_cast<int32_t>(frame->GetWidth()); |
138 int32_t height = base::checked_numeric_cast<int32_t>(frame->GetHeight()); | 139 int32_t height = base::checked_numeric_cast<int32_t>(frame->GetHeight()); |
139 // Create an image data resource to hold the frame pixels. | 140 // Create an image data resource to hold the frame pixels. |
140 PP_ImageDataDesc desc; | 141 PP_ImageDataDesc image_desc; |
141 IPC::PlatformFileForTransit image_handle; | 142 IPC::PlatformFileForTransit image_handle; |
142 uint32_t byte_count; | 143 uint32_t byte_count; |
143 ppapi::ScopedPPResource resource( | 144 ppapi::ScopedPPResource resource( |
144 ppapi::ScopedPPResource::PassRef(), | 145 ppapi::ScopedPPResource::PassRef(), |
145 ppapi::proxy::PPB_ImageData_Proxy::CreateImageData( | 146 ppapi::proxy::PPB_ImageData_Proxy::CreateImageData( |
146 pp_instance(), | 147 pp_instance(), |
147 PP_IMAGEDATAFORMAT_BGRA_PREMUL, | 148 PP_IMAGEDATAFORMAT_BGRA_PREMUL, |
148 PP_MakeSize(width, height), | 149 PP_MakeSize(width, height), |
149 false /* init_to_zero */, | 150 false /* init_to_zero */, |
150 false /* is_nacl_plugin */, | 151 false /* is_nacl_plugin */, |
151 &desc, &image_handle, &byte_count)); | 152 &image_desc, &image_handle, &byte_count)); |
152 if (!resource.get()) | 153 if (!resource.get()) { |
153 return PP_ERROR_FAILED; | 154 SendGetFrameErrorReply(PP_ERROR_FAILED); |
| 155 return; |
| 156 } |
154 | 157 |
155 ppapi::thunk::EnterResourceNoLock<ppapi::thunk::PPB_ImageData_API> | 158 ppapi::thunk::EnterResourceNoLock<ppapi::thunk::PPB_ImageData_API> |
156 enter_resource(resource, false); | 159 enter_resource(resource, false); |
157 if (enter_resource.failed()) | 160 if (enter_resource.failed()) { |
158 return PP_ERROR_FAILED; | 161 SendGetFrameErrorReply(PP_ERROR_FAILED); |
| 162 return; |
| 163 } |
159 | 164 |
160 webkit::ppapi::PPB_ImageData_Impl* image_data = | 165 webkit::ppapi::PPB_ImageData_Impl* image_data = |
161 static_cast<webkit::ppapi::PPB_ImageData_Impl*>(enter_resource.object()); | 166 static_cast<webkit::ppapi::PPB_ImageData_Impl*>(enter_resource.object()); |
162 webkit::ppapi::ImageDataAutoMapper mapper(image_data); | 167 webkit::ppapi::ImageDataAutoMapper mapper(image_data); |
163 if (!mapper.is_valid()) | 168 if (!mapper.is_valid()) { |
164 return PP_ERROR_FAILED; | 169 SendGetFrameErrorReply(PP_ERROR_FAILED); |
| 170 return; |
| 171 } |
165 | 172 |
166 const SkBitmap* bitmap = image_data->GetMappedBitmap(); | 173 const SkBitmap* bitmap = image_data->GetMappedBitmap(); |
167 if (!bitmap) | 174 if (!bitmap) { |
168 return PP_ERROR_FAILED; | 175 SendGetFrameErrorReply(PP_ERROR_FAILED); |
| 176 return; |
| 177 } |
169 uint8_t* bitmap_pixels = static_cast<uint8_t*>(bitmap->getPixels()); | 178 uint8_t* bitmap_pixels = static_cast<uint8_t*>(bitmap->getPixels()); |
170 if (!bitmap_pixels) | 179 if (!bitmap_pixels) { |
171 return PP_ERROR_FAILED; | 180 SendGetFrameErrorReply(PP_ERROR_FAILED); |
| 181 return; |
| 182 } |
172 | 183 |
173 size_t bitmap_size = bitmap->getSize(); | 184 size_t bitmap_size = bitmap->getSize(); |
174 frame->ConvertToRgbBuffer(cricket::FOURCC_BGRA, | 185 frame->ConvertToRgbBuffer(cricket::FOURCC_BGRA, |
175 bitmap_pixels, | 186 bitmap_pixels, |
176 bitmap_size, | 187 bitmap_size, |
177 bitmap->rowBytes()); | 188 bitmap->rowBytes()); |
178 | 189 |
179 image_data_resource->SetHostResource(pp_instance(), resource.get()); | 190 ppapi::HostResource host_resource; |
| 191 host_resource.SetHostResource(pp_instance(), resource.get()); |
180 | 192 |
181 // Convert a video timestamp (int64, in nanoseconds) to a time delta (int64, | 193 // Convert a video timestamp (int64, in nanoseconds) to a time delta (int64, |
182 // microseconds) and then to a PP_TimeTicks (a double, in seconds). All times | 194 // microseconds) and then to a PP_TimeTicks (a double, in seconds). All times |
183 // are relative to the Unix Epoch. | 195 // are relative to the Unix Epoch. |
184 base::TimeDelta time_delta = base::TimeDelta::FromMicroseconds( | 196 base::TimeDelta time_delta = base::TimeDelta::FromMicroseconds( |
185 frame->GetTimeStamp() / base::Time::kNanosecondsPerMicrosecond); | 197 frame->GetTimeStamp() / base::Time::kNanosecondsPerMicrosecond); |
186 *timestamp = time_delta.InSecondsF(); | 198 PP_TimeTicks timestamp = time_delta.InSecondsF(); |
187 return PP_OK; | 199 |
| 200 reply_context_.params.set_result(PP_OK); |
| 201 |
| 202 // TODO(bbudge) Change the PDF Host's image creation code to match. |
| 203 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_ANDROID) |
| 204 ppapi::proxy::SerializedHandle serialized_handle; |
| 205 PpapiPluginMsg_VideoSource_GetFrameReply reply_msg(host_resource, |
| 206 image_desc, |
| 207 0, |
| 208 timestamp); |
| 209 serialized_handle.set_shmem(image_handle, byte_count); |
| 210 reply_context_.params.AppendHandle(serialized_handle); |
| 211 #elif defined(OS_LINUX) |
| 212 // For Linux, we pass the SysV shared memory key in the message. |
| 213 PpapiPluginMsg_VideoSource_GetFrameReply reply_msg(host_resource, |
| 214 image_desc, |
| 215 image_handle.fd, |
| 216 timestamp); |
| 217 #else |
| 218 // Not supported on other platforms. |
| 219 // This is a stub reply_msg to not break the build. |
| 220 PpapiPluginMsg_VideoSource_GetFrameReply reply_msg(host_resource, |
| 221 image_desc, |
| 222 0, |
| 223 timestamp); |
| 224 NOTIMPLEMENTED(); |
| 225 SendGetFrameErrorReply(PP_ERROR_NOTSUPPORTED); |
| 226 return; |
| 227 #endif |
| 228 |
| 229 host()->SendReply(reply_context_, reply_msg); |
| 230 |
| 231 reply_context_ = ppapi::host::ReplyMessageContext(); |
| 232 |
| 233 // Keep a reference once we know this method succeeds. |
| 234 resource.Release(); |
188 } | 235 } |
189 | 236 |
190 void PepperVideoSourceHost::SendFrame( | 237 void PepperVideoSourceHost::SendGetFrameErrorReply(int32_t error) { |
191 const ppapi::HostResource& image_data_resource, | 238 reply_context_.params.set_result(error); |
192 PP_TimeTicks timestamp, | |
193 int32_t result) { | |
194 DCHECK(get_frame_pending_); | |
195 reply_context_.params.set_result(result); | |
196 host()->SendReply( | 239 host()->SendReply( |
197 reply_context_, | 240 reply_context_, |
198 PpapiPluginMsg_VideoSource_GetFrameReply(image_data_resource, timestamp)); | 241 PpapiPluginMsg_VideoSource_GetFrameReply( |
199 | 242 ppapi::HostResource(), PP_ImageDataDesc(), -1, 0.0)); |
200 reply_context_ = ppapi::host::ReplyMessageContext(); | 243 reply_context_ = ppapi::host::ReplyMessageContext(); |
201 get_frame_pending_ = false; | |
202 } | 244 } |
203 | 245 |
204 void PepperVideoSourceHost::Close() { | 246 void PepperVideoSourceHost::Close() { |
205 if (source_handler_.get()) { | 247 if (source_handler_.get() && !stream_url_.empty()) |
206 source_handler_->Close(stream_url_, this); | 248 source_handler_->Close(stream_url_, frame_receiver_.get()); |
207 source_handler_.reset(NULL); | 249 |
208 } | 250 source_handler_.reset(NULL); |
| 251 stream_url_.clear(); |
209 } | 252 } |
210 | 253 |
211 } // namespace content | 254 } // namespace content |
OLD | NEW |