1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18 /*********************************************************************************/
19 /* ------------------------------------------------------------------- */
20 /* MPEG-4 MediaAtom Class */
21 /* ------------------------------------------------------------------- */
22 /*********************************************************************************/
23 /*
24 This MediaAtom Class contains all the objects that declare information
25 about the media data within the stream.
26 */
27
28 #define IMPLEMENT_MediaAtom_H__
29
30 #include "mediaatom.h"
31 #include "atomdefs.h"
32 #include "atomutils.h"
33
34
35 // Stream-in ctor
MediaAtom(MP4_FF_FILE * fp,OSCL_wString & filename,uint32 size,uint32 type,bool oPVContentDownloadable,uint32 parsingMode)36 MediaAtom::MediaAtom(MP4_FF_FILE *fp,
37 OSCL_wString& filename,
38 uint32 size,
39 uint32 type,
40 bool oPVContentDownloadable,
41 uint32 parsingMode)
42 : Atom(fp, size, type)
43 {
44 _pmediaHeader = NULL;
45 _phandler = NULL;
46 _pmediaInformation = NULL;
47
48 if (_success)
49 {
50 uint32 mediaType = 0;
51 uint32 handlerType;
52
53 // Still need to be able to read in these atoms in ANY ORDER!!!
54 _pparent = NULL;
55
56 int32 count = _size - DEFAULT_ATOM_SIZE;
57 uint32 minf_fp = 0;
58
59 uint32 atomType = UNKNOWN_ATOM;
60 uint32 atomSize = 0;
61
62 // Can read in mdhd, hdlr, minf atoms in ANY order
63 while (((atomType == MEDIA_HEADER_ATOM) ||
64 (atomType == HANDLER_ATOM) ||
65 (atomType == MEDIA_INFORMATION_ATOM) ||
66 (atomType == UUID_ATOM) ||
67 (atomType == UNKNOWN_ATOM)) &&
68 (count > 0))
69 {
70 AtomUtils::getNextAtomType(fp, atomSize, atomType);
71
72 if ((atomType == UUID_ATOM)
73 || (atomType == UNKNOWN_ATOM)
74 || (atomType == MEDIA_INFORMATION_ATOM))
75 {
76 if (atomSize < DEFAULT_ATOM_SIZE)
77 {
78 _success = false;
79 _mp4ErrorCode = ZERO_OR_NEGATIVE_ATOM_SIZE;
80 break;
81 }
82 if (count < (int32)atomSize)
83 {
84 _success = false;
85 _mp4ErrorCode = READ_FAILED;
86 break;
87 }
88 count -= atomSize;
89 atomSize -= DEFAULT_ATOM_SIZE;
90 if (atomType == MEDIA_INFORMATION_ATOM)
91 {
92 minf_fp = AtomUtils::getCurrentFilePosition(fp);
93 minf_fp -= DEFAULT_ATOM_SIZE;
94 }
95 AtomUtils::seekFromCurrPos(fp, atomSize);
96 }
97 else if (atomType == MEDIA_HEADER_ATOM)
98 {
99 //"mdhd"
100 PV_MP4_FF_NEW(fp->auditCB, MediaHeaderAtom, (fp, atomSize, atomType), _pmediaHeader);//"mdhd"
101
102 // Error checking
103 if (!_pmediaHeader->MP4Success())
104 {
105 _success = false;
106 _mp4ErrorCode = _pmediaHeader->GetMP4Error();
107 return;
108 }
109
110 _pmediaHeader->setParent(this);
111 count -= _pmediaHeader->getSize();
112 }
113 else if (atomType == HANDLER_ATOM)
114 {
115 //"hdlr"
116
117 PV_MP4_FF_NEW(fp->auditCB, HandlerAtom, (fp, atomSize, atomType), _phandler);
118
119 // Error checking
120 if (!_phandler->MP4Success())
121 {
122 _success = false;
123 _mp4ErrorCode = _phandler->GetMP4Error();
124 return;
125 }
126
127
128 // Need to get media and handler types from handler before can read in media information atom
129 //handlerType should be correct now first one fp "vide",
130 handlerType = _phandler->getHandlerType();
131 //mediaType should be the same as handlerType, if that handlerType do exists
132 mediaType = AtomUtils::getMediaTypeFromHandlerType(handlerType);
133
134 _phandler->setParent(this);
135 count -= _phandler->getSize();
136 }
137 }
138
139 if (minf_fp)
140 {
141 if ((mediaType == MEDIA_TYPE_AUDIO) ||
142 (mediaType == MEDIA_TYPE_VISUAL) ||
143 (mediaType == MEDIA_TYPE_TEXT))
144 {
145 int32 filePointer;
146 filePointer = AtomUtils::getCurrentFilePosition(fp);
147
148 //move file pointer to the beginning of minf atom
149 AtomUtils::seekFromStart(fp, minf_fp);
150
151 PV_MP4_FF_NEW(fp->auditCB, MediaInformationAtom, (fp, mediaType, filename,
152 oPVContentDownloadable,
153 parsingMode),
154 _pmediaInformation);
155
156 // Error checking
157 if (!_pmediaInformation->MP4Success())
158 {
159 _success = false;
160 _mp4ErrorCode = _pmediaInformation->GetMP4Error();
161 return;
162 }
163 _pmediaInformation->setParent(this);
164 //restore file pointer back
165 AtomUtils::seekFromStart(fp, filePointer);
166
167 if ((_pmediaHeader == NULL) ||
168 (_phandler == NULL) ||
169 (_pmediaInformation == NULL))
170
171 {
172 _success = false;
173 _mp4ErrorCode = READ_MEDIA_ATOM_FAILED;
174 return;
175 }
176 }
177 }
178 }
179 else
180 {
181 _mp4ErrorCode = READ_MEDIA_ATOM_FAILED;
182 }
183 }
184
185 // Destructor
~MediaAtom()186 MediaAtom::~MediaAtom()
187 {
188 if (_pmediaHeader != NULL)
189 {
190 PV_MP4_FF_DELETE(NULL, MediaHeaderAtom, _pmediaHeader);
191 }
192
193 if (_phandler != NULL)
194 {
195 PV_MP4_FF_DELETE(NULL, HandlerAtom, _phandler);
196 }
197
198 if (_pmediaInformation != NULL)
199 {
200 PV_MP4_FF_DELETE(NULL, MediaInformationAtom, _pmediaInformation);
201 }
202 }
203
204
205