1 /*****************************************************************
2 |
3 | AP4 - odda Atoms
4 |
5 | Copyright 2002-2008 Axiomatic Systems, LLC
6 |
7 |
8 | This file is part of Bento4/AP4 (MP4 Atom Processing Library).
9 |
10 | Unless you have obtained Bento4 under a difference license,
11 | this version of Bento4 is Bento4|GPL.
12 | Bento4|GPL is free software; you can redistribute it and/or modify
13 | it under the terms of the GNU General Public License as published by
14 | the Free Software Foundation; either version 2, or (at your option)
15 | any later version.
16 |
17 | Bento4|GPL is distributed in the hope that it will be useful,
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 | GNU General Public License for more details.
21 |
22 | You should have received a copy of the GNU General Public License
23 | along with Bento4|GPL; see the file COPYING. If not, write to the
24 | Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 | 02111-1307, USA.
26 |
27 ****************************************************************/
28
29 /*----------------------------------------------------------------------
30 | includes
31 +---------------------------------------------------------------------*/
32 #include "Ap4Utils.h"
33 #include "Ap4OddaAtom.h"
34
35 /*----------------------------------------------------------------------
36 | dynamic cast support
37 +---------------------------------------------------------------------*/
AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_OddaAtom)38 AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_OddaAtom)
39
40 /*----------------------------------------------------------------------
41 | AP4_OddaAtom::Create
42 +---------------------------------------------------------------------*/
43 AP4_OddaAtom*
44 AP4_OddaAtom::Create(AP4_UI64 size,
45 AP4_ByteStream& stream)
46 {
47 AP4_UI08 version;
48 AP4_UI32 flags;
49 if (AP4_FAILED(AP4_Atom::ReadFullHeader(stream, version, flags))) return NULL;
50 if (version != 0) return NULL;
51 return new AP4_OddaAtom(size, version, flags, stream);
52 }
53
54 /*----------------------------------------------------------------------
55 | AP4_OddaAtom::AP4_OddaAtom
56 +---------------------------------------------------------------------*/
AP4_OddaAtom(AP4_UI64 size,AP4_UI08 version,AP4_UI32 flags,AP4_ByteStream & stream)57 AP4_OddaAtom::AP4_OddaAtom(AP4_UI64 size,
58 AP4_UI08 version,
59 AP4_UI32 flags,
60 AP4_ByteStream& stream) :
61 AP4_Atom(AP4_ATOM_TYPE_ODDA, size, true, version, flags)
62 {
63 // data length
64 stream.ReadUI64(m_EncryptedDataLength);
65
66 // get the source stream position
67 AP4_Position position;
68 stream.Tell(position);
69
70 // create a substream to represent the payload
71 m_EncryptedPayload = new AP4_SubStream(stream, position, m_EncryptedDataLength);
72
73 // seek to the end
74 stream.Seek(position+m_EncryptedDataLength);
75 }
76
77 /*----------------------------------------------------------------------
78 | AP4_OddaAtom::AP4_OddaAtom
79 +---------------------------------------------------------------------*/
AP4_OddaAtom(AP4_ByteStream & encrypted_payload)80 AP4_OddaAtom::AP4_OddaAtom(AP4_ByteStream& encrypted_payload) :
81 AP4_Atom(AP4_ATOM_TYPE_ODDA, 0, true, 0, 0)
82 {
83 // encrypted data length
84 encrypted_payload.GetSize(m_EncryptedDataLength);
85
86 // update our size
87 SetSize(AP4_FULL_ATOM_HEADER_SIZE_64+8+m_EncryptedDataLength, true);
88
89 // keep a reference to the encrypted payload
90 m_EncryptedPayload = &encrypted_payload;
91 m_EncryptedPayload->AddReference();
92 }
93
94 /*----------------------------------------------------------------------
95 | AP4_OddaAtom::~AP4_OddaAtom
96 +---------------------------------------------------------------------*/
~AP4_OddaAtom()97 AP4_OddaAtom::~AP4_OddaAtom()
98 {
99 if (m_EncryptedPayload) m_EncryptedPayload->Release();
100 }
101
102
103 /*----------------------------------------------------------------------
104 | AP4_OddaAtom::SetEncryptedPayload
105 +---------------------------------------------------------------------*/
106 AP4_Result
SetEncryptedPayload(AP4_ByteStream & stream,AP4_LargeSize length)107 AP4_OddaAtom::SetEncryptedPayload(AP4_ByteStream& stream, AP4_LargeSize length)
108 {
109 // keep a reference to the stream
110 if (m_EncryptedPayload) {
111 m_EncryptedPayload->Release();
112 }
113 m_EncryptedPayload = &stream;
114 m_EncryptedPayload->AddReference();
115
116 // update the size
117 m_EncryptedDataLength = length;
118 SetSize(AP4_FULL_ATOM_HEADER_SIZE_64 + 8 + length, true);
119 if (m_Parent) m_Parent->OnChildChanged(this);
120
121 return AP4_SUCCESS;
122 }
123
124 /*----------------------------------------------------------------------
125 | AP4_OddaAtom::SetEncryptedPayload
126 +---------------------------------------------------------------------*/
127 AP4_Result
SetEncryptedPayload(AP4_ByteStream & stream)128 AP4_OddaAtom::SetEncryptedPayload(AP4_ByteStream& stream)
129 {
130 // the new encrypted data length is the size of the stream
131 AP4_LargeSize length;
132 AP4_Result result = stream.GetSize(length);
133 if (AP4_FAILED(result)) return result;
134
135 return SetEncryptedPayload(stream, length);
136 }
137
138 /*----------------------------------------------------------------------
139 | AP4_OddaAtom::WriteFields
140 +---------------------------------------------------------------------*/
141 AP4_Result
WriteFields(AP4_ByteStream & stream)142 AP4_OddaAtom::WriteFields(AP4_ByteStream& stream)
143 {
144 // write the content type
145 AP4_CHECK(stream.WriteUI64(m_EncryptedDataLength));
146
147 // check that we have a source stream
148 // and a normal size
149 if (m_EncryptedPayload == NULL || GetSize() < 8) {
150 return AP4_FAILURE;
151 }
152
153 // rewind the encrypted stream
154 AP4_CHECK(m_EncryptedPayload->Seek(0));
155
156 // copy the encrypted stream to the output
157 AP4_CHECK(m_EncryptedPayload->CopyTo(stream, m_EncryptedDataLength));
158
159 return AP4_SUCCESS;
160 }
161
162 /*----------------------------------------------------------------------
163 | AP4_OddaAtom::InspectFields
164 +---------------------------------------------------------------------*/
165 AP4_Result
InspectFields(AP4_AtomInspector & inspector)166 AP4_OddaAtom::InspectFields(AP4_AtomInspector& inspector)
167 {
168 inspector.AddField("encrypted_data_length", (AP4_UI32)m_EncryptedDataLength);
169 return AP4_SUCCESS;
170 }
171