2015-10-08 21:48:07 +00:00
|
|
|
// 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.
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
#include "packager/media/formats/webm/webm_tracks_parser.h"
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/base/strings/string_number_conversions.h"
|
|
|
|
#include "packager/base/strings/string_util.h"
|
|
|
|
#include "packager/media/base/timestamp_constants.h"
|
|
|
|
#include "packager/media/formats/webm/webm_constants.h"
|
|
|
|
#include "packager/media/formats/webm/webm_content_encodings.h"
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
namespace edash_packager {
|
2015-10-08 21:48:07 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
static TextKind CodecIdToTextKind(const std::string& codec_id) {
|
|
|
|
if (codec_id == kWebMCodecSubtitles)
|
|
|
|
return kTextSubtitles;
|
|
|
|
|
|
|
|
if (codec_id == kWebMCodecCaptions)
|
|
|
|
return kTextCaptions;
|
|
|
|
|
|
|
|
if (codec_id == kWebMCodecDescriptions)
|
|
|
|
return kTextDescriptions;
|
|
|
|
|
|
|
|
if (codec_id == kWebMCodecMetadata)
|
|
|
|
return kTextMetadata;
|
|
|
|
|
|
|
|
return kTextNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
static base::TimeDelta PrecisionCappedDefaultDuration(
|
2015-10-14 22:46:23 +00:00
|
|
|
const double timecode_scale_in_us,
|
|
|
|
const int64_t duration_in_ns) {
|
2015-10-08 21:48:07 +00:00
|
|
|
if (duration_in_ns <= 0)
|
|
|
|
return kNoTimestamp();
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
int64_t mult = duration_in_ns / 1000;
|
2015-10-08 21:48:07 +00:00
|
|
|
mult /= timecode_scale_in_us;
|
|
|
|
if (mult == 0)
|
|
|
|
return kNoTimestamp();
|
|
|
|
|
|
|
|
mult = static_cast<double>(mult) * timecode_scale_in_us;
|
|
|
|
return base::TimeDelta::FromMicroseconds(mult);
|
|
|
|
}
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
WebMTracksParser::WebMTracksParser(bool ignore_text_tracks)
|
2015-10-08 21:48:07 +00:00
|
|
|
: track_type_(-1),
|
|
|
|
track_num_(-1),
|
|
|
|
seek_preroll_(-1),
|
|
|
|
codec_delay_(-1),
|
|
|
|
default_duration_(-1),
|
|
|
|
audio_track_num_(-1),
|
|
|
|
audio_default_duration_(-1),
|
|
|
|
video_track_num_(-1),
|
|
|
|
video_default_duration_(-1),
|
|
|
|
ignore_text_tracks_(ignore_text_tracks),
|
2015-10-14 22:46:23 +00:00
|
|
|
audio_client_(),
|
|
|
|
video_client_() {
|
2015-10-08 21:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WebMTracksParser::~WebMTracksParser() {}
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
int WebMTracksParser::Parse(const uint8_t* buf, int size) {
|
2015-10-08 21:48:07 +00:00
|
|
|
track_type_ =-1;
|
|
|
|
track_num_ = -1;
|
|
|
|
default_duration_ = -1;
|
|
|
|
track_name_.clear();
|
|
|
|
track_language_.clear();
|
|
|
|
audio_track_num_ = -1;
|
|
|
|
audio_default_duration_ = -1;
|
|
|
|
audio_decoder_config_ = AudioDecoderConfig();
|
|
|
|
video_track_num_ = -1;
|
|
|
|
video_default_duration_ = -1;
|
|
|
|
video_decoder_config_ = VideoDecoderConfig();
|
|
|
|
text_tracks_.clear();
|
|
|
|
ignored_tracks_.clear();
|
|
|
|
|
|
|
|
WebMListParser parser(kWebMIdTracks, this);
|
|
|
|
int result = parser.Parse(buf, size);
|
|
|
|
|
|
|
|
if (result <= 0)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
// For now we do all or nothing parsing.
|
|
|
|
return parser.IsParsingComplete() ? result : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
base::TimeDelta WebMTracksParser::GetAudioDefaultDuration(
|
|
|
|
const double timecode_scale_in_us) const {
|
|
|
|
return PrecisionCappedDefaultDuration(timecode_scale_in_us,
|
|
|
|
audio_default_duration_);
|
|
|
|
}
|
|
|
|
|
|
|
|
base::TimeDelta WebMTracksParser::GetVideoDefaultDuration(
|
|
|
|
const double timecode_scale_in_us) const {
|
|
|
|
return PrecisionCappedDefaultDuration(timecode_scale_in_us,
|
|
|
|
video_default_duration_);
|
|
|
|
}
|
|
|
|
|
|
|
|
WebMParserClient* WebMTracksParser::OnListStart(int id) {
|
|
|
|
if (id == kWebMIdContentEncodings) {
|
|
|
|
DCHECK(!track_content_encodings_client_.get());
|
2015-10-14 22:46:23 +00:00
|
|
|
track_content_encodings_client_.reset(new WebMContentEncodingsClient());
|
2015-10-08 21:48:07 +00:00
|
|
|
return track_content_encodings_client_->OnListStart(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == kWebMIdTrackEntry) {
|
|
|
|
track_type_ = -1;
|
|
|
|
track_num_ = -1;
|
|
|
|
default_duration_ = -1;
|
|
|
|
track_name_.clear();
|
|
|
|
track_language_.clear();
|
|
|
|
codec_id_ = "";
|
|
|
|
codec_private_.clear();
|
|
|
|
audio_client_.Reset();
|
|
|
|
video_client_.Reset();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == kWebMIdAudio)
|
|
|
|
return &audio_client_;
|
|
|
|
|
|
|
|
if (id == kWebMIdVideo)
|
|
|
|
return &video_client_;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebMTracksParser::OnListEnd(int id) {
|
|
|
|
if (id == kWebMIdContentEncodings) {
|
|
|
|
DCHECK(track_content_encodings_client_.get());
|
|
|
|
return track_content_encodings_client_->OnListEnd(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == kWebMIdTrackEntry) {
|
|
|
|
if (track_type_ == -1 || track_num_ == -1) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Missing TrackEntry data for "
|
|
|
|
<< " TrackType " << track_type_ << " TrackNum " << track_num_;
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (track_type_ != kWebMTrackTypeAudio &&
|
|
|
|
track_type_ != kWebMTrackTypeVideo &&
|
|
|
|
track_type_ != kWebMTrackTypeSubtitlesOrCaptions &&
|
|
|
|
track_type_ != kWebMTrackTypeDescriptionsOrMetadata) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Unexpected TrackType " << track_type_;
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextKind text_track_kind = kTextNone;
|
|
|
|
if (track_type_ == kWebMTrackTypeSubtitlesOrCaptions) {
|
|
|
|
text_track_kind = CodecIdToTextKind(codec_id_);
|
|
|
|
if (text_track_kind == kTextNone) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Missing TrackEntry CodecID"
|
|
|
|
<< " TrackNum " << track_num_;
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (text_track_kind != kTextSubtitles &&
|
|
|
|
text_track_kind != kTextCaptions) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Wrong TrackEntry CodecID"
|
|
|
|
<< " TrackNum " << track_num_;
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (track_type_ == kWebMTrackTypeDescriptionsOrMetadata) {
|
|
|
|
text_track_kind = CodecIdToTextKind(codec_id_);
|
|
|
|
if (text_track_kind == kTextNone) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Missing TrackEntry CodecID"
|
|
|
|
<< " TrackNum " << track_num_;
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (text_track_kind != kTextDescriptions &&
|
|
|
|
text_track_kind != kTextMetadata) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Wrong TrackEntry CodecID"
|
|
|
|
<< " TrackNum " << track_num_;
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string encryption_key_id;
|
|
|
|
if (track_content_encodings_client_) {
|
|
|
|
DCHECK(!track_content_encodings_client_->content_encodings().empty());
|
|
|
|
// If we have multiple ContentEncoding in one track. Always choose the
|
|
|
|
// key id in the first ContentEncoding as the key id of the track.
|
|
|
|
encryption_key_id = track_content_encodings_client_->
|
|
|
|
content_encodings()[0]->encryption_key_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (track_type_ == kWebMTrackTypeAudio) {
|
|
|
|
if (audio_track_num_ == -1) {
|
|
|
|
audio_track_num_ = track_num_;
|
|
|
|
audio_encryption_key_id_ = encryption_key_id;
|
|
|
|
|
|
|
|
if (default_duration_ == 0) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Illegal 0ns audio TrackEntry "
|
|
|
|
"DefaultDuration";
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
audio_default_duration_ = default_duration_;
|
|
|
|
|
|
|
|
DCHECK(!audio_decoder_config_.IsValidConfig());
|
|
|
|
if (!audio_client_.InitializeConfig(
|
|
|
|
codec_id_, codec_private_, seek_preroll_, codec_delay_,
|
|
|
|
!audio_encryption_key_id_.empty(), &audio_decoder_config_)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2015-10-14 22:46:23 +00:00
|
|
|
DLOG(INFO) << "Ignoring audio track " << track_num_;
|
2015-10-08 21:48:07 +00:00
|
|
|
ignored_tracks_.insert(track_num_);
|
|
|
|
}
|
|
|
|
} else if (track_type_ == kWebMTrackTypeVideo) {
|
|
|
|
if (video_track_num_ == -1) {
|
|
|
|
video_track_num_ = track_num_;
|
|
|
|
video_encryption_key_id_ = encryption_key_id;
|
|
|
|
|
|
|
|
if (default_duration_ == 0) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Illegal 0ns video TrackEntry "
|
|
|
|
"DefaultDuration";
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
video_default_duration_ = default_duration_;
|
|
|
|
|
|
|
|
DCHECK(!video_decoder_config_.IsValidConfig());
|
|
|
|
if (!video_client_.InitializeConfig(
|
|
|
|
codec_id_, codec_private_, !video_encryption_key_id_.empty(),
|
|
|
|
&video_decoder_config_)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2015-10-14 22:46:23 +00:00
|
|
|
DLOG(INFO) << "Ignoring video track " << track_num_;
|
2015-10-08 21:48:07 +00:00
|
|
|
ignored_tracks_.insert(track_num_);
|
|
|
|
}
|
|
|
|
} else if (track_type_ == kWebMTrackTypeSubtitlesOrCaptions ||
|
|
|
|
track_type_ == kWebMTrackTypeDescriptionsOrMetadata) {
|
|
|
|
if (ignore_text_tracks_) {
|
2015-10-14 22:46:23 +00:00
|
|
|
DLOG(INFO) << "Ignoring text track " << track_num_;
|
2015-10-08 21:48:07 +00:00
|
|
|
ignored_tracks_.insert(track_num_);
|
|
|
|
} else {
|
|
|
|
std::string track_num = base::Int64ToString(track_num_);
|
|
|
|
text_tracks_[track_num_] = TextTrackConfig(
|
|
|
|
text_track_kind, track_name_, track_language_, track_num);
|
|
|
|
}
|
|
|
|
} else {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Unexpected TrackType " << track_type_;
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
track_type_ = -1;
|
|
|
|
track_num_ = -1;
|
|
|
|
default_duration_ = -1;
|
|
|
|
track_name_.clear();
|
|
|
|
track_language_.clear();
|
|
|
|
codec_id_ = "";
|
|
|
|
codec_private_.clear();
|
|
|
|
track_content_encodings_client_.reset();
|
|
|
|
|
|
|
|
audio_client_.Reset();
|
|
|
|
video_client_.Reset();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
bool WebMTracksParser::OnUInt(int id, int64_t val) {
|
|
|
|
int64_t* dst = NULL;
|
2015-10-08 21:48:07 +00:00
|
|
|
|
|
|
|
switch (id) {
|
|
|
|
case kWebMIdTrackNumber:
|
|
|
|
dst = &track_num_;
|
|
|
|
break;
|
|
|
|
case kWebMIdTrackType:
|
|
|
|
dst = &track_type_;
|
|
|
|
break;
|
|
|
|
case kWebMIdSeekPreRoll:
|
|
|
|
dst = &seek_preroll_;
|
|
|
|
break;
|
|
|
|
case kWebMIdCodecDelay:
|
|
|
|
dst = &codec_delay_;
|
|
|
|
break;
|
|
|
|
case kWebMIdDefaultDuration:
|
|
|
|
dst = &default_duration_;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*dst != -1) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Multiple values for id " << std::hex << id << " specified";
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dst = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebMTracksParser::OnFloat(int id, double val) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
bool WebMTracksParser::OnBinary(int id, const uint8_t* data, int size) {
|
2015-10-08 21:48:07 +00:00
|
|
|
if (id == kWebMIdCodecPrivate) {
|
|
|
|
if (!codec_private_.empty()) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Multiple CodecPrivate fields in a track.";
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
codec_private_.assign(data, data + size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebMTracksParser::OnString(int id, const std::string& str) {
|
|
|
|
if (id == kWebMIdCodecID) {
|
|
|
|
if (!codec_id_.empty()) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Multiple CodecID fields in a track";
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
codec_id_ = str;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == kWebMIdName) {
|
|
|
|
track_name_ = str;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == kWebMIdLanguage) {
|
|
|
|
track_language_ = str;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2015-10-14 22:46:23 +00:00
|
|
|
} // namespace edash_packager
|