• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 AUDIO_CHANNEL_SEND_FRAME_TRANSFORMER_DELEGATE_H_
12 #define AUDIO_CHANNEL_SEND_FRAME_TRANSFORMER_DELEGATE_H_
13 
14 #include <memory>
15 
16 #include "api/frame_transformer_interface.h"
17 #include "modules/audio_coding/include/audio_coding_module_typedefs.h"
18 #include "rtc_base/buffer.h"
19 #include "rtc_base/synchronization/mutex.h"
20 #include "rtc_base/synchronization/sequence_checker.h"
21 #include "rtc_base/task_queue.h"
22 
23 namespace webrtc {
24 
25 // Delegates calls to FrameTransformerInterface to transform frames, and to
26 // ChannelSend to send the transformed frames using |send_frame_callback_| on
27 // the |encoder_queue_|.
28 // OnTransformedFrame() can be called from any thread, the delegate ensures
29 // thread-safe access to the ChannelSend callback.
30 class ChannelSendFrameTransformerDelegate : public TransformedFrameCallback {
31  public:
32   using SendFrameCallback =
33       std::function<int32_t(AudioFrameType frameType,
34                             uint8_t payloadType,
35                             uint32_t rtp_timestamp,
36                             rtc::ArrayView<const uint8_t> payload,
37                             int64_t absolute_capture_timestamp_ms)>;
38   ChannelSendFrameTransformerDelegate(
39       SendFrameCallback send_frame_callback,
40       rtc::scoped_refptr<FrameTransformerInterface> frame_transformer,
41       rtc::TaskQueue* encoder_queue);
42 
43   // Registers |this| as callback for |frame_transformer_|, to get the
44   // transformed frames.
45   void Init();
46 
47   // Unregisters and releases the |frame_transformer_| reference, and resets
48   // |send_frame_callback_| under lock. Called from ChannelSend destructor to
49   // prevent running the callback on a dangling channel.
50   void Reset();
51 
52   // Delegates the call to FrameTransformerInterface::TransformFrame, to
53   // transform the frame asynchronously.
54   void Transform(AudioFrameType frame_type,
55                  uint8_t payload_type,
56                  uint32_t rtp_timestamp,
57                  uint32_t rtp_start_timestamp,
58                  const uint8_t* payload_data,
59                  size_t payload_size,
60                  int64_t absolute_capture_timestamp_ms,
61                  uint32_t ssrc);
62 
63   // Implements TransformedFrameCallback. Can be called on any thread.
64   void OnTransformedFrame(
65       std::unique_ptr<TransformableFrameInterface> frame) override;
66 
67   // Delegates the call to ChannelSend::SendRtpAudio on the |encoder_queue_|,
68   // by calling |send_audio_callback_|.
69   void SendFrame(std::unique_ptr<TransformableFrameInterface> frame) const;
70 
71  protected:
72   ~ChannelSendFrameTransformerDelegate() override = default;
73 
74  private:
75   mutable Mutex send_lock_;
76   SendFrameCallback send_frame_callback_ RTC_GUARDED_BY(send_lock_);
77   rtc::scoped_refptr<FrameTransformerInterface> frame_transformer_;
78   rtc::TaskQueue* encoder_queue_ RTC_GUARDED_BY(send_lock_);
79 };
80 }  // namespace webrtc
81 #endif  // AUDIO_CHANNEL_SEND_FRAME_TRANSFORMER_DELEGATE_H_
82