michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 tw=80 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsMediaSniffer.h" michael@0: #include "nsIHttpChannel.h" michael@0: #include "nsString.h" michael@0: #include "nsMimeTypes.h" michael@0: #include "mozilla/ArrayUtils.h" michael@0: #include "mozilla/ModuleUtils.h" michael@0: #include "mp3sniff.h" michael@0: #ifdef MOZ_WEBM michael@0: #include "nestegg/nestegg.h" michael@0: #endif michael@0: michael@0: #include "nsIClassInfoImpl.h" michael@0: #include michael@0: michael@0: // The minimum number of bytes that are needed to attempt to sniff an mp4 file. michael@0: static const unsigned MP4_MIN_BYTES_COUNT = 12; michael@0: // The maximum number of bytes to consider when attempting to sniff a file. michael@0: static const uint32_t MAX_BYTES_SNIFFED = 512; michael@0: // The maximum number of bytes to consider when attempting to sniff for a mp3 michael@0: // bitstream. michael@0: // This is 320kbps * 144 / 32kHz + 1 padding byte + 4 bytes of capture pattern. michael@0: static const uint32_t MAX_BYTES_SNIFFED_MP3 = 320 * 144 / 32 + 1 + 4; michael@0: michael@0: NS_IMPL_ISUPPORTS(nsMediaSniffer, nsIContentSniffer) michael@0: michael@0: nsMediaSniffer::nsMediaSnifferEntry nsMediaSniffer::sSnifferEntries[] = { michael@0: // The string OggS, followed by the null byte. michael@0: PATTERN_ENTRY("\xFF\xFF\xFF\xFF\xFF", "OggS", APPLICATION_OGG), michael@0: // The string RIFF, followed by four bytes, followed by the string WAVE michael@0: PATTERN_ENTRY("\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF", "RIFF\x00\x00\x00\x00WAVE", AUDIO_WAV), michael@0: // mp3 with ID3 tags, the string "ID3". michael@0: PATTERN_ENTRY("\xFF\xFF\xFF", "ID3", AUDIO_MP3) michael@0: }; michael@0: michael@0: // This function implements mp4 sniffing algorithm, described at michael@0: // http://mimesniff.spec.whatwg.org/#signature-for-mp4 michael@0: static bool MatchesMP4(const uint8_t* aData, const uint32_t aLength) michael@0: { michael@0: if (aLength <= MP4_MIN_BYTES_COUNT) { michael@0: return false; michael@0: } michael@0: // Conversion from big endian to host byte order. michael@0: uint32_t boxSize = (uint32_t)(aData[3] | aData[2] << 8 | aData[1] << 16 | aData[0] << 24); michael@0: michael@0: // Boxsize should be evenly divisible by 4. michael@0: if (boxSize % 4 || aLength < boxSize) { michael@0: return false; michael@0: } michael@0: // The string "ftyp". michael@0: if (aData[4] != 0x66 || michael@0: aData[5] != 0x74 || michael@0: aData[6] != 0x79 || michael@0: aData[7] != 0x70) { michael@0: return false; michael@0: } michael@0: for (uint32_t i = 2; i <= boxSize / 4 - 1 ; i++) { michael@0: if (i == 3) { michael@0: continue; michael@0: } michael@0: // The string "mp42" or "mp41". michael@0: if (aData[4*i] == 0x6D && michael@0: aData[4*i+1] == 0x70 && michael@0: aData[4*i+2] == 0x34) { michael@0: return true; michael@0: } michael@0: // The string "isom" or "iso2". michael@0: if (aData[4*i] == 0x69 && michael@0: aData[4*i+1] == 0x73 && michael@0: aData[4*i+2] == 0x6F && michael@0: (aData[4*i+3] == 0x6D || aData[4*i+3] == 0x32)) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static bool MatchesWebM(const uint8_t* aData, const uint32_t aLength) michael@0: { michael@0: #ifdef MOZ_WEBM michael@0: return nestegg_sniff((uint8_t*)aData, aLength) ? true : false; michael@0: #else michael@0: return false; michael@0: #endif michael@0: } michael@0: michael@0: // This function implements mp3 sniffing based on parsing michael@0: // packet headers and looking for expected boundaries. michael@0: static bool MatchesMP3(const uint8_t* aData, const uint32_t aLength) michael@0: { michael@0: return mp3_sniff(aData, (long)aLength); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsMediaSniffer::GetMIMETypeFromContent(nsIRequest* aRequest, michael@0: const uint8_t* aData, michael@0: const uint32_t aLength, michael@0: nsACString& aSniffedType) michael@0: { michael@0: // For media, we want to sniff only if the Content-Type is unknown, or if it michael@0: // is application/octet-stream. michael@0: nsCOMPtr channel = do_QueryInterface(aRequest); michael@0: if (channel) { michael@0: nsAutoCString contentType; michael@0: nsresult rv = channel->GetContentType(contentType); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (!contentType.IsEmpty() && michael@0: !contentType.EqualsLiteral(APPLICATION_OCTET_STREAM) && michael@0: !contentType.EqualsLiteral(UNKNOWN_CONTENT_TYPE)) { michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: } michael@0: michael@0: const uint32_t clampedLength = std::min(aLength, MAX_BYTES_SNIFFED); michael@0: michael@0: for (uint32_t i = 0; i < mozilla::ArrayLength(sSnifferEntries); ++i) { michael@0: const nsMediaSnifferEntry& currentEntry = sSnifferEntries[i]; michael@0: if (clampedLength < currentEntry.mLength || currentEntry.mLength == 0) { michael@0: continue; michael@0: } michael@0: bool matched = true; michael@0: for (uint32_t j = 0; j < currentEntry.mLength; ++j) { michael@0: if ((currentEntry.mMask[j] & aData[j]) != currentEntry.mPattern[j]) { michael@0: matched = false; michael@0: break; michael@0: } michael@0: } michael@0: if (matched) { michael@0: aSniffedType.AssignASCII(currentEntry.mContentType); michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: if (MatchesMP4(aData, clampedLength)) { michael@0: aSniffedType.AssignLiteral(VIDEO_MP4); michael@0: return NS_OK; michael@0: } michael@0: michael@0: if (MatchesWebM(aData, clampedLength)) { michael@0: aSniffedType.AssignLiteral(VIDEO_WEBM); michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Bug 950023: 512 bytes are often not enough to sniff for mp3. michael@0: if (MatchesMP3(aData, std::min(aLength, MAX_BYTES_SNIFFED_MP3))) { michael@0: aSniffedType.AssignLiteral(AUDIO_MP3); michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Could not sniff the media type, we are required to set it to michael@0: // application/octet-stream. michael@0: aSniffedType.AssignLiteral(APPLICATION_OCTET_STREAM); michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: }