2023-12-01 17:32:19 +00:00
|
|
|
// Copyright 2015 Google LLC. All rights reserved.
|
2015-10-28 17:23:08 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/media/formats/webm/seek_head.h>
|
2015-10-28 17:23:08 +00:00
|
|
|
|
2016-09-29 00:13:15 +00:00
|
|
|
#include <algorithm>
|
2015-10-28 17:23:08 +00:00
|
|
|
#include <limits>
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <absl/log/check.h>
|
|
|
|
#include <absl/log/log.h>
|
|
|
|
#include <common/webmids.h>
|
|
|
|
#include <mkvmuxer/mkvmuxerutil.h>
|
|
|
|
|
|
|
|
using namespace mkvmuxer;
|
2015-10-28 17:23:08 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2015-10-28 17:23:08 +00:00
|
|
|
namespace media {
|
|
|
|
namespace {
|
2016-09-29 00:13:15 +00:00
|
|
|
|
|
|
|
// Cluster, Cues, Info, Tracks.
|
|
|
|
const size_t kElementIdCount = 4u;
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
uint64_t EbmlMasterElementWithPayloadSize(libwebm::MkvId id,
|
|
|
|
uint64_t payload_size) {
|
2016-09-29 00:13:15 +00:00
|
|
|
return EbmlMasterElementSize(id, payload_size) + payload_size;
|
|
|
|
}
|
2015-10-28 17:23:08 +00:00
|
|
|
|
|
|
|
uint64_t MaxSeekEntrySize() {
|
|
|
|
const uint64_t max_entry_payload_size =
|
|
|
|
EbmlElementSize(
|
2023-12-01 17:32:19 +00:00
|
|
|
libwebm::kMkvSeekID,
|
2015-10-28 17:23:08 +00:00
|
|
|
static_cast<mkvmuxer::uint64>(std::numeric_limits<uint32_t>::max())) +
|
2023-12-01 17:32:19 +00:00
|
|
|
EbmlElementSize(libwebm::kMkvSeekPosition,
|
2015-10-28 17:23:08 +00:00
|
|
|
std::numeric_limits<mkvmuxer::uint64>::max());
|
2023-12-01 17:32:19 +00:00
|
|
|
return EbmlMasterElementWithPayloadSize(libwebm::kMkvSeek,
|
2016-09-29 00:13:15 +00:00
|
|
|
max_entry_payload_size);
|
2015-10-28 17:23:08 +00:00
|
|
|
}
|
2016-09-29 00:13:15 +00:00
|
|
|
|
2015-10-28 17:23:08 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
SeekHead::SeekHead()
|
2016-09-29 00:13:15 +00:00
|
|
|
: total_void_size_(EbmlMasterElementWithPayloadSize(
|
2023-12-01 17:32:19 +00:00
|
|
|
libwebm::kMkvSeekHead,
|
2016-09-29 00:13:15 +00:00
|
|
|
kElementIdCount * MaxSeekEntrySize())) {}
|
2015-10-28 17:23:08 +00:00
|
|
|
|
|
|
|
SeekHead::~SeekHead() {}
|
|
|
|
|
|
|
|
bool SeekHead::Write(mkvmuxer::IMkvWriter* writer) {
|
2016-09-29 00:13:15 +00:00
|
|
|
std::vector<SeekElement> seek_elements = CreateSeekElements();
|
|
|
|
if (seek_elements.empty())
|
2015-10-28 17:23:08 +00:00
|
|
|
return true;
|
2016-09-29 00:13:15 +00:00
|
|
|
|
|
|
|
uint64_t payload_size = 0;
|
|
|
|
for (const SeekHead::SeekElement& seek_element : seek_elements) {
|
|
|
|
payload_size +=
|
2023-12-01 17:32:19 +00:00
|
|
|
EbmlMasterElementWithPayloadSize(libwebm::kMkvSeek, seek_element.size);
|
2015-10-28 17:23:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const int64_t start_pos = writer->Position();
|
2023-12-01 17:32:19 +00:00
|
|
|
if (!WriteEbmlMasterElement(writer, libwebm::kMkvSeekHead, payload_size))
|
2015-10-28 17:23:08 +00:00
|
|
|
return false;
|
|
|
|
|
2016-09-29 00:13:15 +00:00
|
|
|
for (const SeekHead::SeekElement& element : seek_elements) {
|
2023-12-01 17:32:19 +00:00
|
|
|
if (!WriteEbmlMasterElement(writer, libwebm::kMkvSeek, element.size) ||
|
|
|
|
!WriteEbmlElement(writer, libwebm::kMkvSeekID, element.id) ||
|
|
|
|
!WriteEbmlElement(writer, libwebm::kMkvSeekPosition, element.position))
|
2015-10-28 17:23:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we wrote void before, then fill in the extra with void.
|
|
|
|
if (wrote_void_) {
|
|
|
|
const uint64_t extra_void =
|
2016-09-29 00:13:15 +00:00
|
|
|
total_void_size_ - (writer->Position() - start_pos);
|
2015-10-28 17:23:08 +00:00
|
|
|
if (!WriteVoidElement(writer, extra_void))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SeekHead::WriteVoid(mkvmuxer::IMkvWriter* writer) {
|
2016-09-29 00:13:15 +00:00
|
|
|
const uint64_t written = WriteVoidElement(writer, total_void_size_);
|
2015-10-28 17:23:08 +00:00
|
|
|
if (!written)
|
|
|
|
return false;
|
2016-09-29 00:13:15 +00:00
|
|
|
wrote_void_ = true;
|
2015-10-28 17:23:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-29 00:13:15 +00:00
|
|
|
std::vector<SeekHead::SeekElement> SeekHead::CreateSeekElements() {
|
|
|
|
std::vector<SeekHead::SeekElement> seek_elements;
|
|
|
|
if (info_pos_ != 0)
|
2023-12-01 17:32:19 +00:00
|
|
|
seek_elements.emplace_back(libwebm::kMkvInfo, info_pos_);
|
2016-09-29 00:13:15 +00:00
|
|
|
if (tracks_pos_ != 0)
|
2023-12-01 17:32:19 +00:00
|
|
|
seek_elements.emplace_back(libwebm::kMkvTracks, tracks_pos_);
|
2016-09-29 00:13:15 +00:00
|
|
|
if (cues_pos_ != 0)
|
2023-12-01 17:32:19 +00:00
|
|
|
seek_elements.emplace_back(libwebm::kMkvCues, cues_pos_);
|
2016-09-29 00:13:15 +00:00
|
|
|
if (cluster_pos_ != 0)
|
2023-12-01 17:32:19 +00:00
|
|
|
seek_elements.emplace_back(libwebm::kMkvCluster, cluster_pos_);
|
2016-09-29 00:13:15 +00:00
|
|
|
DCHECK_LE(seek_elements.size(), kElementIdCount);
|
|
|
|
|
|
|
|
std::sort(seek_elements.begin(), seek_elements.end(),
|
|
|
|
[](const SeekHead::SeekElement& left,
|
|
|
|
const SeekHead::SeekElement& right) {
|
|
|
|
return left.position < right.position;
|
|
|
|
});
|
|
|
|
for (SeekHead::SeekElement& element : seek_elements) {
|
2023-12-01 17:32:19 +00:00
|
|
|
element.size = EbmlElementSize(libwebm::kMkvSeekID, element.id) +
|
|
|
|
EbmlElementSize(libwebm::kMkvSeekPosition, element.position);
|
2015-10-28 17:23:08 +00:00
|
|
|
}
|
2016-09-29 00:13:15 +00:00
|
|
|
return seek_elements;
|
2015-10-28 17:23:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|