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 "content/public/renderer/renderer_ppapi_host.h" | 8 #include "content/public/renderer/renderer_ppapi_host.h" |
9 #include "content/renderer/render_thread_impl.h" | |
raymes1
2013/05/06 16:00:11
Is this needed?
bbudge
2013/05/06 19:27:50
Not any more. Done.
On 2013/05/06 16:00:11, raymes
| |
9 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
10 #include "ppapi/host/dispatch_host_message.h" | 11 #include "ppapi/host/dispatch_host_message.h" |
11 #include "ppapi/host/host_message_context.h" | |
12 #include "ppapi/host/ppapi_host.h" | 12 #include "ppapi/host/ppapi_host.h" |
13 #include "ppapi/proxy/ppapi_messages.h" | 13 #include "ppapi/proxy/ppapi_messages.h" |
14 #include "ppapi/proxy/ppb_image_data_proxy.h" | 14 #include "ppapi/proxy/ppb_image_data_proxy.h" |
15 #include "ppapi/shared_impl/scoped_pp_resource.h" | 15 #include "ppapi/shared_impl/scoped_pp_resource.h" |
16 #include "ppapi/thunk/enter.h" | 16 #include "ppapi/thunk/enter.h" |
17 #include "ppapi/thunk/ppb_image_data_api.h" | 17 #include "ppapi/thunk/ppb_image_data_api.h" |
18 #include "skia/ext/platform_canvas.h" | 18 #include "third_party/libjingle/source/talk/media/base/videocommon.h" |
19 #include "third_party/libjingle/source/talk/media/base/videoframe.h" | |
20 #include "third_party/skia/include/core/SkBitmap.h" | |
19 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" | 21 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" |
20 | 22 |
21 using ppapi::host::HostMessageContext; | 23 using ppapi::host::HostMessageContext; |
22 using ppapi::host::ReplyMessageContext; | 24 using ppapi::host::ReplyMessageContext; |
23 | 25 |
24 namespace content { | 26 namespace content { |
25 | 27 |
26 PepperVideoSourceHost::PepperVideoSourceHost( | 28 PepperVideoSourceHost::PepperVideoSourceHost( |
27 RendererPpapiHost* host, | 29 RendererPpapiHost* host, |
28 PP_Instance instance, | 30 PP_Instance instance, |
29 PP_Resource resource) | 31 PP_Resource resource) |
30 : ResourceHost(host->GetPpapiHost(), instance, resource), | 32 : ResourceHost(host->GetPpapiHost(), instance, resource), |
31 renderer_ppapi_host_(host), | 33 renderer_ppapi_host_(host), |
32 weak_factory_(this), | 34 weak_factory_(this), |
33 last_timestamp_(0) { | 35 source_handler_(new content::VideoSourceHandler(NULL)), |
36 get_frame_pending_(false) { | |
34 } | 37 } |
35 | 38 |
36 PepperVideoSourceHost::~PepperVideoSourceHost() { | 39 PepperVideoSourceHost::~PepperVideoSourceHost() { |
37 } | 40 } |
38 | 41 |
39 int32_t PepperVideoSourceHost::OnResourceMessageReceived( | 42 int32_t PepperVideoSourceHost::OnResourceMessageReceived( |
40 const IPC::Message& msg, | 43 const IPC::Message& msg, |
41 HostMessageContext* context) { | 44 HostMessageContext* context) { |
42 IPC_BEGIN_MESSAGE_MAP(PepperVideoSourceHost, msg) | 45 IPC_BEGIN_MESSAGE_MAP(PepperVideoSourceHost, msg) |
43 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoSource_Open, | 46 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoSource_Open, |
44 OnHostMsgOpen) | 47 OnHostMsgOpen) |
45 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_GetFrame, | 48 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_GetFrame, |
46 OnHostMsgGetFrame) | 49 OnHostMsgGetFrame) |
47 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_Close, | 50 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_Close, |
48 OnHostMsgClose) | 51 OnHostMsgClose) |
49 IPC_END_MESSAGE_MAP() | 52 IPC_END_MESSAGE_MAP() |
50 return PP_ERROR_FAILED; | 53 return PP_ERROR_FAILED; |
51 } | 54 } |
52 | 55 |
53 int32_t PepperVideoSourceHost::OnHostMsgOpen(HostMessageContext* context, | 56 bool PepperVideoSourceHost::GotFrame(cricket::VideoFrame* frame) { |
54 const std::string& stream_url) { | 57 // Take ownership of the new frame, and possibly delete any unsent one. |
55 GURL gurl(stream_url); | 58 base::AutoLock lock(lock_); |
raymes
2013/05/06 05:11:33
Usually posting tasks is preferred over locks if i
bbudge
2013/05/06 19:27:50
Yes, I grepped and didn't find any resource using
| |
56 if (!gurl.is_valid()) | 59 last_frame_.reset(frame); |
57 return PP_ERROR_BADARGUMENT; | 60 |
58 // TODO(ronghuawu) Check that gurl is a valid MediaStream video track URL. | 61 if (get_frame_pending_) { |
59 // TODO(ronghuawu) Open a MediaStream video track. | 62 ppapi::HostResource image_data_resource; |
60 ReplyMessageContext reply_context = context->MakeReplyMessageContext(); | 63 PP_TimeTicks timestamp = 0; |
61 reply_context.params.set_result(PP_OK); | 64 int32_t result = ConvertFrame(&image_data_resource, ×tamp); |
raymes1
2013/05/06 16:00:11
Since we always call ConvertFrame and then SendFra
bbudge
2013/05/06 19:27:50
The reason I split these is that there were a lot
| |
62 host()->SendReply(reply_context, PpapiPluginMsg_VideoSource_OpenReply()); | 65 SendFrame(image_data_resource, timestamp, result); |
63 return PP_OK_COMPLETIONPENDING; | 66 } |
67 | |
68 return true; | |
64 } | 69 } |
65 | 70 |
66 int32_t PepperVideoSourceHost::OnHostMsgGetFrame( | 71 int32_t PepperVideoSourceHost::ConvertFrame( |
yzshen1
2013/05/06 16:47:28
We are not supposed to manipulate resource from a
bbudge
2013/05/06 19:27:50
Done.
| |
67 HostMessageContext* context) { | 72 ppapi::HostResource* image_data_resource, |
68 ReplyMessageContext reply_context = context->MakeReplyMessageContext(); | 73 PP_TimeTicks* timestamp) { |
69 // TODO(ronghuawu) Wait until a frame with timestamp > last_timestamp_ is | 74 scoped_ptr<cricket::VideoFrame> frame(last_frame_.release()); |
70 // available. | 75 int32_t width = static_cast<int32_t>(frame->GetWidth()); |
76 int32_t height = static_cast<int32_t>(frame->GetHeight()); | |
Tom Sepez
2013/05/06 17:19:35
How do we know these don't overflow when you cast
bbudge
2013/05/06 19:27:50
I'm assuming they're relatively small integers. I'
| |
71 // Create an image data resource to hold the frame pixels. | 77 // Create an image data resource to hold the frame pixels. |
72 PP_ImageDataDesc desc; | 78 PP_ImageDataDesc desc; |
73 IPC::PlatformFileForTransit image_handle; | 79 IPC::PlatformFileForTransit image_handle; |
74 uint32_t byte_count; | 80 uint32_t byte_count; |
75 ppapi::ScopedPPResource resource( | 81 ppapi::ScopedPPResource resource( |
76 ppapi::ScopedPPResource::PassRef(), | 82 ppapi::ScopedPPResource::PassRef(), |
77 ppapi::proxy::PPB_ImageData_Proxy::CreateImageData( | 83 ppapi::proxy::PPB_ImageData_Proxy::CreateImageData( |
78 pp_instance(), | 84 pp_instance(), |
79 webkit::ppapi::PPB_ImageData_Impl::GetNativeImageDataFormat(), | 85 PP_IMAGEDATAFORMAT_BGRA_PREMUL, |
80 PP_MakeSize(0, 0), | 86 PP_MakeSize(width, height), |
81 false /* init_to_zero */, | 87 false /* init_to_zero */, |
82 false /* is_nacl_plugin */, | 88 false /* is_nacl_plugin */, |
83 &desc, &image_handle, &byte_count)); | 89 &desc, &image_handle, &byte_count)); |
84 if (!resource.get()) | 90 if (!resource.get()) |
85 return PP_ERROR_FAILED; | 91 return PP_ERROR_FAILED; |
86 | 92 |
87 ppapi::thunk::EnterResourceNoLock<ppapi::thunk::PPB_ImageData_API> | 93 ppapi::thunk::EnterResourceNoLock<ppapi::thunk::PPB_ImageData_API> |
88 enter_resource(resource, false); | 94 enter_resource(resource, false); |
89 if (enter_resource.failed()) | 95 if (enter_resource.failed()) |
90 return PP_ERROR_FAILED; | 96 return PP_ERROR_FAILED; |
91 | 97 |
92 webkit::ppapi::PPB_ImageData_Impl* image_data = | 98 webkit::ppapi::PPB_ImageData_Impl* image_data = |
93 static_cast<webkit::ppapi::PPB_ImageData_Impl*>(enter_resource.object()); | 99 static_cast<webkit::ppapi::PPB_ImageData_Impl*>(enter_resource.object()); |
94 webkit::ppapi::ImageDataAutoMapper mapper(image_data); | 100 webkit::ppapi::ImageDataAutoMapper mapper(image_data); |
95 if (!mapper.is_valid()) | 101 if (!mapper.is_valid()) |
96 return PP_ERROR_FAILED; | 102 return PP_ERROR_FAILED; |
97 | 103 |
98 // TODO(ronghuawu) Copy frame pixels to canvas. | 104 const SkBitmap* bitmap = image_data->GetMappedBitmap(); |
105 if (!bitmap) | |
106 return PP_ERROR_FAILED; | |
107 uint8_t* bitmap_pixels = static_cast<uint8_t*>(bitmap->getPixels()); | |
108 if (!bitmap_pixels) | |
109 return PP_ERROR_FAILED; | |
99 | 110 |
100 ppapi::HostResource image_data_resource; | 111 size_t bitmap_size = bitmap->getSize(); |
101 image_data_resource.SetHostResource(pp_instance(), resource.get()); | 112 frame->ConvertToRgbBuffer(cricket::FOURCC_BGRA, |
102 double timestamp = 0; | 113 bitmap_pixels, |
114 bitmap_size, | |
115 bitmap->rowBytes()); | |
116 | |
117 image_data_resource->SetHostResource(pp_instance(), resource.get()); | |
118 *timestamp = static_cast<PP_TimeTicks>(frame->GetTimeStamp()) / | |
119 talk_base::kNumNanosecsPerSec; | |
raymes1
2013/05/06 16:00:11
nit: 4 space indent
bbudge
2013/05/06 19:27:50
Done.
| |
120 return PP_OK; | |
121 } | |
122 | |
123 void PepperVideoSourceHost::SendFrame( | |
124 const ppapi::HostResource& image_data_resource, | |
125 PP_TimeTicks timestamp, | |
126 int32_t result) { | |
127 reply_context_.params.set_result(result); | |
128 host()->SendReply( | |
yzshen1
2013/05/06 16:47:28
host is not guaranteed to be thread safe as well.
bbudge
2013/05/06 19:27:50
Done.
| |
129 reply_context_, | |
130 PpapiPluginMsg_VideoSource_GetFrameReply(image_data_resource, timestamp)); | |
131 | |
132 reply_context_ = ppapi::host::ReplyMessageContext(); | |
133 get_frame_pending_ = false; | |
134 } | |
135 | |
136 int32_t PepperVideoSourceHost::OnHostMsgOpen(HostMessageContext* context, | |
137 const std::string& stream_url) { | |
138 GURL gurl(stream_url); | |
139 if (!gurl.is_valid()) | |
140 return PP_ERROR_BADARGUMENT; | |
141 | |
142 stream_url_ = gurl.spec(); | |
yzshen1
2013/05/06 16:47:28
nit: better to move it below the failure return at
bbudge
2013/05/06 19:27:50
Done.
| |
143 if (!source_handler_->Open(stream_url_, this)) | |
144 return PP_ERROR_BADARGUMENT; | |
145 | |
146 ReplyMessageContext reply_context = context->MakeReplyMessageContext(); | |
103 reply_context.params.set_result(PP_OK); | 147 reply_context.params.set_result(PP_OK); |
104 host()->SendReply( | 148 host()->SendReply(reply_context, PpapiPluginMsg_VideoSource_OpenReply()); |
105 reply_context, | 149 return PP_OK_COMPLETIONPENDING; |
106 PpapiPluginMsg_VideoSource_GetFrameReply(image_data_resource, timestamp)); | 150 } |
107 last_timestamp_ = timestamp; | 151 |
152 int32_t PepperVideoSourceHost::OnHostMsgGetFrame( | |
153 HostMessageContext* context) { | |
raymes1
2013/05/06 16:00:11
Should you check that Open() was successful before
bbudge
2013/05/06 19:27:50
Done.
| |
154 if (get_frame_pending_) | |
155 return PP_ERROR_INPROGRESS; | |
156 | |
157 reply_context_ = context->MakeReplyMessageContext(); | |
158 get_frame_pending_ = true; | |
Ronghua Wu (Left Chromium)
2013/05/06 04:42:07
You need to protect get_frame_pending_ as well. Mo
bbudge
2013/05/06 19:27:50
Done.
| |
159 | |
160 // If a frame is ready, try to convert it and reply. | |
161 base::AutoLock lock(lock_); | |
162 if (last_frame_.get()) { | |
163 ppapi::HostResource image_data_resource; | |
164 PP_TimeTicks timestamp = 0; | |
165 int32_t result = ConvertFrame(&image_data_resource, ×tamp); | |
166 if (result == PP_OK) { | |
167 SendFrame(image_data_resource, timestamp, result); | |
168 } else { | |
169 reply_context_ = ppapi::host::ReplyMessageContext(); | |
170 return result; | |
171 } | |
172 } | |
173 | |
108 return PP_OK_COMPLETIONPENDING; | 174 return PP_OK_COMPLETIONPENDING; |
109 } | 175 } |
110 | 176 |
111 int32_t PepperVideoSourceHost::OnHostMsgClose(HostMessageContext* context) { | 177 int32_t PepperVideoSourceHost::OnHostMsgClose(HostMessageContext* context) { |
112 // TODO(ronghuawu) Close the video stream. | 178 source_handler_->Close(stream_url_, this); |
raymes
2013/05/06 05:11:33
Do we need to worry about the case where the resou
bbudge
2013/05/06 19:27:50
I'm not sure whether the deletion of VideoSourceHa
| |
113 return PP_OK; | 179 return PP_OK; |
114 } | 180 } |
115 | 181 |
116 } // namespace content | 182 } // namespace content |
OLD | NEW |