summaryrefslogtreecommitdiffstats
path: root/content/renderer/media/canvas_capture_handler.h
blob: 5424994e8ac25d64fcecefe269ef36cee995b591 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_RENDERER_MEDIA_CANVAS_CAPTURE_HANDLER_H_
#define CONTENT_RENDERER_MEDIA_CANVAS_CAPTURE_HANDLER_H_

#include <stddef.h>
#include <stdint.h>

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/threading/thread_checker.h"
#include "content/common/content_export.h"
#include "media/base/video_capturer_source.h"
#include "media/base/video_frame_pool.h"
#include "third_party/WebKit/public/platform/WebCanvasCaptureHandler.h"
#include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
#include "third_party/WebKit/public/platform/WebSize.h"
#include "third_party/skia/include/core/SkImage.h"

namespace content {

// CanvasCaptureHandler acts as the link between Blink side HTMLCanvasElement
// and Chrome side VideoCapturerSource. It is responsible for handling
// SkImage instances sent from the Blink side, convert them to
// media::VideoFrame and plug them to the MediaStreamTrack.
// CanvasCaptureHandler instance is owned by a blink::CanvasDrawListener which
// is owned by a CanvasCaptureMediaStreamTrack.
// All methods are called on the same thread as construction and destruction,
// i.e. the Main Render thread. Note that a CanvasCaptureHandlerDelegate is
// used to send back frames to |io_task_runner_|, i.e. IO thread.
class CONTENT_EXPORT CanvasCaptureHandler final
    : public NON_EXPORTED_BASE(blink::WebCanvasCaptureHandler) {
 public:
  ~CanvasCaptureHandler() override;

  // Creates a CanvasCaptureHandler instance and updates UMA histogram.
  static CanvasCaptureHandler* CreateCanvasCaptureHandler(
      const blink::WebSize& size,
      double frame_rate,
      const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
      blink::WebMediaStreamTrack* track);

  // blink::WebCanvasCaptureHandler Implementation.
  void sendNewFrame(const SkImage* image) override;
  bool needsNewFrame() const override;

  // Functions called by media::VideoCapturerSource implementation.
  void StartVideoCapture(
      const media::VideoCaptureParams& params,
      const media::VideoCapturerSource::VideoCaptureDeliverFrameCB&
          new_frame_callback,
      const media::VideoCapturerSource::RunningCallback& running_callback);
  void RequestRefreshFrame();
  void StopVideoCapture();
  blink::WebSize GetSourceSize() const { return size_; }

 private:
  // A VideoCapturerSource instance is created, which is responsible for handing
  // stop&start callbacks back to CanvasCaptureHandler. That VideoCapturerSource
  // is then plugged into a MediaStreamTrack passed as |track|, and it is owned
  // by the Blink side MediaStreamSource.
  CanvasCaptureHandler(
      const blink::WebSize& size,
      double frame_rate,
      const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
      blink::WebMediaStreamTrack* track);

  void CreateNewFrame(const SkImage* image);
  void AddVideoCapturerSourceToVideoTrack(
      scoped_ptr<media::VideoCapturerSource> source,
      blink::WebMediaStreamTrack* web_track);

  // Object that does all the work of running |new_frame_callback_|.
  // Destroyed on |frame_callback_task_runner_| after the class is destroyed.
  class CanvasCaptureHandlerDelegate;

  media::VideoCaptureFormat capture_format_;
  bool ask_for_new_frame_;

  const blink::WebSize size_;
  gfx::Size last_size;
  std::vector<uint8_t> temp_data_;
  size_t row_bytes_;
  SkImageInfo image_info_;
  media::VideoFramePool frame_pool_;

  scoped_refptr<media::VideoFrame> last_frame_;

  const scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
  scoped_ptr<CanvasCaptureHandlerDelegate> delegate_;

  // Bound to Main Render thread.
  base::ThreadChecker main_render_thread_checker_;
  base::WeakPtrFactory<CanvasCaptureHandler> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(CanvasCaptureHandler);
};

}  // namespace content

#endif  // CONTENT_RENDERER_MEDIA_CANVAS_CAPTURE_HANDLER_H_