Wire up ssrc check in ViEEncoder for intra requests.

Review URL: https://webrtc-codereview.appspot.com/872004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@2884 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
mflodman@webrtc.org 2012-10-08 18:58:14 +00:00
parent c530043684
commit 15e4e34872
11 changed files with 171 additions and 34 deletions

View File

@ -64,13 +64,16 @@ bool EncoderStateFeedback::AddEncoder(uint32_t ssrc, ViEEncoder* encoder) {
return true; return true;
} }
void EncoderStateFeedback::RemoveEncoder(uint32_t ssrc) { void EncoderStateFeedback::RemoveEncoder(const ViEEncoder* encoder) {
CriticalSectionScoped lock(crit_.get()); CriticalSectionScoped lock(crit_.get());
SsrcEncoderMap::iterator it = encoders_.find(ssrc); SsrcEncoderMap::iterator it = encoders_.begin();
if (it == encoders_.end()) while (it != encoders_.end()) {
return; if (it->second == encoder) {
encoders_.erase(it++);
encoders_.erase(it); } else {
++it;
}
}
} }
RtcpIntraFrameObserver* EncoderStateFeedback::GetRtcpIntraFrameObserver() { RtcpIntraFrameObserver* EncoderStateFeedback::GetRtcpIntraFrameObserver() {
@ -112,8 +115,10 @@ void EncoderStateFeedback::OnLocalSsrcChanged(uint32_t old_ssrc,
return; return;
} }
encoders_[new_ssrc] = it->second; ViEEncoder* encoder = it->second;
encoders_.erase(it); encoders_.erase(it);
encoders_[new_ssrc] = encoder;
encoder->OnLocalSsrcChanged(old_ssrc, new_ssrc);
} }
} // namespace webrtc } // namespace webrtc

View File

@ -38,7 +38,7 @@ class EncoderStateFeedback {
bool AddEncoder(uint32_t ssrc, ViEEncoder* encoder); bool AddEncoder(uint32_t ssrc, ViEEncoder* encoder);
// Removes a registered ViEEncoder. // Removes a registered ViEEncoder.
void RemoveEncoder(uint32_t ssrc); void RemoveEncoder(const ViEEncoder* encoder);
// Returns an observer to register at the requesting class. The observer has // Returns an observer to register at the requesting class. The observer has
// the same lifetime as the EncoderStateFeedback instance. // the same lifetime as the EncoderStateFeedback instance.

View File

@ -81,7 +81,7 @@ TEST_F(VieKeyRequestTest, CreateAndTriggerRequests) {
encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedRPSI( encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedRPSI(
ssrc, rpsi_picture_id); ssrc, rpsi_picture_id);
encoder_state_feedback_->RemoveEncoder(ssrc); encoder_state_feedback_->RemoveEncoder(&encoder);
} }
// Register multiple encoders and make sure the request is relayed to correct // Register multiple encoders and make sure the request is relayed to correct
@ -125,12 +125,12 @@ TEST_F(VieKeyRequestTest, MultipleEncoders) {
encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedRPSI( encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedRPSI(
ssrc_2, rpsi_pid_2); ssrc_2, rpsi_pid_2);
encoder_state_feedback_->RemoveEncoder(ssrc_1); encoder_state_feedback_->RemoveEncoder(&encoder_1);
EXPECT_CALL(encoder_2, OnReceivedIntraFrameRequest(ssrc_2)) EXPECT_CALL(encoder_2, OnReceivedIntraFrameRequest(ssrc_2))
.Times(1); .Times(1);
encoder_state_feedback_->GetRtcpIntraFrameObserver()-> encoder_state_feedback_->GetRtcpIntraFrameObserver()->
OnReceivedIntraFrameRequest(ssrc_2); OnReceivedIntraFrameRequest(ssrc_2);
encoder_state_feedback_->RemoveEncoder(ssrc_2); encoder_state_feedback_->RemoveEncoder(&encoder_2);
} }
TEST_F(VieKeyRequestTest, AddTwiceError) { TEST_F(VieKeyRequestTest, AddTwiceError) {
@ -138,7 +138,7 @@ TEST_F(VieKeyRequestTest, AddTwiceError) {
MockVieEncoder encoder(process_thread_.get()); MockVieEncoder encoder(process_thread_.get());
EXPECT_TRUE(encoder_state_feedback_->AddEncoder(ssrc, &encoder)); EXPECT_TRUE(encoder_state_feedback_->AddEncoder(ssrc, &encoder));
EXPECT_FALSE(encoder_state_feedback_->AddEncoder(ssrc, &encoder)); EXPECT_FALSE(encoder_state_feedback_->AddEncoder(ssrc, &encoder));
encoder_state_feedback_->RemoveEncoder(ssrc); encoder_state_feedback_->RemoveEncoder(&encoder);
} }
} // namespace webrtc } // namespace webrtc

