toolkit/components/mediasniffer/nsMediaSniffer.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/mediasniffer/nsMediaSniffer.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,159 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 tw=80 et cindent: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "nsMediaSniffer.h"
    1.11 +#include "nsIHttpChannel.h"
    1.12 +#include "nsString.h"
    1.13 +#include "nsMimeTypes.h"
    1.14 +#include "mozilla/ArrayUtils.h"
    1.15 +#include "mozilla/ModuleUtils.h"
    1.16 +#include "mp3sniff.h"
    1.17 +#ifdef MOZ_WEBM
    1.18 +#include "nestegg/nestegg.h"
    1.19 +#endif
    1.20 +
    1.21 +#include "nsIClassInfoImpl.h"
    1.22 +#include <algorithm>
    1.23 +
    1.24 +// The minimum number of bytes that are needed to attempt to sniff an mp4 file.
    1.25 +static const unsigned MP4_MIN_BYTES_COUNT = 12;
    1.26 +// The maximum number of bytes to consider when attempting to sniff a file.
    1.27 +static const uint32_t MAX_BYTES_SNIFFED = 512;
    1.28 +// The maximum number of bytes to consider when attempting to sniff for a mp3
    1.29 +// bitstream.
    1.30 +// This is 320kbps * 144 / 32kHz + 1 padding byte + 4 bytes of capture pattern.
    1.31 +static const uint32_t MAX_BYTES_SNIFFED_MP3 = 320 * 144 / 32 + 1 + 4;
    1.32 +
    1.33 +NS_IMPL_ISUPPORTS(nsMediaSniffer, nsIContentSniffer)
    1.34 +
    1.35 +nsMediaSniffer::nsMediaSnifferEntry nsMediaSniffer::sSnifferEntries[] = {
    1.36 +  // The string OggS, followed by the null byte.
    1.37 +  PATTERN_ENTRY("\xFF\xFF\xFF\xFF\xFF", "OggS", APPLICATION_OGG),
    1.38 +  // The string RIFF, followed by four bytes, followed by the string WAVE
    1.39 +  PATTERN_ENTRY("\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF", "RIFF\x00\x00\x00\x00WAVE", AUDIO_WAV),
    1.40 +  // mp3 with ID3 tags, the string "ID3".
    1.41 +  PATTERN_ENTRY("\xFF\xFF\xFF", "ID3", AUDIO_MP3)
    1.42 +};
    1.43 +
    1.44 +// This function implements mp4 sniffing algorithm, described at
    1.45 +// http://mimesniff.spec.whatwg.org/#signature-for-mp4
    1.46 +static bool MatchesMP4(const uint8_t* aData, const uint32_t aLength)
    1.47 +{
    1.48 +  if (aLength <= MP4_MIN_BYTES_COUNT) {
    1.49 +    return false;
    1.50 +  }
    1.51 +  // Conversion from big endian to host byte order.
    1.52 +  uint32_t boxSize = (uint32_t)(aData[3] | aData[2] << 8 | aData[1] << 16 | aData[0] << 24);
    1.53 +
    1.54 +  // Boxsize should be evenly divisible by 4.
    1.55 +  if (boxSize % 4 || aLength < boxSize) {
    1.56 +    return false;
    1.57 +  }
    1.58 +  // The string "ftyp".
    1.59 +  if (aData[4] != 0x66 ||
    1.60 +      aData[5] != 0x74 ||
    1.61 +      aData[6] != 0x79 ||
    1.62 +      aData[7] != 0x70) {
    1.63 +    return false;
    1.64 +  }
    1.65 +  for (uint32_t i = 2; i <= boxSize / 4 - 1 ; i++) {
    1.66 +    if (i == 3) {
    1.67 +      continue;
    1.68 +    }
    1.69 +    // The string "mp42" or "mp41".
    1.70 +    if (aData[4*i]   == 0x6D &&
    1.71 +        aData[4*i+1] == 0x70 &&
    1.72 +        aData[4*i+2] == 0x34) {
    1.73 +      return true;
    1.74 +    }
    1.75 +    // The string "isom" or "iso2".
    1.76 +    if (aData[4*i]   == 0x69 &&
    1.77 +        aData[4*i+1] == 0x73 &&
    1.78 +        aData[4*i+2] == 0x6F &&
    1.79 +        (aData[4*i+3] == 0x6D || aData[4*i+3] == 0x32)) {
    1.80 +      return true;
    1.81 +    }
    1.82 +  }
    1.83 +  return false;
    1.84 +}
    1.85 +
    1.86 +static bool MatchesWebM(const uint8_t* aData, const uint32_t aLength)
    1.87 +{
    1.88 +#ifdef MOZ_WEBM
    1.89 +  return nestegg_sniff((uint8_t*)aData, aLength) ? true : false;
    1.90 +#else
    1.91 +  return false;
    1.92 +#endif
    1.93 +}
    1.94 +
    1.95 +// This function implements mp3 sniffing based on parsing
    1.96 +// packet headers and looking for expected boundaries.
    1.97 +static bool MatchesMP3(const uint8_t* aData, const uint32_t aLength)
    1.98 +{
    1.99 +  return mp3_sniff(aData, (long)aLength);
   1.100 +}
   1.101 +
   1.102 +NS_IMETHODIMP
   1.103 +nsMediaSniffer::GetMIMETypeFromContent(nsIRequest* aRequest,
   1.104 +                                       const uint8_t* aData,
   1.105 +                                       const uint32_t aLength,
   1.106 +                                       nsACString& aSniffedType)
   1.107 +{
   1.108 +  // For media, we want to sniff only if the Content-Type is unknown, or if it
   1.109 +  // is application/octet-stream.
   1.110 +  nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
   1.111 +  if (channel) {
   1.112 +    nsAutoCString contentType;
   1.113 +    nsresult rv = channel->GetContentType(contentType);
   1.114 +    NS_ENSURE_SUCCESS(rv, rv);
   1.115 +    if (!contentType.IsEmpty() &&
   1.116 +        !contentType.EqualsLiteral(APPLICATION_OCTET_STREAM) &&
   1.117 +        !contentType.EqualsLiteral(UNKNOWN_CONTENT_TYPE)) {
   1.118 +      return NS_ERROR_NOT_AVAILABLE;
   1.119 +    }
   1.120 +  }
   1.121 +
   1.122 +  const uint32_t clampedLength = std::min(aLength, MAX_BYTES_SNIFFED);
   1.123 +
   1.124 +  for (uint32_t i = 0; i < mozilla::ArrayLength(sSnifferEntries); ++i) {
   1.125 +    const nsMediaSnifferEntry& currentEntry = sSnifferEntries[i];
   1.126 +    if (clampedLength < currentEntry.mLength || currentEntry.mLength == 0) {
   1.127 +      continue;
   1.128 +    }
   1.129 +    bool matched = true;
   1.130 +    for (uint32_t j = 0; j < currentEntry.mLength; ++j) {
   1.131 +      if ((currentEntry.mMask[j] & aData[j]) != currentEntry.mPattern[j]) {
   1.132 +        matched = false;
   1.133 +        break;
   1.134 +      }
   1.135 +    }
   1.136 +    if (matched) {
   1.137 +      aSniffedType.AssignASCII(currentEntry.mContentType);
   1.138 +      return NS_OK;
   1.139 +    }
   1.140 +  }
   1.141 +
   1.142 +  if (MatchesMP4(aData, clampedLength)) {
   1.143 +    aSniffedType.AssignLiteral(VIDEO_MP4);
   1.144 +    return NS_OK;
   1.145 +  }
   1.146 +
   1.147 +  if (MatchesWebM(aData, clampedLength)) {
   1.148 +    aSniffedType.AssignLiteral(VIDEO_WEBM);
   1.149 +    return NS_OK;
   1.150 +  }
   1.151 +
   1.152 +  // Bug 950023: 512 bytes are often not enough to sniff for mp3.
   1.153 +  if (MatchesMP3(aData, std::min(aLength, MAX_BYTES_SNIFFED_MP3))) {
   1.154 +    aSniffedType.AssignLiteral(AUDIO_MP3);
   1.155 +    return NS_OK;
   1.156 +  }
   1.157 +
   1.158 +  // Could not sniff the media type, we are required to set it to
   1.159 +  // application/octet-stream.
   1.160 +  aSniffedType.AssignLiteral(APPLICATION_OCTET_STREAM);
   1.161 +  return NS_ERROR_NOT_AVAILABLE;
   1.162 +}

mercurial