7 #include "packager/media/formats/webm/seek_head.h"
11 #include "packager/third_party/libwebm/src/mkvmuxerutil.hpp"
12 #include "packager/third_party/libwebm/src/webmids.hpp"
17 const mkvmuxer::uint64 kElementIds[] = {
18 mkvmuxer::kMkvInfo, mkvmuxer::kMkvTracks, mkvmuxer::kMkvCluster,
21 const int kElementIdCount = arraysize(kElementIds);
23 uint64_t MaxSeekEntrySize() {
24 const uint64_t max_entry_payload_size =
27 static_cast<mkvmuxer::uint64>(std::numeric_limits<uint32_t>::max())) +
28 EbmlElementSize(mkvmuxer::kMkvSeekPosition,
29 std::numeric_limits<mkvmuxer::uint64>::max());
30 const uint64_t max_entry_size =
31 EbmlMasterElementSize(mkvmuxer::kMkvSeek, max_entry_payload_size) +
32 max_entry_payload_size;
34 return max_entry_size;
45 SeekHead::~SeekHead() {}
48 std::vector<uint64_t> element_sizes;
49 const uint64_t payload_size = GetPayloadSize(&element_sizes);
51 if (payload_size == 0) {
55 const int64_t start_pos = writer->Position();
56 if (!WriteEbmlMasterElement(writer, mkvmuxer::kMkvSeekHead, payload_size))
59 const int64_t positions[] = {info_pos_, tracks_pos_, cluster_pos_, cues_pos_};
60 for (
int i = 0; i < kElementIdCount; ++i) {
61 if (element_sizes[i] == 0)
64 const mkvmuxer::uint64 position =
65 static_cast<mkvmuxer::uint64
>(positions[i]);
66 if (!WriteEbmlMasterElement(writer, mkvmuxer::kMkvSeek, element_sizes[i]) ||
67 !WriteEbmlElement(writer, mkvmuxer::kMkvSeekID, kElementIds[i]) ||
68 !WriteEbmlElement(writer, mkvmuxer::kMkvSeekPosition, position))
74 const uint64_t max_payload_size = kElementIdCount * MaxSeekEntrySize();
75 const uint64_t total_void_size =
76 EbmlMasterElementSize(mkvmuxer::kMkvSeekHead, max_payload_size) +
79 const uint64_t extra_void =
80 total_void_size - (writer->Position() - start_pos);
81 if (!WriteVoidElement(writer, extra_void))
89 const uint64_t payload_size = kElementIdCount * MaxSeekEntrySize();
90 const uint64_t total_size =
91 EbmlMasterElementSize(mkvmuxer::kMkvSeekHead, payload_size) +
95 const uint64_t written = WriteVoidElement(writer, total_size);
102 uint64_t SeekHead::GetPayloadSize(std::vector<uint64_t>* data) {
103 const int64_t positions[] = {info_pos_, tracks_pos_, cluster_pos_, cues_pos_};
104 uint64_t total_payload_size = 0;
105 data->resize(kElementIdCount);
106 for (
int i = 0; i < kElementIdCount; ++i) {
107 if (positions[i] < 0) {
112 const mkvmuxer::uint64 position =
113 static_cast<mkvmuxer::uint64
>(positions[i]);
114 (*data)[i] = EbmlElementSize(mkvmuxer::kMkvSeekID, kElementIds[i]) +
115 EbmlElementSize(mkvmuxer::kMkvSeekPosition, position);
116 total_payload_size +=
117 data->at(i) + EbmlMasterElementSize(mkvmuxer::kMkvSeek, data->at(i));
120 return total_payload_size;