View File

@ -804,10 +804,26 @@ WebRtc_Word32 ViEChannel::SetRemoteSSRCType(const StreamType usage,
return rtp_rtcp_->SetRTXReceiveStatus(true, SSRC); return rtp_rtcp_->SetRTXReceiveStatus(true, SSRC);
} }
WebRtc_Word32 ViEChannel::GetLocalSSRC(uint32_t* ssrc) { // TODO(mflodman) Add kViEStreamTypeRtx.
WebRtc_Word32 ViEChannel::GetLocalSSRC(uint8_t idx, unsigned int* ssrc) {
WEBRTC_TRACE(kTraceInfo, kTraceVideo, ViEId(engine_id_, channel_id_), WEBRTC_TRACE(kTraceInfo, kTraceVideo, ViEId(engine_id_, channel_id_),
"%s", __FUNCTION__); "%s", __FUNCTION__);
*ssrc = rtp_rtcp_->SSRC();
if (idx == 0) {
*ssrc = rtp_rtcp_->SSRC();
return 0;
}
CriticalSectionScoped cs(rtp_rtcp_cs_.get());
if (idx > simulcast_rtp_rtcp_.size()) {
return -1;
}
std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
for (int i = 1; i < idx; ++i, ++it) {
if (it == simulcast_rtp_rtcp_.end()) {
return -1;
}
}
*ssrc = (*it)->SSRC();
return 0; return 0;
} }

View File

@ -118,8 +118,8 @@ class ViEChannel
const StreamType usage, const StreamType usage,
const unsigned char simulcast_idx); const unsigned char simulcast_idx);
// Gets SSRC for outgoing stream. // Gets SSRC for outgoing stream number |idx|.
WebRtc_Word32 GetLocalSSRC(uint32_t* ssrc); WebRtc_Word32 GetLocalSSRC(uint8_t idx, unsigned int* ssrc);
// Gets SSRC for the incoming stream. // Gets SSRC for the incoming stream.
WebRtc_Word32 GetRemoteSSRC(uint32_t* ssrc); WebRtc_Word32 GetRemoteSSRC(uint32_t* ssrc);

View File

