7 #include "packager/media/formats/webm/seek_head.h"
12 #include "packager/base/logging.h"
13 #include "packager/third_party/libwebm/src/mkvmuxerutil.hpp"
14 #include "packager/third_party/libwebm/src/webmids.hpp"
21 const size_t kElementIdCount = 4u;
23 uint64_t EbmlMasterElementWithPayloadSize(mkvmuxer::MkvId
id, uint64_t payload_size) {
24 return EbmlMasterElementSize(
id, payload_size) + payload_size;
27 uint64_t MaxSeekEntrySize() {
28 const uint64_t max_entry_payload_size =
31 static_cast<mkvmuxer::uint64>(std::numeric_limits<uint32_t>::max())) +
32 EbmlElementSize(mkvmuxer::kMkvSeekPosition,
33 std::numeric_limits<mkvmuxer::uint64>::max());
34 return EbmlMasterElementWithPayloadSize(mkvmuxer::kMkvSeek,
35 max_entry_payload_size);
41 : total_void_size_(EbmlMasterElementWithPayloadSize(
42 mkvmuxer::kMkvSeekHead,
43 kElementIdCount * MaxSeekEntrySize())) {}
45 SeekHead::~SeekHead() {}
48 std::vector<SeekElement> seek_elements = CreateSeekElements();
49 if (seek_elements.empty())
52 uint64_t payload_size = 0;
53 for (
const SeekHead::SeekElement& seek_element : seek_elements) {
55 EbmlMasterElementWithPayloadSize(mkvmuxer::kMkvSeek, seek_element.size);
58 const int64_t start_pos = writer->Position();
59 if (!WriteEbmlMasterElement(writer, mkvmuxer::kMkvSeekHead, payload_size))
62 for (
const SeekHead::SeekElement& element : seek_elements) {
63 if (!WriteEbmlMasterElement(writer, mkvmuxer::kMkvSeek, element.size) ||
64 !WriteEbmlElement(writer, mkvmuxer::kMkvSeekID, element.id) ||
65 !WriteEbmlElement(writer, mkvmuxer::kMkvSeekPosition, element.position))
71 const uint64_t extra_void =
72 total_void_size_ - (writer->Position() - start_pos);
73 if (!WriteVoidElement(writer, extra_void))
81 const uint64_t written = WriteVoidElement(writer, total_void_size_);
88 std::vector<SeekHead::SeekElement> SeekHead::CreateSeekElements() {
89 std::vector<SeekHead::SeekElement> seek_elements;
91 seek_elements.emplace_back(mkvmuxer::kMkvInfo, info_pos_);
93 seek_elements.emplace_back(mkvmuxer::kMkvTracks, tracks_pos_);
95 seek_elements.emplace_back(mkvmuxer::kMkvCues, cues_pos_);
96 if (cluster_pos_ != 0)
97 seek_elements.emplace_back(mkvmuxer::kMkvCluster, cluster_pos_);
98 DCHECK_LE(seek_elements.size(), kElementIdCount);
100 std::sort(seek_elements.begin(), seek_elements.end(),
101 [](
const SeekHead::SeekElement& left,
102 const SeekHead::SeekElement& right) {
103 return left.position < right.position;
105 for (SeekHead::SeekElement& element : seek_elements) {
107 EbmlElementSize(mkvmuxer::kMkvSeekID, element.id) +
108 EbmlElementSize(mkvmuxer::kMkvSeekPosition, element.position);
110 return seek_elements;