Danil Chapovalov | c0fd5f9 | 2017-11-16 14:35:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | #include "modules/rtp_rtcp/source/rtcp_transceiver.h" |
| 12 | |
| 13 | #include <utility> |
| 14 | |
Danil Chapovalov | d5cae4d | 2017-12-14 11:14:35 +0100 | [diff] [blame] | 15 | #include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h" |
Danil Chapovalov | c0fd5f9 | 2017-11-16 14:35:32 +0100 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
| 17 | #include "rtc_base/event.h" |
| 18 | #include "rtc_base/ptr_util.h" |
| 19 | #include "rtc_base/timeutils.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | RtcpTransceiver::RtcpTransceiver(const RtcpTransceiverConfig& config) |
| 24 | : task_queue_(config.task_queue), |
| 25 | rtcp_transceiver_(rtc::MakeUnique<RtcpTransceiverImpl>(config)), |
| 26 | ptr_factory_(rtcp_transceiver_.get()), |
| 27 | // Creating first weak ptr can be done on any thread, but is not |
| 28 | // thread-safe, thus do it at construction. Creating second (e.g. making a |
| 29 | // copy) is thread-safe. |
| 30 | ptr_(ptr_factory_.GetWeakPtr()) { |
| 31 | RTC_DCHECK(task_queue_); |
| 32 | } |
| 33 | |
| 34 | RtcpTransceiver::~RtcpTransceiver() { |
| 35 | if (task_queue_->IsCurrent()) |
| 36 | return; |
| 37 | |
| 38 | rtc::Event done(false, false); |
| 39 | // TODO(danilchap): Merge cleanup into main closure when task queue does not |
| 40 | // silently drop tasks. |
| 41 | task_queue_->PostTask(rtc::NewClosure( |
| 42 | [this] { |
| 43 | // Destructor steps that has to run on the task_queue_. |
| 44 | ptr_factory_.InvalidateWeakPtrs(); |
| 45 | rtcp_transceiver_.reset(); |
| 46 | }, |
| 47 | /*cleanup=*/[&done] { done.Set(); })); |
| 48 | // Wait until destruction is complete to be sure weak pointers invalidated and |
| 49 | // rtcp_transceiver destroyed on the queue while |this| still valid. |
| 50 | done.Wait(rtc::Event::kForever); |
| 51 | RTC_CHECK(!rtcp_transceiver_) << "Task queue is too busy to handle rtcp"; |
| 52 | } |
| 53 | |
Danil Chapovalov | a32d710 | 2017-12-14 17:28:27 +0100 | [diff] [blame^] | 54 | void RtcpTransceiver::AddMediaReceiverRtcpObserver( |
| 55 | uint32_t remote_ssrc, |
| 56 | MediaReceiverRtcpObserver* observer) { |
| 57 | rtc::WeakPtr<RtcpTransceiverImpl> ptr = ptr_; |
| 58 | task_queue_->PostTask([ptr, remote_ssrc, observer] { |
| 59 | if (ptr) |
| 60 | ptr->AddMediaReceiverRtcpObserver(remote_ssrc, observer); |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | void RtcpTransceiver::RemoveMediaReceiverRtcpObserver( |
| 65 | uint32_t remote_ssrc, |
| 66 | MediaReceiverRtcpObserver* observer, |
| 67 | std::unique_ptr<rtc::QueuedTask> on_removed) { |
| 68 | rtc::WeakPtr<RtcpTransceiverImpl> ptr = ptr_; |
| 69 | auto remove = [ptr, remote_ssrc, observer] { |
| 70 | if (ptr) |
| 71 | ptr->RemoveMediaReceiverRtcpObserver(remote_ssrc, observer); |
| 72 | }; |
| 73 | task_queue_->PostTaskAndReply(std::move(remove), std::move(on_removed)); |
| 74 | } |
| 75 | |
Danil Chapovalov | c0fd5f9 | 2017-11-16 14:35:32 +0100 | [diff] [blame] | 76 | void RtcpTransceiver::ReceivePacket(rtc::CopyOnWriteBuffer packet) { |
| 77 | rtc::WeakPtr<RtcpTransceiverImpl> ptr = ptr_; |
| 78 | int64_t now_us = rtc::TimeMicros(); |
| 79 | task_queue_->PostTask([ptr, packet, now_us] { |
| 80 | if (ptr) |
| 81 | ptr->ReceivePacket(packet, now_us); |
| 82 | }); |
| 83 | } |
| 84 | |
| 85 | void RtcpTransceiver::SendCompoundPacket() { |
| 86 | rtc::WeakPtr<RtcpTransceiverImpl> ptr = ptr_; |
| 87 | task_queue_->PostTask([ptr] { |
| 88 | if (ptr) |
| 89 | ptr->SendCompoundPacket(); |
| 90 | }); |
| 91 | } |
| 92 | |
Danil Chapovalov | 1de4b62 | 2017-12-13 13:35:10 +0100 | [diff] [blame] | 93 | void RtcpTransceiver::SetRemb(int64_t bitrate_bps, |
| 94 | std::vector<uint32_t> ssrcs) { |
Danil Chapovalov | c0fd5f9 | 2017-11-16 14:35:32 +0100 | [diff] [blame] | 95 | // TODO(danilchap): Replace with lambda with move capture when available. |
| 96 | struct SetRembClosure { |
| 97 | void operator()() { |
| 98 | if (ptr) |
| 99 | ptr->SetRemb(bitrate_bps, std::move(ssrcs)); |
| 100 | } |
| 101 | |
| 102 | rtc::WeakPtr<RtcpTransceiverImpl> ptr; |
Danil Chapovalov | 1de4b62 | 2017-12-13 13:35:10 +0100 | [diff] [blame] | 103 | int64_t bitrate_bps; |
Danil Chapovalov | c0fd5f9 | 2017-11-16 14:35:32 +0100 | [diff] [blame] | 104 | std::vector<uint32_t> ssrcs; |
| 105 | }; |
| 106 | task_queue_->PostTask(SetRembClosure{ptr_, bitrate_bps, std::move(ssrcs)}); |
| 107 | } |
| 108 | |
| 109 | void RtcpTransceiver::UnsetRemb() { |
| 110 | rtc::WeakPtr<RtcpTransceiverImpl> ptr = ptr_; |
| 111 | task_queue_->PostTask([ptr] { |
| 112 | if (ptr) |
| 113 | ptr->UnsetRemb(); |
| 114 | }); |
| 115 | } |
| 116 | |
Danil Chapovalov | d5cae4d | 2017-12-14 11:14:35 +0100 | [diff] [blame] | 117 | uint32_t RtcpTransceiver::SSRC() const { |
| 118 | return rtcp_transceiver_->sender_ssrc(); |
| 119 | } |
| 120 | |
| 121 | bool RtcpTransceiver::SendFeedbackPacket( |
| 122 | const rtcp::TransportFeedback& packet) { |
| 123 | struct Closure { |
| 124 | void operator()() { |
| 125 | if (ptr) |
| 126 | ptr->SendRawPacket(raw_packet); |
| 127 | } |
| 128 | rtc::WeakPtr<RtcpTransceiverImpl> ptr; |
| 129 | rtc::Buffer raw_packet; |
| 130 | }; |
| 131 | task_queue_->PostTask(Closure{ptr_, packet.Build()}); |
| 132 | return true; |
| 133 | } |
| 134 | |
Danil Chapovalov | 327c43c | 2017-11-27 17:23:04 +0100 | [diff] [blame] | 135 | void RtcpTransceiver::SendNack(uint32_t ssrc, |
| 136 | std::vector<uint16_t> sequence_numbers) { |
| 137 | // TODO(danilchap): Replace with lambda with move capture when available. |
| 138 | struct Closure { |
| 139 | void operator()() { |
| 140 | if (ptr) |
| 141 | ptr->SendNack(ssrc, std::move(sequence_numbers)); |
| 142 | } |
| 143 | |
| 144 | rtc::WeakPtr<RtcpTransceiverImpl> ptr; |
| 145 | uint32_t ssrc; |
| 146 | std::vector<uint16_t> sequence_numbers; |
| 147 | }; |
| 148 | task_queue_->PostTask(Closure{ptr_, ssrc, std::move(sequence_numbers)}); |
| 149 | } |
| 150 | |
Danil Chapovalov | 8d19e03 | 2017-11-28 19:53:33 +0100 | [diff] [blame] | 151 | void RtcpTransceiver::SendPictureLossIndication(uint32_t ssrc) { |
| 152 | rtc::WeakPtr<RtcpTransceiverImpl> ptr = ptr_; |
| 153 | task_queue_->PostTask([ptr, ssrc] { |
| 154 | if (ptr) |
| 155 | ptr->SendPictureLossIndication(ssrc); |
| 156 | }); |
Danil Chapovalov | 2ddf98d | 2017-11-22 14:00:41 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void RtcpTransceiver::SendFullIntraRequest(std::vector<uint32_t> ssrcs) { |
| 160 | // TODO(danilchap): Replace with lambda with move capture when available. |
| 161 | struct Closure { |
| 162 | void operator()() { |
| 163 | if (ptr) |
| 164 | ptr->SendFullIntraRequest(ssrcs); |
| 165 | } |
| 166 | |
| 167 | rtc::WeakPtr<RtcpTransceiverImpl> ptr; |
| 168 | std::vector<uint32_t> ssrcs; |
| 169 | }; |
| 170 | task_queue_->PostTask(Closure{ptr_, std::move(ssrcs)}); |
Danil Chapovalov | a7e418c | 2017-11-21 11:08:53 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Danil Chapovalov | c0fd5f9 | 2017-11-16 14:35:32 +0100 | [diff] [blame] | 173 | } // namespace webrtc |