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" | |
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 last_frame_.reset(frame); |
Ronghua Wu (Left Chromium)
2013/05/05 15:39:35
This callback will be called from the libjingle wo
bbudge
2013/05/06 04:02:15
Done. I added a lock. It wasn't clear from comment
Ronghua Wu (Left Chromium)
2013/05/06 04:42:07
Thanks. My bad.
| |
56 if (!gurl.is_valid()) | 59 |
57 return PP_ERROR_BADARGUMENT; | 60 if (get_frame_pending_) { |
58 // TODO(ronghuawu) Check that gurl is a valid MediaStream video track URL. | 61 ppapi::HostResource image_data_resource; |
59 // TODO(ronghuawu) Open a MediaStream video track. | 62 PP_TimeTicks timestamp = 0; |
60 ReplyMessageContext reply_context = context->MakeReplyMessageContext(); | 63 int32_t result = ConvertFrame(&image_data_resource, ×tamp); |
61 reply_context.params.set_result(PP_OK); | 64 SendFrame(image_data_resource, timestamp, result); |
62 host()->SendReply(reply_context, PpapiPluginMsg_VideoSource_OpenReply()); | 65 } |
63 return PP_OK_COMPLETIONPENDING; | 66 |
67 return true; | |
64 } | 68 } |
65 | 69 |
66 int32_t PepperVideoSourceHost::OnHostMsgGetFrame( | 70 int32_t PepperVideoSourceHost::ConvertFrame( |
67 HostMessageContext* context) { | 71 ppapi::HostResource* image_data_resource, |
68 ReplyMessageContext reply_context = context->MakeReplyMessageContext(); | 72 PP_TimeTicks* timestamp) { |
69 // TODO(ronghuawu) Wait until a frame with timestamp > last_timestamp_ is | 73 scoped_ptr<cricket::VideoFrame> frame(last_frame_.release()); |
70 // available. | 74 int32_t width = static_cast<int32_t>(frame->GetWidth()); |
75 int32_t height = static_cast<int32_t>(frame->GetHeight()); | |
71 // Create an image data resource to hold the frame pixels. | 76 // Create an image data resource to hold the frame pixels. |
72 PP_ImageDataDesc desc; | 77 PP_ImageDataDesc desc; |
73 IPC::PlatformFileForTransit image_handle; | 78 IPC::PlatformFileForTransit image_handle; |
74 uint32_t byte_count; | 79 uint32_t byte_count; |
75 ppapi::ScopedPPResource resource( | 80 ppapi::ScopedPPResource resource( |
76 ppapi::ScopedPPResource::PassRef(), | 81 ppapi::ScopedPPResource::PassRef(), |
77 ppapi::proxy::PPB_ImageData_Proxy::CreateImageData( | 82 ppapi::proxy::PPB_ImageData_Proxy::CreateImageData( |
78 pp_instance(), | 83 pp_instance(), |
79 webkit::ppapi::PPB_ImageData_Impl::GetNativeImageDataFormat(), | 84 PP_IMAGEDATAFORMAT_BGRA_PREMUL, |
80 PP_MakeSize(0, 0), | 85 PP_MakeSize(width, height), |
81 false /* init_to_zero */, | 86 false /* init_to_zero */, |
82 false /* is_nacl_plugin */, | 87 false /* is_nacl_plugin */, |
83 &desc, &image_handle, &byte_count)); | 88 &desc, &image_handle, &byte_count)); |
84 if (!resource.get()) | 89 if (!resource.get()) |
85 return PP_ERROR_FAILED; | 90 return PP_ERROR_FAILED; |
86 | 91 |
87 ppapi::thunk::EnterResourceNoLock<ppapi::thunk::PPB_ImageData_API> | 92 ppapi::thunk::EnterResourceNoLock<ppapi::thunk::PPB_ImageData_API> |
88 enter_resource(resource, false); | 93 enter_resource(resource, false); |
89 if (enter_resource.failed()) | 94 if (enter_resource.failed()) |
90 return PP_ERROR_FAILED; | 95 return PP_ERROR_FAILED; |
91 | 96 |
92 webkit::ppapi::PPB_ImageData_Impl* image_data = | 97 webkit::ppapi::PPB_ImageData_Impl* image_data = |
93 static_cast<webkit::ppapi::PPB_ImageData_Impl*>(enter_resource.object()); | 98 static_cast<webkit::ppapi::PPB_ImageData_Impl*>(enter_resource.object()); |
94 webkit::ppapi::ImageDataAutoMapper mapper(image_data); | 99 webkit::ppapi::ImageDataAutoMapper mapper(image_data); |
95 if (!mapper.is_valid()) | 100 if (!mapper.is_valid()) |
96 return PP_ERROR_FAILED; | 101 return PP_ERROR_FAILED; |
97 | 102 |
98 // TODO(ronghuawu) Copy frame pixels to canvas. | 103 const SkBitmap* bitmap = image_data->GetMappedBitmap(); |
104 if (!bitmap) | |
105 return PP_ERROR_FAILED; | |
106 uint8_t* bitmap_pixels = static_cast<uint8_t*>(bitmap->getPixels()); | |
107 if (!bitmap_pixels) | |
108 return PP_ERROR_FAILED; | |
99 | 109 |
100 ppapi::HostResource image_data_resource; | 110 size_t bitmap_size = bitmap->getSize(); |
101 image_data_resource.SetHostResource(pp_instance(), resource.get()); | 111 frame->ConvertToRgbBuffer(cricket::FOURCC_BGRA, |
102 double timestamp = 0; | 112 bitmap_pixels, |
113 bitmap_size, | |
114 bitmap->rowBytes()); | |
115 | |
116 image_data_resource->SetHostResource(pp_instance(), resource.get()); | |
117 *timestamp = static_cast<PP_TimeTicks>(frame->GetTimeStamp()) / | |
118 talk_base::kNumNanosecsPerSec; | |
119 return PP_OK; | |
120 } | |
121 | |
122 void PepperVideoSourceHost::SendFrame( | |
123 const ppapi::HostResource& image_data_resource, | |
124 PP_TimeTicks timestamp, | |
125 int32_t result) { | |
126 reply_context_.params.set_result(result); | |
127 host()->SendReply( | |
128 reply_context_, | |
129 PpapiPluginMsg_VideoSource_GetFrameReply(image_data_resource, timestamp)); | |
130 | |
131 reply_context_ = ppapi::host::ReplyMessageContext(); | |
132 get_frame_pending_ = false; | |
133 } | |
134 | |
135 int32_t PepperVideoSourceHost::OnHostMsgOpen(HostMessageContext* context, | |
136 const std::string& stream_url) { | |
137 GURL gurl(stream_url); | |
138 if (!gurl.is_valid()) | |
139 return PP_ERROR_BADARGUMENT; | |
140 | |
141 stream_url_ = gurl.spec(); | |
142 if (!source_handler_->Open(stream_url_, this)) | |
143 return PP_ERROR_BADARGUMENT; | |
144 | |
145 ReplyMessageContext reply_context = context->MakeReplyMessageContext(); | |
103 reply_context.params.set_result(PP_OK); | 146 reply_context.params.set_result(PP_OK); |
104 host()->SendReply( | 147 host()->SendReply(reply_context, PpapiPluginMsg_VideoSource_OpenReply()); |
105 reply_context, | 148 return PP_OK_COMPLETIONPENDING; |
106 PpapiPluginMsg_VideoSource_GetFrameReply(image_data_resource, timestamp)); | 149 } |
107 last_timestamp_ = timestamp; | 150 |
151 int32_t PepperVideoSourceHost::OnHostMsgGetFrame( | |
152 HostMessageContext* context) { | |
153 if (get_frame_pending_) | |
154 return PP_ERROR_INPROGRESS; | |
155 | |
156 reply_context_ = context->MakeReplyMessageContext(); | |
157 get_frame_pending_ = true; | |
158 | |
159 // If a frame is ready, try to convert it and reply. | |
160 if (last_frame_.get()) { | |
161 ppapi::HostResource image_data_resource; | |
162 PP_TimeTicks timestamp = 0; | |
163 int32_t result = ConvertFrame(&image_data_resource, ×tamp); | |
164 if (result == PP_OK) { | |
165 SendFrame(image_data_resource, timestamp, result); | |
166 } else { | |
167 reply_context_ = ppapi::host::ReplyMessageContext(); | |
168 return result; | |
169 } | |
170 } | |
171 | |
108 return PP_OK_COMPLETIONPENDING; | 172 return PP_OK_COMPLETIONPENDING; |
109 } | 173 } |
110 | 174 |
111 int32_t PepperVideoSourceHost::OnHostMsgClose(HostMessageContext* context) { | 175 int32_t PepperVideoSourceHost::OnHostMsgClose(HostMessageContext* context) { |
112 // TODO(ronghuawu) Close the video stream. | 176 source_handler_->Close(stream_url_, this); |
113 return PP_OK; | 177 return PP_OK; |
114 } | 178 } |
115 | 179 |
116 } // namespace content | 180 } // namespace content |
OLD | NEW |