blob: 667715a2725aa917b7da6e1497c1ef204995878b [file] [log] [blame]
nisse6f5a6c32016-09-22 01:25:59 -07001/*
2 * Copyright (c) 2016 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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef MEDIA_BASE_ADAPTED_VIDEO_TRACK_SOURCE_H_
12#define MEDIA_BASE_ADAPTED_VIDEO_TRACK_SOURCE_H_
nisse6f5a6c32016-09-22 01:25:59 -070013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stdint.h>
15
16#include "absl/types/optional.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "api/media_stream_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/notifier.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "api/video/video_frame.h"
20#include "api/video/video_sink_interface.h"
21#include "api/video/video_source_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "media/base/video_adapter.h"
23#include "media/base/video_broadcaster.h"
24#include "rtc_base/critical_section.h"
Mirko Bonadei66e76792019-04-02 11:33:59 +020025#include "rtc_base/system/rtc_export.h"
Yves Gerey3e707812018-11-28 16:47:49 +010026#include "rtc_base/thread_annotations.h"
nisse6f5a6c32016-09-22 01:25:59 -070027
28namespace rtc {
29
30// Base class for sources which needs video adaptation, e.g., video
31// capture sources. Sinks must be added and removed on one and only
32// one thread, while AdaptFrame and OnFrame may be called on any
33// thread.
Mirko Bonadei66e76792019-04-02 11:33:59 +020034class RTC_EXPORT AdaptedVideoTrackSource
nisse6f5a6c32016-09-22 01:25:59 -070035 : public webrtc::Notifier<webrtc::VideoTrackSourceInterface> {
36 public:
37 AdaptedVideoTrackSource();
Paulina Hensmana680a6a2018-04-05 11:42:24 +020038 ~AdaptedVideoTrackSource() override;
nisse6f5a6c32016-09-22 01:25:59 -070039
40 protected:
kthelgasonc8474172016-12-08 08:04:51 -080041 // Allows derived classes to initialize |video_adapter_| with a custom
42 // alignment.
Steve Antone78bcb92017-10-31 09:53:08 -070043 explicit AdaptedVideoTrackSource(int required_alignment);
nisse6f5a6c32016-09-22 01:25:59 -070044 // Checks the apply_rotation() flag. If the frame needs rotation, and it is a
45 // plain memory frame, it is rotated. Subclasses producing native frames must
46 // handle apply_rotation() themselves.
nisseacd935b2016-11-11 03:55:13 -080047 void OnFrame(const webrtc::VideoFrame& frame);
nisse6f5a6c32016-09-22 01:25:59 -070048
49 // Reports the appropriate frame size after adaptation. Returns true
50 // if a frame is wanted. Returns false if there are no interested
51 // sinks, or if the VideoAdapter decides to drop the frame.
52 bool AdaptFrame(int width,
53 int height,
54 int64_t time_us,
55 int* out_width,
56 int* out_height,
57 int* crop_width,
58 int* crop_height,
59 int* crop_x,
60 int* crop_y);
61
62 // Returns the current value of the apply_rotation flag, derived
63 // from the VideoSinkWants of registered sinks. The value is derived
64 // from sinks' wants, in AddOrUpdateSink and RemoveSink. Beware that
65 // when using this method from a different thread, the value may
66 // become stale before it is used.
67 bool apply_rotation();
68
69 cricket::VideoAdapter* video_adapter() { return &video_adapter_; }
70
71 private:
72 // Implements rtc::VideoSourceInterface.
nisseacd935b2016-11-11 03:55:13 -080073 void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
nisse6f5a6c32016-09-22 01:25:59 -070074 const rtc::VideoSinkWants& wants) override;
nisseacd935b2016-11-11 03:55:13 -080075 void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
nisse6f5a6c32016-09-22 01:25:59 -070076
77 // Part of VideoTrackSourceInterface.
78 bool GetStats(Stats* stats) override;
79
80 void OnSinkWantsChanged(const rtc::VideoSinkWants& wants);
81
nisse6f5a6c32016-09-22 01:25:59 -070082 cricket::VideoAdapter video_adapter_;
83
84 rtc::CriticalSection stats_crit_;
Danil Chapovalov00c71832018-06-15 15:58:38 +020085 absl::optional<Stats> stats_ RTC_GUARDED_BY(stats_crit_);
nisse6f5a6c32016-09-22 01:25:59 -070086
87 VideoBroadcaster broadcaster_;
88};
89
90} // namespace rtc
91
Steve Anton10542f22019-01-11 09:11:00 -080092#endif // MEDIA_BASE_ADAPTED_VIDEO_TRACK_SOURCE_H_