summaryrefslogtreecommitdiff
path: root/chromium/components/viz/host/client_frame_sink_video_capturer.h
blob: dd6c5fb4f1829c9e2b7eb2b0b5a0d0c14b7b9357 (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
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2018 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 COMPONENTS_VIZ_HOST_CLIENT_FRAME_SINK_VIDEO_CAPTURER_H_
#define COMPONENTS_VIZ_HOST_CLIENT_FRAME_SINK_VIDEO_CAPTURER_H_

#include "base/callback.h"
#include "base/time/time.h"
#include "components/viz/common/surfaces/frame_sink_id.h"
#include "components/viz/host/viz_host_export.h"
#include "media/base/video_types.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "services/viz/privileged/interfaces/compositing/frame_sink_video_capture.mojom.h"
#include "ui/gfx/geometry/size.h"

namespace viz {

// Client library for using FrameSinkVideoCapturer. Clients should use this
// class instead of talking directly to FrameSinkVideoCapturer in order to
// survive Viz crashes.
// TODO(samans): Move this class and all its dependencies to the client
// directory.
class VIZ_HOST_EXPORT ClientFrameSinkVideoCapturer
    : private mojom::FrameSinkVideoConsumer {
 public:
  using EstablishConnectionCallback =
      base::RepeatingCallback<void(mojom::FrameSinkVideoCapturerRequest)>;

  explicit ClientFrameSinkVideoCapturer(EstablishConnectionCallback callback);
  ~ClientFrameSinkVideoCapturer() override;

  // See FrameSinkVideoCapturer for documentation.
  void SetFormat(media::VideoPixelFormat format, media::ColorSpace color_space);
  void SetMinCapturePeriod(base::TimeDelta min_capture_period);
  void SetMinSizeChangePeriod(base::TimeDelta min_period);
  void SetResolutionConstraints(const gfx::Size& min_size,
                                const gfx::Size& max_size,
                                bool use_fixed_aspect_ratio);
  void SetAutoThrottlingEnabled(bool enabled);
  void ChangeTarget(const FrameSinkId& frame_sink_id);
  void Stop();
  void RequestRefreshFrame();

  // Similar to FrameSinkVideoCapturer::Start, but takes in a pointer directly
  // to the FrameSinkVideoConsumer implemenation class (as opposed to a
  // mojo::InterfacePtr or a proxy object).
  void Start(mojom::FrameSinkVideoConsumer* consumer);

  // Similar to Stop() but also resets the consumer immediately so no further
  // messages (even OnStopped()) will be delivered to the consumer.
  void StopAndResetConsumer();

 private:
  struct Format {
    Format(media::VideoPixelFormat pixel_format, media::ColorSpace color_space);

    media::VideoPixelFormat pixel_format;
    media::ColorSpace color_space;
  };

  struct ResolutionConstraints {
    ResolutionConstraints(const gfx::Size& min_size,
                          const gfx::Size& max_size,
                          bool use_fixed_aspect_ratio);

    gfx::Size min_size;
    gfx::Size max_size;
    bool use_fixed_aspect_ratio;
  };

  // mojom::FrameSinkVideoConsumer implementation.
  void OnFrameCaptured(
      mojo::ScopedSharedBufferHandle buffer,
      uint32_t buffer_size,
      media::mojom::VideoFrameInfoPtr info,
      const gfx::Rect& update_rect,
      const gfx::Rect& content_rect,
      mojom::FrameSinkVideoConsumerFrameCallbacksPtr callbacks) final;
  void OnTargetLost(const FrameSinkId& frame_sink_id) final;
  void OnStopped() final;

  // Establishes connection to FrameSinkVideoCapturer and sends the existing
  // configuration.
  void EstablishConnection();

  // Called when the message pipe is gone. Will call EstablishConnection after
  // some delay.
  void OnConnectionError();

  void StartInternal();

  // The following variables keep the latest arguments provided to their
  // corresponding method in mojom::FrameSinkVideoCapturer. The arguments are
  // saved so we can resend them if viz crashes and a new FrameSinkVideoCapturer
  // has to be created.
  base::Optional<Format> format_;
  base::Optional<base::TimeDelta> min_capture_period_;
  base::Optional<base::TimeDelta> min_size_change_period_;
  base::Optional<ResolutionConstraints> resolution_constraints_;
  base::Optional<bool> auto_throttling_enabled_;
  base::Optional<FrameSinkId> target_;
  bool is_started_ = false;

  mojom::FrameSinkVideoConsumer* consumer_ = nullptr;
  EstablishConnectionCallback establish_connection_callback_;
  mojom::FrameSinkVideoCapturerPtr capturer_;
  mojo::Binding<mojom::FrameSinkVideoConsumer> consumer_binding_;

  base::WeakPtrFactory<ClientFrameSinkVideoCapturer> weak_factory_;
};

}  // namespace viz

#endif  // COMPONENTS_VIZ_HOST_CLIENT_FRAME_SINK_VIDEO_CAPTURER_H_