@ -121,8 +121,12 @@ int ViEChannelManager::CreateChannel(int* channel_id) {
// Add ViEEncoder to EncoderFeedBackObserver. // Add ViEEncoder to EncoderFeedBackObserver.
unsigned int ssrc = 0; unsigned int ssrc = 0;
channel_map_[new_channel_id]->GetLocalSSRC(&ssrc); int idx = 0;
channel_map_[new_channel_id]->GetLocalSSRC(idx, &ssrc);
encoder_state_feedback->AddEncoder(ssrc, vie_encoder); encoder_state_feedback->AddEncoder(ssrc, vie_encoder);
std::list<unsigned int> ssrcs;
ssrcs.push_back(ssrc);
vie_encoder->SetSsrcs(ssrcs);
*channel_id = new_channel_id; *channel_id = new_channel_id;
group->AddChannel(*channel_id); group->AddChannel(*channel_id);
@ -170,7 +174,8 @@ int ViEChannelManager::CreateChannel(int* channel_id,
} }
// Register the ViEEncoder to get key frame requests for this channel. // Register the ViEEncoder to get key frame requests for this channel.
unsigned int ssrc = 0; unsigned int ssrc = 0;
channel_map_[new_channel_id]->GetLocalSSRC(&ssrc); int stream_idx = 0;
channel_map_[new_channel_id]->GetLocalSSRC(stream_idx, &ssrc);
encoder_state_feedback->AddEncoder(ssrc, vie_encoder); encoder_state_feedback->AddEncoder(ssrc, vie_encoder);
} else { } else {
vie_encoder = ViEEncoderPtr(original_channel); vie_encoder = ViEEncoderPtr(original_channel);
@ -224,14 +229,12 @@ int ViEChannelManager::DeleteChannel(int channel_id) {
group = FindGroup(channel_id); group = FindGroup(channel_id);
group->SetChannelRembStatus(channel_id, false, false, vie_channel, group->SetChannelRembStatus(channel_id, false, false, vie_channel,
vie_encoder); vie_encoder);
group->GetEncoderStateFeedback()->RemoveEncoder(vie_encoder);
unsigned int remote_ssrc = 0; unsigned int remote_ssrc = 0;
vie_channel->GetRemoteSSRC(&remote_ssrc); vie_channel->GetRemoteSSRC(&remote_ssrc);
group->RemoveChannel(channel_id, remote_ssrc); group->RemoveChannel(channel_id, remote_ssrc);
unsigned int local_ssrc = 0;
vie_channel->GetLocalSSRC(&local_ssrc);
group->GetEncoderStateFeedback()->RemoveEncoder(local_ssrc);
// Check if other channels are using the same encoder. // Check if other channels are using the same encoder.
if (ChannelUsingViEEncoder(channel_id)) { if (ChannelUsingViEEncoder(channel_id)) {
vie_encoder = NULL; vie_encoder = NULL;
@ -370,6 +373,24 @@ bool ViEChannelManager::SetBandwidthEstimationMode(
return true; return true;
} }
void ViEChannelManager::UpdateSsrcs(int channel_id,
const std::list<unsigned int>& ssrcs) {
CriticalSectionScoped cs(*channel_id_critsect_);
ChannelGroup* channel_group = FindGroup(channel_id);
if (channel_group == NULL) {
return;
}
ViEEncoder* encoder = ViEEncoderPtr(channel_id);
assert(encoder);
EncoderStateFeedback* encoder_state_feedback =
channel_group->GetEncoderStateFeedback();
for (std::list<unsigned int>::const_iterator it = ssrcs.begin();
it != ssrcs.end(); ++it) {
encoder_state_feedback->AddEncoder(*it, encoder);
}
}
bool ViEChannelManager::CreateChannelObject( bool ViEChannelManager::CreateChannelObject(
int channel_id, int channel_id,
ViEEncoder* vie_encoder, ViEEncoder* vie_encoder,

View File

@ -79,6 +79,10 @@ class ViEChannelManager: private ViEManagerBase {
// adding a channel. // adding a channel.
bool SetBandwidthEstimationMode(BandwidthEstimationMode mode); bool SetBandwidthEstimationMode(BandwidthEstimationMode mode);
// Updates the SSRCs for a channel. If one of the SSRCs already is registered,
// it will simply be ignored and no error is returned.
void UpdateSsrcs(int channel_id, const std::list<unsigned int>& ssrcs);
private: private:
// Creates a channel object connected to |vie_encoder|. Assumed to be called // Creates a channel object connected to |vie_encoder|. Assumed to be called
// protected. // protected.

View File

@ -10,6 +10,8 @@
#include "video_engine/vie_codec_impl.h" #include "video_engine/vie_codec_impl.h"
#include <list>
#include "engine_configurations.h" // NOLINT #include "engine_configurations.h" // NOLINT
#include "modules/video_coding/main/interface/video_coding.h" #include "modules/video_coding/main/interface/video_coding.h"
#include "system_wrappers/interface/trace.h" #include "system_wrappers/interface/trace.h"
@ -238,6 +240,32 @@ int ViECodecImpl::SetSendCodec(const int video_channel,
} }
} }
// TODO(mflodman) Break out this part in GetLocalSsrcListi().
// Update all SSRCs to ViEEncoder.
std::list<unsigned int> ssrcs;
if (video_codec_internal.numberOfSimulcastStreams == 0) {
unsigned int ssrc = 0;
if (vie_channel->GetLocalSSRC(0, &ssrc) != 0) {
WEBRTC_TRACE(kTraceError, kTraceVideo,
ViEId(shared_data_->instance_id(), video_channel),
"%s: Could not get ssrc", __FUNCTION__);
}
ssrcs.push_back(ssrc);
} else {
for (int idx = 0; idx < video_codec_internal.numberOfSimulcastStreams;
++idx) {
unsigned int ssrc = 0;
if (vie_channel->GetLocalSSRC(idx, &ssrc) != 0) {
WEBRTC_TRACE(kTraceError, kTraceVideo,
ViEId(shared_data_->instance_id(), video_channel),
"%s: Could not get ssrc for idx %d", __FUNCTION__, idx);
}
ssrcs.push_back(ssrc);
}
}
vie_encoder->SetSsrcs(ssrcs);
shared_data_->channel_manager()->UpdateSsrcs(video_channel, ssrcs);
// Update the protection mode, we might be switching NACK/FEC. // Update the protection mode, we might be switching NACK/FEC.
vie_encoder->UpdateProtectionMethod(); vie_encoder->UpdateProtectionMethod();

View File

@ -72,7 +72,6 @@ ViEEncoder::ViEEncoder(WebRtc_Word32 engine_id,
data_cs_(CriticalSectionWrapper::CreateCriticalSection()), data_cs_(CriticalSectionWrapper::CreateCriticalSection()),
bitrate_controller_(bitrate_controller), bitrate_controller_(bitrate_controller),
paused_(false), paused_(false),
time_last_intra_request_ms_(0),
channels_dropping_delta_frames_(0), channels_dropping_delta_frames_(0),
drop_next_frame_(false), drop_next_frame_(false),
fec_enabled_(false), fec_enabled_(false),
@ -806,23 +805,79 @@ void ViEEncoder::OnReceivedRPSI(uint32_t /*ssrc*/,
has_received_rpsi_ = true; has_received_rpsi_ = true;
} }
void ViEEncoder::OnReceivedIntraFrameRequest(uint32_t /*ssrc*/) { void ViEEncoder::OnReceivedIntraFrameRequest(uint32_t ssrc) {
// Key frame request from remote side, signal to VCM. // Key frame request from remote side, signal to VCM.
WEBRTC_TRACE(webrtc::kTraceStateInfo, webrtc::kTraceVideo, WEBRTC_TRACE(webrtc::kTraceStateInfo, webrtc::kTraceVideo,
ViEId(engine_id_, channel_id_), "%s", __FUNCTION__); ViEId(engine_id_, channel_id_), "%s", __FUNCTION__);
WebRtc_Word64 now = TickTime::MillisecondTimestamp(); int idx = 0;
if (time_last_intra_request_ms_ + kViEMinKeyRequestIntervalMs > now) { {
WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceVideo, CriticalSectionScoped cs(data_cs_.get());
ViEId(engine_id_, channel_id_), std::map<unsigned int, int>::iterator stream_it = ssrc_streams_.find(ssrc);
"%s: Not not encoding new intra due to timing", __FUNCTION__); if (stream_it == ssrc_streams_.end()) {
return; assert(false);
return;
}
std::map<unsigned int, WebRtc_Word64>::iterator time_it =
time_last_intra_request_ms_.find(ssrc);
if (time_it == time_last_intra_request_ms_.end()) {
time_last_intra_request_ms_[ssrc] = 0;
}
WebRtc_Word64 now = TickTime::MillisecondTimestamp();
if (time_last_intra_request_ms_[ssrc] + kViEMinKeyRequestIntervalMs > now) {
WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceVideo,
ViEId(engine_id_, channel_id_),
"%s: Not encoding new intra due to timing", __FUNCTION__);
return;
}
time_last_intra_request_ms_[ssrc] = now;
idx = stream_it->second;
} }
vcm_.IntraFrameRequest(0); // Release the critsect before triggering key frame.
time_last_intra_request_ms_ = now; vcm_.IntraFrameRequest(idx);
} }
void ViEEncoder::OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc) { void ViEEncoder::OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc) {
CriticalSectionScoped cs(data_cs_.get());
std::map<unsigned int, int>::iterator it = ssrc_streams_.find(old_ssrc);
if (it == ssrc_streams_.end()) {
return;
}
ssrc_streams_[new_ssrc] = it->second;
ssrc_streams_.erase(it);
std::map<unsigned int, int64_t>::iterator time_it =
time_last_intra_request_ms_.find(old_ssrc);
int64_t last_intra_request_ms = 0;
if (time_it != time_last_intra_request_ms_.end()) {
last_intra_request_ms = time_it->second;
time_last_intra_request_ms_.erase(time_it);
}
time_last_intra_request_ms_[new_ssrc] = last_intra_request_ms;
}
bool ViEEncoder::SetSsrcs(const std::list<unsigned int>& ssrcs) {
VideoCodec codec;
if (vcm_.SendCodec(&codec) != 0)
return false;
if (codec.numberOfSimulcastStreams > 0 &&
ssrcs.size() != codec.numberOfSimulcastStreams) {
return -1;
}
CriticalSectionScoped cs(data_cs_.get());
ssrc_streams_.clear();
time_last_intra_request_ms_.clear();
int idx = 0;
for (std::list<unsigned int>::const_iterator it = ssrcs.begin();
it != ssrcs.end(); ++it, ++idx) {
unsigned int ssrc = *it;
ssrc_streams_[ssrc] = idx;
}
return 0;
} }
// Called from ViEBitrateObserver. // Called from ViEBitrateObserver.

