Deliver video frames on Android, on the decode thread.

VideoCoding
* Adding a method for polling for frames on Android only until the capture implementation takes care of this (longer term plan).

CodecDatabase
* Add an accessor for the current decoder
* Use std::unique_ptr<> for ownership.
* Remove "Release()" and "ReleaseDecoder()". Instead just delete.
* Remove |friend| relationship between CodecDatabase and VCMGenericDecoder.

VCMDecodedFrameCallback
* DCHECKs for thread correctness.
* Remove |lock_| now that a threading model has been established and verified.

VCMGenericDecoder
* All methods now have thread checks.
* Variable access associated with thread checkers.

VideoReceiver
* Added two notification methods, DecoderThreadStarting() and DecoderThreadStopped()
  * Allows us to establish a period when the decoder thread is not running and it is safe to modify variables such as callbacks, that are only read when the decoder thread is running.
  * Allows us to DCHECK thread guarantees.
  * Allows synchronizing callbacks from the module process thread and have them only active while the decoder thread is running.
  * The above, allows us to establish two modes for the thread, single-threaded-mutable and multi-threaded-const.
  * Using that knowledge, we can remove |receive_crit_| as well as locking for a number of member variables.

MediaCodecVideoDecoder
* Removed frame polling code from this class, since this is now done from the root thread function in VideoReceiveStream.

VideoReceiveStream
* On Android: Polls for decoded frames every 10ms (same interval as previously in MediaCodecVideoDecoder)
* [Un]Registers the |video_receiver_| with the module thread only around the time the decoder thread is started/stopped.
* Notifies the receiver of start/stop events of the decoder thread.
* Changed the decoder thread to use the new PlatformThread callback type.

BUG=webrtc:7361, 695438

