Update contrib.
1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
18 static const TUint32 KRMFileHeaderId = MAKE_INT32('.', 'R', 'M', 'F');
19 static const TUint32 KRMMediaPropertiesId = MAKE_INT32('M', 'D', 'P', 'R');
20 static const TInt KRMMaxMimeTypeLen = 255; // Specified by an 8-bit field
22 #define KRMExtensionBit KBit0
23 #define KRMFileHeaderBit KBit1
24 #define KRMMediaPropertiesBit KBit2
25 #define KRMVideoBit KBit3
27 #define KRMVideoMask 0x08 // 0000 1000
28 #define KRMConfidenceMask 0x07 // 0000 0111
31 // This truth table maps the following flags to a confidence level.
32 // -----------------------------------------------------------------
33 // A: MediaProperties chunk found
34 // B: FileHeader signature found
35 // C: Extension recognised.
37 // A B C -> Confidence
38 // ===================
39 // 0 0 0 -> ENotRecognized
41 // 0 1 0 -> EPossible (EPossible beacuse the header is ASCII and may match plaintext)
43 // 1 0 0 -> ENotRecognised (Can't have MediaProperties without FileHeader)
44 // 1 0 1 -> ENotRecognised (Can't have MediaProperties without FileHeader)
48 static const TInt KRMFlagsToConfidence[] =
64 TRMParser::TRMParser(CReader& aReader, TFlags& aFlags)
72 // The main RealMedia parsing function.
74 void TRMParser::DoRecognise(const TDesC& aFileExt, CReader& aReader, TMatch& aMatch)
77 TRMParser parser(aReader, flags);
79 parser.MatchExtension(aFileExt);
81 TRAP_IGNORE(parser.ParseL());
83 TInt confIndex = flags.GetBitField(KRMConfidenceMask);
84 aMatch.iConfidence = KRMFlagsToConfidence[confIndex];
86 if (aMatch.iConfidence != KConfNotRecognised)
88 aMatch.iMime = (flags.GetBitField(KRMVideoMask) ? KMimeRM_V : KMimeRM_A);
94 // Match the file extension given by AppArc against known
95 // extensions for this format.
97 void TRMParser::MatchExtension(const TDesC& aFileExt)
101 match = (aFileExt.MatchF(TPtrC(KExtRMF)) != KErrNotFound);
102 match |= (aFileExt.MatchF(TPtrC(KExtRM)) != KErrNotFound);
106 iFlags.SetExtensionFlag();
111 // RealMedia files are divided into chunks. Chunks begins with a 32-bit identifier
112 // which is followed by a 32-bit unsigned field that specifies the size of the chunk
113 // in bytes. Since the size field is unsigned, it must be cast to a TInt64 in order
114 // for SeekL() to work reliably. The size field also includes the length of the chunk
115 // identifier and the length of the size field itself, so these must be subtracted
116 // from any seek operation.
118 void TRMParser::ParseL()
122 const TInt KChunkHeaderSize = sizeof(TUint32) * 2; // sizeof objectId and size fields.
124 // The first chunk must be the FileHeader.
125 ReadChunkHeaderL(objectId, size, ETrue);
127 // Assume there's video content if we only have buffer data.
128 if (iReader.Type() == CReader::EBuffer)
130 iFlags.SetBit(KRMVideoBit);
133 iReader.SeekL(TInt64(size - KChunkHeaderSize));
135 // The other chunks can occur in any order.
136 TBool complete = EFalse;
139 ReadChunkHeaderL(objectId, size);
141 if (objectId == KRMMediaPropertiesId)
143 iFlags.SetBit(KRMMediaPropertiesBit);
144 if (ReadMediaPropertiesL())
146 // Exit early if the video flag has been set by ReadMediaPropertiesL.
152 // Skip over the unneeded chunk.
153 iReader.SeekL(TInt64(size - KChunkHeaderSize));
161 // Reads the chunk identifier and size.
163 void TRMParser::ReadChunkHeaderL(TUint32& aObjectId, TUint32& aSize, TBool aFirstChunk)
165 iReader.Read32L(aObjectId);
169 if (aObjectId == KRMFileHeaderId)
171 iFlags.SetBit(KRMFileHeaderBit);
175 User::Leave(KErrCorrupt);
179 iReader.Read32L(aSize);
182 User::Leave(KErrCorrupt);
188 // The MediaProperties chunk describes a stream in the RM file. There
189 // may be several MediaProperties chunks in one file. In order to determine
190 // if video content is present we must look at the MIME-type that may
191 // be present in each MediaProperties chunk.
192 // Return ETrue if this function set the video bit.
194 TBool TRMParser::ReadMediaPropertiesL()
196 const TInt KBytesToFieldSize = 30;
198 // ObjectId and size fields have already been read.
199 TBool setVideo = EFalse;
200 TUint16 objectVersion;
202 iReader.Read16L(objectVersion);
204 if (objectVersion == 0)
207 TBuf8<KRMMaxMimeTypeLen> mimeType;
208 _LIT8(KVideoStar, "video/*");
210 // Skip unneeded information.
211 iReader.SeekL(KBytesToFieldSize);
213 // Read stream_name_size so we can skip over it.
214 iReader.ReadByteL(fieldSize);
215 iReader.SeekL(fieldSize);
217 // Read the mime_type_size. Because it's 8-bit, it has a max length of 256 bytes.
218 iReader.ReadByteL(fieldSize);
220 mimeType.SetLength(fieldSize);
221 iReader.ReadBytesL(mimeType);
223 // Now check if the mime-type field indicates video content.
224 if (mimeType.MatchF(KVideoStar) != KErrNotFound)
226 iFlags.SetBit(KRMVideoBit);
230 // Read the type_specific_len so we can skip over it.
231 TUint32 typeSpecificLen;
232 iReader.Read32L(typeSpecificLen);
233 iReader.SeekL(TInt64(typeSpecificLen));