LCOV - code coverage report
Current view: directory - content/media/wave - nsWaveReader.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 224 0 0.0 %
Date: 2012-06-02 Functions: 25 0 0.0 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2                 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
       3                 : /* ***** BEGIN LICENSE BLOCK *****
       4                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       5                 :  *
       6                 :  * The contents of this file are subject to the Mozilla Public License Version
       7                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       8                 :  * the License. You may obtain a copy of the License at
       9                 :  * http://www.mozilla.org/MPL/
      10                 :  *
      11                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      12                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      13                 :  * for the specific language governing rights and limitations under the
      14                 :  * License.
      15                 :  *
      16                 :  * The Original Code is Mozilla code.
      17                 :  *
      18                 :  * The Initial Developer of the Original Code is the Mozilla Foundation.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 2010
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *  Matthew Gregan <kinetik@flim.org>
      24                 :  *
      25                 :  * Alternatively, the contents of this file may be used under the terms of
      26                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      27                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      28                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      29                 :  * of those above. If you wish to allow use of your version of this file only
      30                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      31                 :  * use your version of this file under the terms of the MPL, indicate your
      32                 :  * decision by deleting the provisions above and replace them with the notice
      33                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      34                 :  * the provisions above, a recipient may use your version of this file under
      35                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      36                 :  *
      37                 :  * ***** END LICENSE BLOCK ***** */
      38                 : #include "nsError.h"
      39                 : #include "nsBuiltinDecoder.h"
      40                 : #include "MediaResource.h"
      41                 : #include "nsWaveReader.h"
      42                 : #include "nsTimeRanges.h"
      43                 : #include "VideoUtils.h"
      44                 : 
      45                 : #include "mozilla/StandardInteger.h"
      46                 : 
      47                 : using namespace mozilla;
      48                 : 
      49                 : // Un-comment to enable logging of seek bisections.
      50                 : //#define SEEK_LOGGING
      51                 : 
      52                 : #ifdef PR_LOGGING
      53                 : extern PRLogModuleInfo* gBuiltinDecoderLog;
      54                 : #define LOG(type, msg) PR_LOG(gBuiltinDecoderLog, type, msg)
      55                 : #ifdef SEEK_LOGGING
      56                 : #define SEEK_LOG(type, msg) PR_LOG(gBuiltinDecoderLog, type, msg)
      57                 : #else
      58                 : #define SEEK_LOG(type, msg)
      59                 : #endif
      60                 : #else
      61                 : #define LOG(type, msg)
      62                 : #define SEEK_LOG(type, msg)
      63                 : #endif
      64                 : 
      65                 : // Magic values that identify RIFF chunks we're interested in.
      66                 : static const PRUint32 RIFF_CHUNK_MAGIC = 0x52494646;
      67                 : static const PRUint32 WAVE_CHUNK_MAGIC = 0x57415645;
      68                 : static const PRUint32 FRMT_CHUNK_MAGIC = 0x666d7420;
      69                 : static const PRUint32 DATA_CHUNK_MAGIC = 0x64617461;
      70                 : 
      71                 : // Size of RIFF chunk header.  4 byte chunk header type and 4 byte size field.
      72                 : static const PRUint16 RIFF_CHUNK_HEADER_SIZE = 8;
      73                 : 
      74                 : // Size of RIFF header.  RIFF chunk and 4 byte RIFF type.
      75                 : static const PRUint16 RIFF_INITIAL_SIZE = RIFF_CHUNK_HEADER_SIZE + 4;
      76                 : 
      77                 : // Size of required part of format chunk.  Actual format chunks may be
      78                 : // extended (for non-PCM encodings), but we skip any extended data.
      79                 : static const PRUint16 WAVE_FORMAT_CHUNK_SIZE = 16;
      80                 : 
      81                 : // PCM encoding type from format chunk.  Linear PCM is the only encoding
      82                 : // supported by nsAudioStream.
      83                 : static const PRUint16 WAVE_FORMAT_ENCODING_PCM = 1;
      84                 : 
      85                 : // Maximum number of channels supported
      86                 : static const PRUint8 MAX_CHANNELS = 2;
      87                 : 
      88                 : namespace {
      89                 :   PRUint32
      90               0 :   ReadUint32BE(const char** aBuffer)
      91                 :   {
      92                 :     PRUint32 result =
      93               0 :       PRUint8((*aBuffer)[0]) << 24 |
      94               0 :       PRUint8((*aBuffer)[1]) << 16 |
      95               0 :       PRUint8((*aBuffer)[2]) << 8 |
      96               0 :       PRUint8((*aBuffer)[3]);
      97               0 :     *aBuffer += sizeof(PRUint32);
      98               0 :     return result;
      99                 :   }
     100                 : 
     101                 :   PRUint32
     102               0 :   ReadUint32LE(const char** aBuffer)
     103                 :   {
     104                 :     PRUint32 result =
     105               0 :       PRUint8((*aBuffer)[3]) << 24 |
     106               0 :       PRUint8((*aBuffer)[2]) << 16 |
     107               0 :       PRUint8((*aBuffer)[1]) << 8 |
     108               0 :       PRUint8((*aBuffer)[0]);
     109               0 :     *aBuffer += sizeof(PRUint32);
     110               0 :     return result;
     111                 :   }
     112                 : 
     113                 :   PRUint16
     114               0 :   ReadUint16LE(const char** aBuffer)
     115                 :   {
     116                 :     PRUint16 result =
     117               0 :       PRUint8((*aBuffer)[1]) << 8 |
     118               0 :       PRUint8((*aBuffer)[0]) << 0;
     119               0 :     *aBuffer += sizeof(PRUint16);
     120               0 :     return result;
     121                 :   }
     122                 : 
     123                 :   PRInt16
     124               0 :   ReadInt16LE(const char** aBuffer)
     125                 :   {
     126               0 :     return static_cast<PRInt16>(ReadUint16LE(aBuffer));
     127                 :   }
     128                 : 
     129                 :   PRUint8
     130               0 :   ReadUint8(const char** aBuffer)
     131                 :   {
     132               0 :     PRUint8 result = PRUint8((*aBuffer)[0]);
     133               0 :     *aBuffer += sizeof(PRUint8);
     134               0 :     return result;
     135                 :   }
     136                 : }
     137                 : 
     138               0 : nsWaveReader::nsWaveReader(nsBuiltinDecoder* aDecoder)
     139               0 :   : nsBuiltinDecoderReader(aDecoder)
     140                 : {
     141               0 :   MOZ_COUNT_CTOR(nsWaveReader);
     142               0 : }
     143                 : 
     144               0 : nsWaveReader::~nsWaveReader()
     145                 : {
     146               0 :   MOZ_COUNT_DTOR(nsWaveReader);
     147               0 : }
     148                 : 
     149               0 : nsresult nsWaveReader::Init(nsBuiltinDecoderReader* aCloneDonor)
     150                 : {
     151               0 :   return NS_OK;
     152                 : }
     153                 : 
     154               0 : nsresult nsWaveReader::ReadMetadata(nsVideoInfo* aInfo)
     155                 : {
     156               0 :   NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
     157                 : 
     158               0 :   bool loaded = LoadRIFFChunk() && LoadFormatChunk() && FindDataOffset();
     159               0 :   if (!loaded) {
     160               0 :     return NS_ERROR_FAILURE;
     161                 :   }
     162                 : 
     163               0 :   mInfo.mHasAudio = true;
     164               0 :   mInfo.mHasVideo = false;
     165               0 :   mInfo.mAudioRate = mSampleRate;
     166               0 :   mInfo.mAudioChannels = mChannels;
     167                 : 
     168               0 :   *aInfo = mInfo;
     169                 : 
     170               0 :   ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
     171                 : 
     172               0 :   mDecoder->GetStateMachine()->SetDuration(
     173               0 :     static_cast<PRInt64>(BytesToTime(GetDataLength()) * USECS_PER_S));
     174                 : 
     175               0 :   return NS_OK;
     176                 : }
     177                 : 
     178               0 : bool nsWaveReader::DecodeAudioData()
     179                 : {
     180               0 :   NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
     181                 : 
     182               0 :   PRInt64 pos = GetPosition() - mWavePCMOffset;
     183               0 :   PRInt64 len = GetDataLength();
     184               0 :   PRInt64 remaining = len - pos;
     185               0 :   NS_ASSERTION(remaining >= 0, "Current wave position is greater than wave file length");
     186                 : 
     187                 :   static const PRInt64 BLOCK_SIZE = 4096;
     188               0 :   PRInt64 readSize = NS_MIN(BLOCK_SIZE, remaining);
     189               0 :   PRInt64 frames = readSize / mFrameSize;
     190                 : 
     191                 :   PR_STATIC_ASSERT(PRUint64(BLOCK_SIZE) < UINT_MAX / sizeof(AudioDataValue) / MAX_CHANNELS);
     192               0 :   const size_t bufferSize = static_cast<size_t>(frames * mChannels);
     193               0 :   nsAutoArrayPtr<AudioDataValue> sampleBuffer(new AudioDataValue[bufferSize]);
     194                 : 
     195                 :   PR_STATIC_ASSERT(PRUint64(BLOCK_SIZE) < UINT_MAX / sizeof(char));
     196               0 :   nsAutoArrayPtr<char> dataBuffer(new char[static_cast<size_t>(readSize)]);
     197                 : 
     198               0 :   if (!ReadAll(dataBuffer, readSize)) {
     199               0 :     mAudioQueue.Finish();
     200               0 :     return false;
     201                 :   }
     202                 : 
     203                 :   // convert data to samples
     204               0 :   const char* d = dataBuffer.get();
     205               0 :   AudioDataValue* s = sampleBuffer.get();
     206               0 :   for (int i = 0; i < frames; ++i) {
     207               0 :     for (unsigned int j = 0; j < mChannels; ++j) {
     208               0 :       if (mSampleFormat == nsAudioStream::FORMAT_U8) {
     209               0 :         PRUint8 v =  ReadUint8(&d);
     210                 : #if defined(MOZ_SAMPLE_TYPE_S16LE)
     211                 :         *s++ = (v * (1.F/PR_UINT8_MAX)) * PR_UINT16_MAX + PR_INT16_MIN;
     212                 : #elif defined(MOZ_SAMPLE_TYPE_FLOAT32)
     213               0 :         *s++ = (v * (1.F/PR_UINT8_MAX)) * 2.F - 1.F;
     214                 : #endif
     215                 :       }
     216               0 :       else if (mSampleFormat == nsAudioStream::FORMAT_S16_LE) {
     217               0 :         PRInt16 v =  ReadInt16LE(&d);
     218                 : #if defined(MOZ_SAMPLE_TYPE_S16LE)
     219                 :         *s++ = v;
     220                 : #elif defined(MOZ_SAMPLE_TYPE_FLOAT32)
     221               0 :         *s++ = (PRInt32(v) - PR_INT16_MIN) / float(PR_UINT16_MAX) * 2.F - 1.F;
     222                 : #endif
     223                 :       }
     224                 :     }
     225                 :   }
     226                 : 
     227               0 :   double posTime = BytesToTime(pos);
     228               0 :   double readSizeTime = BytesToTime(readSize);
     229               0 :   NS_ASSERTION(posTime <= INT64_MAX / USECS_PER_S, "posTime overflow");
     230               0 :   NS_ASSERTION(readSizeTime <= INT64_MAX / USECS_PER_S, "readSizeTime overflow");
     231               0 :   NS_ASSERTION(frames < PR_INT32_MAX, "frames overflow");
     232                 : 
     233                 :   mAudioQueue.Push(new AudioData(pos,
     234                 :                                  static_cast<PRInt64>(posTime * USECS_PER_S),
     235                 :                                  static_cast<PRInt64>(readSizeTime * USECS_PER_S),
     236                 :                                  static_cast<PRInt32>(frames),
     237                 :                                  sampleBuffer.forget(),
     238               0 :                                  mChannels));
     239                 : 
     240               0 :   return true;
     241                 : }
     242                 : 
     243               0 : bool nsWaveReader::DecodeVideoFrame(bool &aKeyframeSkip,
     244                 :                                       PRInt64 aTimeThreshold)
     245                 : {
     246               0 :   NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
     247                 : 
     248               0 :   return false;
     249                 : }
     250                 : 
     251               0 : nsresult nsWaveReader::Seek(PRInt64 aTarget, PRInt64 aStartTime, PRInt64 aEndTime, PRInt64 aCurrentTime)
     252                 : {
     253               0 :   NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
     254               0 :   LOG(PR_LOG_DEBUG, ("%p About to seek to %lld", mDecoder, aTarget));
     255               0 :   if (NS_FAILED(ResetDecode())) {
     256               0 :     return NS_ERROR_FAILURE;
     257                 :   }
     258               0 :   double d = BytesToTime(GetDataLength());
     259               0 :   NS_ASSERTION(d < INT64_MAX / USECS_PER_S, "Duration overflow"); 
     260               0 :   PRInt64 duration = static_cast<PRInt64>(d * USECS_PER_S);
     261               0 :   double seekTime = NS_MIN(aTarget, duration) / static_cast<double>(USECS_PER_S);
     262               0 :   PRInt64 position = RoundDownToFrame(static_cast<PRInt64>(TimeToBytes(seekTime)));
     263               0 :   NS_ASSERTION(INT64_MAX - mWavePCMOffset > position, "Integer overflow during wave seek");
     264               0 :   position += mWavePCMOffset;
     265               0 :   return mDecoder->GetResource()->Seek(nsISeekableStream::NS_SEEK_SET, position);
     266                 : }
     267                 : 
     268               0 : static double RoundToUsecs(double aSeconds) {
     269               0 :   return floor(aSeconds * USECS_PER_S) / USECS_PER_S;
     270                 : }
     271                 : 
     272               0 : nsresult nsWaveReader::GetBuffered(nsTimeRanges* aBuffered, PRInt64 aStartTime)
     273                 : {
     274               0 :   PRInt64 startOffset = mDecoder->GetResource()->GetNextCachedData(mWavePCMOffset);
     275               0 :   while (startOffset >= 0) {
     276               0 :     PRInt64 endOffset = mDecoder->GetResource()->GetCachedDataEnd(startOffset);
     277                 :     // Bytes [startOffset..endOffset] are cached.
     278               0 :     NS_ASSERTION(startOffset >= mWavePCMOffset, "Integer underflow in GetBuffered");
     279               0 :     NS_ASSERTION(endOffset >= mWavePCMOffset, "Integer underflow in GetBuffered");
     280                 : 
     281                 :     // We need to round the buffered ranges' times to microseconds so that they
     282                 :     // have the same precision as the currentTime and duration attribute on 
     283                 :     // the media element.
     284                 :     aBuffered->Add(RoundToUsecs(BytesToTime(startOffset - mWavePCMOffset)),
     285               0 :                    RoundToUsecs(BytesToTime(endOffset - mWavePCMOffset)));
     286               0 :     startOffset = mDecoder->GetResource()->GetNextCachedData(endOffset);
     287                 :   }
     288               0 :   return NS_OK;
     289                 : }
     290                 : 
     291                 : bool
     292               0 : nsWaveReader::ReadAll(char* aBuf, PRInt64 aSize, PRInt64* aBytesRead)
     293                 : {
     294               0 :   PRUint32 got = 0;
     295               0 :   if (aBytesRead) {
     296               0 :     *aBytesRead = 0;
     297                 :   }
     298               0 :   do {
     299               0 :     PRUint32 read = 0;
     300               0 :     if (NS_FAILED(mDecoder->GetResource()->Read(aBuf + got, PRUint32(aSize - got), &read))) {
     301               0 :       NS_WARNING("Resource read failed");
     302               0 :       return false;
     303                 :     }
     304               0 :     if (read == 0) {
     305               0 :       return false;
     306                 :     }
     307               0 :     mDecoder->NotifyBytesConsumed(read);
     308               0 :     got += read;
     309               0 :     if (aBytesRead) {
     310               0 :       *aBytesRead = got;
     311                 :     }
     312                 :   } while (got != aSize);
     313               0 :   return true;
     314                 : }
     315                 : 
     316                 : bool
     317               0 : nsWaveReader::LoadRIFFChunk()
     318                 : {
     319                 :   char riffHeader[RIFF_INITIAL_SIZE];
     320               0 :   const char* p = riffHeader;
     321                 : 
     322               0 :   NS_ABORT_IF_FALSE(mDecoder->GetResource()->Tell() == 0,
     323                 :                     "LoadRIFFChunk called when resource in invalid state");
     324                 : 
     325               0 :   if (!ReadAll(riffHeader, sizeof(riffHeader))) {
     326               0 :     return false;
     327                 :   }
     328                 : 
     329                 :   PR_STATIC_ASSERT(sizeof(PRUint32) * 2 <= RIFF_INITIAL_SIZE);
     330               0 :   if (ReadUint32BE(&p) != RIFF_CHUNK_MAGIC) {
     331               0 :     NS_WARNING("resource data not in RIFF format");
     332               0 :     return false;
     333                 :   }
     334                 : 
     335                 :   // Skip over RIFF size field.
     336               0 :   p += 4;
     337                 : 
     338               0 :   if (ReadUint32BE(&p) != WAVE_CHUNK_MAGIC) {
     339               0 :     NS_WARNING("Expected WAVE chunk");
     340               0 :     return false;
     341                 :   }
     342                 : 
     343               0 :   return true;
     344                 : }
     345                 : 
     346                 : bool
     347               0 : nsWaveReader::ScanForwardUntil(PRUint32 aWantedChunk, PRUint32* aChunkSize)
     348                 : {
     349               0 :   NS_ABORT_IF_FALSE(aChunkSize, "Require aChunkSize argument");
     350               0 :   *aChunkSize = 0;
     351                 : 
     352               0 :   for (;;) {
     353                 :     static const unsigned int CHUNK_HEADER_SIZE = 8;
     354                 :     char chunkHeader[CHUNK_HEADER_SIZE];
     355               0 :     const char* p = chunkHeader;
     356                 : 
     357               0 :     if (!ReadAll(chunkHeader, sizeof(chunkHeader))) {
     358               0 :       return false;
     359                 :     }
     360                 : 
     361                 :     PR_STATIC_ASSERT(sizeof(PRUint32) * 2 <= CHUNK_HEADER_SIZE);
     362               0 :     PRUint32 magic = ReadUint32BE(&p);
     363               0 :     PRUint32 chunkSize = ReadUint32LE(&p);
     364                 : 
     365               0 :     if (magic == aWantedChunk) {
     366               0 :       *aChunkSize = chunkSize;
     367               0 :       return true;
     368                 :     }
     369                 : 
     370                 :     // RIFF chunks are two-byte aligned, so round up if necessary.
     371               0 :     chunkSize += chunkSize % 2;
     372                 : 
     373                 :     static const unsigned int MAX_CHUNK_SIZE = 1 << 16;
     374                 :     PR_STATIC_ASSERT(MAX_CHUNK_SIZE < UINT_MAX / sizeof(char));
     375               0 :     nsAutoArrayPtr<char> chunk(new char[MAX_CHUNK_SIZE]);
     376               0 :     while (chunkSize > 0) {
     377               0 :       PRUint32 size = NS_MIN(chunkSize, MAX_CHUNK_SIZE);
     378               0 :       if (!ReadAll(chunk.get(), size)) {
     379               0 :         return false;
     380                 :       }
     381               0 :       chunkSize -= size;
     382                 :     }
     383                 :   }
     384                 : }
     385                 : 
     386                 : bool
     387               0 : nsWaveReader::LoadFormatChunk()
     388                 : {
     389                 :   PRUint32 fmtSize, rate, channels, frameSize, sampleFormat;
     390                 :   char waveFormat[WAVE_FORMAT_CHUNK_SIZE];
     391               0 :   const char* p = waveFormat;
     392                 : 
     393                 :   // RIFF chunks are always word (two byte) aligned.
     394               0 :   NS_ABORT_IF_FALSE(mDecoder->GetResource()->Tell() % 2 == 0,
     395                 :                     "LoadFormatChunk called with unaligned resource");
     396                 : 
     397                 :   // The "format" chunk may not directly follow the "riff" chunk, so skip
     398                 :   // over any intermediate chunks.
     399               0 :   if (!ScanForwardUntil(FRMT_CHUNK_MAGIC, &fmtSize)) {
     400               0 :     return false;
     401                 :   }
     402                 : 
     403               0 :   if (!ReadAll(waveFormat, sizeof(waveFormat))) {
     404               0 :     return false;
     405                 :   }
     406                 : 
     407                 :   PR_STATIC_ASSERT(sizeof(PRUint16) +
     408                 :                    sizeof(PRUint16) +
     409                 :                    sizeof(PRUint32) +
     410                 :                    4 +
     411                 :                    sizeof(PRUint16) +
     412                 :                    sizeof(PRUint16) <= sizeof(waveFormat));
     413               0 :   if (ReadUint16LE(&p) != WAVE_FORMAT_ENCODING_PCM) {
     414               0 :     NS_WARNING("WAVE is not uncompressed PCM, compressed encodings are not supported");
     415               0 :     return false;
     416                 :   }
     417                 : 
     418               0 :   channels = ReadUint16LE(&p);
     419               0 :   rate = ReadUint32LE(&p);
     420                 : 
     421                 :   // Skip over average bytes per second field.
     422               0 :   p += 4;
     423                 : 
     424               0 :   frameSize = ReadUint16LE(&p);
     425                 : 
     426               0 :   sampleFormat = ReadUint16LE(&p);
     427                 : 
     428                 :   // PCM encoded WAVEs are not expected to have an extended "format" chunk,
     429                 :   // but I have found WAVEs that have a extended "format" chunk with an
     430                 :   // extension size of 0 bytes.  Be polite and handle this rather than
     431                 :   // considering the file invalid.  This code skips any extension of the
     432                 :   // "format" chunk.
     433               0 :   if (fmtSize > WAVE_FORMAT_CHUNK_SIZE) {
     434                 :     char extLength[2];
     435               0 :     const char* p = extLength;
     436                 : 
     437               0 :     if (!ReadAll(extLength, sizeof(extLength))) {
     438               0 :       return false;
     439                 :     }
     440                 : 
     441                 :     PR_STATIC_ASSERT(sizeof(PRUint16) <= sizeof(extLength));
     442               0 :     PRUint16 extra = ReadUint16LE(&p);
     443               0 :     if (fmtSize - (WAVE_FORMAT_CHUNK_SIZE + 2) != extra) {
     444               0 :       NS_WARNING("Invalid extended format chunk size");
     445               0 :       return false;
     446                 :     }
     447               0 :     extra += extra % 2;
     448                 : 
     449               0 :     if (extra > 0) {
     450                 :       PR_STATIC_ASSERT(PR_UINT16_MAX + (PR_UINT16_MAX % 2) < UINT_MAX / sizeof(char));
     451               0 :       nsAutoArrayPtr<char> chunkExtension(new char[extra]);
     452               0 :       if (!ReadAll(chunkExtension.get(), extra)) {
     453               0 :         return false;
     454                 :       }
     455                 :     }
     456                 :   }
     457                 : 
     458                 :   // RIFF chunks are always word (two byte) aligned.
     459               0 :   NS_ABORT_IF_FALSE(mDecoder->GetResource()->Tell() % 2 == 0,
     460                 :                     "LoadFormatChunk left resource unaligned");
     461                 : 
     462                 :   // Make sure metadata is fairly sane.  The rate check is fairly arbitrary,
     463                 :   // but the channels check is intentionally limited to mono or stereo
     464                 :   // because that's what the audio backend currently supports.
     465               0 :   if (rate < 100 || rate > 96000 ||
     466                 :       channels < 1 || channels > MAX_CHANNELS ||
     467                 :       (frameSize != 1 && frameSize != 2 && frameSize != 4) ||
     468                 :       (sampleFormat != 8 && sampleFormat != 16)) {
     469               0 :     NS_WARNING("Invalid WAVE metadata");
     470               0 :     return false;
     471                 :   }
     472                 : 
     473               0 :   ReentrantMonitorAutoEnter monitor(mDecoder->GetReentrantMonitor());
     474               0 :   mSampleRate = rate;
     475               0 :   mChannels = channels;
     476               0 :   mFrameSize = frameSize;
     477               0 :   if (sampleFormat == 8) {
     478               0 :     mSampleFormat = nsAudioStream::FORMAT_U8;
     479                 :   } else {
     480               0 :     mSampleFormat = nsAudioStream::FORMAT_S16_LE;
     481                 :   }
     482               0 :   return true;
     483                 : }
     484                 : 
     485                 : bool
     486               0 : nsWaveReader::FindDataOffset()
     487                 : {
     488                 :   // RIFF chunks are always word (two byte) aligned.
     489               0 :   NS_ABORT_IF_FALSE(mDecoder->GetResource()->Tell() % 2 == 0,
     490                 :                     "FindDataOffset called with unaligned resource");
     491                 : 
     492                 :   // The "data" chunk may not directly follow the "format" chunk, so skip
     493                 :   // over any intermediate chunks.
     494                 :   PRUint32 length;
     495               0 :   if (!ScanForwardUntil(DATA_CHUNK_MAGIC, &length)) {
     496               0 :     return false;
     497                 :   }
     498                 : 
     499               0 :   PRInt64 offset = mDecoder->GetResource()->Tell();
     500               0 :   if (offset <= 0 || offset > PR_UINT32_MAX) {
     501               0 :     NS_WARNING("PCM data offset out of range");
     502               0 :     return false;
     503                 :   }
     504                 : 
     505               0 :   ReentrantMonitorAutoEnter monitor(mDecoder->GetReentrantMonitor());
     506               0 :   mWaveLength = length;
     507               0 :   mWavePCMOffset = PRUint32(offset);
     508               0 :   return true;
     509                 : }
     510                 : 
     511                 : double
     512               0 : nsWaveReader::BytesToTime(PRInt64 aBytes) const
     513                 : {
     514               0 :   NS_ABORT_IF_FALSE(aBytes >= 0, "Must be >= 0");
     515               0 :   return float(aBytes) / mSampleRate / mFrameSize;
     516                 : }
     517                 : 
     518                 : PRInt64
     519               0 : nsWaveReader::TimeToBytes(double aTime) const
     520                 : {
     521               0 :   NS_ABORT_IF_FALSE(aTime >= 0.0f, "Must be >= 0");
     522               0 :   return RoundDownToFrame(PRInt64(aTime * mSampleRate * mFrameSize));
     523                 : }
     524                 : 
     525                 : PRInt64
     526               0 : nsWaveReader::RoundDownToFrame(PRInt64 aBytes) const
     527                 : {
     528               0 :   NS_ABORT_IF_FALSE(aBytes >= 0, "Must be >= 0");
     529               0 :   return aBytes - (aBytes % mFrameSize);
     530                 : }
     531                 : 
     532                 : PRInt64
     533               0 : nsWaveReader::GetDataLength()
     534                 : {
     535               0 :   PRInt64 length = mWaveLength;
     536                 :   // If the decoder has a valid content length, and it's shorter than the
     537                 :   // expected length of the PCM data, calculate the playback duration from
     538                 :   // the content length rather than the expected PCM data length.
     539               0 :   PRInt64 streamLength = mDecoder->GetResource()->GetLength();
     540               0 :   if (streamLength >= 0) {
     541               0 :     PRInt64 dataLength = NS_MAX<PRInt64>(0, streamLength - mWavePCMOffset);
     542               0 :     length = NS_MIN(dataLength, length);
     543                 :   }
     544               0 :   return length;
     545                 : }
     546                 : 
     547                 : PRInt64
     548               0 : nsWaveReader::GetPosition()
     549                 : {
     550               0 :   return mDecoder->GetResource()->Tell();
     551                 : }

Generated by: LCOV version 1.7