1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/filters/decoder_stream_traits.h"
#include <limits>
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "media/base/audio_buffer.h"
#include "media/base/audio_decoder.h"
#include "media/base/audio_decoder_config.h"
#include "media/base/video_decoder.h"
#include "media/base/video_frame.h"
namespace media {
// Audio decoder stream traits implementation.
// static
std::string DecoderStreamTraits<DemuxerStream::AUDIO>::ToString() {
return "audio";
}
// static
bool DecoderStreamTraits<DemuxerStream::AUDIO>::NeedsBitstreamConversion(
DecoderType* decoder) {
return decoder->NeedsBitstreamConversion();
}
// static
scoped_refptr<DecoderStreamTraits<DemuxerStream::AUDIO>::OutputType>
DecoderStreamTraits<DemuxerStream::AUDIO>::CreateEOSOutput() {
return OutputType::CreateEOSBuffer();
}
DecoderStreamTraits<DemuxerStream::AUDIO>::DecoderStreamTraits(
const scoped_refptr<MediaLog>& media_log)
: media_log_(media_log) {}
void DecoderStreamTraits<DemuxerStream::AUDIO>::ReportStatistics(
const StatisticsCB& statistics_cb,
int bytes_decoded) {
PipelineStatistics statistics;
statistics.audio_bytes_decoded = bytes_decoded;
statistics_cb.Run(statistics);
}
void DecoderStreamTraits<DemuxerStream::AUDIO>::InitializeDecoder(
DecoderType* decoder,
DemuxerStream* stream,
CdmContext* cdm_context,
const InitCB& init_cb,
const OutputCB& output_cb) {
DCHECK(stream->audio_decoder_config().IsValidConfig());
decoder->Initialize(stream->audio_decoder_config(), cdm_context, init_cb,
output_cb);
}
void DecoderStreamTraits<DemuxerStream::AUDIO>::OnStreamReset(
DemuxerStream* stream) {
DCHECK(stream);
// Stream is likely being seeked to a new timestamp, so make new validator to
// build new timestamp expectations.
audio_ts_validator_.reset(
new AudioTimestampValidator(stream->audio_decoder_config(), media_log_));
}
void DecoderStreamTraits<DemuxerStream::AUDIO>::OnDecode(
const scoped_refptr<DecoderBuffer>& buffer) {
audio_ts_validator_->CheckForTimestampGap(buffer);
}
void DecoderStreamTraits<DemuxerStream::AUDIO>::OnDecodeDone(
const scoped_refptr<OutputType>& buffer) {
audio_ts_validator_->RecordOutputDuration(buffer);
}
// Video decoder stream traits implementation.
// static
std::string DecoderStreamTraits<DemuxerStream::VIDEO>::ToString() {
return "video";
}
// static
bool DecoderStreamTraits<DemuxerStream::VIDEO>::NeedsBitstreamConversion(
DecoderType* decoder) {
return decoder->NeedsBitstreamConversion();
}
// static
scoped_refptr<DecoderStreamTraits<DemuxerStream::VIDEO>::OutputType>
DecoderStreamTraits<DemuxerStream::VIDEO>::CreateEOSOutput() {
return OutputType::CreateEOSFrame();
}
DecoderStreamTraits<DemuxerStream::VIDEO>::DecoderStreamTraits(
const scoped_refptr<MediaLog>& media_log)
// Randomly selected number of samples to keep.
: keyframe_distance_average_(16) {}
void DecoderStreamTraits<DemuxerStream::VIDEO>::ReportStatistics(
const StatisticsCB& statistics_cb,
int bytes_decoded) {
PipelineStatistics statistics;
statistics.video_bytes_decoded = bytes_decoded;
if (keyframe_distance_average_.count()) {
statistics.video_keyframe_distance_average =
keyframe_distance_average_.Average();
} else {
// Before we have enough keyframes to calculate the average distance, we
// will assume the average keyframe distance is infinitely large.
statistics.video_keyframe_distance_average = base::TimeDelta::Max();
}
statistics_cb.Run(statistics);
}
void DecoderStreamTraits<DemuxerStream::VIDEO>::InitializeDecoder(
DecoderType* decoder,
DemuxerStream* stream,
CdmContext* cdm_context,
const InitCB& init_cb,
const OutputCB& output_cb) {
DCHECK(stream->video_decoder_config().IsValidConfig());
decoder->Initialize(stream->video_decoder_config(),
stream->liveness() == DemuxerStream::LIVENESS_LIVE,
cdm_context, init_cb, output_cb);
}
void DecoderStreamTraits<DemuxerStream::VIDEO>::OnStreamReset(
DemuxerStream* stream) {
DCHECK(stream);
last_keyframe_timestamp_ = base::TimeDelta();
}
void DecoderStreamTraits<DemuxerStream::VIDEO>::OnDecode(
const scoped_refptr<DecoderBuffer>& buffer) {
if (!buffer)
return;
if (buffer->end_of_stream()) {
last_keyframe_timestamp_ = base::TimeDelta();
return;
}
if (!buffer->is_key_frame())
return;
base::TimeDelta current_frame_timestamp = buffer->timestamp();
if (last_keyframe_timestamp_.is_zero()) {
last_keyframe_timestamp_ = current_frame_timestamp;
return;
}
base::TimeDelta frame_distance =
current_frame_timestamp - last_keyframe_timestamp_;
UMA_HISTOGRAM_MEDIUM_TIMES("Media.Video.KeyFrameDistance", frame_distance);
last_keyframe_timestamp_ = current_frame_timestamp;
keyframe_distance_average_.AddSample(frame_distance);
}
} // namespace media
|