#include "media/formats/mp2t/es_adapter_video.h"
#include <stddef.h>
#include "base/logging.h"
#include "media/base/timestamp_constants.h"
#include "media/base/video_decoder_config.h"
#include "media/formats/mp2t/mp2t_common.h"
namespace media {
namespace mp2t {
static const int kDefaultFrameDurationMs = 40;
static const size_t kHistorySize = 5;
EsAdapterVideo::EsAdapterVideo(NewVideoConfigCB new_video_config_cb,
EmitBufferCB emit_buffer_cb)
: new_video_config_cb_(std::move(new_video_config_cb)),
emit_buffer_cb_(std::move(emit_buffer_cb)),
has_valid_config_(false),
has_valid_frame_(false),
last_frame_duration_(base::Milliseconds(kDefaultFrameDurationMs)),
buffer_index_(0),
has_valid_initial_timestamp_(false),
discarded_frame_count_(0) {}
EsAdapterVideo::~EsAdapterVideo() {
}
void EsAdapterVideo::Flush() {
ProcessPendingBuffers(true);
}
void EsAdapterVideo::Reset() {
has_valid_config_ = false;
has_valid_frame_ = false;
last_frame_duration_ = base::Milliseconds(kDefaultFrameDurationMs);
config_list_.clear();
buffer_index_ = 0;
buffer_list_.clear();
emitted_pts_.clear();
has_valid_initial_timestamp_ = false;
min_pts_ = base::TimeDelta();
min_dts_ = DecodeTimestamp();
discarded_frame_count_ = 0;
}
void EsAdapterVideo::OnConfigChanged(
const VideoDecoderConfig& video_decoder_config) {
config_list_.push_back(
ConfigEntry(buffer_index_ + buffer_list_.size(), video_decoder_config));
has_valid_config_ = true;
ProcessPendingBuffers(false);
}
bool EsAdapterVideo::OnNewBuffer(
scoped_refptr<StreamParserBuffer> stream_parser_buffer) {
if (stream_parser_buffer->timestamp() == kNoTimestamp) {
if (has_valid_frame_) {
DVLOG(1) << "Missing timestamp in the middle of the stream";
return false;
}
if (!has_valid_initial_timestamp_) {
DVLOG(1)
<< "Stream not compliant: ignoring leading frame with no timestamp";
return true;
}
DCHECK(has_valid_initial_timestamp_);
DCHECK_GE(discarded_frame_count_, 1);
discarded_frame_count_++;
return true;
}
if (!has_valid_initial_timestamp_) {
min_pts_ = stream_parser_buffer->timestamp();
min_dts_ = stream_parser_buffer->GetDecodeTimestamp();
has_valid_initial_timestamp_ = true;
}
if (stream_parser_buffer->timestamp() < min_pts_)
min_pts_ = stream_parser_buffer->timestamp();
if (!has_valid_config_ ||
(!has_valid_frame_ && !stream_parser_buffer->is_key_frame())) {
discarded_frame_count_++;
return true;
}
has_valid_frame_ = true;
if (discarded_frame_count_ > 0)
ReplaceDiscardedFrames(*stream_parser_buffer);
buffer_list_.emplace_back(std::move(stream_parser_buffer));
ProcessPendingBuffers(false);
return true;
}
void EsAdapterVideo::ProcessPendingBuffers(bool flush) {
DCHECK(has_valid_config_);
while (!buffer_list_.empty() &&
(flush || buffer_list_.size() > kHistorySize)) {
if (!config_list_.empty() && config_list_.front().first == buffer_index_) {
new_video_config_cb_.Run(config_list_.front().second);
config_list_.pop_front();
}
scoped_refptr<StreamParserBuffer> buffer = std::move(buffer_list_.front());
buffer_list_.pop_front();
buffer_index_++;
if (buffer->duration() == kNoTimestamp) {
base::TimeDelta next_frame_pts = GetNextFramePts(buffer->timestamp());
if (next_frame_pts == kNoTimestamp) {
DVLOG(LOG_LEVEL_ES) << "Using last frame duration: "
<< last_frame_duration_.InMilliseconds();
buffer->set_duration(last_frame_duration_);
} else {
base::TimeDelta duration = next_frame_pts - buffer->timestamp();
DVLOG(LOG_LEVEL_ES) << "Frame duration: " << duration.InMilliseconds();
buffer->set_duration(duration);
}
}
emitted_pts_.push_back(buffer->timestamp());
if (emitted_pts_.size() > kHistorySize)
emitted_pts_.pop_front();
last_frame_duration_ = buffer->duration();
emit_buffer_cb_.Run(std::move(buffer));
}
}
base::TimeDelta EsAdapterVideo::GetNextFramePts(base::TimeDelta current_pts) {
base::TimeDelta next_pts = kNoTimestamp;
for (BufferQueue::const_iterator it = buffer_list_.begin();
it != buffer_list_.end(); ++it) {
if ((*it)->timestamp() < current_pts)
continue;
if (next_pts == kNoTimestamp || next_pts > (*it)->timestamp())
next_pts = (*it)->timestamp();
}
for (std::list<base::TimeDelta>::const_iterator it = emitted_pts_.begin();
it != emitted_pts_.end(); ++it) {
if (*it < current_pts)
continue;
if (next_pts == kNoTimestamp || next_pts > *it)
next_pts = *it;
}
return next_pts;
}
void EsAdapterVideo::ReplaceDiscardedFrames(
const StreamParserBuffer& stream_parser_buffer) {
DCHECK_GT(discarded_frame_count_, 0);
DCHECK(stream_parser_buffer.is_key_frame());
base::TimeDelta pts = min_pts_;
base::TimeDelta pts_delta =
(stream_parser_buffer.timestamp() - pts) / discarded_frame_count_;
DecodeTimestamp dts = min_dts_;
base::TimeDelta dts_delta =
(stream_parser_buffer.GetDecodeTimestamp() - dts) /
discarded_frame_count_;
auto stream_parser_buffer_span = base::span(stream_parser_buffer);
for (int i = 0; i < discarded_frame_count_; i++) {
scoped_refptr<StreamParserBuffer> frame = StreamParserBuffer::CopyFrom(
stream_parser_buffer_span, stream_parser_buffer.is_key_frame(),
stream_parser_buffer.type(), stream_parser_buffer.track_id());
frame->SetDecodeTimestamp(dts);
frame->set_timestamp(pts);
frame->set_duration(pts_delta);
buffer_list_.emplace_back(std::move(frame));
pts += pts_delta;
dts += dts_delta;
}
discarded_frame_count_ = 0;
}
}
}