blob: 0db381f5fbc3b55476f0389818fed2da64a72578 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MEDIA_BASE_ADAPTEDVIDEOTRACKSOURCE_H_
12#define MEDIA_BASE_ADAPTEDVIDEOTRACKSOURCE_H_
nisse6f5a6c32016-09-22 01:25:59 -070013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "api/mediastreaminterface.h"
15#include "api/notifier.h"
16#include "media/base/videoadapter.h"
17#include "media/base/videobroadcaster.h"
nisse6f5a6c32016-09-22 01:25:59 -070018
19namespace rtc {
20
21// Base class for sources which needs video adaptation, e.g., video
22// capture sources. Sinks must be added and removed on one and only
23// one thread, while AdaptFrame and OnFrame may be called on any
24// thread.
25class AdaptedVideoTrackSource
26 : public webrtc::Notifier<webrtc::VideoTrackSourceInterface> {
27 public:
28 AdaptedVideoTrackSource();
29
30 protected:
kthelgasonc8474172016-12-08 08:04:51 -080031 // Allows derived classes to initialize |video_adapter_| with a custom
32 // alignment.
Steve Antone78bcb92017-10-31 09:53:08 -070033 explicit AdaptedVideoTrackSource(int required_alignment);
nisse6f5a6c32016-09-22 01:25:59 -070034 // Checks the apply_rotation() flag. If the frame needs rotation, and it is a
35 // plain memory frame, it is rotated. Subclasses producing native frames must
36 // handle apply_rotation() themselves.
nisseacd935b2016-11-11 03:55:13 -080037 void OnFrame(const webrtc::VideoFrame& frame);
nisse6f5a6c32016-09-22 01:25:59 -070038
39 // Reports the appropriate frame size after adaptation. Returns true
40 // if a frame is wanted. Returns false if there are no interested
41 // sinks, or if the VideoAdapter decides to drop the frame.
42 bool AdaptFrame(int width,
43 int height,
44 int64_t time_us,
45 int* out_width,
46 int* out_height,
47 int* crop_width,
48 int* crop_height,
49 int* crop_x,
50 int* crop_y);
51
52 // Returns the current value of the apply_rotation flag, derived
53 // from the VideoSinkWants of registered sinks. The value is derived
54 // from sinks' wants, in AddOrUpdateSink and RemoveSink. Beware that
55 // when using this method from a different thread, the value may
56 // become stale before it is used.
57 bool apply_rotation();
58
59 cricket::VideoAdapter* video_adapter() { return &video_adapter_; }
60
61 private:
62 // Implements rtc::VideoSourceInterface.
nisseacd935b2016-11-11 03:55:13 -080063 void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
nisse6f5a6c32016-09-22 01:25:59 -070064 const rtc::VideoSinkWants& wants) override;
nisseacd935b2016-11-11 03:55:13 -080065 void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
nisse6f5a6c32016-09-22 01:25:59 -070066
67 // Part of VideoTrackSourceInterface.
68 bool GetStats(Stats* stats) override;
69
70 void OnSinkWantsChanged(const rtc::VideoSinkWants& wants);
71
72 rtc::ThreadChecker thread_checker_;
73
74 cricket::VideoAdapter video_adapter_;
75
76 rtc::CriticalSection stats_crit_;
danilchapa37de392017-09-09 04:17:22 -070077 rtc::Optional<Stats> stats_ RTC_GUARDED_BY(stats_crit_);
nisse6f5a6c32016-09-22 01:25:59 -070078
79 VideoBroadcaster broadcaster_;
80};
81
82} // namespace rtc
83
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020084#endif // MEDIA_BASE_ADAPTEDVIDEOTRACKSOURCE_H_