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() { |
raymes
2013/05/10 15:14:48
I think the state is a little hard to follow, alth
bbudge
2013/05/10 17:38:21
Done.
| |
132 ppapi::HostResource* image_data_resource, | |
133 PP_TimeTicks* timestamp) { | |
134 DCHECK(last_frame_.get()); | 132 DCHECK(last_frame_.get()); |
135 scoped_ptr<cricket::VideoFrame> frame(last_frame_.release()); | 133 scoped_ptr<cricket::VideoFrame> frame(last_frame_.release()); |
136 | 134 |
137 int32_t width = base::checked_numeric_cast<int32_t>(frame->GetWidth()); | 135 int32_t width = base::checked_numeric_cast<int32_t>(frame->GetWidth()); |
138 int32_t height = base::checked_numeric_cast<int32_t>(frame->GetHeight()); | 136 int32_t height = base::checked_numeric_cast<int32_t>(frame->GetHeight()); |
139 // Create an image data resource to hold the frame pixels. | 137 // Create an image data resource to hold the frame pixels. |
140 PP_ImageDataDesc desc; | 138 PP_ImageDataDesc image_desc; |
141 IPC::PlatformFileForTransit image_handle; | 139 IPC::PlatformFileForTransit image_handle; |
142 uint32_t byte_count; | 140 uint32_t byte_count; |
143 ppapi::ScopedPPResource resource( | 141 ppapi::ScopedPPResource resource( |
144 ppapi::ScopedPPResource::PassRef(), | 142 ppapi::ScopedPPResource::PassRef(), |
145 ppapi::proxy::PPB_ImageData_Proxy::CreateImageData( | 143 ppapi::proxy::PPB_ImageData_Proxy::CreateImageData( |
146 pp_instance(), | 144 pp_instance(), |
147 PP_IMAGEDATAFORMAT_BGRA_PREMUL, | 145 PP_IMAGEDATAFORMAT_BGRA_PREMUL, |
148 PP_MakeSize(width, height), | 146 PP_MakeSize(width, height), |
149 false /* init_to_zero */, | 147 false /* init_to_zero */, |
150 false /* is_nacl_plugin */, | 148 false /* is_nacl_plugin */, |
151 &desc, &image_handle, &byte_count)); | 149 &image_desc, &image_handle, &byte_count)); |
152 if (!resource.get()) | 150 if (!resource.get()) |
153 return PP_ERROR_FAILED; | 151 return ReportGetFrameError(PP_ERROR_FAILED); |
raymes
2013/05/10 15:14:48
Since ReportGetFrameError() doesn't return a value
bbudge
2013/05/10 17:38:21
Done.
| |
154 | 152 |
155 ppapi::thunk::EnterResourceNoLock<ppapi::thunk::PPB_ImageData_API> | 153 ppapi::thunk::EnterResourceNoLock<ppapi::thunk::PPB_ImageData_API> |
156 enter_resource(resource, false); | 154 enter_resource(resource, false); |
157 if (enter_resource.failed()) | 155 if (enter_resource.failed()) |
158 return PP_ERROR_FAILED; | 156 return ReportGetFrameError(PP_ERROR_FAILED); |
159 | 157 |
160 webkit::ppapi::PPB_ImageData_Impl* image_data = | 158 webkit::ppapi::PPB_ImageData_Impl* image_data = |
161 static_cast<webkit::ppapi::PPB_ImageData_Impl*>(enter_resource.object()); | 159 static_cast<webkit::ppapi::PPB_ImageData_Impl*>(enter_resource.object()); |
162 webkit::ppapi::ImageDataAutoMapper mapper(image_data); | 160 webkit::ppapi::ImageDataAutoMapper mapper(image_data); |
163 if (!mapper.is_valid()) | 161 if (!mapper.is_valid()) |
164 return PP_ERROR_FAILED; | 162 return ReportGetFrameError(PP_ERROR_FAILED); |
165 | 163 |
166 const SkBitmap* bitmap = image_data->GetMappedBitmap(); | 164 const SkBitmap* bitmap = image_data->GetMappedBitmap(); |
167 if (!bitmap) | 165 if (!bitmap) |
168 return PP_ERROR_FAILED; | 166 return ReportGetFrameError(PP_ERROR_FAILED); |
169 uint8_t* bitmap_pixels = static_cast<uint8_t*>(bitmap->getPixels()); | 167 uint8_t* bitmap_pixels = static_cast<uint8_t*>(bitmap->getPixels()); |
170 if (!bitmap_pixels) | 168 if (!bitmap_pixels) |
171 return PP_ERROR_FAILED; | 169 return ReportGetFrameError(PP_ERROR_FAILED); |
172 | 170 |
173 size_t bitmap_size = bitmap->getSize(); | 171 size_t bitmap_size = bitmap->getSize(); |
174 frame->ConvertToRgbBuffer(cricket::FOURCC_BGRA, | 172 frame->ConvertToRgbBuffer(cricket::FOURCC_BGRA, |
175 bitmap_pixels, | 173 bitmap_pixels, |
176 bitmap_size, | 174 bitmap_size, |
177 bitmap->rowBytes()); | 175 bitmap->rowBytes()); |
178 | 176 |
179 image_data_resource->SetHostResource(pp_instance(), resource.get()); | 177 ppapi::HostResource host_resource; |
178 host_resource.SetHostResource(pp_instance(), resource.get()); | |
180 | 179 |
181 // Convert a video timestamp (int64, in nanoseconds) to a time delta (int64, | 180 // 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 | 181 // microseconds) and then to a PP_TimeTicks (a double, in seconds). All times |
183 // are relative to the Unix Epoch. | 182 // are relative to the Unix Epoch. |
184 base::TimeDelta time_delta = base::TimeDelta::FromMicroseconds( | 183 base::TimeDelta time_delta = base::TimeDelta::FromMicroseconds( |
185 frame->GetTimeStamp() / base::Time::kNanosecondsPerMicrosecond); | 184 frame->GetTimeStamp() / base::Time::kNanosecondsPerMicrosecond); |
186 *timestamp = time_delta.InSecondsF(); | 185 PP_TimeTicks timestamp = time_delta.InSecondsF(); |
187 return PP_OK; | 186 |
187 DCHECK(get_frame_pending_); | |
188 reply_context_.params.set_result(PP_OK); | |
189 | |
190 // TODO(bbudge) Change the PDF Host's image creation code to match. | |
191 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_ANDROID) | |
192 ppapi::proxy::SerializedHandle serialized_handle; | |
193 PpapiPluginMsg_VideoSource_GetFrameReply reply_msg(host_resource, | |
194 image_desc, | |
195 0, | |
196 timestamp); | |
197 serialized_handle.set_shmem(image_handle, byte_count); | |
198 reply_context_.params.AppendHandle(serialized_handle); | |
199 #elif defined(OS_LINUX) | |
200 // For Linux, we pass the SysV shared memory key in the message. | |
201 PpapiPluginMsg_VideoSource_GetFrameReply reply_msg(host_resource, | |
202 image_desc, | |
203 image_handle.fd, | |
204 timestamp); | |
205 #else | |
206 // Not supported on other platforms. | |
207 // This is a stub reply_msg to not break the build. | |
208 PpapiPluginMsg_VideoSource_GetFrameReply reply_msg(host_resource, | |
209 image_desc, | |
210 0, | |
211 timestamp); | |
212 NOTIMPLEMENTED(); | |
213 return ReportGetFrameError(PP_ERROR_NOTSUPPORTED); | |
214 #endif | |
215 | |
216 host()->SendReply(reply_context_, reply_msg); | |
217 | |
218 reply_context_ = ppapi::host::ReplyMessageContext(); | |
219 get_frame_pending_ = false; | |
220 | |
221 // Keep a reference once we know this method succeeds. | |
222 resource.Release(); | |
188 } | 223 } |
189 | 224 |
190 void PepperVideoSourceHost::SendFrame( | 225 void PepperVideoSourceHost::ReportGetFrameError(int32_t error) { |
191 const ppapi::HostResource& image_data_resource, | |
192 PP_TimeTicks timestamp, | |
193 int32_t result) { | |
194 DCHECK(get_frame_pending_); | 226 DCHECK(get_frame_pending_); |
195 reply_context_.params.set_result(result); | 227 reply_context_.params.set_result(error); |
196 host()->SendReply( | 228 host()->SendReply( |
197 reply_context_, | 229 reply_context_, |
198 PpapiPluginMsg_VideoSource_GetFrameReply(image_data_resource, timestamp)); | 230 PpapiPluginMsg_VideoSource_GetFrameReply( |
199 | 231 ppapi::HostResource(), PP_ImageDataDesc(), -1, 0.0)); |
200 reply_context_ = ppapi::host::ReplyMessageContext(); | 232 reply_context_ = ppapi::host::ReplyMessageContext(); |
201 get_frame_pending_ = false; | 233 get_frame_pending_ = false; |
202 } | 234 } |
203 | 235 |
204 void PepperVideoSourceHost::Close() { | 236 void PepperVideoSourceHost::Close() { |
205 if (source_handler_.get()) { | 237 if (source_handler_.get() && !stream_url_.empty()) { |
206 source_handler_->Close(stream_url_, this); | 238 source_handler_->Close(stream_url_, frame_receiver_.get()); |
207 source_handler_.reset(NULL); | 239 source_handler_.reset(NULL); |
240 stream_url_.clear(); | |
208 } | 241 } |
209 } | 242 } |
210 | 243 |
211 } // namespace content | 244 } // namespace content |
OLD | NEW |