Shaka Packager SDK
box.cc
1 // Copyright 2014 Google LLC. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include <packager/media/formats/mp4/box.h>
8 
9 #include <absl/log/check.h>
10 #include <absl/log/log.h>
11 
12 #include <packager/media/formats/mp4/box_buffer.h>
13 
14 namespace shaka {
15 namespace media {
16 namespace mp4 {
17 
18 Box::Box() : box_size_(0) {}
19 Box::~Box() {}
20 
21 bool Box::Parse(BoxReader* reader) {
22  DCHECK(reader);
23  BoxBuffer buffer(reader);
24  return ReadWriteInternal(&buffer);
25 }
26 
27 void Box::Write(BufferWriter* writer) {
28  DCHECK(writer);
29  // Compute and update box size.
30  uint32_t size = ComputeSize();
31  DCHECK_EQ(size, box_size_);
32 
33  size_t buffer_size_before_write = writer->Size();
34  BoxBuffer buffer(writer);
35  CHECK(ReadWriteInternal(&buffer));
36  DCHECK_EQ(box_size_, writer->Size() - buffer_size_before_write)
37  << FourCCToString(BoxType());
38 }
39 
40 void Box::WriteHeader(BufferWriter* writer) {
41  DCHECK(writer);
42  // Compute and update box size.
43  uint32_t size = ComputeSize();
44  DCHECK_EQ(size, box_size_);
45 
46  size_t buffer_size_before_write = writer->Size();
47  BoxBuffer buffer(writer);
48  CHECK(ReadWriteHeaderInternal(&buffer));
49  DCHECK_EQ(HeaderSize(), writer->Size() - buffer_size_before_write);
50 }
51 
52 uint32_t Box::ComputeSize() {
53  box_size_ = static_cast<uint32_t>(ComputeSizeInternal());
54  return box_size_;
55 }
56 
57 uint32_t Box::HeaderSize() const {
58  const uint32_t kFourCCSize = 4;
59  // We don't support 64-bit size.
60  return kFourCCSize + sizeof(uint32_t);
61 }
62 
63 bool Box::ReadWriteHeaderInternal(BoxBuffer* buffer) {
64  if (buffer->Reading()) {
65  // Skip for read mode, which is handled already in BoxReader.
66  } else {
67  CHECK(buffer->ReadWriteUInt32(&box_size_));
68  FourCC fourcc = BoxType();
69  CHECK(buffer->ReadWriteFourCC(&fourcc));
70  }
71  return true;
72 }
73 
74 FullBox::FullBox() = default;
75 FullBox::~FullBox() = default;
76 
77 uint32_t FullBox::HeaderSize() const {
78  // Additional 1-byte version and 3-byte flags.
79  return Box::HeaderSize() + 1 + 3;
80 }
81 
82 bool FullBox::ReadWriteHeaderInternal(BoxBuffer* buffer) {
83  RCHECK(Box::ReadWriteHeaderInternal(buffer));
84 
85  uint32_t vflags;
86  if (buffer->Reading()) {
87  RCHECK(buffer->ReadWriteUInt32(&vflags));
88  this->version = vflags >> 24;
89  this->flags = vflags & 0x00FFFFFF;
90  } else {
91  vflags = (this->version << 24) | this->flags;
92  RCHECK(buffer->ReadWriteUInt32(&vflags));
93  }
94  return true;
95 }
96 
97 } // namespace mp4
98 } // namespace media
99 } // namespace shaka
Class for reading MP4 boxes.
Definition: box_reader.h:28
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66