Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(670)

Side by Side Diff: content/renderer/pepper/pepper_video_source_host.cc

Issue 14968002: Glue code to connect PPAPI proxy to MediaStream sources and destinations. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix casts. Use base::Time for timestamp calculation.x Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "content/public/renderer/renderer_ppapi_host.h" 9 #include "content/public/renderer/renderer_ppapi_host.h"
10 #include "content/renderer/render_thread_impl.h"
9 #include "ppapi/c/pp_errors.h" 11 #include "ppapi/c/pp_errors.h"
10 #include "ppapi/host/dispatch_host_message.h" 12 #include "ppapi/host/dispatch_host_message.h"
11 #include "ppapi/host/host_message_context.h"
12 #include "ppapi/host/ppapi_host.h" 13 #include "ppapi/host/ppapi_host.h"
13 #include "ppapi/proxy/ppapi_messages.h" 14 #include "ppapi/proxy/ppapi_messages.h"
14 #include "ppapi/proxy/ppb_image_data_proxy.h" 15 #include "ppapi/proxy/ppb_image_data_proxy.h"
15 #include "ppapi/shared_impl/scoped_pp_resource.h" 16 #include "ppapi/shared_impl/scoped_pp_resource.h"
16 #include "ppapi/thunk/enter.h" 17 #include "ppapi/thunk/enter.h"
17 #include "ppapi/thunk/ppb_image_data_api.h" 18 #include "ppapi/thunk/ppb_image_data_api.h"
18 #include "skia/ext/platform_canvas.h" 19 #include "third_party/libjingle/source/talk/media/base/videocommon.h"
20 #include "third_party/libjingle/source/talk/media/base/videoframe.h"
21 #include "third_party/skia/include/core/SkBitmap.h"
19 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" 22 #include "webkit/plugins/ppapi/ppb_image_data_impl.h"
20 23
21 using ppapi::host::HostMessageContext; 24 using ppapi::host::HostMessageContext;
22 using ppapi::host::ReplyMessageContext; 25 using ppapi::host::ReplyMessageContext;
23 26
24 namespace content { 27 namespace content {
25 28
26 PepperVideoSourceHost::PepperVideoSourceHost( 29 PepperVideoSourceHost::PepperVideoSourceHost(
27 RendererPpapiHost* host, 30 RendererPpapiHost* host,
28 PP_Instance instance, 31 PP_Instance instance,
29 PP_Resource resource) 32 PP_Resource resource)
30 : ResourceHost(host->GetPpapiHost(), instance, resource), 33 : ResourceHost(host->GetPpapiHost(), instance, resource),
31 renderer_ppapi_host_(host), 34 renderer_ppapi_host_(host),
32 weak_factory_(this), 35 weak_factory_(this),
33 last_timestamp_(0) { 36 main_message_loop_proxy_(base::MessageLoopProxy::current()),
37 source_handler_(new content::VideoSourceHandler(NULL)),
38 get_frame_pending_(false) {
34 } 39 }
35 40
36 PepperVideoSourceHost::~PepperVideoSourceHost() { 41 PepperVideoSourceHost::~PepperVideoSourceHost() {
42 Close();
37 } 43 }
38 44
39 int32_t PepperVideoSourceHost::OnResourceMessageReceived( 45 int32_t PepperVideoSourceHost::OnResourceMessageReceived(
40 const IPC::Message& msg, 46 const IPC::Message& msg,
41 HostMessageContext* context) { 47 HostMessageContext* context) {
42 IPC_BEGIN_MESSAGE_MAP(PepperVideoSourceHost, msg) 48 IPC_BEGIN_MESSAGE_MAP(PepperVideoSourceHost, msg)
43 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoSource_Open, 49 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoSource_Open,
44 OnHostMsgOpen) 50 OnHostMsgOpen)
45 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_GetFrame, 51 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_GetFrame,
46 OnHostMsgGetFrame) 52 OnHostMsgGetFrame)
47 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_Close, 53 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_Close,
48 OnHostMsgClose) 54 OnHostMsgClose)
49 IPC_END_MESSAGE_MAP() 55 IPC_END_MESSAGE_MAP()
50 return PP_ERROR_FAILED; 56 return PP_ERROR_FAILED;
51 } 57 }
52 58
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, &timestamp);
79 SendFrame(image_data_resource, timestamp, result);
80 }
81 }
82
53 int32_t PepperVideoSourceHost::OnHostMsgOpen(HostMessageContext* context, 83 int32_t PepperVideoSourceHost::OnHostMsgOpen(HostMessageContext* context,
54 const std::string& stream_url) { 84 const std::string& stream_url) {
55 GURL gurl(stream_url); 85 GURL gurl(stream_url);
56 if (!gurl.is_valid()) 86 if (!gurl.is_valid())
57 return PP_ERROR_BADARGUMENT; 87 return PP_ERROR_BADARGUMENT;
58 // TODO(ronghuawu) Check that gurl is a valid MediaStream video track URL. 88
59 // TODO(ronghuawu) Open a MediaStream video track. 89 if (!source_handler_->Open(gurl.spec(), this))
90 return PP_ERROR_BADARGUMENT;
91
92 stream_url_ = gurl.spec();
93
60 ReplyMessageContext reply_context = context->MakeReplyMessageContext(); 94 ReplyMessageContext reply_context = context->MakeReplyMessageContext();
61 reply_context.params.set_result(PP_OK); 95 reply_context.params.set_result(PP_OK);
62 host()->SendReply(reply_context, PpapiPluginMsg_VideoSource_OpenReply()); 96 host()->SendReply(reply_context, PpapiPluginMsg_VideoSource_OpenReply());
63 return PP_OK_COMPLETIONPENDING; 97 return PP_OK_COMPLETIONPENDING;
64 } 98 }
65 99
66 int32_t PepperVideoSourceHost::OnHostMsgGetFrame( 100 int32_t PepperVideoSourceHost::OnHostMsgGetFrame(
67 HostMessageContext* context) { 101 HostMessageContext* context) {
68 ReplyMessageContext reply_context = context->MakeReplyMessageContext(); 102 if (!source_handler_.get())
69 // TODO(ronghuawu) Wait until a frame with timestamp > last_timestamp_ is 103 return PP_ERROR_FAILED;
70 // available. 104 if (get_frame_pending_)
105 return PP_ERROR_INPROGRESS;
106
107 reply_context_ = context->MakeReplyMessageContext();
108 get_frame_pending_ = true;
109
110 // If a frame is ready, try to convert it and reply.
111 if (last_frame_.get()) {
112 ppapi::HostResource image_data_resource;
113 PP_TimeTicks timestamp = 0;
114 int32_t result = ConvertFrame(&image_data_resource, &timestamp);
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
123 return PP_OK_COMPLETIONPENDING;
124 }
125
126 int32_t PepperVideoSourceHost::OnHostMsgClose(HostMessageContext* context) {
127 Close();
128 return PP_OK;
129 }
130
131 int32_t PepperVideoSourceHost::ConvertFrame(
132 ppapi::HostResource* image_data_resource,
133 PP_TimeTicks* timestamp) {
134 DCHECK(last_frame_.get());
135 scoped_ptr<cricket::VideoFrame> frame(last_frame_.release());
136
137 int32_t width = base::checked_numeric_cast<int32_t>(frame->GetWidth());
138 int32_t height = base::checked_numeric_cast<int32_t>(frame->GetHeight());
71 // Create an image data resource to hold the frame pixels. 139 // Create an image data resource to hold the frame pixels.
72 PP_ImageDataDesc desc; 140 PP_ImageDataDesc desc;
73 IPC::PlatformFileForTransit image_handle; 141 IPC::PlatformFileForTransit image_handle;
74 uint32_t byte_count; 142 uint32_t byte_count;
75 ppapi::ScopedPPResource resource( 143 ppapi::ScopedPPResource resource(
76 ppapi::ScopedPPResource::PassRef(), 144 ppapi::ScopedPPResource::PassRef(),
77 ppapi::proxy::PPB_ImageData_Proxy::CreateImageData( 145 ppapi::proxy::PPB_ImageData_Proxy::CreateImageData(
78 pp_instance(), 146 pp_instance(),
79 webkit::ppapi::PPB_ImageData_Impl::GetNativeImageDataFormat(), 147 PP_IMAGEDATAFORMAT_BGRA_PREMUL,
80 PP_MakeSize(0, 0), 148 PP_MakeSize(width, height),
81 false /* init_to_zero */, 149 false /* init_to_zero */,
82 false /* is_nacl_plugin */, 150 false /* is_nacl_plugin */,
83 &desc, &image_handle, &byte_count)); 151 &desc, &image_handle, &byte_count));
84 if (!resource.get()) 152 if (!resource.get())
85 return PP_ERROR_FAILED; 153 return PP_ERROR_FAILED;
86 154
87 ppapi::thunk::EnterResourceNoLock<ppapi::thunk::PPB_ImageData_API> 155 ppapi::thunk::EnterResourceNoLock<ppapi::thunk::PPB_ImageData_API>
88 enter_resource(resource, false); 156 enter_resource(resource, false);
89 if (enter_resource.failed()) 157 if (enter_resource.failed())
90 return PP_ERROR_FAILED; 158 return PP_ERROR_FAILED;
91 159
92 webkit::ppapi::PPB_ImageData_Impl* image_data = 160 webkit::ppapi::PPB_ImageData_Impl* image_data =
93 static_cast<webkit::ppapi::PPB_ImageData_Impl*>(enter_resource.object()); 161 static_cast<webkit::ppapi::PPB_ImageData_Impl*>(enter_resource.object());
94 webkit::ppapi::ImageDataAutoMapper mapper(image_data); 162 webkit::ppapi::ImageDataAutoMapper mapper(image_data);
95 if (!mapper.is_valid()) 163 if (!mapper.is_valid())
96 return PP_ERROR_FAILED; 164 return PP_ERROR_FAILED;
97 165
98 // TODO(ronghuawu) Copy frame pixels to canvas. 166 const SkBitmap* bitmap = image_data->GetMappedBitmap();
167 if (!bitmap)
168 return PP_ERROR_FAILED;
169 uint8_t* bitmap_pixels = static_cast<uint8_t*>(bitmap->getPixels());
170 if (!bitmap_pixels)
171 return PP_ERROR_FAILED;
99 172
100 ppapi::HostResource image_data_resource; 173 size_t bitmap_size = bitmap->getSize();
101 image_data_resource.SetHostResource(pp_instance(), resource.get()); 174 frame->ConvertToRgbBuffer(cricket::FOURCC_BGRA,
102 double timestamp = 0; 175 bitmap_pixels,
103 reply_context.params.set_result(PP_OK); 176 bitmap_size,
104 host()->SendReply( 177 bitmap->rowBytes());
105 reply_context,
106 PpapiPluginMsg_VideoSource_GetFrameReply(image_data_resource, timestamp));
107 last_timestamp_ = timestamp;
108 return PP_OK_COMPLETIONPENDING;
109 }
110 178
111 int32_t PepperVideoSourceHost::OnHostMsgClose(HostMessageContext* context) { 179 image_data_resource->SetHostResource(pp_instance(), resource.get());
112 // TODO(ronghuawu) Close the video stream. 180 // The frame timestamp is in nanoseconds. Convert to TimeDelta (microseconds)
181 // and then to Pepper PP_TimeTicks, which is a double holding seconds.
182 base::TimeDelta time_delta = base::TimeDelta::FromMicroseconds(
183 frame->GetTimeStamp() / base::Time::kNanosecondsPerMicrosecond);
184 *timestamp = time_delta.InSecondsF();
113 return PP_OK; 185 return PP_OK;
114 } 186 }
115 187
188 void PepperVideoSourceHost::SendFrame(
189 const ppapi::HostResource& image_data_resource,
190 PP_TimeTicks timestamp,
191 int32_t result) {
192 DCHECK(get_frame_pending_);
193 reply_context_.params.set_result(result);
194 host()->SendReply(
195 reply_context_,
196 PpapiPluginMsg_VideoSource_GetFrameReply(image_data_resource, timestamp));
197
198 reply_context_ = ppapi::host::ReplyMessageContext();
199 get_frame_pending_ = false;
200 }
201
202 void PepperVideoSourceHost::Close() {
203 if (source_handler_.get()) {
204 source_handler_->Close(stream_url_, this);
205 source_handler_.reset(NULL);
206 }
207 }
208
116 } // namespace content 209 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698