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 != NULL);
21  BoxBuffer buffer(reader);
22  return ReadWrite(&buffer);
23 }
24 
25 void Box::Write(BufferWriter* writer) {
26  DCHECK(writer != NULL);
27  uint32_t size = ComputeSize();
28  DCHECK_EQ(size, this->atom_size);
29 
30  size_t buffer_size_before_write = writer->Size();
31  BoxBuffer buffer(writer);
32  CHECK(ReadWrite(&buffer));
33  DCHECK_EQ(this->atom_size, writer->Size() - buffer_size_before_write);
34 }
35 
36 bool Box::ReadWrite(BoxBuffer* buffer) {
37  if (buffer->Reading()) {
38  // Skip for read mode, which is handled already in BoxReader.
39  } else {
40  CHECK(buffer->ReadWriteUInt32(&this->atom_size));
41  FourCC fourcc = BoxType();
42  CHECK(buffer->ReadWriteFourCC(&fourcc));
43  }
44  return true;
45 }
46 
47 FullBox::FullBox() : version(0), flags(0) {}
48 FullBox::~FullBox() {}
49 
51  RCHECK(Box::ReadWrite(buffer));
52 
53  uint32_t vflags;
54  if (buffer->Reading()) {
55  RCHECK(buffer->ReadWriteUInt32(&vflags));
56  this->version = vflags >> 24;
57  this->flags = vflags & 0x00FFFFFF;
58  } else {
59  vflags = (this->version << 24) | this->flags;
60  RCHECK(buffer->ReadWriteUInt32(&vflags));
61  }
62  return true;
63 }
64 
65 } // namespace mp4
66 } // namespace media
67 } // namespace edash_packager
virtual bool ReadWrite(BoxBuffer *buffer)
Read/write the mp4 box from/to BoxBuffer.
Definition: box.cc:36
virtual uint32_t ComputeSize()=0
bool ReadWrite(BoxBuffer *buffer) override
Read/write the mp4 box from/to BoxBuffer.
Definition: box.cc:50
bool Parse(BoxReader *reader)
Definition: box.cc:19
void Write(BufferWriter *writer)
Definition: box.cc:25
Class for reading MP4 boxes.
Definition: box_reader.h:24