7 #include <packager/media/formats/webm/seek_head.h>
12 #include <absl/log/check.h>
13 #include <absl/log/log.h>
14 #include <common/webmids.h>
15 #include <mkvmuxer/mkvmuxerutil.h>
17 using namespace mkvmuxer;
24 const size_t kElementIdCount = 4u;
26 uint64_t EbmlMasterElementWithPayloadSize(libwebm::MkvId
id,
27 uint64_t payload_size) {
28 return EbmlMasterElementSize(
id, payload_size) + payload_size;
31 uint64_t MaxSeekEntrySize() {
32 const uint64_t max_entry_payload_size =
35 static_cast<mkvmuxer::uint64
>(std::numeric_limits<uint32_t>::max())) +
36 EbmlElementSize(libwebm::kMkvSeekPosition,
37 std::numeric_limits<mkvmuxer::uint64>::max());
38 return EbmlMasterElementWithPayloadSize(libwebm::kMkvSeek,
39 max_entry_payload_size);
45 : total_void_size_(EbmlMasterElementWithPayloadSize(
46 libwebm::kMkvSeekHead,
47 kElementIdCount * MaxSeekEntrySize())) {}
49 SeekHead::~SeekHead() {}
51 bool SeekHead::Write(mkvmuxer::IMkvWriter* writer) {
52 std::vector<SeekElement> seek_elements = CreateSeekElements();
53 if (seek_elements.empty())
56 uint64_t payload_size = 0;
57 for (
const SeekHead::SeekElement& seek_element : seek_elements) {
59 EbmlMasterElementWithPayloadSize(libwebm::kMkvSeek, seek_element.size);
62 const int64_t start_pos = writer->Position();
63 if (!WriteEbmlMasterElement(writer, libwebm::kMkvSeekHead, payload_size))
66 for (
const SeekHead::SeekElement& element : seek_elements) {
67 if (!WriteEbmlMasterElement(writer, libwebm::kMkvSeek, element.size) ||
68 !WriteEbmlElement(writer, libwebm::kMkvSeekID, element.id) ||
69 !WriteEbmlElement(writer, libwebm::kMkvSeekPosition, element.position))
75 const uint64_t extra_void =
76 total_void_size_ - (writer->Position() - start_pos);
77 if (!WriteVoidElement(writer, extra_void))
84 bool SeekHead::WriteVoid(mkvmuxer::IMkvWriter* writer) {
85 const uint64_t written = WriteVoidElement(writer, total_void_size_);
92 std::vector<SeekHead::SeekElement> SeekHead::CreateSeekElements() {
93 std::vector<SeekHead::SeekElement> seek_elements;
95 seek_elements.emplace_back(libwebm::kMkvInfo, info_pos_);
97 seek_elements.emplace_back(libwebm::kMkvTracks, tracks_pos_);
99 seek_elements.emplace_back(libwebm::kMkvCues, cues_pos_);
100 if (cluster_pos_ != 0)
101 seek_elements.emplace_back(libwebm::kMkvCluster, cluster_pos_);
102 DCHECK_LE(seek_elements.size(), kElementIdCount);
104 std::sort(seek_elements.begin(), seek_elements.end(),
105 [](
const SeekHead::SeekElement& left,
106 const SeekHead::SeekElement& right) {
107 return left.position < right.position;
109 for (SeekHead::SeekElement& element : seek_elements) {
110 element.size = EbmlElementSize(libwebm::kMkvSeekID, element.id) +
111 EbmlElementSize(libwebm::kMkvSeekPosition, element.position);
113 return seek_elements;
All the methods that are virtual are virtual for mocking.