Shaka Packager SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
box.cc
1 // Copyright 2014 Google Inc. 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 "packager/base/logging.h"
10 #include "packager/media/formats/mp4/box_buffer.h"
11 
12 namespace shaka {
13 namespace media {
14 namespace mp4 {
15 
16 Box::Box() : box_size_(0) {}
17 Box::~Box() {}
18 
19 bool Box::Parse(BoxReader* reader) {
20  DCHECK(reader);
21  BoxBuffer buffer(reader);
22  return ReadWriteInternal(&buffer);
23 }
24 
25 void Box::Write(BufferWriter* writer) {
26  DCHECK(writer);
27  // Compute and update box size.
28  uint32_t size = ComputeSize();
29  DCHECK_EQ(size, box_size_);
30 
31  size_t buffer_size_before_write = writer->Size();
32  BoxBuffer buffer(writer);
33  CHECK(ReadWriteInternal(&buffer));
34  DCHECK_EQ(box_size_, writer->Size() - buffer_size_before_write)
35  << FourCCToString(BoxType());
36 }
37 
38 void Box::WriteHeader(BufferWriter* writer) {
39  DCHECK(writer);
40  // Compute and update box size.
41  uint32_t size = ComputeSize();
42  DCHECK_EQ(size, box_size_);
43 
44  size_t buffer_size_before_write = writer->Size();
45  BoxBuffer buffer(writer);
46  CHECK(ReadWriteHeaderInternal(&buffer));
47  DCHECK_EQ(HeaderSize(), writer->Size() - buffer_size_before_write);
48 }
49 
50 uint32_t Box::ComputeSize() {
51  box_size_ = static_cast<uint32_t>(ComputeSizeInternal());
52  return box_size_;
53 }
54 
55 uint32_t Box::HeaderSize() const {
56  const uint32_t kFourCCSize = 4;
57  // We don't support 64-bit size.
58  return kFourCCSize + sizeof(uint32_t);
59 }
60 
61 bool Box::ReadWriteHeaderInternal(BoxBuffer* buffer) {
62  if (buffer->Reading()) {
63  // Skip for read mode, which is handled already in BoxReader.
64  } else {
65  CHECK(buffer->ReadWriteUInt32(&box_size_));
66  FourCC fourcc = BoxType();
67  CHECK(buffer->ReadWriteFourCC(&fourcc));
68  }
69  return true;
70 }
71 
72 FullBox::FullBox() : version(0), flags(0) {}
73 FullBox::~FullBox() {}
74 
75 uint32_t FullBox::HeaderSize() const {
76  // Additional 1-byte version and 3-byte flags.
77  return Box::HeaderSize() + 1 + 3;
78 }
79 
81  RCHECK(Box::ReadWriteHeaderInternal(buffer));
82 
83  uint32_t vflags;
84  if (buffer->Reading()) {
85  RCHECK(buffer->ReadWriteUInt32(&vflags));
86  this->version = vflags >> 24;
87  this->flags = vflags & 0x00FFFFFF;
88  } else {
89  vflags = (this->version << 24) | this->flags;
90  RCHECK(buffer->ReadWriteUInt32(&vflags));
91  }
92  return true;
93 }
94 
95 } // namespace mp4
96 } // namespace media
97 } // namespace shaka
uint32_t HeaderSize() const final
Definition: box.cc:75
bool ReadWriteHeaderInternal(BoxBuffer *buffer) final
Definition: box.cc:80
virtual bool ReadWriteHeaderInternal(BoxBuffer *buffer)
Definition: box.cc:61
virtual uint32_t HeaderSize() const
Definition: box.cc:55
Class for reading MP4 boxes.
Definition: box_reader.h:25