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

Side by Side Diff: content/renderer/media/canvas_capture_handler.cc

Issue 1467103003: Basic use implementation for MediaStream from Canvas: captureStream() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years 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
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/renderer/media/canvas_capture_handler.h"
6
7 #include "base/bind_helpers.h"
8 #include "content/public/renderer/media_stream_api.h"
9 #include "content/public/renderer/render_thread.h"
10 #include "content/renderer/render_thread_impl.h"
11 #include "third_party/libyuv/include/libyuv.h"
12
13 namespace {
14 const float kDefaultFrameRate = 60.0f;
15 } // anonymous namespace
16
17 namespace content {
18
19 class CanvasCaptureHandler::VideoCapturerSource
20 : public media::VideoCapturerSource {
21 public:
22 explicit VideoCapturerSource(const scoped_refptr<CanvasCaptureHandler>
23 canvas_handler)
perkj_chrome 2015/11/26 18:52:14 Have you tried git cl format?
emircan 2015/12/01 00:25:44 Fixed it. I am using clang-format:Chromium on Ecli
24 : canvas_handler_(canvas_handler) {}
25
26 protected:
27 void GetCurrentSupportedFormats(
28 int max_requested_width,
29 int max_requested_height,
30 double max_requested_frame_rate,
31 const VideoCaptureDeviceFormatsCB& callback) override;
32 void StartCapture(const media::VideoCaptureParams& params,
33 const VideoCaptureDeliverFrameCB& frame_callback,
34 const RunningCallback& running_callback) override {
35 canvas_handler_->StartVideoCapture(params, frame_callback,
36 running_callback);
37 }
38 void StopCapture() override {
39 canvas_handler_->StopVideoCapture();
40 }
41
42 private:
43 // CanvasCaptureHandler is owned by blink and guaranteed to be alive during
44 // the lifetime of this class.
45 scoped_refptr<CanvasCaptureHandler> canvas_handler_;
46 };
47
48 void CanvasCaptureHandler::VideoCapturerSource::GetCurrentSupportedFormats(
49 int max_requested_width,
50 int max_requested_height,
51 double max_requested_frame_rate,
52 const VideoCaptureDeviceFormatsCB& callback) {
53 const blink::WebSize& size = canvas_handler_->GetSourceSize();
54 const media::VideoCaptureFormat format(gfx::Size(size.width, size.height),
55 kDefaultFrameRate,
56 media::PIXEL_FORMAT_I420);
57 media::VideoCaptureFormats formats;
58 formats.push_back(format);
59 callback.Run(formats);
60 }
61
62 class CanvasCaptureHandler::CanvasCaptureHandlerDelegate {
63 public:
64 explicit CanvasCaptureHandlerDelegate(
65 media::VideoCapturerSource::VideoCaptureDeliverFrameCB new_frame_callback)
66 : new_frame_callback_(new_frame_callback),
67 io_task_runner_(content::RenderThread::Get()->GetIOMessageLoopProxy()) {
68 }
69
70 void sendNewFrameOnIOThread(
71 const scoped_refptr<media::VideoFrame>& video_frame,
72 const base::TimeTicks& current_time) {
73 DCHECK(io_task_runner_->BelongsToCurrentThread());
74 new_frame_callback_.Run(video_frame, current_time);
75 }
76
77 private:
78 const media::VideoCapturerSource::VideoCaptureDeliverFrameCB
79 new_frame_callback_;
80 const scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
81 DISALLOW_COPY_AND_ASSIGN(CanvasCaptureHandlerDelegate);
82 };
83
84 CanvasCaptureHandler::CanvasCaptureHandler(const blink::WebSize& size,
85 blink::WebMediaStream* stream)
86 : ask_for_new_frame_(false),
87 size_(size),
88 io_task_runner_(content::RenderThread::Get()->GetIOMessageLoopProxy()) {
89 scoped_ptr<media::VideoCapturerSource> video(
90 new CanvasCaptureHandler::VideoCapturerSource(
91 scoped_refptr<CanvasCaptureHandler>(this)));
92 content::AddVideoTrackToMediaStream(video.Pass(), false, false, stream);
perkj_chrome 2015/11/26 18:52:14 I think you should mark this as read_only. Ie - yo
emircan 2015/12/01 00:25:44 Done.
93 }
94
95 CanvasCaptureHandler::~CanvasCaptureHandler() {
96 DVLOG(3) << __FUNCTION__;
97 DCHECK(thread_checker_.CalledOnValidThread());
98 }
99
100 void CanvasCaptureHandler::sendNewFrame(const blink::WebSkImage& image) {
101 createNewFrame(image);
perkj_chrome 2015/11/26 18:52:14 add thread checker
emircan 2015/12/01 00:25:45 Done.
102 }
103
104 bool CanvasCaptureHandler::needsNewFrameCapture() const {
105 return ask_for_new_frame_;
perkj_chrome 2015/11/26 18:52:14 add thread checker
perkj_chrome 2015/11/26 18:52:14 ask_for_new_frame_ this seems to be unnessecary?
emircan 2015/12/01 00:25:44 When CanvasCaptureHandler::VideoCapturerSource::St
emircan 2015/12/01 00:25:44 Done.
106 }
107
108 void CanvasCaptureHandler::StartVideoCapture(
109 const media::VideoCaptureParams& params,
110 const media::VideoCapturerSource::VideoCaptureDeliverFrameCB&
111 new_frame_callback,
112 const media::VideoCapturerSource::RunningCallback& running_callback) {
113 DVLOG(3) << __FUNCTION__ << " requested "
114 << media::VideoCaptureFormat::ToString(params.requested_format);
115 DCHECK(params.requested_format.IsValid());
116 DCHECK(thread_checker_.CalledOnValidThread());
117
118 // TODO(emircan): Accomodate to the given frame rate constraints here.
119 capture_format_ = params.requested_format;
120
121 delegate_ =
122 make_scoped_ptr(new CanvasCaptureHandlerDelegate(new_frame_callback));
123 DCHECK(delegate_);
124 ask_for_new_frame_ = true;
125 running_callback.Run(true);
126 }
127
128 void CanvasCaptureHandler::StopVideoCapture() {
129 DVLOG(3) << __FUNCTION__;
130 DCHECK(thread_checker_.CalledOnValidThread());
131 ask_for_new_frame_ = false;
132 delegate_.reset();
133 }
134
135 void CanvasCaptureHandler::createNewFrame(const blink::WebSkImage& image) {
136 DCHECK(thread_checker_.CalledOnValidThread());
137
138 DCHECK(!image.isNull());
139 const gfx::Size size(image.width(), image.height());
140 if (size != last_size) {
141 temp_data_.resize(
142 media::VideoFrame::AllocationSize(media::PIXEL_FORMAT_ARGB, size));
143 row_bytes_ = media::VideoFrame::RowBytes(
144 0, media::PIXEL_FORMAT_ARGB, capture_format_.frame_size.width());
145 image_info_ =
146 SkImageInfo::Make(size.width(), size.height(),
147 kBGRA_8888_SkColorType, kPremul_SkAlphaType);
148 last_size = size;
149 }
150
151 image.readPixels(image_info_, &temp_data_[0], row_bytes_, 0, 0);
152 scoped_refptr<media::VideoFrame> video_frame =
153 frame_pool_.CreateFrame(media::PIXEL_FORMAT_I420, size, gfx::Rect(size),
154 size, base::TimeTicks::Now() - base::TimeTicks());
155 libyuv::ARGBToI420(temp_data_.data(), row_bytes_,
156 video_frame->data(media::VideoFrame::kYPlane),
157 video_frame->stride(media::VideoFrame::kYPlane),
158 video_frame->data(media::VideoFrame::kUPlane),
159 video_frame->stride(media::VideoFrame::kUPlane),
160 video_frame->data(media::VideoFrame::kVPlane),
161 video_frame->stride(media::VideoFrame::kVPlane),
162 size.width(), size.height());
163
164 if (!delegate_)
165 return;
166 io_task_runner_->PostTask(
167 FROM_HERE,
168 base::Bind(&CanvasCaptureHandler::CanvasCaptureHandlerDelegate::
169 sendNewFrameOnIOThread,
170 base::Unretained(delegate_.get()), video_frame,
perkj_chrome 2015/11/26 18:52:14 This does not seem to be safe. You post to the io
emircan 2015/12/01 00:25:45 Thanks for pointing out. Added a weak_ptr for the
171 base::TimeTicks()));
172 }
173
174 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698