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) {
56 EbmlMasterElementSize(mkvmuxer::kMkvSeek, seek_element.size);
59 const int64_t start_pos = writer->Position();
60 if (!WriteEbmlMasterElement(writer, mkvmuxer::kMkvSeekHead, payload_size))
63 for (
const SeekHead::SeekElement& element : seek_elements) {
64 if (!WriteEbmlMasterElement(writer, mkvmuxer::kMkvSeek, element.size) ||
65 !WriteEbmlElement(writer, mkvmuxer::kMkvSeekID, element.id) ||
66 !WriteEbmlElement(writer, mkvmuxer::kMkvSeekPosition, element.position))
72 const uint64_t extra_void =
73 total_void_size_ - (writer->Position() - start_pos);
74 if (!WriteVoidElement(writer, extra_void))
82 const uint64_t written = WriteVoidElement(writer, total_void_size_);
89 std::vector<SeekHead::SeekElement> SeekHead::CreateSeekElements() {
90 std::vector<SeekHead::SeekElement> seek_elements;
92 seek_elements.emplace_back(mkvmuxer::kMkvInfo, info_pos_);
94 seek_elements.emplace_back(mkvmuxer::kMkvTracks, tracks_pos_);
96 seek_elements.emplace_back(mkvmuxer::kMkvCues, cues_pos_);
97 if (cluster_pos_ != 0)
98 seek_elements.emplace_back(mkvmuxer::kMkvCluster, cluster_pos_);
99 DCHECK_LE(seek_elements.size(), kElementIdCount);
101 std::sort(seek_elements.begin(), seek_elements.end(),
102 [](
const SeekHead::SeekElement& left,
103 const SeekHead::SeekElement& right) {
104 return left.position < right.position;
106 for (SeekHead::SeekElement& element : seek_elements) {
108 EbmlElementSize(mkvmuxer::kMkvSeekID, element.id) +
109 EbmlElementSize(mkvmuxer::kMkvSeekPosition, element.position);
111 return seek_elements;