webrtc_m130/modules/video_coding/video_sender.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

364 lines
14 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2013 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 <algorithm> // std::max
#include "api/video/video_bitrate_allocator.h"
#include "common_types.h" // NOLINT(build/include)
#include "common_video/libyuv/include/webrtc_libyuv.h"
#include "modules/video_coding/encoded_frame.h"
#include "modules/video_coding/include/video_codec_interface.h"
#include "modules/video_coding/utility/default_video_bitrate_allocator.h"
#include "modules/video_coding/utility/quality_scaler.h"
#include "modules/video_coding/video_coding_impl.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
#include "system_wrappers/include/clock.h"
#include "system_wrappers/include/field_trial.h"
namespace webrtc {
namespace vcm {
namespace {
constexpr char kFrameDropperFieldTrial[] = "WebRTC-FrameDropper";
} // namespace
VideoSender::VideoSender(Clock* clock,
EncodedImageCallback* post_encode_callback)
: _encoder(nullptr),
_mediaOpt(clock),
_encodedFrameCallback(post_encode_callback, &_mediaOpt),
post_encode_callback_(post_encode_callback),
_codecDataBase(&_encodedFrameCallback),
frame_dropper_enabled_(true),
current_codec_(),
encoder_params_({VideoBitrateAllocation(), 0, 0, 0}),
encoder_has_internal_source_(false),
next_frame_types_(1, kVideoFrameDelta) {
// Allow VideoSender to be created on one thread but used on another, post
// construction. This is currently how this class is being used by at least
// one external project (diffractor).
sequenced_checker_.Detach();
}
VideoSender::~VideoSender() {}
// Register the send codec to be used.
int32_t VideoSender::RegisterSendCodec(const VideoCodec* sendCodec,
uint32_t numberOfCores,
uint32_t maxPayloadSize) {
RTC_DCHECK(sequenced_checker_.CalledSequentially());
rtc::CritScope lock(&encoder_crit_);
if (sendCodec == nullptr) {
return VCM_PARAMETER_ERROR;
}
bool ret =
_codecDataBase.SetSendCodec(sendCodec, numberOfCores, maxPayloadSize);
// Update encoder regardless of result to make sure that we're not holding on
// to a deleted instance.
_encoder = _codecDataBase.GetEncoder();
// Cache the current codec here so they can be fetched from this thread
// without requiring the _sendCritSect lock.
current_codec_ = *sendCodec;
if (!ret) {
RTC_LOG(LS_ERROR) << "Failed to initialize set encoder with codec type '"
<< sendCodec->codecType << "'.";
return VCM_CODEC_ERROR;
}
// SetSendCodec succeeded, _encoder should be set.
RTC_DCHECK(_encoder);
int numLayers;
if (sendCodec->codecType == kVideoCodecVP8) {
numLayers = sendCodec->VP8().numberOfTemporalLayers;
} else if (sendCodec->codecType == kVideoCodecVP9) {
numLayers = sendCodec->VP9().numberOfTemporalLayers;
} else if (sendCodec->codecType == kVideoCodecGeneric &&
sendCodec->numberOfSimulcastStreams > 0) {
// This is mainly for unit testing, disabling frame dropping.
// TODO(sprang): Add a better way to disable frame dropping.
numLayers = sendCodec->simulcastStream[0].numberOfTemporalLayers;
} else {
numLayers = 1;
}
// If we have screensharing and we have layers, we disable frame dropper.
const bool disable_frame_dropper =
field_trial::IsDisabled(kFrameDropperFieldTrial) ||
(numLayers > 1 && sendCodec->mode == VideoCodecMode::kScreensharing);
if (disable_frame_dropper) {
_mediaOpt.EnableFrameDropper(false);
} else if (frame_dropper_enabled_) {
_mediaOpt.EnableFrameDropper(true);
}
{
rtc::CritScope cs(&params_crit_);
next_frame_types_.clear();
next_frame_types_.resize(VCM_MAX(sendCodec->numberOfSimulcastStreams, 1),
kVideoFrameKey);
// Cache InternalSource() to have this available from IntraFrameRequest()
// without having to acquire encoder_crit_ (avoid blocking on encoder use).
encoder_has_internal_source_ = _encoder->InternalSource();
}
RTC_LOG(LS_VERBOSE) << " max bitrate " << sendCodec->maxBitrate
<< " start bitrate " << sendCodec->startBitrate
<< " max frame rate " << sendCodec->maxFramerate
<< " max payload size " << maxPayloadSize;
_mediaOpt.SetEncodingData(sendCodec->maxBitrate * 1000,
sendCodec->startBitrate * 1000,
sendCodec->maxFramerate);
return VCM_OK;
}
// Register an external decoder object.
// This can not be used together with external decoder callbacks.
void VideoSender::RegisterExternalEncoder(VideoEncoder* externalEncoder,
bool internalSource /*= false*/) {
RTC_DCHECK(sequenced_checker_.CalledSequentially());
Reland 8631 "Speculative revert of 8631 "Remove lock from Bitrat..." > Speculative revert of 8631 "Remove lock from Bitrate() and FrameRate() in Video..." > > We ran into the alignment problem on Mac 10.9 debug again. This is the only CL I see in the range that adds an rtc::CriticalSection, so I'm trying out reverting it before attempting another roll. > > > Remove lock from Bitrate() and FrameRate() in VideoSender. > > These methods are called on the VideoSender's construction thread, which is the same thread as modifies the value of _encoder. It's therefore safe to not require a lock to access _encoder on this thread. > > > > I'm making access to the rate variables from VCMGenericEncoder, thread safe, by using a lock that's not associated with the encoder. There should be little to no contention there. While modifying VCMGenericEncoder, I noticed that a couple of member variables weren't needed, so I removed them. > > > > The reason for this change is that getStats is currently contending with the encoder when Bitrate() is called. On my machine, this means that getStats can take about 25-30ms instead of ~1ms. > > > > Also adding some documentation for other methods and a suggestion for how we could avoid contention between the encoder and the network thread. > > > > BUG=2822 > > R=mflodman@webrtc.org > > > > Review URL: https://webrtc-codereview.appspot.com/43479004 > > TBR=tommi@webrtc.org > > Review URL: https://webrtc-codereview.appspot.com/45529004 TBR=tommi@webrtc.org Review URL: https://webrtc-codereview.appspot.com/46519004 Cr-Commit-Position: refs/heads/master@{#8645} git-svn-id: http://webrtc.googlecode.com/svn/trunk@8645 4adac7df-926f-26a2-2b94-8c16560cd09d
2015-03-07 20:55:56 +00:00
rtc::CritScope lock(&encoder_crit_);
if (externalEncoder == nullptr) {
_codecDataBase.DeregisterExternalEncoder();
{
// Make sure the VCM doesn't use the de-registered codec
rtc::CritScope params_lock(&params_crit_);
_encoder = nullptr;
encoder_has_internal_source_ = false;
}
return;
}
_codecDataBase.RegisterExternalEncoder(externalEncoder,
internalSource);
}
Reland #2 of Issue 2434073003: Extract bitrate allocation ... This is yet another reland of https://codereview.webrtc.org/2434073003/ including two fixes: 1. SimulcastRateAllocator did not handle the screenshare settings properly for numSimulcastStreams = 1. Additional test case was added for that. 2. In VideoSender, when rate allocation is updated after setting a new VideoCodec config, only update the state of the EncoderParameters, but don't actually run SetRateAllocation on the encoder itself. This caused some problems upstreams. Please review only the changes after patch set 1. Original description: Extract bitrate allocation of spatial/temporal layers out of codec impl. This CL makes a number of intervowen changes: * Add BitrateAllocation struct, that contains a codec independent view of how the target bitrate is distributed over spatial and temporal layers. * Adds the BitrateAllocator interface, which takes a bitrate and frame rate and produces a BitrateAllocation. * A default (non layered) implementation is added, and SimulcastRateAllocator is extended to fully handle VP8 allocation. This includes capturing TemporalLayer instances created by the encoder. * ViEEncoder now owns both the bitrate allocator and the temporal layer factories for VP8. This allows allocation to happen fully outside of the encoder implementation. This refactoring will make it possible for ViEEncoder to signal the full picture of target bitrates to the RTCP module. BUG=webrtc:6301 R=stefan@webrtc.org Review URL: https://codereview.webrtc.org/2510583002 . Cr-Commit-Position: refs/heads/master@{#15105}
2016-11-16 16:41:30 +01:00
EncoderParameters VideoSender::UpdateEncoderParameters(
const EncoderParameters& params,
VideoBitrateAllocator* bitrate_allocator,
uint32_t target_bitrate_bps) {
uint32_t video_target_rate_bps = _mediaOpt.SetTargetRates(target_bitrate_bps);
Reland 8631 "Speculative revert of 8631 "Remove lock from Bitrat..." > Speculative revert of 8631 "Remove lock from Bitrate() and FrameRate() in Video..." > > We ran into the alignment problem on Mac 10.9 debug again. This is the only CL I see in the range that adds an rtc::CriticalSection, so I'm trying out reverting it before attempting another roll. > > > Remove lock from Bitrate() and FrameRate() in VideoSender. > > These methods are called on the VideoSender's construction thread, which is the same thread as modifies the value of _encoder. It's therefore safe to not require a lock to access _encoder on this thread. > > > > I'm making access to the rate variables from VCMGenericEncoder, thread safe, by using a lock that's not associated with the encoder. There should be little to no contention there. While modifying VCMGenericEncoder, I noticed that a couple of member variables weren't needed, so I removed them. > > > > The reason for this change is that getStats is currently contending with the encoder when Bitrate() is called. On my machine, this means that getStats can take about 25-30ms instead of ~1ms. > > > > Also adding some documentation for other methods and a suggestion for how we could avoid contention between the encoder and the network thread. > > > > BUG=2822 > > R=mflodman@webrtc.org > > > > Review URL: https://webrtc-codereview.appspot.com/43479004 > > TBR=tommi@webrtc.org > > Review URL: https://webrtc-codereview.appspot.com/45529004 TBR=tommi@webrtc.org Review URL: https://webrtc-codereview.appspot.com/46519004 Cr-Commit-Position: refs/heads/master@{#8645} git-svn-id: http://webrtc.googlecode.com/svn/trunk@8645 4adac7df-926f-26a2-2b94-8c16560cd09d
2015-03-07 20:55:56 +00:00
uint32_t input_frame_rate = _mediaOpt.InputFrameRate();
if (input_frame_rate == 0)
input_frame_rate = current_codec_.maxFramerate;
VideoBitrateAllocation bitrate_allocation;
// Only call allocators if bitrate > 0 (ie, not suspended), otherwise they
// might cap the bitrate to the min bitrate configured.
if (target_bitrate_bps > 0) {
if (bitrate_allocator) {
bitrate_allocation = bitrate_allocator->GetAllocation(
video_target_rate_bps, input_frame_rate);
} else {
DefaultVideoBitrateAllocator default_allocator(current_codec_);
bitrate_allocation = default_allocator.GetAllocation(
video_target_rate_bps, input_frame_rate);
}
Reland #2 of Issue 2434073003: Extract bitrate allocation ... This is yet another reland of https://codereview.webrtc.org/2434073003/ including two fixes: 1. SimulcastRateAllocator did not handle the screenshare settings properly for numSimulcastStreams = 1. Additional test case was added for that. 2. In VideoSender, when rate allocation is updated after setting a new VideoCodec config, only update the state of the EncoderParameters, but don't actually run SetRateAllocation on the encoder itself. This caused some problems upstreams. Please review only the changes after patch set 1. Original description: Extract bitrate allocation of spatial/temporal layers out of codec impl. This CL makes a number of intervowen changes: * Add BitrateAllocation struct, that contains a codec independent view of how the target bitrate is distributed over spatial and temporal layers. * Adds the BitrateAllocator interface, which takes a bitrate and frame rate and produces a BitrateAllocation. * A default (non layered) implementation is added, and SimulcastRateAllocator is extended to fully handle VP8 allocation. This includes capturing TemporalLayer instances created by the encoder. * ViEEncoder now owns both the bitrate allocator and the temporal layer factories for VP8. This allows allocation to happen fully outside of the encoder implementation. This refactoring will make it possible for ViEEncoder to signal the full picture of target bitrates to the RTCP module. BUG=webrtc:6301 R=stefan@webrtc.org Review URL: https://codereview.webrtc.org/2510583002 . Cr-Commit-Position: refs/heads/master@{#15105}
2016-11-16 16:41:30 +01:00
}
EncoderParameters new_encoder_params = {bitrate_allocation, params.loss_rate,
params.rtt, input_frame_rate};
return new_encoder_params;
}
void VideoSender::UpdateChannelParameters(
VideoBitrateAllocator* bitrate_allocator,
VideoBitrateAllocationObserver* bitrate_updated_callback) {
VideoBitrateAllocation target_rate;
{
rtc::CritScope cs(&params_crit_);
encoder_params_ =
UpdateEncoderParameters(encoder_params_, bitrate_allocator,
encoder_params_.target_bitrate.get_sum_bps());
target_rate = encoder_params_.target_bitrate;
}
if (bitrate_updated_callback && target_rate.get_sum_bps() > 0)
bitrate_updated_callback->OnBitrateAllocationUpdated(target_rate);
Reland #2 of Issue 2434073003: Extract bitrate allocation ... This is yet another reland of https://codereview.webrtc.org/2434073003/ including two fixes: 1. SimulcastRateAllocator did not handle the screenshare settings properly for numSimulcastStreams = 1. Additional test case was added for that. 2. In VideoSender, when rate allocation is updated after setting a new VideoCodec config, only update the state of the EncoderParameters, but don't actually run SetRateAllocation on the encoder itself. This caused some problems upstreams. Please review only the changes after patch set 1. Original description: Extract bitrate allocation of spatial/temporal layers out of codec impl. This CL makes a number of intervowen changes: * Add BitrateAllocation struct, that contains a codec independent view of how the target bitrate is distributed over spatial and temporal layers. * Adds the BitrateAllocator interface, which takes a bitrate and frame rate and produces a BitrateAllocation. * A default (non layered) implementation is added, and SimulcastRateAllocator is extended to fully handle VP8 allocation. This includes capturing TemporalLayer instances created by the encoder. * ViEEncoder now owns both the bitrate allocator and the temporal layer factories for VP8. This allows allocation to happen fully outside of the encoder implementation. This refactoring will make it possible for ViEEncoder to signal the full picture of target bitrates to the RTCP module. BUG=webrtc:6301 R=stefan@webrtc.org Review URL: https://codereview.webrtc.org/2510583002 . Cr-Commit-Position: refs/heads/master@{#15105}
2016-11-16 16:41:30 +01:00
}
int32_t VideoSender::SetChannelParameters(
uint32_t target_bitrate_bps,
uint8_t loss_rate,
Reland #2 of Issue 2434073003: Extract bitrate allocation ... This is yet another reland of https://codereview.webrtc.org/2434073003/ including two fixes: 1. SimulcastRateAllocator did not handle the screenshare settings properly for numSimulcastStreams = 1. Additional test case was added for that. 2. In VideoSender, when rate allocation is updated after setting a new VideoCodec config, only update the state of the EncoderParameters, but don't actually run SetRateAllocation on the encoder itself. This caused some problems upstreams. Please review only the changes after patch set 1. Original description: Extract bitrate allocation of spatial/temporal layers out of codec impl. This CL makes a number of intervowen changes: * Add BitrateAllocation struct, that contains a codec independent view of how the target bitrate is distributed over spatial and temporal layers. * Adds the BitrateAllocator interface, which takes a bitrate and frame rate and produces a BitrateAllocation. * A default (non layered) implementation is added, and SimulcastRateAllocator is extended to fully handle VP8 allocation. This includes capturing TemporalLayer instances created by the encoder. * ViEEncoder now owns both the bitrate allocator and the temporal layer factories for VP8. This allows allocation to happen fully outside of the encoder implementation. This refactoring will make it possible for ViEEncoder to signal the full picture of target bitrates to the RTCP module. BUG=webrtc:6301 R=stefan@webrtc.org Review URL: https://codereview.webrtc.org/2510583002 . Cr-Commit-Position: refs/heads/master@{#15105}
2016-11-16 16:41:30 +01:00
int64_t rtt,
VideoBitrateAllocator* bitrate_allocator,
VideoBitrateAllocationObserver* bitrate_updated_callback) {
Reland #2 of Issue 2434073003: Extract bitrate allocation ... This is yet another reland of https://codereview.webrtc.org/2434073003/ including two fixes: 1. SimulcastRateAllocator did not handle the screenshare settings properly for numSimulcastStreams = 1. Additional test case was added for that. 2. In VideoSender, when rate allocation is updated after setting a new VideoCodec config, only update the state of the EncoderParameters, but don't actually run SetRateAllocation on the encoder itself. This caused some problems upstreams. Please review only the changes after patch set 1. Original description: Extract bitrate allocation of spatial/temporal layers out of codec impl. This CL makes a number of intervowen changes: * Add BitrateAllocation struct, that contains a codec independent view of how the target bitrate is distributed over spatial and temporal layers. * Adds the BitrateAllocator interface, which takes a bitrate and frame rate and produces a BitrateAllocation. * A default (non layered) implementation is added, and SimulcastRateAllocator is extended to fully handle VP8 allocation. This includes capturing TemporalLayer instances created by the encoder. * ViEEncoder now owns both the bitrate allocator and the temporal layer factories for VP8. This allows allocation to happen fully outside of the encoder implementation. This refactoring will make it possible for ViEEncoder to signal the full picture of target bitrates to the RTCP module. BUG=webrtc:6301 R=stefan@webrtc.org Review URL: https://codereview.webrtc.org/2510583002 . Cr-Commit-Position: refs/heads/master@{#15105}
2016-11-16 16:41:30 +01:00
EncoderParameters encoder_params;
encoder_params.loss_rate = loss_rate;
Reland #2 of Issue 2434073003: Extract bitrate allocation ... This is yet another reland of https://codereview.webrtc.org/2434073003/ including two fixes: 1. SimulcastRateAllocator did not handle the screenshare settings properly for numSimulcastStreams = 1. Additional test case was added for that. 2. In VideoSender, when rate allocation is updated after setting a new VideoCodec config, only update the state of the EncoderParameters, but don't actually run SetRateAllocation on the encoder itself. This caused some problems upstreams. Please review only the changes after patch set 1. Original description: Extract bitrate allocation of spatial/temporal layers out of codec impl. This CL makes a number of intervowen changes: * Add BitrateAllocation struct, that contains a codec independent view of how the target bitrate is distributed over spatial and temporal layers. * Adds the BitrateAllocator interface, which takes a bitrate and frame rate and produces a BitrateAllocation. * A default (non layered) implementation is added, and SimulcastRateAllocator is extended to fully handle VP8 allocation. This includes capturing TemporalLayer instances created by the encoder. * ViEEncoder now owns both the bitrate allocator and the temporal layer factories for VP8. This allows allocation to happen fully outside of the encoder implementation. This refactoring will make it possible for ViEEncoder to signal the full picture of target bitrates to the RTCP module. BUG=webrtc:6301 R=stefan@webrtc.org Review URL: https://codereview.webrtc.org/2510583002 . Cr-Commit-Position: refs/heads/master@{#15105}
2016-11-16 16:41:30 +01:00
encoder_params.rtt = rtt;
encoder_params = UpdateEncoderParameters(encoder_params, bitrate_allocator,
target_bitrate_bps);
if (bitrate_updated_callback && target_bitrate_bps > 0) {
bitrate_updated_callback->OnBitrateAllocationUpdated(
encoder_params.target_bitrate);
}
Reland 8631 "Speculative revert of 8631 "Remove lock from Bitrat..." > Speculative revert of 8631 "Remove lock from Bitrate() and FrameRate() in Video..." > > We ran into the alignment problem on Mac 10.9 debug again. This is the only CL I see in the range that adds an rtc::CriticalSection, so I'm trying out reverting it before attempting another roll. > > > Remove lock from Bitrate() and FrameRate() in VideoSender. > > These methods are called on the VideoSender's construction thread, which is the same thread as modifies the value of _encoder. It's therefore safe to not require a lock to access _encoder on this thread. > > > > I'm making access to the rate variables from VCMGenericEncoder, thread safe, by using a lock that's not associated with the encoder. There should be little to no contention there. While modifying VCMGenericEncoder, I noticed that a couple of member variables weren't needed, so I removed them. > > > > The reason for this change is that getStats is currently contending with the encoder when Bitrate() is called. On my machine, this means that getStats can take about 25-30ms instead of ~1ms. > > > > Also adding some documentation for other methods and a suggestion for how we could avoid contention between the encoder and the network thread. > > > > BUG=2822 > > R=mflodman@webrtc.org > > > > Review URL: https://webrtc-codereview.appspot.com/43479004 > > TBR=tommi@webrtc.org > > Review URL: https://webrtc-codereview.appspot.com/45529004 TBR=tommi@webrtc.org Review URL: https://webrtc-codereview.appspot.com/46519004 Cr-Commit-Position: refs/heads/master@{#8645} git-svn-id: http://webrtc.googlecode.com/svn/trunk@8645 4adac7df-926f-26a2-2b94-8c16560cd09d
2015-03-07 20:55:56 +00:00
bool encoder_has_internal_source;
{
rtc::CritScope cs(&params_crit_);
encoder_params_ = encoder_params;
encoder_has_internal_source = encoder_has_internal_source_;
}
// For encoders with internal sources, we need to tell the encoder directly,
// instead of waiting for an AddVideoFrame that will never come (internal
// source encoders don't get input frames).
if (encoder_has_internal_source) {
rtc::CritScope cs(&encoder_crit_);
if (_encoder) {
SetEncoderParameters(encoder_params, encoder_has_internal_source);
}
}
return VCM_OK;
}
void VideoSender::SetEncoderParameters(EncoderParameters params,
bool has_internal_source) {
// |target_bitrate == 0 | means that the network is down or the send pacer is
// full. We currently only report this if the encoder has an internal source.
// If the encoder does not have an internal source, higher levels are expected
// to not call AddVideoFrame. We do this since its unclear how current
// encoder implementations behave when given a zero target bitrate.
// TODO(perkj): Make sure all known encoder implementations handle zero
// target bitrate and remove this check.
Reland #2 of Issue 2434073003: Extract bitrate allocation ... This is yet another reland of https://codereview.webrtc.org/2434073003/ including two fixes: 1. SimulcastRateAllocator did not handle the screenshare settings properly for numSimulcastStreams = 1. Additional test case was added for that. 2. In VideoSender, when rate allocation is updated after setting a new VideoCodec config, only update the state of the EncoderParameters, but don't actually run SetRateAllocation on the encoder itself. This caused some problems upstreams. Please review only the changes after patch set 1. Original description: Extract bitrate allocation of spatial/temporal layers out of codec impl. This CL makes a number of intervowen changes: * Add BitrateAllocation struct, that contains a codec independent view of how the target bitrate is distributed over spatial and temporal layers. * Adds the BitrateAllocator interface, which takes a bitrate and frame rate and produces a BitrateAllocation. * A default (non layered) implementation is added, and SimulcastRateAllocator is extended to fully handle VP8 allocation. This includes capturing TemporalLayer instances created by the encoder. * ViEEncoder now owns both the bitrate allocator and the temporal layer factories for VP8. This allows allocation to happen fully outside of the encoder implementation. This refactoring will make it possible for ViEEncoder to signal the full picture of target bitrates to the RTCP module. BUG=webrtc:6301 R=stefan@webrtc.org Review URL: https://codereview.webrtc.org/2510583002 . Cr-Commit-Position: refs/heads/master@{#15105}
2016-11-16 16:41:30 +01:00
if (!has_internal_source && params.target_bitrate.get_sum_bps() == 0)
return;
Reland 8631 "Speculative revert of 8631 "Remove lock from Bitrat..." > Speculative revert of 8631 "Remove lock from Bitrate() and FrameRate() in Video..." > > We ran into the alignment problem on Mac 10.9 debug again. This is the only CL I see in the range that adds an rtc::CriticalSection, so I'm trying out reverting it before attempting another roll. > > > Remove lock from Bitrate() and FrameRate() in VideoSender. > > These methods are called on the VideoSender's construction thread, which is the same thread as modifies the value of _encoder. It's therefore safe to not require a lock to access _encoder on this thread. > > > > I'm making access to the rate variables from VCMGenericEncoder, thread safe, by using a lock that's not associated with the encoder. There should be little to no contention there. While modifying VCMGenericEncoder, I noticed that a couple of member variables weren't needed, so I removed them. > > > > The reason for this change is that getStats is currently contending with the encoder when Bitrate() is called. On my machine, this means that getStats can take about 25-30ms instead of ~1ms. > > > > Also adding some documentation for other methods and a suggestion for how we could avoid contention between the encoder and the network thread. > > > > BUG=2822 > > R=mflodman@webrtc.org > > > > Review URL: https://webrtc-codereview.appspot.com/43479004 > > TBR=tommi@webrtc.org > > Review URL: https://webrtc-codereview.appspot.com/45529004 TBR=tommi@webrtc.org Review URL: https://webrtc-codereview.appspot.com/46519004 Cr-Commit-Position: refs/heads/master@{#8645} git-svn-id: http://webrtc.googlecode.com/svn/trunk@8645 4adac7df-926f-26a2-2b94-8c16560cd09d
2015-03-07 20:55:56 +00:00
if (params.input_frame_rate == 0) {
// No frame rate estimate available, use default.
params.input_frame_rate = current_codec_.maxFramerate;
}
if (_encoder != nullptr)
_encoder->SetEncoderParameters(params);
}
// Add one raw video frame to the encoder, blocking.
int32_t VideoSender::AddVideoFrame(const VideoFrame& videoFrame,
const CodecSpecificInfo* codecSpecificInfo) {
EncoderParameters encoder_params;
std::vector<FrameType> next_frame_types;
bool encoder_has_internal_source = false;
{
rtc::CritScope lock(&params_crit_);
encoder_params = encoder_params_;
next_frame_types = next_frame_types_;
encoder_has_internal_source = encoder_has_internal_source_;
}
rtc::CritScope lock(&encoder_crit_);
if (_encoder == nullptr)
return VCM_UNINITIALIZED;
SetEncoderParameters(encoder_params, encoder_has_internal_source);
if (_mediaOpt.DropFrame()) {
RTC_LOG(LS_VERBOSE) << "Drop Frame "
<< "target bitrate "
<< encoder_params.target_bitrate.get_sum_bps()
<< " loss rate " << encoder_params.loss_rate << " rtt "
<< encoder_params.rtt << " input frame rate "
<< encoder_params.input_frame_rate;
post_encode_callback_->OnDroppedFrame(
EncodedImageCallback::DropReason::kDroppedByMediaOptimizations);
return VCM_OK;
}
// TODO(pbos): Make sure setting send codec is synchronized with video
// processing so frame size always matches.
if (!_codecDataBase.MatchesCurrentResolution(videoFrame.width(),
videoFrame.height())) {
RTC_LOG(LS_ERROR)
<< "Incoming frame doesn't match set resolution. Dropping.";
return VCM_PARAMETER_ERROR;
}
VideoFrame converted_frame = videoFrame;
Revert "Revert "Update video_coding/codecs to new VideoFrameBuffer interface"" This reverts commit 88f94fa36aa61f7904d30251205c544ada2c4301. Chromium code has been updated. Original change's description: > Revert "Update video_coding/codecs to new VideoFrameBuffer interface" > > This reverts commit 20ebf4ede803cd4f628ef9378700f60b72f2eab0. > > Reason for revert: > > Suspect of breaking FYI bots. > See https://build.chromium.org/p/chromium.webrtc.fyi/builders/Win7%20Tester/builds/9036 and others. > > Sample logs: > Backtrace: > [5024:1036:0607/173649.857:FATAL:webrtc_video_frame_adapter.cc(98)] Check failed: false. > Backtrace: > base::debug::StackTrace::StackTrace [0x02D04A37+55] > base::debug::StackTrace::StackTrace [0x02CCBB8A+10] > content::WebRtcVideoFrameAdapter::NativeToI420Buffer [0x0508AD71+305] > webrtc::VideoFrameBuffer::ToI420 [0x0230BF67+39] > webrtc::H264EncoderImpl::Encode [0x057E8D0B+267] > webrtc::VCMGenericEncoder::Encode [0x057E0E34+333] > webrtc::vcm::VideoSender::AddVideoFrame [0x057DED9B+796] > webrtc::ViEEncoder::EncodeVideoFrame [0x057C00F6+884] > webrtc::ViEEncoder::EncodeTask::Run [0x057C12D7+215] > rtc::TaskQueue::PostTask [0x03EE5CFB+194] > base::internal::Invoker<base::internal::BindState<enum extensions::`anonymous namespace'::VerificationResult (__cdecl*)(std::unique_ptr<extensions::NetworkingCastPrivateDelegate::Credentials,std::default_delete<extensions::NetworkingCastPrivateDelegate::C [0x02DDCAA5+31] > base::internal::Invoker<base::internal::BindState<enum extensions::`anonymous namespace'::VerificationResult (__cdecl*)(std::unique_ptr<extensions::NetworkingCastPrivateDelegate::Credentials,std::default_delete<extensions::NetworkingCastPrivateDelegate::C [0x02DDEE86+22] > base::debug::TaskAnnotator::RunTask [0x02D08289+409] > base::MessageLoop::RunTask [0x02C8CEC1+1233] > base::MessageLoop::DoWork [0x02C8C1AD+765] > base::MessagePumpDefault::Run [0x02D0A20B+219] > base::MessageLoop::Run [0x02C8C9DB+107] > base::RunLoop::Run [0x02C89583+147] > base::Thread::Run [0x02CBEFCD+173] > base::Thread::ThreadMain [0x02CBFADE+622] > base::PlatformThread::Sleep [0x02C9E1A2+290] > BaseThreadInitThunk [0x75C3338A+18] > RtlInitializeExceptionChain [0x773A9902+99] > RtlInitializeExceptionChain [0x773A98D5+54] > > Original change's description: > > Update video_coding/codecs to new VideoFrameBuffer interface > > > > This is a follow-up cleanup for CL > > https://codereview.webrtc.org/2847383002/. > > > > Bug: webrtc:7632 > > Change-Id: I47861d779968f2fee94db9c017102a8e87e67fb7 > > Reviewed-on: https://chromium-review.googlesource.com/524163 > > Reviewed-by: Rasmus Brandt <brandtr@webrtc.org> > > Reviewed-by: Niels Moller <nisse@webrtc.org> > > Commit-Queue: Magnus Jedvert <magjed@webrtc.org> > > Cr-Commit-Position: refs/heads/master@{#18477} > > TBR=magjed@webrtc.org,nisse@webrtc.org,brandtr@webrtc.org > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: webrtc:7632 > > Change-Id: I3b73fc7d16ff19ceba196e964dcb36a36510912c > Reviewed-on: https://chromium-review.googlesource.com/527793 > Reviewed-by: Guido Urdaneta <guidou@chromium.org> > Commit-Queue: Guido Urdaneta <guidou@chromium.org> > Cr-Commit-Position: refs/heads/master@{#18489} TBR=tterriberry@mozilla.com,mflodman@webrtc.org,magjed@webrtc.org,stefan@webrtc.org,guidou@chromium.org,nisse@webrtc.org,brandtr@webrtc.org,webrtc-reviews@webrtc.org # Not skipping CQ checks because original CL landed > 1 day ago. No-Presubmit: true Bug: webrtc:7632 Change-Id: I0962a704e8a9939d4364ce9069c863c9951654c9 Reviewed-on: https://chromium-review.googlesource.com/530684 Commit-Queue: Magnus Jedvert <magjed@webrtc.org> Reviewed-by: Magnus Jedvert <magjed@webrtc.org> Cr-Commit-Position: refs/heads/master@{#18527}
2017-06-10 17:03:37 +00:00
const VideoFrameBuffer::Type buffer_type =
converted_frame.video_frame_buffer()->type();
const bool is_buffer_type_supported =
buffer_type == VideoFrameBuffer::Type::kI420 ||
(buffer_type == VideoFrameBuffer::Type::kNative &&
_encoder->SupportsNativeHandle());
if (!is_buffer_type_supported) {
// This module only supports software encoding.
// TODO(pbos): Offload conversion from the encoder thread.
Revert "Revert "Update video_coding/codecs to new VideoFrameBuffer interface"" This reverts commit 88f94fa36aa61f7904d30251205c544ada2c4301. Chromium code has been updated. Original change's description: > Revert "Update video_coding/codecs to new VideoFrameBuffer interface" > > This reverts commit 20ebf4ede803cd4f628ef9378700f60b72f2eab0. > > Reason for revert: > > Suspect of breaking FYI bots. > See https://build.chromium.org/p/chromium.webrtc.fyi/builders/Win7%20Tester/builds/9036 and others. > > Sample logs: > Backtrace: > [5024:1036:0607/173649.857:FATAL:webrtc_video_frame_adapter.cc(98)] Check failed: false. > Backtrace: > base::debug::StackTrace::StackTrace [0x02D04A37+55] > base::debug::StackTrace::StackTrace [0x02CCBB8A+10] > content::WebRtcVideoFrameAdapter::NativeToI420Buffer [0x0508AD71+305] > webrtc::VideoFrameBuffer::ToI420 [0x0230BF67+39] > webrtc::H264EncoderImpl::Encode [0x057E8D0B+267] > webrtc::VCMGenericEncoder::Encode [0x057E0E34+333] > webrtc::vcm::VideoSender::AddVideoFrame [0x057DED9B+796] > webrtc::ViEEncoder::EncodeVideoFrame [0x057C00F6+884] > webrtc::ViEEncoder::EncodeTask::Run [0x057C12D7+215] > rtc::TaskQueue::PostTask [0x03EE5CFB+194] > base::internal::Invoker<base::internal::BindState<enum extensions::`anonymous namespace'::VerificationResult (__cdecl*)(std::unique_ptr<extensions::NetworkingCastPrivateDelegate::Credentials,std::default_delete<extensions::NetworkingCastPrivateDelegate::C [0x02DDCAA5+31] > base::internal::Invoker<base::internal::BindState<enum extensions::`anonymous namespace'::VerificationResult (__cdecl*)(std::unique_ptr<extensions::NetworkingCastPrivateDelegate::Credentials,std::default_delete<extensions::NetworkingCastPrivateDelegate::C [0x02DDEE86+22] > base::debug::TaskAnnotator::RunTask [0x02D08289+409] > base::MessageLoop::RunTask [0x02C8CEC1+1233] > base::MessageLoop::DoWork [0x02C8C1AD+765] > base::MessagePumpDefault::Run [0x02D0A20B+219] > base::MessageLoop::Run [0x02C8C9DB+107] > base::RunLoop::Run [0x02C89583+147] > base::Thread::Run [0x02CBEFCD+173] > base::Thread::ThreadMain [0x02CBFADE+622] > base::PlatformThread::Sleep [0x02C9E1A2+290] > BaseThreadInitThunk [0x75C3338A+18] > RtlInitializeExceptionChain [0x773A9902+99] > RtlInitializeExceptionChain [0x773A98D5+54] > > Original change's description: > > Update video_coding/codecs to new VideoFrameBuffer interface > > > > This is a follow-up cleanup for CL > > https://codereview.webrtc.org/2847383002/. > > > > Bug: webrtc:7632 > > Change-Id: I47861d779968f2fee94db9c017102a8e87e67fb7 > > Reviewed-on: https://chromium-review.googlesource.com/524163 > > Reviewed-by: Rasmus Brandt <brandtr@webrtc.org> > > Reviewed-by: Niels Moller <nisse@webrtc.org> > > Commit-Queue: Magnus Jedvert <magjed@webrtc.org> > > Cr-Commit-Position: refs/heads/master@{#18477} > > TBR=magjed@webrtc.org,nisse@webrtc.org,brandtr@webrtc.org > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: webrtc:7632 > > Change-Id: I3b73fc7d16ff19ceba196e964dcb36a36510912c > Reviewed-on: https://chromium-review.googlesource.com/527793 > Reviewed-by: Guido Urdaneta <guidou@chromium.org> > Commit-Queue: Guido Urdaneta <guidou@chromium.org> > Cr-Commit-Position: refs/heads/master@{#18489} TBR=tterriberry@mozilla.com,mflodman@webrtc.org,magjed@webrtc.org,stefan@webrtc.org,guidou@chromium.org,nisse@webrtc.org,brandtr@webrtc.org,webrtc-reviews@webrtc.org # Not skipping CQ checks because original CL landed > 1 day ago. No-Presubmit: true Bug: webrtc:7632 Change-Id: I0962a704e8a9939d4364ce9069c863c9951654c9 Reviewed-on: https://chromium-review.googlesource.com/530684 Commit-Queue: Magnus Jedvert <magjed@webrtc.org> Reviewed-by: Magnus Jedvert <magjed@webrtc.org> Cr-Commit-Position: refs/heads/master@{#18527}
2017-06-10 17:03:37 +00:00
rtc::scoped_refptr<I420BufferInterface> converted_buffer(
converted_frame.video_frame_buffer()->ToI420());
Partial reland of Delete unused and almost unused frame-related methods. (patchset #1 id:1 of https://codereview.webrtc.org/2076113002/ ) Reason for revert: Taking out the VideoFrameBuffer changes which broke downstream. Original issue's description: > Revert of Delete unused and almost unused frame-related methods. (patchset #12 id:220001 of https://codereview.webrtc.org/2065733003/ ) > > Reason for revert: > Breaks downstream applications which inherits webrtc::VideoFrameBuffer and tries to override deleted methods data(), stride() and MutableData(). > > Original issue's description: > > Delete unused and almost unused frame-related methods. > > > > webrtc::VideoFrame::set_video_frame_buffer > > webrtc::VideoFrame::ConvertNativeToI420Frame > > > > cricket::WebRtcVideoFrame::InitToBlack > > > > VideoFrameBuffer::data > > VideoFrameBuffer::stride > > VideoFrameBuffer::MutableData > > > > TBR=tkchin@webrtc.org # Refactoring affecting RTCVideoFrame > > BUG=webrtc:5682 > > > > Committed: https://crrev.com/76270de4bc2dac188f10f805e6e2fb86693ef864 > > Cr-Commit-Position: refs/heads/master@{#13183} > > TBR=perkj@webrtc.org,pbos@webrtc.org,marpan@webrtc.org,tkchin@webrtc.org > # Skipping CQ checks because original CL landed less than 1 days ago. > NOPRESUBMIT=true > NOTREECHECKS=true > NOTRY=true > BUG=webrtc:5682 > > Committed: https://crrev.com/72e735d3867a0fd6ab7e4d0761c7ba5f6c068617 > Cr-Commit-Position: refs/heads/master@{#13184} TBR=perkj@webrtc.org,pbos@webrtc.org,marpan@webrtc.org,tkchin@webrtc.org # Skipping CQ checks because original CL landed less than 1 days ago. NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true BUG=webrtc:5682 Review-Url: https://codereview.webrtc.org/2076123002 Cr-Commit-Position: refs/heads/master@{#13189}
2016-06-17 05:03:04 -07:00
if (!converted_buffer) {
RTC_LOG(LS_ERROR) << "Frame conversion failed, dropping frame.";
Partial reland of Delete unused and almost unused frame-related methods. (patchset #1 id:1 of https://codereview.webrtc.org/2076113002/ ) Reason for revert: Taking out the VideoFrameBuffer changes which broke downstream. Original issue's description: > Revert of Delete unused and almost unused frame-related methods. (patchset #12 id:220001 of https://codereview.webrtc.org/2065733003/ ) > > Reason for revert: > Breaks downstream applications which inherits webrtc::VideoFrameBuffer and tries to override deleted methods data(), stride() and MutableData(). > > Original issue's description: > > Delete unused and almost unused frame-related methods. > > > > webrtc::VideoFrame::set_video_frame_buffer > > webrtc::VideoFrame::ConvertNativeToI420Frame > > > > cricket::WebRtcVideoFrame::InitToBlack > > > > VideoFrameBuffer::data > > VideoFrameBuffer::stride > > VideoFrameBuffer::MutableData > > > > TBR=tkchin@webrtc.org # Refactoring affecting RTCVideoFrame > > BUG=webrtc:5682 > > > > Committed: https://crrev.com/76270de4bc2dac188f10f805e6e2fb86693ef864 > > Cr-Commit-Position: refs/heads/master@{#13183} > > TBR=perkj@webrtc.org,pbos@webrtc.org,marpan@webrtc.org,tkchin@webrtc.org > # Skipping CQ checks because original CL landed less than 1 days ago. > NOPRESUBMIT=true > NOTREECHECKS=true > NOTRY=true > BUG=webrtc:5682 > > Committed: https://crrev.com/72e735d3867a0fd6ab7e4d0761c7ba5f6c068617 > Cr-Commit-Position: refs/heads/master@{#13184} TBR=perkj@webrtc.org,pbos@webrtc.org,marpan@webrtc.org,tkchin@webrtc.org # Skipping CQ checks because original CL landed less than 1 days ago. NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true BUG=webrtc:5682 Review-Url: https://codereview.webrtc.org/2076123002 Cr-Commit-Position: refs/heads/master@{#13189}
2016-06-17 05:03:04 -07:00
return VCM_PARAMETER_ERROR;
}
converted_frame = VideoFrame(converted_buffer,
converted_frame.timestamp(),
Partial reland of Delete unused and almost unused frame-related methods. (patchset #1 id:1 of https://codereview.webrtc.org/2076113002/ ) Reason for revert: Taking out the VideoFrameBuffer changes which broke downstream. Original issue's description: > Revert of Delete unused and almost unused frame-related methods. (patchset #12 id:220001 of https://codereview.webrtc.org/2065733003/ ) > > Reason for revert: > Breaks downstream applications which inherits webrtc::VideoFrameBuffer and tries to override deleted methods data(), stride() and MutableData(). > > Original issue's description: > > Delete unused and almost unused frame-related methods. > > > > webrtc::VideoFrame::set_video_frame_buffer > > webrtc::VideoFrame::ConvertNativeToI420Frame > > > > cricket::WebRtcVideoFrame::InitToBlack > > > > VideoFrameBuffer::data > > VideoFrameBuffer::stride > > VideoFrameBuffer::MutableData > > > > TBR=tkchin@webrtc.org # Refactoring affecting RTCVideoFrame > > BUG=webrtc:5682 > > > > Committed: https://crrev.com/76270de4bc2dac188f10f805e6e2fb86693ef864 > > Cr-Commit-Position: refs/heads/master@{#13183} > > TBR=perkj@webrtc.org,pbos@webrtc.org,marpan@webrtc.org,tkchin@webrtc.org > # Skipping CQ checks because original CL landed less than 1 days ago. > NOPRESUBMIT=true > NOTREECHECKS=true > NOTRY=true > BUG=webrtc:5682 > > Committed: https://crrev.com/72e735d3867a0fd6ab7e4d0761c7ba5f6c068617 > Cr-Commit-Position: refs/heads/master@{#13184} TBR=perkj@webrtc.org,pbos@webrtc.org,marpan@webrtc.org,tkchin@webrtc.org # Skipping CQ checks because original CL landed less than 1 days ago. NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true BUG=webrtc:5682 Review-Url: https://codereview.webrtc.org/2076123002 Cr-Commit-Position: refs/heads/master@{#13189}
2016-06-17 05:03:04 -07:00
converted_frame.render_time_ms(),
converted_frame.rotation());
}
int32_t ret =
_encoder->Encode(converted_frame, codecSpecificInfo, next_frame_types);
if (ret < 0) {
RTC_LOG(LS_ERROR) << "Failed to encode frame. Error code: " << ret;
return ret;
}
{
rtc::CritScope lock(&params_crit_);
// Change all keyframe requests to encode delta frames the next time.
for (size_t i = 0; i < next_frame_types_.size(); ++i) {
// Check for equality (same requested as before encoding) to not
// accidentally drop a keyframe request while encoding.
if (next_frame_types[i] == next_frame_types_[i])
next_frame_types_[i] = kVideoFrameDelta;
}
}
return VCM_OK;
}
int32_t VideoSender::IntraFrameRequest(size_t stream_index) {
{
rtc::CritScope lock(&params_crit_);
if (stream_index >= next_frame_types_.size()) {
return -1;
}
next_frame_types_[stream_index] = kVideoFrameKey;
if (!encoder_has_internal_source_)
return VCM_OK;
}
// TODO(pbos): Remove when InternalSource() is gone. Both locks have to be
// held here for internal consistency, since _encoder could be removed while
// not holding encoder_crit_. Checks have to be performed again since
// params_crit_ was dropped to not cause lock-order inversions with
// encoder_crit_.
rtc::CritScope lock(&encoder_crit_);
rtc::CritScope params_lock(&params_crit_);
if (stream_index >= next_frame_types_.size())
return -1;
if (_encoder != nullptr && _encoder->InternalSource()) {
// Try to request the frame if we have an external encoder with
// internal source since AddVideoFrame never will be called.
if (_encoder->RequestFrame(next_frame_types_) == WEBRTC_VIDEO_CODEC_OK) {
// Try to remove just-performed keyframe request, if stream still exists.
next_frame_types_[stream_index] = kVideoFrameDelta;
}
}
return VCM_OK;
}
int32_t VideoSender::EnableFrameDropper(bool enable) {
rtc::CritScope lock(&encoder_crit_);
frame_dropper_enabled_ = enable;
_mediaOpt.EnableFrameDropper(enable);
return VCM_OK;
}
} // namespace vcm
} // namespace webrtc