View File

@ -11,6 +11,9 @@
#ifndef WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_ #ifndef WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_
#define WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_ #define WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_
#include <list>
#include <map>
#include "common_types.h" // NOLINT #include "common_types.h" // NOLINT
#include "typedefs.h" //NOLINT #include "typedefs.h" //NOLINT
#include "modules/bitrate_controller/include/bitrate_controller.h" #include "modules/bitrate_controller/include/bitrate_controller.h"
@ -134,6 +137,9 @@ class ViEEncoder
virtual void OnReceivedRPSI(uint32_t ssrc, uint64_t picture_id); virtual void OnReceivedRPSI(uint32_t ssrc, uint64_t picture_id);
virtual void OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc); virtual void OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc);
// Sets SSRCs for all streams.
bool SetSsrcs(const std::list<unsigned int>& ssrcs);
// Effect filter. // Effect filter.
WebRtc_Word32 RegisterEffectFilter(ViEEffectFilter* effect_filter); WebRtc_Word32 RegisterEffectFilter(ViEEffectFilter* effect_filter);
@ -167,7 +173,7 @@ class ViEEncoder
BitrateController* bitrate_controller_; BitrateController* bitrate_controller_;
bool paused_; bool paused_;
WebRtc_Word64 time_last_intra_request_ms_; std::map<unsigned int, int64_t> time_last_intra_request_ms_;
WebRtc_Word32 channels_dropping_delta_frames_; WebRtc_Word32 channels_dropping_delta_frames_;
bool drop_next_frame_; bool drop_next_frame_;
@ -182,6 +188,7 @@ class ViEEncoder
WebRtc_UWord8 picture_id_sli_; WebRtc_UWord8 picture_id_sli_;
bool has_received_rpsi_; bool has_received_rpsi_;
WebRtc_UWord64 picture_id_rpsi_; WebRtc_UWord64 picture_id_rpsi_;
std::map<unsigned int, int> ssrc_streams_;
ViEFileRecorder file_recorder_; ViEFileRecorder file_recorder_;

View File

@ -183,7 +183,8 @@ int ViERTP_RTCPImpl::GetLocalSSRC(const int video_channel,
shared_data_->SetLastError(kViERtpRtcpInvalidChannelId); shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
return -1; return -1;
} }
if (vie_channel->GetLocalSSRC(&SSRC) != 0) { uint8_t idx = 0;
if (vie_channel->GetLocalSSRC(idx, &SSRC) != 0) {
shared_data_->SetLastError(kViERtpRtcpUnknownError); shared_data_->SetLastError(kViERtpRtcpUnknownError);
return -1; return -1;
} }