webrtc_m130/webrtc/modules/video_coding/generic_encoder.cc

163 lines
5.5 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "webrtc/modules/video_coding/generic_encoder.h"
#include <vector>
#include "webrtc/base/checks.h"
#include "webrtc/base/logging.h"
#include "webrtc/base/trace_event.h"
#include "webrtc/engine_configurations.h"
#include "webrtc/modules/video_coding/encoded_frame.h"
#include "webrtc/modules/video_coding/media_optimization.h"
#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
namespace webrtc {
VCMGenericEncoder::VCMGenericEncoder(
VideoEncoder* encoder,
VideoEncoderRateObserver* rate_observer,
VCMEncodedFrameCallback* encoded_frame_callback,
bool internal_source)
: encoder_(encoder),
rate_observer_(rate_observer),
vcm_encoded_frame_callback_(encoded_frame_callback),
internal_source_(internal_source),
encoder_params_({0, 0, 0, 0}),
is_screenshare_(false) {}
VCMGenericEncoder::~VCMGenericEncoder() {}
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
int32_t VCMGenericEncoder::Release() {
TRACE_EVENT0("webrtc", "VCMGenericEncoder::Release");
return encoder_->Release();
}
int32_t VCMGenericEncoder::InitEncode(const VideoCodec* settings,
int32_t number_of_cores,
size_t max_payload_size) {
TRACE_EVENT0("webrtc", "VCMGenericEncoder::InitEncode");
is_screenshare_ = settings->mode == VideoCodecMode::kScreensharing;
if (encoder_->InitEncode(settings, number_of_cores, max_payload_size) != 0) {
LOG(LS_ERROR) << "Failed to initialize the encoder associated with "
"payload name: "
<< settings->plName;
return -1;
}
encoder_->RegisterEncodeCompleteCallback(vcm_encoded_frame_callback_);
return 0;
}
int32_t VCMGenericEncoder::Encode(const VideoFrame& frame,
const CodecSpecificInfo* codec_specific,
const std::vector<FrameType>& frame_types) {
TRACE_EVENT1("webrtc", "VCMGenericEncoder::Encode", "timestamp",
frame.timestamp());
for (FrameType frame_type : frame_types)
RTC_DCHECK(frame_type == kVideoFrameKey || frame_type == kVideoFrameDelta);
int32_t result = encoder_->Encode(frame, codec_specific, &frame_types);
if (is_screenshare_ &&
result == WEBRTC_VIDEO_CODEC_TARGET_BITRATE_OVERSHOOT) {
// Target bitrate exceeded, encoder state has been reset - try again.
return encoder_->Encode(frame, codec_specific, &frame_types);
}
return result;
}
const char* VCMGenericEncoder::ImplementationName() const {
return encoder_->ImplementationName();
}
void VCMGenericEncoder::SetEncoderParameters(const EncoderParameters& params) {
bool channel_parameters_have_changed;
bool rates_have_changed;
{
rtc::CritScope lock(&params_lock_);
channel_parameters_have_changed =
params.loss_rate != encoder_params_.loss_rate ||
params.rtt != encoder_params_.rtt;
rates_have_changed =
params.target_bitrate != encoder_params_.target_bitrate ||
params.input_frame_rate != encoder_params_.input_frame_rate;
encoder_params_ = params;
}
if (channel_parameters_have_changed)
encoder_->SetChannelParameters(params.loss_rate, params.rtt);
if (rates_have_changed) {
uint32_t target_bitrate_kbps = (params.target_bitrate + 500) / 1000;
encoder_->SetRates(target_bitrate_kbps, params.input_frame_rate);
if (rate_observer_) {
rate_observer_->OnSetRates(params.target_bitrate,
params.input_frame_rate);
}
}
}
EncoderParameters VCMGenericEncoder::GetEncoderParameters() const {
rtc::CritScope lock(&params_lock_);
return encoder_params_;
}
int32_t VCMGenericEncoder::SetPeriodicKeyFrames(bool enable) {
return encoder_->SetPeriodicKeyFrames(enable);
}
int32_t VCMGenericEncoder::RequestFrame(
const std::vector<FrameType>& frame_types) {
VideoFrame image;
return encoder_->Encode(image, NULL, &frame_types);
}
bool VCMGenericEncoder::InternalSource() const {
return internal_source_;
}
void VCMGenericEncoder::OnDroppedFrame() {
encoder_->OnDroppedFrame();
}
bool VCMGenericEncoder::SupportsNativeHandle() const {
return encoder_->SupportsNativeHandle();
}
VCMEncodedFrameCallback::VCMEncodedFrameCallback(
EncodedImageCallback* post_encode_callback,
media_optimization::MediaOptimization* media_opt)
: internal_source_(false),
post_encode_callback_(post_encode_callback),
media_opt_(media_opt) {}
VCMEncodedFrameCallback::~VCMEncodedFrameCallback() {}
int32_t VCMEncodedFrameCallback::Encoded(
const EncodedImage& encoded_image,
const CodecSpecificInfo* codec_specific,
const RTPFragmentationHeader* fragmentation_header) {
TRACE_EVENT_INSTANT1("webrtc", "VCMEncodedFrameCallback::Encoded",
"timestamp", encoded_image._timeStamp);
Reland of Deprecate VCMPacketizationCallback::SendData and use EncodedImageCallback instead. (patchset #1 id:1 of https://codereview.webrtc.org/1903193002/ ) Reason for revert: A fix is being prepared downstream so this can now go in. Original issue's description: > Revert of Deprecate VCMPacketizationCallback::SendData and use EncodedImageCallback instead. (patchset #5 id:80001 of https://codereview.webrtc.org/1897233002/ ) > > Reason for revert: > API changes broke downstream. > > Original issue's description: > > Deprecate VCMPacketizationCallback::SendData and use EncodedImageCallback instead. > > EncodedImageCallback is used by all encoder implementations and seems to be what we should try to use in the transport. > > EncodedImageCallback can of course be cleaned up in the future. > > > > This moves creation of RTPVideoHeader from the GenericEncoder to the PayLoadRouter. > > > > BUG=webrtc::5687 > > > > Committed: https://crrev.com/f5d55aaecdc39e9cc66eb6e87614f04afe28f6eb > > Cr-Commit-Position: refs/heads/master@{#12436} > > TBR=stefan@webrtc.org,pbos@webrtc.org,perkj@webrtc.org > # Skipping CQ checks because original CL landed less than 1 days ago. > NOPRESUBMIT=true > NOTREECHECKS=true > NOTRY=true > BUG=webrtc:5687 > > Committed: https://crrev.com/a261e6136655af33f283eda8e60a6dd93dd746a4 > Cr-Commit-Position: refs/heads/master@{#12441} TBR=stefan@webrtc.org,pbos@webrtc.org,perkj@webrtc.org # Skipping CQ checks because original CL landed less than 1 days ago. NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true BUG=webrtc:5687 Review URL: https://codereview.webrtc.org/1905583002 Cr-Commit-Position: refs/heads/master@{#12442}
2016-04-20 05:05:54 -07:00
int ret_val = post_encode_callback_->Encoded(encoded_image, codec_specific,
fragmentation_header);
if (ret_val < 0)
return ret_val;
if (media_opt_) {
media_opt_->UpdateWithEncodedData(encoded_image);
if (internal_source_)
return media_opt_->DropFrame(); // Signal to encoder to drop next frame.
}
return VCM_OK;
}
} // namespace webrtc