1 /*
2  *  Copyright (c) 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 MODULES_RTP_RTCP_SOURCE_RTP_SENDER_VIDEO_FRAME_TRANSFORMER_DELEGATE_H_
12 #define MODULES_RTP_RTCP_SOURCE_RTP_SENDER_VIDEO_FRAME_TRANSFORMER_DELEGATE_H_
13 
14 #include <memory>
15 
16 #include "api/frame_transformer_interface.h"
17 #include "api/scoped_refptr.h"
18 #include "api/sequence_checker.h"
19 #include "api/task_queue/task_queue_base.h"
20 #include "api/task_queue/task_queue_factory.h"
21 #include "api/video/video_layers_allocation.h"
22 #include "rtc_base/synchronization/mutex.h"
23 
24 namespace webrtc {
25 
26 class RTPSenderVideo;
27 
28 // Delegates calls to FrameTransformerInterface to transform frames, and to
29 // RTPSenderVideo to send the transformed frames. Ensures thread-safe access to
30 // the sender.
31 class RTPSenderVideoFrameTransformerDelegate : public TransformedFrameCallback {
32  public:
33   RTPSenderVideoFrameTransformerDelegate(
34       RTPSenderVideo* sender,
35       rtc::scoped_refptr<FrameTransformerInterface> frame_transformer,
36       uint32_t ssrc,
37       TaskQueueFactory* send_transport_queue);
38 
39   void Init();
40 
41   // Delegates the call to FrameTransformerInterface::TransformFrame.
42   bool TransformFrame(int payload_type,
43                       absl::optional<VideoCodecType> codec_type,
44                       uint32_t rtp_timestamp,
45                       const EncodedImage& encoded_image,
46                       RTPVideoHeader video_header,
47                       absl::optional<int64_t> expected_retransmission_time_ms);
48 
49   // Implements TransformedFrameCallback. Can be called on any thread. Posts
50   // the transformed frame to be sent on the `encoder_queue_`.
51   void OnTransformedFrame(
52       std::unique_ptr<TransformableFrameInterface> frame) override;
53 
54   // Delegates the call to RTPSendVideo::SendVideo on the `encoder_queue_`.
55   void SendVideo(std::unique_ptr<TransformableFrameInterface> frame) const
56       RTC_RUN_ON(transformation_queue_);
57 
58   // Delegates the call to RTPSendVideo::SetVideoStructureAfterTransformation
59   // under `sender_lock_`.
60   void SetVideoStructureUnderLock(
61       const FrameDependencyStructure* video_structure);
62 
63   // Delegates the call to
64   // RTPSendVideo::SetVideoLayersAllocationAfterTransformation under
65   // `sender_lock_`.
66   void SetVideoLayersAllocationUnderLock(VideoLayersAllocation allocation);
67 
68   // Unregisters and releases the `frame_transformer_` reference, and resets
69   // `sender_` under lock. Called from RTPSenderVideo destructor to prevent the
70   // `sender_` to dangle.
71   void Reset();
72 
73  protected:
74   ~RTPSenderVideoFrameTransformerDelegate() override = default;
75 
76  private:
77   void EnsureEncoderQueueCreated();
78 
79   mutable Mutex sender_lock_;
80   RTPSenderVideo* sender_ RTC_GUARDED_BY(sender_lock_);
81   rtc::scoped_refptr<FrameTransformerInterface> frame_transformer_;
82   const uint32_t ssrc_;
83   // Used when the encoded frames arrives without a current task queue. This can
84   // happen if a hardware encoder was used.
85   std::unique_ptr<TaskQueueBase, TaskQueueDeleter> transformation_queue_;
86 };
87 
88 // Method to support cloning a Sender frame from another frame
89 std::unique_ptr<TransformableVideoFrameInterface> CloneSenderVideoFrame(
90     TransformableVideoFrameInterface* original);
91 
92 }  // namespace webrtc
93 
94 #endif  // MODULES_RTP_RTCP_SOURCE_RTP_SENDER_VIDEO_FRAME_TRANSFORMER_DELEGATE_H_
95