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