blob: e62cda70c37bb51502e75744609d401d489ba362 [file] [log] [blame]
Markus Handell15f2ff42019-11-22 10:34:37 +01001/*
2 * Copyright 2019 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 PC_VIDEO_RTP_TRACK_SOURCE_H_
12#define PC_VIDEO_RTP_TRACK_SOURCE_H_
13
Markus Handelld5e2f212019-11-26 09:30:08 +010014#include <vector>
15
Markus Handell15f2ff42019-11-22 10:34:37 +010016#include "media/base/video_broadcaster.h"
17#include "pc/video_track_source.h"
Markus Handelld5e2f212019-11-26 09:30:08 +010018#include "rtc_base/callback.h"
19#include "rtc_base/critical_section.h"
Markus Handell15f2ff42019-11-22 10:34:37 +010020
21namespace webrtc {
22
23// Video track source in use by VideoRtpReceiver
24class VideoRtpTrackSource : public VideoTrackSource {
25 public:
Markus Handelld5e2f212019-11-26 09:30:08 +010026 class Callback {
27 public:
28 virtual ~Callback() = default;
29
30 // Called when a keyframe should be generated
31 virtual void OnGenerateKeyFrame() = 0;
32
33 // Called when the implementor should eventually start to serve encoded
34 // frames using BroadcastEncodedFrameBuffer.
35 // The implementor should cause a keyframe to be eventually generated.
36 virtual void OnEncodedSinkEnabled(bool enable) = 0;
37 };
38
39 explicit VideoRtpTrackSource(Callback* callback);
40
41 // Call before the object implementing Callback finishes it's destructor. No
42 // more callbacks will be fired after completion. Must be called on the
43 // worker thread
44 void ClearCallback();
45
46 // Call to broadcast an encoded frame to registered sinks.
47 // This method can be called on any thread or queue.
48 void BroadcastRecordableEncodedFrame(
49 const RecordableEncodedFrame& frame) const;
Markus Handell15f2ff42019-11-22 10:34:37 +010050
51 // VideoTrackSource
52 rtc::VideoSourceInterface<VideoFrame>* source() override;
53 rtc::VideoSinkInterface<VideoFrame>* sink();
54
Markus Handelld5e2f212019-11-26 09:30:08 +010055 // Returns true. This method can be called on any thread.
56 bool SupportsEncodedOutput() const override;
57
58 // Generates a key frame. Must be called on the worker thread.
59 void GenerateKeyFrame() override;
60
61 // Adds an encoded sink. Must be called on the worker thread.
62 void AddEncodedSink(
63 rtc::VideoSinkInterface<RecordableEncodedFrame>* sink) override;
64
65 // Removes an encoded sink. Must be called on the worker thread.
66 void RemoveEncodedSink(
67 rtc::VideoSinkInterface<RecordableEncodedFrame>* sink) override;
68
Markus Handell15f2ff42019-11-22 10:34:37 +010069 private:
Markus Handelld5e2f212019-11-26 09:30:08 +010070 SequenceChecker worker_sequence_checker_;
Markus Handell15f2ff42019-11-22 10:34:37 +010071 // |broadcaster_| is needed since the decoder can only handle one sink.
72 // It might be better if the decoder can handle multiple sinks and consider
73 // the VideoSinkWants.
74 rtc::VideoBroadcaster broadcaster_;
Markus Handelld5e2f212019-11-26 09:30:08 +010075 rtc::CriticalSection mu_;
76 std::vector<rtc::VideoSinkInterface<RecordableEncodedFrame>*> encoded_sinks_
77 RTC_GUARDED_BY(mu_);
78 Callback* callback_ RTC_GUARDED_BY(worker_sequence_checker_);
Markus Handell15f2ff42019-11-22 10:34:37 +010079
80 RTC_DISALLOW_COPY_AND_ASSIGN(VideoRtpTrackSource);
81};
82
83} // namespace webrtc
84
85#endif // PC_VIDEO_RTP_TRACK_SOURCE_H_