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

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: perkj@ comments. 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(CanvasCaptureHandler* canvas_handler)
23 : canvas_handler_(canvas_handler) {}
24
25 protected:
26 void GetCurrentSupportedFormats(
27 int max_requested_width,
28 int max_requested_height,
29 double max_requested_frame_rate,
30 const VideoCaptureDeviceFormatsCB& callback) override;
31 void StartCapture(const media::VideoCaptureParams& params,
32 const VideoCaptureDeliverFrameCB& frame_callback,
33 const RunningCallback& running_callback) override {
34 canvas_handler_->StartVideoCapture(params, frame_callback,
35 running_callback);
36 }
37 void StopCapture() override {
38 canvas_handler_->StopVideoCapture();
39 }
40
41 private:
42 // CanvasCaptureHandler is owned by blink and guaranteed to be alive during
43 // the lifetime of this class.
44 CanvasCaptureHandler* canvas_handler_;
45 };
46
47 void CanvasCaptureHandler::VideoCapturerSource::GetCurrentSupportedFormats(
perkj_chrome 2015/12/01 19:26:34 nit: please inline this method too or split out st
emircan 2015/12/01 21:07:34 Done.
48 int max_requested_width,
49 int max_requested_height,
50 double max_requested_frame_rate,
51 const VideoCaptureDeviceFormatsCB& callback) {
52 const blink::WebSize& size = canvas_handler_->GetSourceSize();
53 const media::VideoCaptureFormat format(gfx::Size(size.width, size.height),
54 kDefaultFrameRate,
55 media::PIXEL_FORMAT_I420);
56 media::VideoCaptureFormats formats;
57 formats.push_back(format);
58 callback.Run(formats);
59 }
60
61 class CanvasCaptureHandler::CanvasCaptureHandlerDelegate {
62 public:
63 explicit CanvasCaptureHandlerDelegate(
64 media::VideoCapturerSource::VideoCaptureDeliverFrameCB new_frame_callback)
65 : new_frame_callback_(new_frame_callback),
perkj_chrome 2015/12/01 19:26:34 Just use a normal ThreadChecker. In the ctor, call
emircan 2015/12/01 21:07:34 Done.
66 io_task_runner_(content::RenderThread::Get()->GetIOMessageLoopProxy()),
67 weak_ptr_factory_(this) {}
68
69 void SendNewFrameOnIOThread(
70 const scoped_refptr<media::VideoFrame>& video_frame,
71 const base::TimeTicks& current_time) {
72 DCHECK(io_task_runner_->BelongsToCurrentThread());
73 new_frame_callback_.Run(video_frame, current_time);
74 }
75
76 base::WeakPtr<CanvasCaptureHandlerDelegate> GetWeakPtrForIOThread() {
77 return weak_ptr_factory_.GetWeakPtr();
78 }
79
80 private:
81 const media::VideoCapturerSource::VideoCaptureDeliverFrameCB
82 new_frame_callback_;
83 const scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
84 base::WeakPtrFactory<CanvasCaptureHandlerDelegate> weak_ptr_factory_;
85 DISALLOW_COPY_AND_ASSIGN(CanvasCaptureHandlerDelegate);
perkj_chrome 2015/12/01 19:26:34 nit empty line before DISALLOW...
emircan 2015/12/01 21:07:34 Done.
86 };
87
88 CanvasCaptureHandler::CanvasCaptureHandler(const blink::WebSize& size,
89 blink::WebMediaStream* stream)
90 : ask_for_new_frame_(false),
91 size_(size),
92 io_task_runner_(content::RenderThread::Get()->GetIOMessageLoopProxy()) {
93 scoped_ptr<media::VideoCapturerSource> video(
94 new CanvasCaptureHandler::VideoCapturerSource(this));
95 content::AddVideoTrackToMediaStream(video.Pass(), false, true, stream);
96 }
97
98 CanvasCaptureHandler::~CanvasCaptureHandler() {
99 DVLOG(3) << __FUNCTION__;
100 DCHECK(thread_checker_.CalledOnValidThread());
101 io_task_runner_->DeleteSoon(FROM_HERE, delegate_.release());
102 }
103
104 void CanvasCaptureHandler::sendNewFrame(const blink::WebSkImage& image) {
105 DCHECK(thread_checker_.CalledOnValidThread());
106 CreateNewFrame(image);
107 }
108
109 bool CanvasCaptureHandler::needsNewFrameCapture() const {
110 DCHECK(thread_checker_.CalledOnValidThread());
111 return ask_for_new_frame_;
112 }
113
114 void CanvasCaptureHandler::StartVideoCapture(
115 const media::VideoCaptureParams& params,
116 const media::VideoCapturerSource::VideoCaptureDeliverFrameCB&
117 new_frame_callback,
118 const media::VideoCapturerSource::RunningCallback& running_callback) {
119 DVLOG(3) << __FUNCTION__ << " requested "
120 << media::VideoCaptureFormat::ToString(params.requested_format);
121 DCHECK(params.requested_format.IsValid());
122 DCHECK(thread_checker_.CalledOnValidThread());
123
124 // TODO(emircan): Accomodate to the given frame rate constraints here.
125 capture_format_ = params.requested_format;
126 delegate_ =
perkj_chrome 2015/12/01 19:26:34 nit: delegate.reset(new ...) Otherwise you will do
emircan 2015/12/01 21:07:34 Done.
127 make_scoped_ptr(new CanvasCaptureHandlerDelegate(new_frame_callback));
128 DCHECK(delegate_);
129 ask_for_new_frame_ = true;
130 running_callback.Run(true);
131 }
132
133 void CanvasCaptureHandler::StopVideoCapture() {
134 DVLOG(3) << __FUNCTION__;
135 DCHECK(thread_checker_.CalledOnValidThread());
136 ask_for_new_frame_ = false;
137 delegate_.reset();
perkj_chrome 2015/12/01 19:26:34 io_task_runner_->DeleteSoon(FROM ?
emircan 2015/12/01 21:07:34 Done.
138 }
139
140 void CanvasCaptureHandler::CreateNewFrame(const blink::WebSkImage& image) {
141 DCHECK(thread_checker_.CalledOnValidThread());
142
143 DCHECK(!image.isNull());
144 const gfx::Size size(image.width(), image.height());
145 if (size != last_size) {
146 temp_data_.resize(
147 media::VideoFrame::AllocationSize(media::PIXEL_FORMAT_ARGB, size));
148 row_bytes_ = media::VideoFrame::RowBytes(
149 0, media::PIXEL_FORMAT_ARGB, capture_format_.frame_size.width());
150 image_info_ =
151 SkImageInfo::Make(size.width(), size.height(), kBGRA_8888_SkColorType,
152 kPremul_SkAlphaType);
153 last_size = size;
154 }
155
156 image.readPixels(image_info_, &temp_data_[0], row_bytes_, 0, 0);
157 scoped_refptr<media::VideoFrame> video_frame =
158 frame_pool_.CreateFrame(media::PIXEL_FORMAT_I420, size, gfx::Rect(size),
159 size, base::TimeTicks::Now() - base::TimeTicks());
160 libyuv::ARGBToI420(temp_data_.data(), row_bytes_,
161 video_frame->data(media::VideoFrame::kYPlane),
162 video_frame->stride(media::VideoFrame::kYPlane),
163 video_frame->data(media::VideoFrame::kUPlane),
164 video_frame->stride(media::VideoFrame::kUPlane),
165 video_frame->data(media::VideoFrame::kVPlane),
166 video_frame->stride(media::VideoFrame::kVPlane),
167 size.width(), size.height());
168
169 if (!delegate_)
perkj_chrome 2015/12/01 19:26:34 nit: move to the just after the thread_checker_
esprehn 2015/12/01 20:04:26 how do you end up with no delegate here?
emircan 2015/12/01 21:07:34 I dont think it necessary to check any more.
emircan 2015/12/01 21:07:34 I was thinking of a possible call to this function
170 return;
171 io_task_runner_->PostTask(
172 FROM_HERE,
173 base::Bind(&CanvasCaptureHandler::CanvasCaptureHandlerDelegate::
174 SendNewFrameOnIOThread,
175 delegate_->GetWeakPtrForIOThread(), video_frame,
176 base::TimeTicks()));
177 }
178
179 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698