Review-Url: https://codereview.webrtc.org/2764573002
Cr-Commit-Position: refs/heads/master@{#17527}
This commit is contained in:
tommi
2017-04-04 03:53:02 -07:00
committed by Commit bot
parent aa7d935cd5
commit e3aa88bbd5
12 changed files with 511 additions and 321 deletions

View File

@ -71,6 +71,31 @@ VideoCodecH264 VideoEncoder::GetDefaultH264Settings() {
return h264_settings;
}
// Create an internal Decoder given a codec type
static std::unique_ptr<VCMGenericDecoder> CreateDecoder(VideoCodecType type) {
switch (type) {
case kVideoCodecVP8:
return std::unique_ptr<VCMGenericDecoder>(
new VCMGenericDecoder(VP8Decoder::Create()));
case kVideoCodecVP9:
return std::unique_ptr<VCMGenericDecoder>(
new VCMGenericDecoder(VP9Decoder::Create()));
case kVideoCodecI420:
return std::unique_ptr<VCMGenericDecoder>(
new VCMGenericDecoder(new I420Decoder()));
case kVideoCodecH264:
if (H264Decoder::IsSupported()) {
return std::unique_ptr<VCMGenericDecoder>(
new VCMGenericDecoder(H264Decoder::Create()));
}
break;
default:
break;
}
LOG(LS_WARNING) << "No internal decoder of this type exists.";
return std::unique_ptr<VCMGenericDecoder>();
}
VCMDecoderMapItem::VCMDecoderMapItem(VideoCodec* settings,
int number_of_cores,
bool require_key_frame)
@ -98,13 +123,12 @@ VCMCodecDataBase::VCMCodecDataBase(
external_encoder_(nullptr),
internal_source_(false),
encoded_frame_callback_(encoded_frame_callback),
ptr_decoder_(nullptr),
dec_map_(),
dec_external_map_() {}
VCMCodecDataBase::~VCMCodecDataBase() {
DeleteEncoder();
ReleaseDecoder(ptr_decoder_);
ptr_decoder_.reset();
for (auto& kv : dec_map_)
delete kv.second;
for (auto& kv : dec_external_map_)
@ -391,11 +415,10 @@ bool VCMCodecDataBase::DeregisterExternalDecoder(uint8_t payload_type) {
// We can't use payload_type to check if the decoder is currently in use,
// because payload type may be out of date (e.g. before we decode the first
// frame after RegisterReceiveCodec)
if (ptr_decoder_ != nullptr &&
ptr_decoder_->_decoder == (*it).second->external_decoder_instance) {
if (ptr_decoder_ &&
ptr_decoder_->IsSameDecoder((*it).second->external_decoder_instance)) {
// Release it if it was registered and in use.
ReleaseDecoder(ptr_decoder_);
ptr_decoder_ = nullptr;
ptr_decoder_.reset();
}
DeregisterReceiveCodec(payload_type);
delete it->second;
@ -455,12 +478,11 @@ VCMGenericDecoder* VCMCodecDataBase::GetDecoder(
RTC_DCHECK(decoded_frame_callback->UserReceiveCallback());
uint8_t payload_type = frame.PayloadType();
if (payload_type == receive_codec_.plType || payload_type == 0) {
return ptr_decoder_;
return ptr_decoder_.get();
}
// Check for exisitng decoder, if exists - delete.
if (ptr_decoder_) {
ReleaseDecoder(ptr_decoder_);
ptr_decoder_ = nullptr;
ptr_decoder_.reset();
memset(&receive_codec_, 0, sizeof(VideoCodec));
}
ptr_decoder_ = CreateAndInitDecoder(frame, &receive_codec_);
@ -471,36 +493,26 @@ VCMGenericDecoder* VCMCodecDataBase::GetDecoder(
callback->OnIncomingPayloadType(receive_codec_.plType);
if (ptr_decoder_->RegisterDecodeCompleteCallback(decoded_frame_callback) <
0) {
ReleaseDecoder(ptr_decoder_);
ptr_decoder_ = nullptr;
ptr_decoder_.reset();
memset(&receive_codec_, 0, sizeof(VideoCodec));
return nullptr;
}
return ptr_decoder_;
return ptr_decoder_.get();
}
void VCMCodecDataBase::ReleaseDecoder(VCMGenericDecoder* decoder) const {
if (decoder) {
RTC_DCHECK(decoder->_decoder);
decoder->Release();
if (!decoder->External()) {
delete decoder->_decoder;
}
delete decoder;
}
VCMGenericDecoder* VCMCodecDataBase::GetCurrentDecoder() {
return ptr_decoder_.get();
}
bool VCMCodecDataBase::PrefersLateDecoding() const {
if (!ptr_decoder_)
return true;
return ptr_decoder_->PrefersLateDecoding();
return ptr_decoder_ ? ptr_decoder_->PrefersLateDecoding() : true;
}
bool VCMCodecDataBase::MatchesCurrentResolution(int width, int height) const {
return send_codec_.width == width && send_codec_.height == height;
}
VCMGenericDecoder* VCMCodecDataBase::CreateAndInitDecoder(
std::unique_ptr<VCMGenericDecoder> VCMCodecDataBase::CreateAndInitDecoder(
const VCMEncodedFrame& frame,
VideoCodec* new_codec) const {
uint8_t payload_type = frame.PayloadType();
@ -513,13 +525,13 @@ VCMGenericDecoder* VCMCodecDataBase::CreateAndInitDecoder(
<< static_cast<int>(payload_type);
return nullptr;
}
VCMGenericDecoder* ptr_decoder = nullptr;
std::unique_ptr<VCMGenericDecoder> ptr_decoder;
const VCMExtDecoderMapItem* external_dec_item =
FindExternalDecoderItem(payload_type);
if (external_dec_item) {
// External codec.
ptr_decoder = new VCMGenericDecoder(
external_dec_item->external_decoder_instance, true);
ptr_decoder.reset(new VCMGenericDecoder(
external_dec_item->external_decoder_instance, true));
} else {
// Create decoder.
ptr_decoder = CreateDecoder(decoder_item->settings->codecType);
@ -538,7 +550,6 @@ VCMGenericDecoder* VCMCodecDataBase::CreateAndInitDecoder(
}
if (ptr_decoder->InitDecode(decoder_item->settings.get(),
decoder_item->number_of_cores) < 0) {
ReleaseDecoder(ptr_decoder);
return nullptr;
}
memcpy(new_codec, decoder_item->settings.get(), sizeof(VideoCodec));
@ -552,26 +563,6 @@ void VCMCodecDataBase::DeleteEncoder() {
ptr_encoder_.reset();
}
VCMGenericDecoder* VCMCodecDataBase::CreateDecoder(VideoCodecType type) const {
switch (type) {
case kVideoCodecVP8:
return new VCMGenericDecoder(VP8Decoder::Create());
case kVideoCodecVP9:
return new VCMGenericDecoder(VP9Decoder::Create());
case kVideoCodecI420:
return new VCMGenericDecoder(new I420Decoder());
case kVideoCodecH264:
if (H264Decoder::IsSupported()) {
return new VCMGenericDecoder(H264Decoder::Create());
}
break;
default:
break;
}
LOG(LS_WARNING) << "No internal decoder of this type exists.";
return nullptr;
}
const VCMDecoderMapItem* VCMCodecDataBase::FindDecoderItem(
uint8_t payload_type) const {
DecoderMap::const_iterator it = dec_map_.find(payload_type);