blob: 1bb6ef61bf6b429d94fe0fb1c7307bd17e0c8cd6 [file] [log] [blame]
Henrik Boströmce0ea492020-01-13 11:27:18 +01001/*
2 * Copyright 2020 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef VIDEO_VIDEO_SOURCE_SINK_CONTROLLER_H_
12#define VIDEO_VIDEO_SOURCE_SINK_CONTROLLER_H_
13
Evan Shrubsole236e0ed2020-05-20 12:24:57 +020014#include <string>
Henrik Boström1124ed12021-02-25 10:30:39 +010015#include <vector>
Evan Shrubsole236e0ed2020-05-20 12:24:57 +020016
Henrik Boströmce0ea492020-01-13 11:27:18 +010017#include "absl/types/optional.h"
Artem Titovd15a5752021-02-10 14:31:24 +010018#include "api/sequence_checker.h"
Henrik Boströmce0ea492020-01-13 11:27:18 +010019#include "api/video/video_frame.h"
20#include "api/video/video_sink_interface.h"
21#include "api/video/video_source_interface.h"
Henrik Boström62057622020-03-10 19:08:05 +010022#include "call/adaptation/video_source_restrictions.h"
Mirko Bonadei20e4c802020-11-23 11:07:42 +010023#include "rtc_base/system/no_unique_address.h"
Henrik Boströmce0ea492020-01-13 11:27:18 +010024
25namespace webrtc {
26
27// Responsible for configuring source/sink settings, i.e. performing
28// rtc::VideoSourceInterface<VideoFrame>::AddOrUpdateSink(). It does this by
29// storing settings internally which are converted to rtc::VideoSinkWants when
30// PushSourceSinkSettings() is performed.
31class VideoSourceSinkController {
32 public:
33 VideoSourceSinkController(rtc::VideoSinkInterface<VideoFrame>* sink,
34 rtc::VideoSourceInterface<VideoFrame>* source);
35
Tomas Gunnarsson612445e2020-09-21 14:31:23 +020036 ~VideoSourceSinkController();
37
Henrik Boström8234b922020-01-13 17:26:50 +010038 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source);
Tomas Gunnarsson612445e2020-09-21 14:31:23 +020039 bool HasSource() const;
40
Markus Handell2e0f4f02021-12-21 19:14:58 +010041 // Requests a refresh frame from the current source, if set.
42 void RequestRefreshFrame();
43
Henrik Boströmce0ea492020-01-13 11:27:18 +010044 // Must be called in order for changes to settings to have an effect. This
45 // allows you to modify multiple properties in a single push to the sink.
46 void PushSourceSinkSettings();
47
48 VideoSourceRestrictions restrictions() const;
49 absl::optional<size_t> pixels_per_frame_upper_limit() const;
50 absl::optional<double> frame_rate_upper_limit() const;
51 bool rotation_applied() const;
52 int resolution_alignment() const;
Henrik Boström1124ed12021-02-25 10:30:39 +010053 const std::vector<rtc::VideoSinkWants::FrameSize>& resolutions() const;
Jonas Oreland0deda152022-09-23 12:08:57 +020054 bool active() const;
55 absl::optional<rtc::VideoSinkWants::FrameSize> requested_resolution() const;
Henrik Boströmce0ea492020-01-13 11:27:18 +010056
57 // Updates the settings stored internally. In order for these settings to be
58 // applied to the sink, PushSourceSinkSettings() must subsequently be called.
59 void SetRestrictions(VideoSourceRestrictions restrictions);
60 void SetPixelsPerFrameUpperLimit(
61 absl::optional<size_t> pixels_per_frame_upper_limit);
62 void SetFrameRateUpperLimit(absl::optional<double> frame_rate_upper_limit);
63 void SetRotationApplied(bool rotation_applied);
64 void SetResolutionAlignment(int resolution_alignment);
Henrik Boström1124ed12021-02-25 10:30:39 +010065 void SetResolutions(std::vector<rtc::VideoSinkWants::FrameSize> resolutions);
Jonas Oreland0deda152022-09-23 12:08:57 +020066 void SetActive(bool active);
67 void SetRequestedResolution(
68 absl::optional<rtc::VideoSinkWants::FrameSize> requested_resolution);
Henrik Boströmce0ea492020-01-13 11:27:18 +010069
Henrik Boströmce0ea492020-01-13 11:27:18 +010070 private:
Henrik Boström8234b922020-01-13 17:26:50 +010071 rtc::VideoSinkWants CurrentSettingsToSinkWants() const
Tomas Gunnarsson612445e2020-09-21 14:31:23 +020072 RTC_EXCLUSIVE_LOCKS_REQUIRED(sequence_checker_);
Henrik Boströmce0ea492020-01-13 11:27:18 +010073
Tomas Gunnarsson612445e2020-09-21 14:31:23 +020074 // Used to ensure that this class is called on threads/sequences that it and
75 // downstream implementations were designed for.
76 // In practice, this represent's libjingle's worker thread.
Mirko Bonadei20e4c802020-11-23 11:07:42 +010077 RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_;
Tomas Gunnarsson612445e2020-09-21 14:31:23 +020078
Henrik Boströmce0ea492020-01-13 11:27:18 +010079 rtc::VideoSinkInterface<VideoFrame>* const sink_;
Tomas Gunnarsson612445e2020-09-21 14:31:23 +020080 rtc::VideoSourceInterface<VideoFrame>* source_
81 RTC_GUARDED_BY(&sequence_checker_);
Henrik Boströmce0ea492020-01-13 11:27:18 +010082 // Pixel and frame rate restrictions.
Tomas Gunnarsson612445e2020-09-21 14:31:23 +020083 VideoSourceRestrictions restrictions_ RTC_GUARDED_BY(&sequence_checker_);
Henrik Boströmce0ea492020-01-13 11:27:18 +010084 // Ensures that even if we are not restricted, the sink is never configured
Artem Titovab30d722021-07-27 16:22:11 +020085 // above this limit. Example: We are not CPU limited (no `restrictions_`) but
86 // our encoder is capped at 30 fps (= `frame_rate_upper_limit_`).
Tomas Gunnarsson612445e2020-09-21 14:31:23 +020087 absl::optional<size_t> pixels_per_frame_upper_limit_
88 RTC_GUARDED_BY(&sequence_checker_);
89 absl::optional<double> frame_rate_upper_limit_
90 RTC_GUARDED_BY(&sequence_checker_);
91 bool rotation_applied_ RTC_GUARDED_BY(&sequence_checker_) = false;
92 int resolution_alignment_ RTC_GUARDED_BY(&sequence_checker_) = 1;
Henrik Boström1124ed12021-02-25 10:30:39 +010093 std::vector<rtc::VideoSinkWants::FrameSize> resolutions_
94 RTC_GUARDED_BY(&sequence_checker_);
Jonas Oreland0deda152022-09-23 12:08:57 +020095 bool active_ RTC_GUARDED_BY(&sequence_checker_) = true;
96 absl::optional<rtc::VideoSinkWants::FrameSize> requested_resolution_
97 RTC_GUARDED_BY(&sequence_checker_);
Henrik Boströmce0ea492020-01-13 11:27:18 +010098};
99
100} // namespace webrtc
101
102#endif // VIDEO_VIDEO_SOURCE_SINK_CONTROLLER_H_