| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/media/image_capture_frame_grabber.h" | 5 #include "content/renderer/media/image_capture_frame_grabber.h" |
| 6 | 6 |
| 7 #include "cc/paint/paint_canvas.h" |
| 8 #include "cc/paint/paint_surface.h" |
| 7 #include "media/base/bind_to_current_loop.h" | 9 #include "media/base/bind_to_current_loop.h" |
| 8 #include "media/base/video_frame.h" | 10 #include "media/base/video_frame.h" |
| 9 #include "media/base/video_util.h" | 11 #include "media/base/video_util.h" |
| 10 #include "skia/ext/platform_canvas.h" | 12 #include "skia/ext/platform_canvas.h" |
| 11 #include "third_party/WebKit/public/platform/WebCallbacks.h" | 13 #include "third_party/WebKit/public/platform/WebCallbacks.h" |
| 12 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | 14 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
| 13 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" | 15 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
| 14 #include "third_party/libyuv/include/libyuv.h" | 16 #include "third_party/libyuv/include/libyuv.h" |
| 15 #include "third_party/skia/include/core/SkImage.h" | 17 #include "third_party/skia/include/core/SkImage.h" |
| 16 #include "third_party/skia/include/core/SkSurface.h" | |
| 17 | 18 |
| 18 namespace content { | 19 namespace content { |
| 19 | 20 |
| 20 using blink::WebImageCaptureGrabFrameCallbacks; | 21 using blink::WebImageCaptureGrabFrameCallbacks; |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 void OnError(std::unique_ptr<WebImageCaptureGrabFrameCallbacks> callbacks) { | 25 void OnError(std::unique_ptr<WebImageCaptureGrabFrameCallbacks> callbacks) { |
| 25 callbacks->onError(); | 26 callbacks->onError(); |
| 26 } | 27 } |
| 27 | 28 |
| 28 } // anonymous namespace | 29 } // anonymous namespace |
| 29 | 30 |
| 30 // Ref-counted class to receive a single VideoFrame on IO thread, convert it and | 31 // Ref-counted class to receive a single VideoFrame on IO thread, convert it and |
| 31 // send it to |main_task_runner_|, where this class is created and destroyed. | 32 // send it to |main_task_runner_|, where this class is created and destroyed. |
| 32 class ImageCaptureFrameGrabber::SingleShotFrameHandler | 33 class ImageCaptureFrameGrabber::SingleShotFrameHandler |
| 33 : public base::RefCountedThreadSafe<SingleShotFrameHandler> { | 34 : public base::RefCountedThreadSafe<SingleShotFrameHandler> { |
| 34 public: | 35 public: |
| 35 SingleShotFrameHandler() : first_frame_received_(false) {} | 36 SingleShotFrameHandler() : first_frame_received_(false) {} |
| 36 | 37 |
| 37 // Receives a |frame| and converts its pixels into a SkImage via an internal | 38 // Receives a |frame| and converts its pixels into a SkImage via an internal |
| 38 // SkSurface and SkPixmap. Alpha channel, if any, is copied. | 39 // PaintSurface and SkPixmap. Alpha channel, if any, is copied. |
| 39 void OnVideoFrameOnIOThread(SkImageDeliverCB callback, | 40 void OnVideoFrameOnIOThread(SkImageDeliverCB callback, |
| 40 const scoped_refptr<media::VideoFrame>& frame, | 41 const scoped_refptr<media::VideoFrame>& frame, |
| 41 base::TimeTicks current_time); | 42 base::TimeTicks current_time); |
| 42 | 43 |
| 43 private: | 44 private: |
| 44 friend class base::RefCountedThreadSafe<SingleShotFrameHandler>; | 45 friend class base::RefCountedThreadSafe<SingleShotFrameHandler>; |
| 45 virtual ~SingleShotFrameHandler() {} | 46 virtual ~SingleShotFrameHandler() {} |
| 46 | 47 |
| 47 // Flag to indicate that the first frames has been processed, and subsequent | 48 // Flag to indicate that the first frames has been processed, and subsequent |
| 48 // ones can be safely discarded. | 49 // ones can be safely discarded. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 62 if (first_frame_received_) | 63 if (first_frame_received_) |
| 63 return; | 64 return; |
| 64 first_frame_received_ = true; | 65 first_frame_received_ = true; |
| 65 | 66 |
| 66 const SkAlphaType alpha = media::IsOpaque(frame->format()) | 67 const SkAlphaType alpha = media::IsOpaque(frame->format()) |
| 67 ? kOpaque_SkAlphaType | 68 ? kOpaque_SkAlphaType |
| 68 : kPremul_SkAlphaType; | 69 : kPremul_SkAlphaType; |
| 69 const SkImageInfo info = SkImageInfo::MakeN32( | 70 const SkImageInfo info = SkImageInfo::MakeN32( |
| 70 frame->visible_rect().width(), frame->visible_rect().height(), alpha); | 71 frame->visible_rect().width(), frame->visible_rect().height(), alpha); |
| 71 | 72 |
| 72 sk_sp<SkSurface> surface = SkSurface::MakeRaster(info); | 73 sk_sp<cc::PaintSurface> surface = cc::PaintSurface::MakeRaster(info); |
| 73 DCHECK(surface); | 74 DCHECK(surface); |
| 74 | 75 |
| 75 SkPixmap pixmap; | 76 SkPixmap pixmap; |
| 76 if (!skia::GetWritablePixels(surface->getCanvas(), &pixmap)) { | 77 if (!cc::ToPixmap(surface->getCanvas(), &pixmap)) { |
| 77 DLOG(ERROR) << "Error trying to map SkSurface's pixels"; | 78 DLOG(ERROR) << "Error trying to map PaintSurface's pixels"; |
| 78 callback.Run(sk_sp<SkImage>()); | 79 callback.Run(sk_sp<SkImage>()); |
| 79 return; | 80 return; |
| 80 } | 81 } |
| 81 | 82 |
| 82 const uint32 destination_pixel_format = | 83 const uint32 destination_pixel_format = |
| 83 (kN32_SkColorType == kRGBA_8888_SkColorType) ? libyuv::FOURCC_ABGR | 84 (kN32_SkColorType == kRGBA_8888_SkColorType) ? libyuv::FOURCC_ABGR |
| 84 : libyuv::FOURCC_ARGB; | 85 : libyuv::FOURCC_ARGB; |
| 85 | 86 |
| 86 libyuv::ConvertFromI420(frame->visible_data(media::VideoFrame::kYPlane), | 87 libyuv::ConvertFromI420(frame->visible_data(media::VideoFrame::kYPlane), |
| 87 frame->stride(media::VideoFrame::kYPlane), | 88 frame->stride(media::VideoFrame::kYPlane), |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 | 155 |
| 155 MediaStreamVideoSink::DisconnectFromTrack(); | 156 MediaStreamVideoSink::DisconnectFromTrack(); |
| 156 frame_grab_in_progress_ = false; | 157 frame_grab_in_progress_ = false; |
| 157 if (image) | 158 if (image) |
| 158 callbacks.PassCallbacks()->onSuccess(image); | 159 callbacks.PassCallbacks()->onSuccess(image); |
| 159 else | 160 else |
| 160 callbacks.PassCallbacks()->onError(); | 161 callbacks.PassCallbacks()->onError(); |
| 161 } | 162 } |
| 162 | 163 |
| 163 } // namespace content | 164 } // namespace content |
| OLD | NEW |