1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/rtp_rtcp/source/rtp_sender_audio.h"
#include <string.h>
#include <cstdint>
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#include "api/rtp_headers.h"
#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
#include "modules/rtp_rtcp/source/absolute_capture_time_sender.h"
#include "modules/rtp_rtcp/source/byte_io.h"
#include "modules/rtp_rtcp/source/dtmf_queue.h"
#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
#include "modules/rtp_rtcp/source/rtp_packet.h"
#include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
#include "modules/rtp_rtcp/source/rtp_sender.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
#include "rtc_base/numerics/safe_conversions.h"
#include "rtc_base/synchronization/mutex.h"
#include "system_wrappers/include/clock.h"
namespace webrtc {
RTPSenderAudio::RTPSenderAudio(Clock* clock, RTPSender* rtp_sender)
: clock_(clock),
rtp_sender_(rtp_sender),
absolute_capture_time_sender_(clock) {
RTC_DCHECK(clock_);
}
RTPSenderAudio::~RTPSenderAudio() {}
int32_t RTPSenderAudio::RegisterAudioPayload(absl::string_view payload_name,
const int8_t payload_type,
const uint32_t frequency,
const size_t /* channels */,
const uint32_t /* rate */) {
if (absl::EqualsIgnoreCase(payload_name, "cn")) {
MutexLock lock(&send_audio_mutex_);
// we can have multiple CNG payload types
switch (frequency) {
case 8000:
cngnb_payload_type_ = payload_type;
break;
case 16000:
cngwb_payload_type_ = payload_type;
break;
case 32000:
cngswb_payload_type_ = payload_type;
break;
case 48000:
cngfb_payload_type_ = payload_type;
break;
default:
return -1;
}
} else if (absl::EqualsIgnoreCase(payload_name, "telephone-event")) {
MutexLock lock(&send_audio_mutex_);
// Don't add it to the list
// we dont want to allow send with a DTMF payloadtype
dtmf_payload_type_ = payload_type;
dtmf_payload_freq_ = frequency;
return 0;
} else if (payload_name == "audio") {
MutexLock lock(&send_audio_mutex_);
encoder_rtp_timestamp_frequency_ = dchecked_cast<int>(frequency);
return 0;
}
return 0;
}
bool RTPSenderAudio::MarkerBit(AudioFrameType frame_type, int8_t payload_type) {
MutexLock lock(&send_audio_mutex_);
// for audio true for first packet in a speech burst
bool marker_bit = false;
if (last_payload_type_ != payload_type) {
if (payload_type != -1 && (cngnb_payload_type_ == payload_type ||
cngwb_payload_type_ == payload_type ||
cngswb_payload_type_ == payload_type ||
cngfb_payload_type_ == payload_type)) {
// Only set a marker bit when we change payload type to a non CNG
return false;
}
// payload_type differ
if (last_payload_type_ == -1) {
if (frame_type != AudioFrameType::kAudioFrameCN) {
// first packet and NOT CNG
return true;
} else {
// first packet and CNG
inband_vad_active_ = true;
return false;
}
}
// not first packet AND
// not CNG AND
// payload_type changed
// set a marker bit when we change payload type
marker_bit = true;
}
// For G.723 G.729, AMR etc we can have inband VAD
if (frame_type == AudioFrameType::kAudioFrameCN) {
inband_vad_active_ = true;
} else if (inband_vad_active_) {
inband_vad_active_ = false;
marker_bit = true;
}
return marker_bit;
}
bool RTPSenderAudio::SendAudio(const RtpAudioFrame& frame) {
RTC_DCHECK_GE(frame.payload_id, 0);
RTC_DCHECK_LE(frame.payload_id, 127);
// From RFC 4733:
// A source has wide latitude as to how often it sends event updates. A
// natural interval is the spacing between non-event audio packets. [...]
// Alternatively, a source MAY decide to use a different spacing for event
// updates, with a value of 50 ms RECOMMENDED.
constexpr int kDtmfIntervalTimeMs = 50;
uint32_t dtmf_payload_freq = 0;
std::optional<AbsoluteCaptureTime> absolute_capture_time;
{
MutexLock lock(&send_audio_mutex_);
dtmf_payload_freq = dtmf_payload_freq_;
if (frame.capture_time.has_value()) {
// Send absolute capture time periodically in order to optimize and save
// network traffic. Missing absolute capture times can be interpolated on
// the receiving end if sending intervals are small enough.
absolute_capture_time = absolute_capture_time_sender_.OnSendPacket(
rtp_sender_->SSRC(), frame.rtp_timestamp,
// Replace missing value with 0 (invalid frequency), this will trigger
// absolute capture time sending.
encoder_rtp_timestamp_frequency_.value_or(0),
clock_->ConvertTimestampToNtpTime(*frame.capture_time),
/*estimated_capture_clock_offset=*/0);
}
}
// Check if we have pending DTMFs to send
if (!dtmf_event_is_on_ && dtmf_queue_.PendingDtmf()) {
if ((clock_->TimeInMilliseconds() - dtmf_time_last_sent_) >
kDtmfIntervalTimeMs) {
// New tone to play
dtmf_timestamp_ = frame.rtp_timestamp;
if (dtmf_queue_.NextDtmf(&dtmf_current_event_)) {
dtmf_event_first_packet_sent_ = false;
dtmf_length_samples_ =
dtmf_current_event_.duration_ms * (dtmf_payload_freq / 1000);
dtmf_event_is_on_ = true;
}
}
}
// A source MAY send events and coded audio packets for the same time
// but we don't support it
if (dtmf_event_is_on_) {
if (frame.type == AudioFrameType::kEmptyFrame) {
// kEmptyFrame is used to drive the DTMF when in CN mode
// it can be triggered more frequently than we want to send the
// DTMF packets.
const unsigned int dtmf_interval_time_rtp =
dtmf_payload_freq * kDtmfIntervalTimeMs / 1000;
if ((frame.rtp_timestamp - dtmf_timestamp_last_sent_) <
dtmf_interval_time_rtp) {
// not time to send yet
return true;
}
}
dtmf_timestamp_last_sent_ = frame.rtp_timestamp;
uint32_t dtmf_duration_samples = frame.rtp_timestamp - dtmf_timestamp_;
bool ended = false;
bool send = true;
if (dtmf_length_samples_ > dtmf_duration_samples) {
if (dtmf_duration_samples <= 0) {
// Skip send packet at start, since we shouldn't use duration 0
send = false;
}
} else {
ended = true;
dtmf_event_is_on_ = false;
dtmf_time_last_sent_ = clock_->TimeInMilliseconds();
}
if (send) {
if (dtmf_duration_samples > 0xffff) {
// RFC 4733 2.5.2.3 Long-Duration Events
SendTelephoneEventPacket(ended, dtmf_timestamp_,
static_cast<uint16_t>(0xffff), false);
// set new timestap for this segment
dtmf_timestamp_ = frame.rtp_timestamp;
dtmf_duration_samples -= 0xffff;
dtmf_length_samples_ -= 0xffff;
return SendTelephoneEventPacket(
ended, dtmf_timestamp_,
static_cast<uint16_t>(dtmf_duration_samples), false);
} else {
if (!SendTelephoneEventPacket(ended, dtmf_timestamp_,
dtmf_duration_samples,
!dtmf_event_first_packet_sent_)) {
return false;
}
dtmf_event_first_packet_sent_ = true;
return true;
}
}
return true;
}
if (frame.payload.empty()) {
if (frame.type == AudioFrameType::kEmptyFrame) {
// we don't send empty audio RTP packets
// no error since we use it to either drive DTMF when we use VAD, or
// enter DTX.
return true;
}
return false;
}
std::unique_ptr<RtpPacketToSend> packet =
rtp_sender_->AllocatePacket(frame.csrcs);
packet->SetMarker(MarkerBit(frame.type, frame.payload_id));
packet->SetPayloadType(frame.payload_id);
packet->SetTimestamp(frame.rtp_timestamp);
packet->set_capture_time(clock_->CurrentTime());
// Set audio level extension, if included.
packet->SetExtension<AudioLevelExtension>(
AudioLevel(frame.type == AudioFrameType::kAudioFrameSpeech,
frame.audio_level_dbov.value_or(127)));
if (absolute_capture_time.has_value()) {
// It also checks that extension was registered during SDP negotiation. If
// not then setter won't do anything.
packet->SetExtension<AbsoluteCaptureTimeExtension>(*absolute_capture_time);
}
uint8_t* payload = packet->AllocatePayload(frame.payload.size());
RTC_CHECK(payload);
memcpy(payload, frame.payload.data(), frame.payload.size());
{
MutexLock lock(&send_audio_mutex_);
last_payload_type_ = frame.payload_id;
}
packet->set_packet_type(RtpPacketMediaType::kAudio);
packet->set_allow_retransmission(true);
std::vector<std::unique_ptr<RtpPacketToSend>> packets(1);
packets[0] = std::move(packet);
rtp_sender_->EnqueuePackets(std::move(packets));
if (first_packet_sent_()) {
RTC_LOG(LS_INFO) << "First audio RTP packet sent to pacer";
}
return true;
}
// Send a TelephoneEvent tone using RFC 2833 (4733)
int32_t RTPSenderAudio::SendTelephoneEvent(uint8_t key,
uint16_t time_ms,
uint8_t level) {
DtmfQueue::Event event;
{
MutexLock lock(&send_audio_mutex_);
if (dtmf_payload_type_ < 0) {
// TelephoneEvent payloadtype not configured
return -1;
}
event.payload_type = dtmf_payload_type_;
}
event.key = key;
event.duration_ms = time_ms;
event.level = level;
return dtmf_queue_.AddDtmf(event) ? 0 : -1;
}
bool RTPSenderAudio::SendTelephoneEventPacket(bool ended,
uint32_t dtmf_timestamp,
uint16_t duration,
bool marker_bit) {
size_t send_count = ended ? 3 : 1;
std::vector<std::unique_ptr<RtpPacketToSend>> packets;
packets.reserve(send_count);
for (size_t i = 0; i < send_count; ++i) {
// Send DTMF data.
constexpr RtpPacketToSend::ExtensionManager* kNoExtensions = nullptr;
constexpr size_t kDtmfSize = 4;
auto packet = std::make_unique<RtpPacketToSend>(kNoExtensions,
kRtpHeaderSize + kDtmfSize);
packet->SetPayloadType(dtmf_current_event_.payload_type);
packet->SetMarker(marker_bit);
packet->SetSsrc(rtp_sender_->SSRC());
packet->SetTimestamp(dtmf_timestamp);
packet->set_capture_time(clock_->CurrentTime());
// Create DTMF data.
uint8_t* dtmfbuffer = packet->AllocatePayload(kDtmfSize);
RTC_DCHECK(dtmfbuffer);
/* From RFC 2833:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| event |E|R| volume | duration |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
// R bit always cleared
uint8_t R = 0x00;
uint8_t volume = dtmf_current_event_.level;
// First packet un-ended
uint8_t E = ended ? 0x80 : 0x00;
// First byte is Event number, equals key number
dtmfbuffer[0] = dtmf_current_event_.key;
dtmfbuffer[1] = E | R | volume;
ByteWriter<uint16_t>::WriteBigEndian(dtmfbuffer + 2, duration);
packet->set_packet_type(RtpPacketMediaType::kAudio);
packet->set_allow_retransmission(true);
packets.push_back(std::move(packet));
}
rtp_sender_->EnqueuePackets(std::move(packets));
return true;
}
} // namespace webrtc
|