2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/video_engine/vie_channel.h"
16 #include "webrtc/common.h"
17 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
18 #include "webrtc/experiments.h"
19 #include "webrtc/modules/pacing/include/paced_sender.h"
20 #include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h"
21 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
22 #include "webrtc/modules/utility/interface/process_thread.h"
23 #include "webrtc/modules/video_coding/main/interface/video_coding.h"
24 #include "webrtc/modules/video_processing/main/interface/video_processing.h"
25 #include "webrtc/modules/video_render/include/video_render_defines.h"
26 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
27 #include "webrtc/system_wrappers/interface/logging.h"
28 #include "webrtc/system_wrappers/interface/thread_wrapper.h"
29 #include "webrtc/video_engine/call_stats.h"
30 #include "webrtc/video_engine/include/vie_codec.h"
31 #include "webrtc/video_engine/include/vie_errors.h"
32 #include "webrtc/video_engine/include/vie_image_process.h"
33 #include "webrtc/video_engine/include/vie_rtp_rtcp.h"
34 #include "webrtc/frame_callback.h"
35 #include "webrtc/video_engine/vie_defines.h"
39 const int kMaxDecodeWaitTimeMs = 50;
40 const int kInvalidRtpExtensionId = 0;
41 static const int kMaxTargetDelayMs = 10000;
42 static const float kMaxIncompleteTimeMultiplier = 3.5f;
44 // Helper class receiving statistics callbacks.
45 class ChannelStatsObserver : public CallStatsObserver {
47 explicit ChannelStatsObserver(ViEChannel* owner) : owner_(owner) {}
48 virtual ~ChannelStatsObserver() {}
50 // Implements StatsObserver.
51 virtual void OnRttUpdate(uint32_t rtt) {
52 owner_->OnRttUpdate(rtt);
59 ViEChannel::ViEChannel(int32_t channel_id,
61 uint32_t number_of_cores,
63 ProcessThread& module_process_thread,
64 RtcpIntraFrameObserver* intra_frame_observer,
65 RtcpBandwidthObserver* bandwidth_observer,
66 RemoteBitrateEstimator* remote_bitrate_estimator,
67 RtcpRttStats* rtt_stats,
68 PacedSender* paced_sender,
69 RtpRtcp* default_rtp_rtcp,
71 : ViEFrameProviderBase(channel_id, engine_id),
72 channel_id_(channel_id),
73 engine_id_(engine_id),
74 number_of_cores_(number_of_cores),
75 num_socket_threads_(kViESocketThreads),
76 callback_cs_(CriticalSectionWrapper::CreateCriticalSection()),
77 rtp_rtcp_cs_(CriticalSectionWrapper::CreateCriticalSection()),
78 default_rtp_rtcp_(default_rtp_rtcp),
79 vcm_(*VideoCodingModule::Create()),
80 vie_receiver_(channel_id, &vcm_, remote_bitrate_estimator, this),
81 vie_sender_(channel_id),
82 vie_sync_(&vcm_, this),
83 stats_observer_(new ChannelStatsObserver(this)),
84 module_process_thread_(module_process_thread),
85 codec_observer_(NULL),
86 do_key_frame_callbackRequest_(false),
89 intra_frame_observer_(intra_frame_observer),
90 rtt_stats_(rtt_stats),
91 paced_sender_(paced_sender),
92 bandwidth_observer_(bandwidth_observer),
93 send_timestamp_extension_id_(kInvalidRtpExtensionId),
94 absolute_send_time_extension_id_(kInvalidRtpExtensionId),
95 external_transport_(NULL),
97 wait_for_key_frame_(false),
100 color_enhancement_(false),
103 nack_history_size_sender_(kSendSidePacketHistorySize),
104 max_nack_reordering_threshold_(kMaxPacketAgeToNack),
105 pre_render_callback_(NULL),
107 RtpRtcp::Configuration configuration;
108 configuration.id = ViEModuleId(engine_id, channel_id);
109 configuration.audio = false;
110 configuration.default_module = default_rtp_rtcp;
111 configuration.outgoing_transport = &vie_sender_;
112 configuration.rtcp_feedback = this;
113 configuration.intra_frame_callback = intra_frame_observer;
114 configuration.bandwidth_callback = bandwidth_observer;
115 configuration.rtt_stats = rtt_stats;
116 configuration.remote_bitrate_estimator = remote_bitrate_estimator;
117 configuration.paced_sender = paced_sender;
118 configuration.receive_statistics = vie_receiver_.GetReceiveStatistics();
120 rtp_rtcp_.reset(RtpRtcp::CreateRtpRtcp(configuration));
121 vie_receiver_.SetRtpRtcpModule(rtp_rtcp_.get());
122 vcm_.SetNackSettings(kMaxNackListSize, max_nack_reordering_threshold_, 0);
125 int32_t ViEChannel::Init() {
126 if (module_process_thread_.RegisterModule(
127 vie_receiver_.GetReceiveStatistics()) != 0) {
130 // RTP/RTCP initialization.
131 if (rtp_rtcp_->SetSendingMediaStatus(false) != 0) {
134 if (module_process_thread_.RegisterModule(rtp_rtcp_.get()) != 0) {
137 rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqFirRtp);
138 rtp_rtcp_->SetRTCPStatus(kRtcpCompound);
140 rtp_rtcp_->SetStorePacketsStatus(true, nack_history_size_sender_);
142 if (vcm_.InitializeReceiver() != 0) {
145 if (vcm_.SetVideoProtection(kProtectionKeyOnLoss, true)) {
148 if (vcm_.RegisterReceiveCallback(this) != 0) {
151 vcm_.RegisterFrameTypeCallback(this);
152 vcm_.RegisterReceiveStatisticsCallback(this);
153 vcm_.RegisterDecoderTimingCallback(this);
154 vcm_.SetRenderDelay(kViEDefaultRenderDelayMs);
155 if (module_process_thread_.RegisterModule(&vcm_) != 0) {
158 #ifdef VIDEOCODEC_VP8
159 VideoCodec video_codec;
160 if (vcm_.Codec(kVideoCodecVP8, &video_codec) == VCM_OK) {
161 rtp_rtcp_->RegisterSendPayload(video_codec);
162 // TODO(holmer): Can we call SetReceiveCodec() here instead?
163 if (!vie_receiver_.RegisterPayload(video_codec)) {
166 vcm_.RegisterReceiveCodec(&video_codec, number_of_cores_);
167 vcm_.RegisterSendCodec(&video_codec, number_of_cores_,
168 rtp_rtcp_->MaxDataPayloadLength());
177 ViEChannel::~ViEChannel() {
178 // Make sure we don't get more callbacks from the RTP module.
179 module_process_thread_.DeRegisterModule(vie_receiver_.GetReceiveStatistics());
180 module_process_thread_.DeRegisterModule(rtp_rtcp_.get());
181 module_process_thread_.DeRegisterModule(&vcm_);
182 module_process_thread_.DeRegisterModule(&vie_sync_);
183 while (simulcast_rtp_rtcp_.size() > 0) {
184 std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
185 RtpRtcp* rtp_rtcp = *it;
186 module_process_thread_.DeRegisterModule(rtp_rtcp);
188 simulcast_rtp_rtcp_.erase(it);
190 while (removed_rtp_rtcp_.size() > 0) {
191 std::list<RtpRtcp*>::iterator it = removed_rtp_rtcp_.begin();
193 removed_rtp_rtcp_.erase(it);
195 if (decode_thread_) {
199 VideoCodingModule::Destroy(&vcm_);
202 int32_t ViEChannel::SetSendCodec(const VideoCodec& video_codec,
207 if (video_codec.codecType == kVideoCodecRED ||
208 video_codec.codecType == kVideoCodecULPFEC) {
209 LOG_F(LS_ERROR) << "Not a valid send codec " << video_codec.codecType;
212 if (kMaxSimulcastStreams < video_codec.numberOfSimulcastStreams) {
213 LOG_F(LS_ERROR) << "Incorrect config "
214 << video_codec.numberOfSimulcastStreams;
217 // Update the RTP module with the settings.
218 // Stop and Start the RTP module -> trigger new SSRC, if an SSRC hasn't been
220 bool restart_rtp = false;
221 if (rtp_rtcp_->Sending() && new_stream) {
223 rtp_rtcp_->SetSendingStatus(false);
224 for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
225 it != simulcast_rtp_rtcp_.end(); ++it) {
226 (*it)->SetSendingStatus(false);
227 (*it)->SetSendingMediaStatus(false);
231 bool fec_enabled = false;
232 uint8_t payload_type_red;
233 uint8_t payload_type_fec;
234 rtp_rtcp_->GenericFECStatus(fec_enabled, payload_type_red, payload_type_fec);
236 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
238 if (video_codec.numberOfSimulcastStreams > 0) {
239 // Set correct bitrate to base layer.
240 // Create our simulcast RTP modules.
241 int num_modules_to_add = video_codec.numberOfSimulcastStreams -
242 simulcast_rtp_rtcp_.size() - 1;
243 if (num_modules_to_add < 0) {
244 num_modules_to_add = 0;
247 while (removed_rtp_rtcp_.size() > 0 && num_modules_to_add > 0) {
248 RtpRtcp* rtp_rtcp = removed_rtp_rtcp_.front();
249 removed_rtp_rtcp_.pop_front();
250 simulcast_rtp_rtcp_.push_back(rtp_rtcp);
251 rtp_rtcp->SetSendingStatus(rtp_rtcp_->Sending());
252 rtp_rtcp->SetSendingMediaStatus(rtp_rtcp_->SendingMedia());
253 module_process_thread_.RegisterModule(rtp_rtcp);
254 --num_modules_to_add;
257 for (int i = 0; i < num_modules_to_add; ++i) {
258 RtpRtcp::Configuration configuration;
259 configuration.id = ViEModuleId(engine_id_, channel_id_);
260 configuration.audio = false; // Video.
261 configuration.default_module = default_rtp_rtcp_;
262 configuration.outgoing_transport = &vie_sender_;
263 configuration.intra_frame_callback = intra_frame_observer_;
264 configuration.bandwidth_callback = bandwidth_observer_.get();
265 configuration.rtt_stats = rtt_stats_;
266 configuration.paced_sender = paced_sender_;
268 RtpRtcp* rtp_rtcp = RtpRtcp::CreateRtpRtcp(configuration);
270 // Silently ignore error.
271 module_process_thread_.RegisterModule(rtp_rtcp);
272 rtp_rtcp->SetRTCPStatus(rtp_rtcp_->RTCP());
274 if (rtp_rtcp_->StorePackets()) {
275 rtp_rtcp->SetStorePacketsStatus(true, nack_history_size_sender_);
276 } else if (paced_sender_) {
277 rtp_rtcp->SetStorePacketsStatus(true, nack_history_size_sender_);
281 rtp_rtcp->SetGenericFECStatus(fec_enabled, payload_type_red,
284 rtp_rtcp->SetSendingStatus(rtp_rtcp_->Sending());
285 rtp_rtcp->SetSendingMediaStatus(rtp_rtcp_->SendingMedia());
286 simulcast_rtp_rtcp_.push_back(rtp_rtcp);
288 // Remove last in list if we have too many.
289 for (int j = simulcast_rtp_rtcp_.size();
290 j > (video_codec.numberOfSimulcastStreams - 1);
292 RtpRtcp* rtp_rtcp = simulcast_rtp_rtcp_.back();
293 module_process_thread_.DeRegisterModule(rtp_rtcp);
294 rtp_rtcp->SetSendingStatus(false);
295 rtp_rtcp->SetSendingMediaStatus(false);
296 rtp_rtcp->RegisterSendFrameCountObserver(NULL);
297 rtp_rtcp->RegisterSendChannelRtcpStatisticsCallback(NULL);
298 rtp_rtcp->RegisterSendChannelRtpStatisticsCallback(NULL);
299 rtp_rtcp->RegisterVideoBitrateObserver(NULL);
300 simulcast_rtp_rtcp_.pop_back();
301 removed_rtp_rtcp_.push_front(rtp_rtcp);
304 // Configure all simulcast modules.
305 for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
306 it != simulcast_rtp_rtcp_.end();
309 RtpRtcp* rtp_rtcp = *it;
310 rtp_rtcp->DeRegisterSendPayload(video_codec.plType);
311 if (rtp_rtcp->RegisterSendPayload(video_codec) != 0) {
315 rtp_rtcp->SetMaxTransferUnit(mtu_);
318 rtp_rtcp->SetSendingStatus(true);
319 rtp_rtcp->SetSendingMediaStatus(true);
321 if (send_timestamp_extension_id_ != kInvalidRtpExtensionId) {
322 // Deregister in case the extension was previously enabled.
323 rtp_rtcp->DeregisterSendRtpHeaderExtension(
324 kRtpExtensionTransmissionTimeOffset);
325 if (rtp_rtcp->RegisterSendRtpHeaderExtension(
326 kRtpExtensionTransmissionTimeOffset,
327 send_timestamp_extension_id_) != 0) {
330 rtp_rtcp->DeregisterSendRtpHeaderExtension(
331 kRtpExtensionTransmissionTimeOffset);
333 if (absolute_send_time_extension_id_ != kInvalidRtpExtensionId) {
334 // Deregister in case the extension was previously enabled.
335 rtp_rtcp->DeregisterSendRtpHeaderExtension(
336 kRtpExtensionAbsoluteSendTime);
337 if (rtp_rtcp->RegisterSendRtpHeaderExtension(
338 kRtpExtensionAbsoluteSendTime,
339 absolute_send_time_extension_id_) != 0) {
342 rtp_rtcp->DeregisterSendRtpHeaderExtension(
343 kRtpExtensionAbsoluteSendTime);
345 rtp_rtcp->RegisterSendFrameCountObserver(
346 rtp_rtcp_->GetSendFrameCountObserver());
347 rtp_rtcp->RegisterSendChannelRtcpStatisticsCallback(
348 rtp_rtcp_->GetSendChannelRtcpStatisticsCallback());
349 rtp_rtcp->RegisterSendChannelRtpStatisticsCallback(
350 rtp_rtcp_->GetSendChannelRtpStatisticsCallback());
351 rtp_rtcp->RegisterVideoBitrateObserver(
352 rtp_rtcp_->GetVideoBitrateObserver());
354 // |RegisterSimulcastRtpRtcpModules| resets all old weak pointers and old
355 // modules can be deleted after this step.
356 vie_receiver_.RegisterSimulcastRtpRtcpModules(simulcast_rtp_rtcp_);
358 while (!simulcast_rtp_rtcp_.empty()) {
359 RtpRtcp* rtp_rtcp = simulcast_rtp_rtcp_.back();
360 module_process_thread_.DeRegisterModule(rtp_rtcp);
361 rtp_rtcp->SetSendingStatus(false);
362 rtp_rtcp->SetSendingMediaStatus(false);
363 rtp_rtcp->RegisterSendFrameCountObserver(NULL);
364 rtp_rtcp->RegisterSendChannelRtcpStatisticsCallback(NULL);
365 rtp_rtcp->RegisterSendChannelRtpStatisticsCallback(NULL);
366 rtp_rtcp->RegisterVideoBitrateObserver(NULL);
367 simulcast_rtp_rtcp_.pop_back();
368 removed_rtp_rtcp_.push_front(rtp_rtcp);
370 // Clear any previous modules.
371 vie_receiver_.RegisterSimulcastRtpRtcpModules(simulcast_rtp_rtcp_);
373 // Enable this if H264 is available.
374 // This sets the wanted packetization mode.
375 // if (video_codec.plType == kVideoCodecH264) {
376 // if (video_codec.codecSpecific.H264.packetization == kH264SingleMode) {
377 // rtp_rtcp_->SetH264PacketizationMode(H264_SINGLE_NAL_MODE);
379 // rtp_rtcp_->SetH264PacketizationMode(H264_NON_INTERLEAVED_MODE);
381 // if (video_codec.codecSpecific.H264.configParametersSize > 0) {
382 // rtp_rtcp_->SetH264SendModeNALU_PPS_SPS(true);
386 // Don't log this error, no way to check in advance if this pl_type is
387 // registered or not...
388 rtp_rtcp_->DeRegisterSendPayload(video_codec.plType);
389 if (rtp_rtcp_->RegisterSendPayload(video_codec) != 0) {
393 rtp_rtcp_->SetSendingStatus(true);
394 for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
395 it != simulcast_rtp_rtcp_.end(); ++it) {
396 (*it)->SetSendingStatus(true);
397 (*it)->SetSendingMediaStatus(true);
403 int32_t ViEChannel::SetReceiveCodec(const VideoCodec& video_codec) {
404 if (!vie_receiver_.SetReceiveCodec(video_codec)) {
408 if (video_codec.codecType != kVideoCodecRED &&
409 video_codec.codecType != kVideoCodecULPFEC) {
410 // Register codec type with VCM, but do not register RED or ULPFEC.
411 if (vcm_.RegisterReceiveCodec(&video_codec, number_of_cores_,
412 wait_for_key_frame_) != VCM_OK) {
419 int32_t ViEChannel::GetReceiveCodec(VideoCodec* video_codec) {
420 if (vcm_.ReceiveCodec(video_codec) != 0) {
426 int32_t ViEChannel::RegisterCodecObserver(ViEDecoderObserver* observer) {
427 CriticalSectionScoped cs(callback_cs_.get());
429 if (codec_observer_) {
430 LOG_F(LS_ERROR) << "Observer already registered.";
433 codec_observer_ = observer;
435 codec_observer_ = NULL;
440 int32_t ViEChannel::RegisterExternalDecoder(const uint8_t pl_type,
441 VideoDecoder* decoder,
442 bool buffered_rendering,
443 int32_t render_delay) {
445 result = vcm_.RegisterExternalDecoder(decoder, pl_type, buffered_rendering);
446 if (result != VCM_OK) {
449 return vcm_.SetRenderDelay(render_delay);
452 int32_t ViEChannel::DeRegisterExternalDecoder(const uint8_t pl_type) {
453 VideoCodec current_receive_codec;
455 result = vcm_.ReceiveCodec(¤t_receive_codec);
456 if (vcm_.RegisterExternalDecoder(NULL, pl_type, false) != VCM_OK) {
460 if (result == 0 && current_receive_codec.plType == pl_type) {
461 result = vcm_.RegisterReceiveCodec(¤t_receive_codec, number_of_cores_,
462 wait_for_key_frame_);
467 int32_t ViEChannel::ReceiveCodecStatistics(uint32_t* num_key_frames,
468 uint32_t* num_delta_frames) {
469 VCMFrameCount received_frames;
470 if (vcm_.ReceivedFrameCount(received_frames) != VCM_OK) {
473 *num_key_frames = received_frames.numKeyFrames;
474 *num_delta_frames = received_frames.numDeltaFrames;
478 uint32_t ViEChannel::DiscardedPackets() const {
479 return vcm_.DiscardedPackets();
482 int ViEChannel::ReceiveDelay() const {
486 int32_t ViEChannel::WaitForKeyFrame(bool wait) {
487 wait_for_key_frame_ = wait;
491 int32_t ViEChannel::SetSignalPacketLossStatus(bool enable,
492 bool only_key_frames) {
494 if (only_key_frames) {
495 vcm_.SetVideoProtection(kProtectionKeyOnLoss, false);
496 if (vcm_.SetVideoProtection(kProtectionKeyOnKeyLoss, true) != VCM_OK) {
500 vcm_.SetVideoProtection(kProtectionKeyOnKeyLoss, false);
501 if (vcm_.SetVideoProtection(kProtectionKeyOnLoss, true) != VCM_OK) {
506 vcm_.SetVideoProtection(kProtectionKeyOnLoss, false);
507 vcm_.SetVideoProtection(kProtectionKeyOnKeyLoss, false);
512 int32_t ViEChannel::SetRTCPMode(const RTCPMethod rtcp_mode) {
513 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
514 for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
515 it != simulcast_rtp_rtcp_.end();
517 RtpRtcp* rtp_rtcp = *it;
518 rtp_rtcp->SetRTCPStatus(rtcp_mode);
520 return rtp_rtcp_->SetRTCPStatus(rtcp_mode);
523 int32_t ViEChannel::GetRTCPMode(RTCPMethod* rtcp_mode) {
524 *rtcp_mode = rtp_rtcp_->RTCP();
528 int32_t ViEChannel::SetNACKStatus(const bool enable) {
529 // Update the decoding VCM.
530 if (vcm_.SetVideoProtection(kProtectionNack, enable) != VCM_OK) {
534 // Disable possible FEC.
535 SetFECStatus(false, 0, 0);
537 // Update the decoding VCM.
538 if (vcm_.SetVideoProtection(kProtectionNack, enable) != VCM_OK) {
541 return ProcessNACKRequest(enable);
544 int32_t ViEChannel::ProcessNACKRequest(const bool enable) {
547 if (rtp_rtcp_->RTCP() == kRtcpOff) {
550 vie_receiver_.SetNackStatus(true, max_nack_reordering_threshold_);
551 rtp_rtcp_->SetStorePacketsStatus(true, nack_history_size_sender_);
552 vcm_.RegisterPacketRequestCallback(this);
554 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
556 for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
557 it != simulcast_rtp_rtcp_.end();
559 RtpRtcp* rtp_rtcp = *it;
560 rtp_rtcp->SetStorePacketsStatus(true, nack_history_size_sender_);
562 // Don't introduce errors when NACK is enabled.
563 vcm_.SetDecodeErrorMode(kNoErrors);
565 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
566 for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
567 it != simulcast_rtp_rtcp_.end();
569 RtpRtcp* rtp_rtcp = *it;
570 if (paced_sender_ == NULL) {
571 rtp_rtcp->SetStorePacketsStatus(false, 0);
574 vcm_.RegisterPacketRequestCallback(NULL);
575 if (paced_sender_ == NULL) {
576 rtp_rtcp_->SetStorePacketsStatus(false, 0);
578 vie_receiver_.SetNackStatus(false, max_nack_reordering_threshold_);
579 // When NACK is off, allow decoding with errors. Otherwise, the video
580 // will freeze, and will only recover with a complete key frame.
581 vcm_.SetDecodeErrorMode(kWithErrors);
586 int32_t ViEChannel::SetFECStatus(const bool enable,
587 const unsigned char payload_typeRED,
588 const unsigned char payload_typeFEC) {
589 // Disable possible NACK.
591 SetNACKStatus(false);
594 return ProcessFECRequest(enable, payload_typeRED, payload_typeFEC);
597 int32_t ViEChannel::ProcessFECRequest(
599 const unsigned char payload_typeRED,
600 const unsigned char payload_typeFEC) {
601 if (rtp_rtcp_->SetGenericFECStatus(enable, payload_typeRED,
602 payload_typeFEC) != 0) {
605 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
606 for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
607 it != simulcast_rtp_rtcp_.end();
609 RtpRtcp* rtp_rtcp = *it;
610 rtp_rtcp->SetGenericFECStatus(enable, payload_typeRED, payload_typeFEC);
615 int32_t ViEChannel::SetHybridNACKFECStatus(
617 const unsigned char payload_typeRED,
618 const unsigned char payload_typeFEC) {
619 if (vcm_.SetVideoProtection(kProtectionNackFEC, enable) != VCM_OK) {
624 ret_val = ProcessNACKRequest(enable);
628 return ProcessFECRequest(enable, payload_typeRED, payload_typeFEC);
631 int ViEChannel::SetSenderBufferingMode(int target_delay_ms) {
632 if ((target_delay_ms < 0) || (target_delay_ms > kMaxTargetDelayMs)) {
633 LOG(LS_ERROR) << "Invalid send buffer value.";
636 if (target_delay_ms == 0) {
638 nack_history_size_sender_ = kSendSidePacketHistorySize;
640 nack_history_size_sender_ = GetRequiredNackListSize(target_delay_ms);
641 // Don't allow a number lower than the default value.
642 if (nack_history_size_sender_ < kSendSidePacketHistorySize) {
643 nack_history_size_sender_ = kSendSidePacketHistorySize;
646 if (rtp_rtcp_->SetStorePacketsStatus(true, nack_history_size_sender_) != 0) {
652 int ViEChannel::SetReceiverBufferingMode(int target_delay_ms) {
653 if ((target_delay_ms < 0) || (target_delay_ms > kMaxTargetDelayMs)) {
654 LOG(LS_ERROR) << "Invalid receive buffer delay value.";
657 int max_nack_list_size;
658 int max_incomplete_time_ms;
659 if (target_delay_ms == 0) {
660 // Real-time mode - restore default settings.
661 max_nack_reordering_threshold_ = kMaxPacketAgeToNack;
662 max_nack_list_size = kMaxNackListSize;
663 max_incomplete_time_ms = 0;
665 max_nack_list_size = 3 * GetRequiredNackListSize(target_delay_ms) / 4;
666 max_nack_reordering_threshold_ = max_nack_list_size;
667 // Calculate the max incomplete time and round to int.
668 max_incomplete_time_ms = static_cast<int>(kMaxIncompleteTimeMultiplier *
669 target_delay_ms + 0.5f);
671 vcm_.SetNackSettings(max_nack_list_size, max_nack_reordering_threshold_,
672 max_incomplete_time_ms);
673 vcm_.SetMinReceiverDelay(target_delay_ms);
674 if (vie_sync_.SetTargetBufferingDelay(target_delay_ms) < 0)
679 int ViEChannel::GetRequiredNackListSize(int target_delay_ms) {
680 // The max size of the nack list should be large enough to accommodate the
681 // the number of packets (frames) resulting from the increased delay.
682 // Roughly estimating for ~40 packets per frame @ 30fps.
683 return target_delay_ms * 40 * 30 / 1000;
686 int32_t ViEChannel::SetKeyFrameRequestMethod(
687 const KeyFrameRequestMethod method) {
688 return rtp_rtcp_->SetKeyFrameRequestMethod(method);
691 bool ViEChannel::EnableRemb(bool enable) {
692 if (rtp_rtcp_->SetREMBStatus(enable) != 0)
697 int ViEChannel::SetSendTimestampOffsetStatus(bool enable, int id) {
698 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
701 // Enable the extension, but disable possible old id to avoid errors.
702 send_timestamp_extension_id_ = id;
703 rtp_rtcp_->DeregisterSendRtpHeaderExtension(
704 kRtpExtensionTransmissionTimeOffset);
705 error = rtp_rtcp_->RegisterSendRtpHeaderExtension(
706 kRtpExtensionTransmissionTimeOffset, id);
707 for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
708 it != simulcast_rtp_rtcp_.end(); it++) {
709 (*it)->DeregisterSendRtpHeaderExtension(
710 kRtpExtensionTransmissionTimeOffset);
711 error |= (*it)->RegisterSendRtpHeaderExtension(
712 kRtpExtensionTransmissionTimeOffset, id);
715 // Disable the extension.
716 send_timestamp_extension_id_ = kInvalidRtpExtensionId;
717 rtp_rtcp_->DeregisterSendRtpHeaderExtension(
718 kRtpExtensionTransmissionTimeOffset);
719 for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
720 it != simulcast_rtp_rtcp_.end(); it++) {
721 (*it)->DeregisterSendRtpHeaderExtension(
722 kRtpExtensionTransmissionTimeOffset);
728 int ViEChannel::SetReceiveTimestampOffsetStatus(bool enable, int id) {
729 return vie_receiver_.SetReceiveTimestampOffsetStatus(enable, id) ? 0 : -1;
732 int ViEChannel::SetSendAbsoluteSendTimeStatus(bool enable, int id) {
733 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
736 // Enable the extension, but disable possible old id to avoid errors.
737 absolute_send_time_extension_id_ = id;
738 rtp_rtcp_->DeregisterSendRtpHeaderExtension(
739 kRtpExtensionAbsoluteSendTime);
740 error = rtp_rtcp_->RegisterSendRtpHeaderExtension(
741 kRtpExtensionAbsoluteSendTime, id);
742 for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
743 it != simulcast_rtp_rtcp_.end(); it++) {
744 (*it)->DeregisterSendRtpHeaderExtension(
745 kRtpExtensionAbsoluteSendTime);
746 error |= (*it)->RegisterSendRtpHeaderExtension(
747 kRtpExtensionAbsoluteSendTime, id);
750 // Disable the extension.
751 absolute_send_time_extension_id_ = kInvalidRtpExtensionId;
752 rtp_rtcp_->DeregisterSendRtpHeaderExtension(
753 kRtpExtensionAbsoluteSendTime);
754 for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
755 it != simulcast_rtp_rtcp_.end(); it++) {
756 (*it)->DeregisterSendRtpHeaderExtension(
757 kRtpExtensionAbsoluteSendTime);
763 int ViEChannel::SetReceiveAbsoluteSendTimeStatus(bool enable, int id) {
764 return vie_receiver_.SetReceiveAbsoluteSendTimeStatus(enable, id) ? 0 : -1;
767 void ViEChannel::SetRtcpXrRrtrStatus(bool enable) {
768 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
769 rtp_rtcp_->SetRtcpXrRrtrStatus(enable);
772 void ViEChannel::SetTransmissionSmoothingStatus(bool enable) {
773 assert(paced_sender_ && "No paced sender registered.");
774 paced_sender_->SetStatus(enable);
777 int32_t ViEChannel::EnableTMMBR(const bool enable) {
778 return rtp_rtcp_->SetTMMBRStatus(enable);
781 int32_t ViEChannel::EnableKeyFrameRequestCallback(const bool enable) {
783 CriticalSectionScoped cs(callback_cs_.get());
784 if (enable && !codec_observer_) {
785 LOG(LS_ERROR) << "No ViECodecObserver set.";
788 do_key_frame_callbackRequest_ = enable;
792 int32_t ViEChannel::SetSSRC(const uint32_t SSRC,
793 const StreamType usage,
794 const uint8_t simulcast_idx) {
795 int rtx_settings = kRtxRetransmitted;
796 if (config_.Get<PaddingStrategy>().redundant_payloads)
797 rtx_settings |= kRtxRedundantPayloads;
798 if (simulcast_idx == 0) {
799 if (usage == kViEStreamTypeRtx) {
800 return rtp_rtcp_->SetRTXSendStatus(rtx_settings, true, SSRC);
802 return rtp_rtcp_->SetSSRC(SSRC);
804 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
805 if (simulcast_idx > simulcast_rtp_rtcp_.size()) {
808 std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
809 for (int i = 1; i < simulcast_idx; ++i, ++it) {
810 if (it == simulcast_rtp_rtcp_.end()) {
814 RtpRtcp* rtp_rtcp_module = *it;
815 if (usage == kViEStreamTypeRtx) {
816 return rtp_rtcp_module->SetRTXSendStatus(rtx_settings, true, SSRC);
818 return rtp_rtcp_module->SetSSRC(SSRC);
821 int32_t ViEChannel::SetRemoteSSRCType(const StreamType usage,
822 const uint32_t SSRC) {
823 vie_receiver_.SetRtxStatus(true, SSRC);
827 int32_t ViEChannel::GetLocalSSRC(uint8_t idx, unsigned int* ssrc) {
829 *ssrc = rtp_rtcp_->SSRC();
832 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
833 if (idx > simulcast_rtp_rtcp_.size()) {
836 std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
837 for (int i = 1; i < idx; ++i, ++it) {
838 if (it == simulcast_rtp_rtcp_.end()) {
842 *ssrc = (*it)->SSRC();
846 int32_t ViEChannel::GetRemoteSSRC(uint32_t* ssrc) {
847 *ssrc = vie_receiver_.GetRemoteSsrc();
851 int32_t ViEChannel::GetRemoteCSRC(uint32_t CSRCs[kRtpCsrcSize]) {
852 uint32_t arrayCSRC[kRtpCsrcSize];
853 memset(arrayCSRC, 0, sizeof(arrayCSRC));
855 int num_csrcs = vie_receiver_.GetCsrcs(arrayCSRC);
857 memcpy(CSRCs, arrayCSRC, num_csrcs * sizeof(uint32_t));
862 int ViEChannel::SetRtxSendPayloadType(int payload_type) {
863 if (rtp_rtcp_->Sending()) {
866 rtp_rtcp_->SetRtxSendPayloadType(payload_type);
867 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
868 for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
869 it != simulcast_rtp_rtcp_.end(); it++) {
870 (*it)->SetRtxSendPayloadType(payload_type);
875 void ViEChannel::SetRtxReceivePayloadType(int payload_type) {
876 vie_receiver_.SetRtxPayloadType(payload_type);
879 int32_t ViEChannel::SetStartSequenceNumber(uint16_t sequence_number) {
880 if (rtp_rtcp_->Sending()) {
883 return rtp_rtcp_->SetSequenceNumber(sequence_number);
886 int32_t ViEChannel::SetRTCPCName(const char rtcp_cname[]) {
887 if (rtp_rtcp_->Sending()) {
890 return rtp_rtcp_->SetCNAME(rtcp_cname);
893 int32_t ViEChannel::GetRTCPCName(char rtcp_cname[]) {
894 return rtp_rtcp_->CNAME(rtcp_cname);
897 int32_t ViEChannel::GetRemoteRTCPCName(char rtcp_cname[]) {
898 uint32_t remoteSSRC = vie_receiver_.GetRemoteSsrc();
899 return rtp_rtcp_->RemoteCNAME(remoteSSRC, rtcp_cname);
902 int32_t ViEChannel::RegisterRtpObserver(ViERTPObserver* observer) {
903 CriticalSectionScoped cs(callback_cs_.get());
906 LOG_F(LS_ERROR) << "Observer already registered.";
909 rtp_observer_ = observer;
911 rtp_observer_ = NULL;
916 int32_t ViEChannel::RegisterRtcpObserver(ViERTCPObserver* observer) {
917 CriticalSectionScoped cs(callback_cs_.get());
919 if (rtcp_observer_) {
920 LOG_F(LS_ERROR) << "Observer already registered.";
923 rtcp_observer_ = observer;
925 rtcp_observer_ = NULL;
930 int32_t ViEChannel::SendApplicationDefinedRTCPPacket(
931 const uint8_t sub_type,
934 uint16_t data_length_in_bytes) {
935 if (!rtp_rtcp_->Sending()) {
939 LOG_F(LS_ERROR) << "Invalid input.";
942 if (data_length_in_bytes % 4 != 0) {
943 LOG(LS_ERROR) << "Invalid input length.";
946 RTCPMethod rtcp_method = rtp_rtcp_->RTCP();
947 if (rtcp_method == kRtcpOff) {
948 LOG_F(LS_ERROR) << "RTCP not enable.";
951 // Create and send packet.
952 if (rtp_rtcp_->SetRTCPApplicationSpecificData(sub_type, name, data,
953 data_length_in_bytes) != 0) {
959 int32_t ViEChannel::GetSendRtcpStatistics(uint16_t* fraction_lost,
960 uint32_t* cumulative_lost,
961 uint32_t* extended_max,
962 uint32_t* jitter_samples,
964 // TODO(pwestin) how do we do this for simulcast ? average for all
965 // except cumulative_lost that is the sum ?
966 // CriticalSectionScoped cs(rtp_rtcp_cs_.get());
968 // for (std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
969 // it != simulcast_rtp_rtcp_.end();
971 // RtpRtcp* rtp_rtcp = *it;
973 uint32_t remote_ssrc = vie_receiver_.GetRemoteSsrc();
975 // Get all RTCP receiver report blocks that have been received on this
976 // channel. If we receive RTP packets from a remote source we know the
977 // remote SSRC and use the report block from him.
978 // Otherwise use the first report block.
979 std::vector<RTCPReportBlock> remote_stats;
980 if (rtp_rtcp_->RemoteRTCPStat(&remote_stats) != 0 || remote_stats.empty()) {
983 std::vector<RTCPReportBlock>::const_iterator statistics =
984 remote_stats.begin();
985 for (; statistics != remote_stats.end(); ++statistics) {
986 if (statistics->remoteSSRC == remote_ssrc)
990 if (statistics == remote_stats.end()) {
991 // If we have not received any RTCP packets from this SSRC it probably means
992 // we have not received any RTP packets.
993 // Use the first received report block instead.
994 statistics = remote_stats.begin();
995 remote_ssrc = statistics->remoteSSRC;
998 *fraction_lost = statistics->fractionLost;
999 *cumulative_lost = statistics->cumulativeLost;
1000 *extended_max = statistics->extendedHighSeqNum;
1001 *jitter_samples = statistics->jitter;
1005 if (rtp_rtcp_->RTT(remote_ssrc, &rtt, &dummy, &dummy, &dummy) != 0) {
1012 void ViEChannel::RegisterSendChannelRtcpStatisticsCallback(
1013 RtcpStatisticsCallback* callback) {
1014 rtp_rtcp_->RegisterSendChannelRtcpStatisticsCallback(callback);
1015 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1016 for (std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
1017 it != simulcast_rtp_rtcp_.end();
1019 (*it)->RegisterSendChannelRtcpStatisticsCallback(callback);
1023 // TODO(holmer): This is a bad function name as it implies that it returns the
1024 // received RTCP, while it actually returns the statistics which will be sent
1026 int32_t ViEChannel::GetReceivedRtcpStatistics(uint16_t* fraction_lost,
1027 uint32_t* cumulative_lost,
1028 uint32_t* extended_max,
1029 uint32_t* jitter_samples,
1031 uint32_t remote_ssrc = vie_receiver_.GetRemoteSsrc();
1032 StreamStatistician* statistician =
1033 vie_receiver_.GetReceiveStatistics()->GetStatistician(remote_ssrc);
1034 RtcpStatistics receive_stats;
1035 if (!statistician || !statistician->GetStatistics(
1036 &receive_stats, rtp_rtcp_->RTCP() == kRtcpOff)) {
1039 *fraction_lost = receive_stats.fraction_lost;
1040 *cumulative_lost = receive_stats.cumulative_lost;
1041 *extended_max = receive_stats.extended_max_sequence_number;
1042 *jitter_samples = receive_stats.jitter;
1046 rtp_rtcp_->RTT(remote_ssrc, &rtt, &dummy, &dummy, &dummy);
1051 void ViEChannel::RegisterReceiveChannelRtcpStatisticsCallback(
1052 RtcpStatisticsCallback* callback) {
1053 vie_receiver_.GetReceiveStatistics()->RegisterRtcpStatisticsCallback(
1057 int32_t ViEChannel::GetRtpStatistics(uint32_t* bytes_sent,
1058 uint32_t* packets_sent,
1059 uint32_t* bytes_received,
1060 uint32_t* packets_received) const {
1061 StreamStatistician* statistician = vie_receiver_.GetReceiveStatistics()->
1062 GetStatistician(vie_receiver_.GetRemoteSsrc());
1063 *bytes_received = 0;
1064 *packets_received = 0;
1066 statistician->GetDataCounters(bytes_received, packets_received);
1067 if (rtp_rtcp_->DataCountersRTP(bytes_sent, packets_sent) != 0) {
1070 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1071 for (std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
1072 it != simulcast_rtp_rtcp_.end();
1074 uint32_t bytes_sent_temp = 0;
1075 uint32_t packets_sent_temp = 0;
1076 RtpRtcp* rtp_rtcp = *it;
1077 rtp_rtcp->DataCountersRTP(&bytes_sent_temp, &packets_sent_temp);
1078 *bytes_sent += bytes_sent_temp;
1079 *packets_sent += packets_sent_temp;
1081 for (std::list<RtpRtcp*>::const_iterator it = removed_rtp_rtcp_.begin();
1082 it != removed_rtp_rtcp_.end(); ++it) {
1083 uint32_t bytes_sent_temp = 0;
1084 uint32_t packets_sent_temp = 0;
1085 RtpRtcp* rtp_rtcp = *it;
1086 rtp_rtcp->DataCountersRTP(&bytes_sent_temp, &packets_sent_temp);
1087 *bytes_sent += bytes_sent_temp;
1088 *packets_sent += packets_sent_temp;
1093 void ViEChannel::RegisterSendChannelRtpStatisticsCallback(
1094 StreamDataCountersCallback* callback) {
1095 rtp_rtcp_->RegisterSendChannelRtpStatisticsCallback(callback);
1097 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1098 for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
1099 it != simulcast_rtp_rtcp_.end();
1101 (*it)->RegisterSendChannelRtpStatisticsCallback(callback);
1106 void ViEChannel::RegisterReceiveChannelRtpStatisticsCallback(
1107 StreamDataCountersCallback* callback) {
1108 vie_receiver_.GetReceiveStatistics()->RegisterRtpStatisticsCallback(callback);
1111 void ViEChannel::GetRtcpPacketTypeCounters(
1112 RtcpPacketTypeCounter* packets_sent,
1113 RtcpPacketTypeCounter* packets_received) const {
1114 rtp_rtcp_->GetRtcpPacketTypeCounters(packets_sent, packets_received);
1116 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1117 for (std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
1118 it != simulcast_rtp_rtcp_.end(); ++it) {
1119 RtcpPacketTypeCounter sent;
1120 RtcpPacketTypeCounter received;
1121 (*it)->GetRtcpPacketTypeCounters(&sent, &received);
1122 packets_sent->Add(sent);
1123 packets_received->Add(received);
1125 for (std::list<RtpRtcp*>::const_iterator it = removed_rtp_rtcp_.begin();
1126 it != removed_rtp_rtcp_.end(); ++it) {
1127 RtcpPacketTypeCounter sent;
1128 RtcpPacketTypeCounter received;
1129 (*it)->GetRtcpPacketTypeCounters(&sent, &received);
1130 packets_sent->Add(sent);
1131 packets_received->Add(received);
1135 void ViEChannel::GetBandwidthUsage(uint32_t* total_bitrate_sent,
1136 uint32_t* video_bitrate_sent,
1137 uint32_t* fec_bitrate_sent,
1138 uint32_t* nackBitrateSent) const {
1139 rtp_rtcp_->BitrateSent(total_bitrate_sent, video_bitrate_sent,
1140 fec_bitrate_sent, nackBitrateSent);
1141 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1142 for (std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
1143 it != simulcast_rtp_rtcp_.end(); it++) {
1144 uint32_t stream_rate = 0;
1145 uint32_t video_rate = 0;
1146 uint32_t fec_rate = 0;
1147 uint32_t nackRate = 0;
1148 RtpRtcp* rtp_rtcp = *it;
1149 rtp_rtcp->BitrateSent(&stream_rate, &video_rate, &fec_rate, &nackRate);
1150 *total_bitrate_sent += stream_rate;
1151 *video_bitrate_sent += video_rate;
1152 *fec_bitrate_sent += fec_rate;
1153 *nackBitrateSent += nackRate;
1157 bool ViEChannel::GetSendSideDelay(int* avg_send_delay,
1158 int* max_send_delay) const {
1159 *avg_send_delay = 0;
1160 *max_send_delay = 0;
1161 bool valid_estimate = false;
1162 int num_send_delays = 0;
1163 if (rtp_rtcp_->GetSendSideDelay(avg_send_delay, max_send_delay)) {
1165 valid_estimate = true;
1167 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1168 for (std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
1169 it != simulcast_rtp_rtcp_.end(); it++) {
1170 RtpRtcp* rtp_rtcp = *it;
1171 int sub_stream_avg_delay = 0;
1172 int sub_stream_max_delay = 0;
1173 if (rtp_rtcp->GetSendSideDelay(&sub_stream_avg_delay,
1174 &sub_stream_max_delay)) {
1175 *avg_send_delay += sub_stream_avg_delay;
1176 *max_send_delay = std::max(*max_send_delay, sub_stream_max_delay);
1180 if (num_send_delays > 0) {
1181 valid_estimate = true;
1182 *avg_send_delay = *avg_send_delay / num_send_delays;
1183 *avg_send_delay = (*avg_send_delay + num_send_delays / 2) / num_send_delays;
1185 return valid_estimate;
1188 void ViEChannel::RegisterSendBitrateObserver(
1189 BitrateStatisticsObserver* observer) {
1190 rtp_rtcp_->RegisterVideoBitrateObserver(observer);
1191 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1192 for (std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
1193 it != simulcast_rtp_rtcp_.end();
1195 (*it)->RegisterVideoBitrateObserver(observer);
1199 void ViEChannel::GetReceiveBandwidthEstimatorStats(
1200 ReceiveBandwidthEstimatorStats* output) const {
1201 vie_receiver_.GetReceiveBandwidthEstimatorStats(output);
1204 int32_t ViEChannel::StartRTPDump(const char file_nameUTF8[1024],
1205 RTPDirections direction) {
1206 if (direction == kRtpIncoming) {
1207 return vie_receiver_.StartRTPDump(file_nameUTF8);
1209 return vie_sender_.StartRTPDump(file_nameUTF8);
1213 int32_t ViEChannel::StopRTPDump(RTPDirections direction) {
1214 if (direction == kRtpIncoming) {
1215 return vie_receiver_.StopRTPDump();
1217 return vie_sender_.StopRTPDump();
1221 int32_t ViEChannel::StartSend() {
1222 CriticalSectionScoped cs(callback_cs_.get());
1223 if (!external_transport_) {
1224 LOG(LS_ERROR) << "No transport set.";
1227 rtp_rtcp_->SetSendingMediaStatus(true);
1229 if (rtp_rtcp_->Sending()) {
1230 return kViEBaseAlreadySending;
1232 if (rtp_rtcp_->SetSendingStatus(true) != 0) {
1235 CriticalSectionScoped cs_rtp(rtp_rtcp_cs_.get());
1236 for (std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
1237 it != simulcast_rtp_rtcp_.end();
1239 RtpRtcp* rtp_rtcp = *it;
1240 rtp_rtcp->SetSendingMediaStatus(true);
1241 rtp_rtcp->SetSendingStatus(true);
1246 int32_t ViEChannel::StopSend() {
1247 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1248 rtp_rtcp_->SetSendingMediaStatus(false);
1249 for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
1250 it != simulcast_rtp_rtcp_.end();
1252 RtpRtcp* rtp_rtcp = *it;
1253 rtp_rtcp->SetSendingMediaStatus(false);
1255 if (!rtp_rtcp_->Sending()) {
1256 return kViEBaseNotSending;
1260 rtp_rtcp_->ResetSendDataCountersRTP();
1261 if (rtp_rtcp_->SetSendingStatus(false) != 0) {
1264 for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
1265 it != simulcast_rtp_rtcp_.end();
1267 RtpRtcp* rtp_rtcp = *it;
1268 rtp_rtcp->ResetSendDataCountersRTP();
1269 rtp_rtcp->SetSendingStatus(false);
1274 bool ViEChannel::Sending() {
1275 return rtp_rtcp_->Sending();
1278 int32_t ViEChannel::StartReceive() {
1279 CriticalSectionScoped cs(callback_cs_.get());
1280 if (StartDecodeThread() != 0) {
1281 vie_receiver_.StopReceive();
1284 vie_receiver_.StartReceive();
1288 int32_t ViEChannel::StopReceive() {
1289 vie_receiver_.StopReceive();
1291 vcm_.ResetDecoder();
1295 int32_t ViEChannel::RegisterSendTransport(Transport* transport) {
1296 if (rtp_rtcp_->Sending()) {
1300 CriticalSectionScoped cs(callback_cs_.get());
1301 if (external_transport_) {
1302 LOG_F(LS_ERROR) << "Transport already registered.";
1305 external_transport_ = transport;
1306 vie_sender_.RegisterSendTransport(transport);
1310 int32_t ViEChannel::DeregisterSendTransport() {
1311 CriticalSectionScoped cs(callback_cs_.get());
1312 if (!external_transport_) {
1315 if (rtp_rtcp_->Sending()) {
1316 LOG_F(LS_ERROR) << "Can't deregister transport when sending.";
1319 external_transport_ = NULL;
1320 vie_sender_.DeregisterSendTransport();
1324 int32_t ViEChannel::ReceivedRTPPacket(
1325 const void* rtp_packet, const int32_t rtp_packet_length,
1326 const PacketTime& packet_time) {
1328 CriticalSectionScoped cs(callback_cs_.get());
1329 if (!external_transport_) {
1333 return vie_receiver_.ReceivedRTPPacket(
1334 rtp_packet, rtp_packet_length, packet_time);
1337 int32_t ViEChannel::ReceivedRTCPPacket(
1338 const void* rtcp_packet, const int32_t rtcp_packet_length) {
1340 CriticalSectionScoped cs(callback_cs_.get());
1341 if (!external_transport_) {
1345 return vie_receiver_.ReceivedRTCPPacket(rtcp_packet, rtcp_packet_length);
1348 int32_t ViEChannel::SetMTU(uint16_t mtu) {
1349 if (rtp_rtcp_->SetMaxTransferUnit(mtu) != 0) {
1352 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1353 for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
1354 it != simulcast_rtp_rtcp_.end();
1356 RtpRtcp* rtp_rtcp = *it;
1357 rtp_rtcp->SetMaxTransferUnit(mtu);
1363 uint16_t ViEChannel::MaxDataPayloadLength() const {
1364 return rtp_rtcp_->MaxDataPayloadLength();
1367 int32_t ViEChannel::EnableColorEnhancement(bool enable) {
1368 CriticalSectionScoped cs(callback_cs_.get());
1369 color_enhancement_ = enable;
1373 RtpRtcp* ViEChannel::rtp_rtcp() {
1374 return rtp_rtcp_.get();
1377 CallStatsObserver* ViEChannel::GetStatsObserver() {
1378 return stats_observer_.get();
1381 // Do not acquire the lock of |vcm_| in this function. Decode callback won't
1382 // necessarily be called from the decoding thread. The decoding thread may have
1383 // held the lock when calling VideoDecoder::Decode, Reset, or Release. Acquiring
1384 // the same lock in the path of decode callback can deadlock.
1385 int32_t ViEChannel::FrameToRender(
1386 I420VideoFrame& video_frame) { // NOLINT
1387 CriticalSectionScoped cs(callback_cs_.get());
1389 if (decoder_reset_) {
1390 // Trigger a callback to the user if the incoming codec has changed.
1391 if (codec_observer_) {
1392 // The codec set by RegisterReceiveCodec might not be the size we're
1393 // actually decoding.
1394 receive_codec_.width = static_cast<uint16_t>(video_frame.width());
1395 receive_codec_.height = static_cast<uint16_t>(video_frame.height());
1396 codec_observer_->IncomingCodecChanged(channel_id_, receive_codec_);
1398 decoder_reset_ = false;
1400 // Post processing is not supported if the frame is backed by a texture.
1401 if (video_frame.native_handle() == NULL) {
1402 if (pre_render_callback_ != NULL)
1403 pre_render_callback_->FrameCallback(&video_frame);
1404 if (effect_filter_) {
1405 unsigned int length = CalcBufferSize(kI420,
1406 video_frame.width(),
1407 video_frame.height());
1408 scoped_ptr<uint8_t[]> video_buffer(new uint8_t[length]);
1409 ExtractBuffer(video_frame, length, video_buffer.get());
1410 effect_filter_->Transform(length,
1412 video_frame.ntp_time_ms(),
1413 video_frame.timestamp(),
1414 video_frame.width(),
1415 video_frame.height());
1417 if (color_enhancement_) {
1418 VideoProcessingModule::ColorEnhancement(&video_frame);
1422 uint32_t arr_ofCSRC[kRtpCsrcSize];
1423 int32_t no_of_csrcs = vie_receiver_.GetCsrcs(arr_ofCSRC);
1424 if (no_of_csrcs <= 0) {
1425 arr_ofCSRC[0] = vie_receiver_.GetRemoteSsrc();
1428 DeliverFrame(&video_frame, no_of_csrcs, arr_ofCSRC);
1432 int32_t ViEChannel::ReceivedDecodedReferenceFrame(
1433 const uint64_t picture_id) {
1434 return rtp_rtcp_->SendRTCPReferencePictureSelection(picture_id);
1437 void ViEChannel::IncomingCodecChanged(const VideoCodec& codec) {
1438 CriticalSectionScoped cs(callback_cs_.get());
1439 receive_codec_ = codec;
1442 int32_t ViEChannel::OnReceiveStatisticsUpdate(const uint32_t bit_rate,
1443 const uint32_t frame_rate) {
1444 CriticalSectionScoped cs(callback_cs_.get());
1445 if (codec_observer_) {
1446 codec_observer_->IncomingRate(channel_id_, frame_rate, bit_rate);
1451 void ViEChannel::OnDecoderTiming(int decode_ms,
1453 int current_delay_ms,
1454 int target_delay_ms,
1455 int jitter_buffer_ms,
1456 int min_playout_delay_ms,
1457 int render_delay_ms) {
1458 CriticalSectionScoped cs(callback_cs_.get());
1459 if (!codec_observer_)
1461 codec_observer_->DecoderTiming(decode_ms,
1466 min_playout_delay_ms,
1470 int32_t ViEChannel::RequestKeyFrame() {
1472 CriticalSectionScoped cs(callback_cs_.get());
1473 if (codec_observer_ && do_key_frame_callbackRequest_) {
1474 codec_observer_->RequestNewKeyFrame(channel_id_);
1477 return rtp_rtcp_->RequestKeyFrame();
1480 int32_t ViEChannel::SliceLossIndicationRequest(
1481 const uint64_t picture_id) {
1482 return rtp_rtcp_->SendRTCPSliceLossIndication((uint8_t) picture_id);
1485 int32_t ViEChannel::ResendPackets(const uint16_t* sequence_numbers,
1487 return rtp_rtcp_->SendNACK(sequence_numbers, length);
1490 bool ViEChannel::ChannelDecodeThreadFunction(void* obj) {
1491 return static_cast<ViEChannel*>(obj)->ChannelDecodeProcess();
1494 bool ViEChannel::ChannelDecodeProcess() {
1495 vcm_.Decode(kMaxDecodeWaitTimeMs);
1499 void ViEChannel::OnRttUpdate(uint32_t rtt) {
1500 vcm_.SetReceiveChannelParameters(rtt);
1503 int32_t ViEChannel::StartDecodeThread() {
1504 // Start the decode thread
1505 if (decode_thread_) {
1509 decode_thread_ = ThreadWrapper::CreateThread(ChannelDecodeThreadFunction,
1510 this, kHighestPriority,
1512 if (!decode_thread_) {
1516 unsigned int thread_id;
1517 if (decode_thread_->Start(thread_id) == false) {
1518 delete decode_thread_;
1519 decode_thread_ = NULL;
1520 LOG(LS_ERROR) << "Could not start decode thread.";
1526 int32_t ViEChannel::StopDecodeThread() {
1527 if (!decode_thread_) {
1531 decode_thread_->SetNotAlive();
1532 if (decode_thread_->Stop()) {
1533 delete decode_thread_;
1535 assert(false && "could not stop decode thread");
1537 decode_thread_ = NULL;
1541 int32_t ViEChannel::SetVoiceChannel(int32_t ve_channel_id,
1542 VoEVideoSync* ve_sync_interface) {
1543 if (ve_sync_interface) {
1544 // Register lip sync
1545 module_process_thread_.RegisterModule(&vie_sync_);
1547 module_process_thread_.DeRegisterModule(&vie_sync_);
1549 return vie_sync_.ConfigureSync(ve_channel_id,
1552 vie_receiver_.GetRtpReceiver());
1555 int32_t ViEChannel::VoiceChannel() {
1556 return vie_sync_.VoiceChannel();
1559 int32_t ViEChannel::RegisterEffectFilter(ViEEffectFilter* effect_filter) {
1560 CriticalSectionScoped cs(callback_cs_.get());
1561 if (effect_filter && effect_filter_) {
1562 LOG(LS_ERROR) << "Effect filter already registered.";
1565 effect_filter_ = effect_filter;
1569 void ViEChannel::RegisterPreRenderCallback(
1570 I420FrameCallback* pre_render_callback) {
1571 CriticalSectionScoped cs(callback_cs_.get());
1572 pre_render_callback_ = pre_render_callback;
1575 void ViEChannel::RegisterPreDecodeImageCallback(
1576 EncodedImageCallback* pre_decode_callback) {
1577 CriticalSectionScoped cs(callback_cs_.get());
1578 vcm_.RegisterPreDecodeImageCallback(pre_decode_callback);
1581 void ViEChannel::OnApplicationDataReceived(const int32_t id,
1582 const uint8_t sub_type,
1583 const uint32_t name,
1584 const uint16_t length,
1585 const uint8_t* data) {
1586 if (channel_id_ != ChannelId(id)) {
1589 CriticalSectionScoped cs(callback_cs_.get());
1591 if (rtcp_observer_) {
1592 rtcp_observer_->OnApplicationDataReceived(
1593 channel_id_, sub_type, name, reinterpret_cast<const char*>(data),
1599 int32_t ViEChannel::OnInitializeDecoder(
1601 const int8_t payload_type,
1602 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
1603 const int frequency,
1604 const uint8_t channels,
1605 const uint32_t rate) {
1606 LOG(LS_INFO) << "OnInitializeDecoder " << payload_type << " "
1608 vcm_.ResetDecoder();
1610 CriticalSectionScoped cs(callback_cs_.get());
1611 decoder_reset_ = true;
1615 void ViEChannel::OnIncomingSSRCChanged(const int32_t id, const uint32_t ssrc) {
1616 assert(channel_id_ == ChannelId(id));
1617 rtp_rtcp_->SetRemoteSSRC(ssrc);
1619 CriticalSectionScoped cs(callback_cs_.get());
1621 if (rtp_observer_) {
1622 rtp_observer_->IncomingSSRCChanged(channel_id_, ssrc);
1627 void ViEChannel::OnIncomingCSRCChanged(const int32_t id,
1628 const uint32_t CSRC,
1630 assert(channel_id_ == ChannelId(id));
1631 CriticalSectionScoped cs(callback_cs_.get());
1633 if (rtp_observer_) {
1634 rtp_observer_->IncomingCSRCChanged(channel_id_, CSRC, added);
1639 void ViEChannel::ResetStatistics(uint32_t ssrc) {
1640 StreamStatistician* statistician =
1641 vie_receiver_.GetReceiveStatistics()->GetStatistician(ssrc);
1643 statistician->ResetStatistics();
1646 void ViEChannel::RegisterSendFrameCountObserver(
1647 FrameCountObserver* observer) {
1648 rtp_rtcp_->RegisterSendFrameCountObserver(observer);
1649 CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1650 for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
1651 it != simulcast_rtp_rtcp_.end();
1653 (*it)->RegisterSendFrameCountObserver(observer);
1657 void ViEChannel::ReceivedBWEPacket(int64_t arrival_time_ms,
1658 int payload_size, const RTPHeader& header) {
1659 vie_receiver_.ReceivedBWEPacket(arrival_time_ms, payload_size, header);
1661 } // namespace webrtc