blob: 21cff17a946f76a45afb988890434fe7c163a2a8 [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"
Yves Gerey3e707812018-11-28 16:47:49 +010025#include "rtc_base/thread_annotations.h"
nisse6f5a6c32016-09-22 01:25:59 -070026
27namespace rtc {
28
29// Base class for sources which needs video adaptation, e.g., video
30// capture sources. Sinks must be added and removed on one and only
31// one thread, while AdaptFrame and OnFrame may be called on any
32// thread.
33class AdaptedVideoTrackSource
34 : public webrtc::Notifier<webrtc::VideoTrackSourceInterface> {
35 public:
36 AdaptedVideoTrackSource();
Paulina Hensmana680a6a2018-04-05 11:42:24 +020037 ~AdaptedVideoTrackSource() override;
nisse6f5a6c32016-09-22 01:25:59 -070038
39 protected:
kthelgasonc8474172016-12-08 08:04:51 -080040 // Allows derived classes to initialize |video_adapter_| with a custom
41 // alignment.
Steve Antone78bcb92017-10-31 09:53:08 -070042 explicit AdaptedVideoTrackSource(int required_alignment);
nisse6f5a6c32016-09-22 01:25:59 -070043 // Checks the apply_rotation() flag. If the frame needs rotation, and it is a
44 // plain memory frame, it is rotated. Subclasses producing native frames must
45 // handle apply_rotation() themselves.
nisseacd935b2016-11-11 03:55:13 -080046 void OnFrame(const webrtc::VideoFrame& frame);
nisse6f5a6c32016-09-22 01:25:59 -070047
48 // Reports the appropriate frame size after adaptation. Returns true
49 // if a frame is wanted. Returns false if there are no interested
50 // sinks, or if the VideoAdapter decides to drop the frame.
51 bool AdaptFrame(int width,
52 int height,
53 int64_t time_us,
54 int* out_width,
55 int* out_height,
56 int* crop_width,
57 int* crop_height,
58 int* crop_x,
59 int* crop_y);
60
61 // Returns the current value of the apply_rotation flag, derived
62 // from the VideoSinkWants of registered sinks. The value is derived
63 // from sinks' wants, in AddOrUpdateSink and RemoveSink. Beware that
64 // when using this method from a different thread, the value may
65 // become stale before it is used.
66 bool apply_rotation();
67
68 cricket::VideoAdapter* video_adapter() { return &video_adapter_; }
69
70 private:
71 // Implements rtc::VideoSourceInterface.
nisseacd935b2016-11-11 03:55:13 -080072 void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
nisse6f5a6c32016-09-22 01:25:59 -070073 const rtc::VideoSinkWants& wants) override;
nisseacd935b2016-11-11 03:55:13 -080074 void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
nisse6f5a6c32016-09-22 01:25:59 -070075
76 // Part of VideoTrackSourceInterface.
77 bool GetStats(Stats* stats) override;
78
79 void OnSinkWantsChanged(const rtc::VideoSinkWants& wants);
80
nisse6f5a6c32016-09-22 01:25:59 -070081 cricket::VideoAdapter video_adapter_;
82
83 rtc::CriticalSection stats_crit_;
Danil Chapovalov00c71832018-06-15 15:58:38 +020084 absl::optional<Stats> stats_ RTC_GUARDED_BY(stats_crit_);
nisse6f5a6c32016-09-22 01:25:59 -070085
86 VideoBroadcaster broadcaster_;
87};
88
89} // namespace rtc
90
Steve Anton10542f22019-01-11 09:11:00 -080091#endif // MEDIA_BASE_ADAPTED_VIDEO_TRACK_SOURCE_H_