content/media/MP3FrameParser.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     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 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #ifndef MP3FrameParser_h
     8 #define MP3FrameParser_h
    10 #include <stdint.h>
    12 #include "mozilla/Mutex.h"
    13 #include "nsString.h"
    15 namespace mozilla {
    17 // Simple parser to tell whether we've found an ID3 header and how long it is,
    18 // so that we can skip it.
    19 // XXX maybe actually parse this stuff?
    20 class ID3Parser
    21 {
    22 public:
    23   ID3Parser();
    25   void Reset();
    26   bool ParseChar(char ch);
    27   bool IsParsed() const;
    28   uint32_t GetHeaderLength() const;
    30 private:
    31   uint32_t mCurrentChar;
    32   uint32_t mHeaderLength;
    33 };
    35 struct MP3Frame {
    36   uint16_t mSync1 : 8;      // Always all set
    37   uint16_t mProtected : 1;  // Ignored
    38   uint16_t mLayer : 2;
    39   uint16_t mVersion : 2;
    40   uint16_t mSync2 : 3;      // Always all set
    41   uint16_t mPrivate : 1;    // Ignored
    42   uint16_t mPad : 1;
    43   uint16_t mSampleRate : 2; // Index into mpeg_srates above
    44   uint16_t mBitrate : 4;    // Index into mpeg_bitrates above
    46   uint16_t CalculateLength();
    47 };
    49 // Buffering parser for MP3 frames.
    50 class MP3Parser
    51 {
    52 public:
    53   MP3Parser();
    55   // Forget all data the parser has seen so far.
    56   void Reset();
    58   // Parse the given byte. If we have found a frame header, return the length of
    59   // the frame.
    60   uint16_t ParseFrameLength(uint8_t ch);
    62   // Get the sample rate from the current header.
    63   uint32_t GetSampleRate();
    65   // Get the number of samples per frame.
    66   uint32_t GetSamplesPerFrame();
    68 private:
    69   uint32_t mCurrentChar;
    70   union {
    71     uint8_t mRaw[3];
    72     MP3Frame mFrame;
    73   } mData;
    74 };
    77 // A description of the MP3 format and its extensions is available at
    78 //
    79 //  http://www.codeproject.com/Articles/8295/MPEG-Audio-Frame-Header
    80 //
    81 // The data in MP3 streams is split into small frames, with each frame
    82 // containing a fixed number of samples. The duration of a frame depends
    83 // on the frame's bit rate and sample rate. Both values can vary among
    84 // frames, so it is necessary to examine each individual frame of an MP3
    85 // stream to calculate the stream's overall duration.
    86 //
    87 // The MP3 frame parser extracts information from an MP3 data stream. It
    88 // accepts a range of frames of an MP3 stream as input, and parses all
    89 // frames for their duration. Callers can query the stream's overall
    90 // duration from the parser.
    91 //
    92 // Call the methods NotifyDataArrived or Parse to add new data. If you added
    93 // information for a certain stream position, you cannot go back to previous
    94 // positions. The parser will simply ignore the input. If you skip stream
    95 // positions, the duration of the related MP3 frames will be estimated from
    96 // the stream's average.
    97 //
    98 // The method GetDuration returns calculated duration of the stream, including
    99 // estimates for skipped ranges.
   100 //
   101 // All public methods are thread-safe.
   103 class MP3FrameParser
   104 {
   105 public:
   106   MP3FrameParser(int64_t aLength=-1);
   108   bool IsMP3() {
   109     MutexAutoLock mon(mLock);
   110     return mIsMP3 != NOT_MP3;
   111   }
   113   void Parse(const char* aBuffer, uint32_t aLength, uint64_t aStreamOffset);
   115   // Returns the duration, in microseconds. If the entire stream has not
   116   // been parsed yet, this is an estimate based on the bitrate of the
   117   // frames parsed so far.
   118   int64_t GetDuration();
   120   // Returns the offset of the first MP3 frame in the stream, or -1 of
   121   // no MP3 frame has been detected yet.
   122   int64_t GetMP3Offset();
   124   // Returns true if we've seen the whole first frame of the MP3 stream, and
   125   // therefore can make an estimate on the stream duration.
   126   // Otherwise, returns false.
   127   bool ParsedHeaders();
   129   // Returns true if we know the exact duration of the MP3 stream;
   130   // false otherwise.
   131   bool HasExactDuration();
   133   // Returns true if the parser needs more data for duration estimation.
   134   bool NeedsData();
   136 private:
   138   // Parses aBuffer, starting at offset 0. Returns the number of bytes
   139   // parsed, relative to the start of the buffer. Note this may be
   140   // greater than aLength if the headers in the buffer indicate that
   141   // the frame or ID3 tag extends outside of aBuffer. Returns failure
   142   // if too many non-MP3 bytes are parsed.
   143   nsresult ParseBuffer(const uint8_t* aBuffer,
   144                        uint32_t aLength,
   145                        int64_t aStreamOffset,
   146                        uint32_t* aOutBytesRead);
   148   // A low-contention lock for protecting the parser results
   149   Mutex mLock;
   151   // ID3 header parser. Keeps state between reads in case the header falls
   152   // in between.
   153   ID3Parser mID3Parser;
   155   // MP3 frame header parser.
   156   MP3Parser mMP3Parser;
   158   // If we read |MAX_SKIPPED_BYTES| from the stream without finding any MP3
   159   // frames, we give up and report |NOT_MP3|. Here we track the cumulative size
   160   // of any ID3 headers we've seen so big ID3 sections aren't counted towards
   161   // skipped bytes.
   162   uint32_t mTotalID3Size;
   164   // All fields below are protected by mLock
   166   // We keep stats on the size of all the frames we've seen, as well as how many
   167   // so that we can estimate the duration of the rest of the stream.
   168   uint64_t mTotalFrameSize;
   169   uint64_t mFrameCount;
   171   // Offset of the last data parsed. This is the end offset of the last data
   172   // block parsed, so it's the start offset we expect to get on the next
   173   // call to Parse().
   174   uint64_t mOffset;
   176   // Total length of the stream in bytes.
   177   int64_t mLength;
   179   // Offset of first MP3 frame in the bitstream. Has value -1 until the
   180   // first MP3 frame is found.
   181   int64_t mMP3Offset;
   183   // The exact number of frames in this stream, if we know it. -1 otherwise.
   184   int64_t mNumFrames;
   186   // Number of audio samples per second and per frame. Fixed through the whole
   187   // file. If we know these variables as well as the number of frames in the
   188   // file, we can get an exact duration for the stream.
   189   uint16_t mSamplesPerSecond;
   190   uint16_t mSamplesPerFrame;
   192   // If the MP3 has a variable bitrate, then there *should* be metadata about
   193   // the encoding in the first frame. We buffer the first frame here.
   194   nsAutoCString mFirstFrame;
   196   // While we are reading the first frame, this is the stream offset of the
   197   // last byte of that frame. -1 at all other times.
   198   int64_t mFirstFrameEnd;
   200   enum eIsMP3 {
   201     MAYBE_MP3, // We're giving the stream the benefit of the doubt...
   202     DEFINITELY_MP3, // We've hit at least one ID3 tag or MP3 frame.
   203     NOT_MP3 // Not found any evidence of the stream being MP3.
   204   };
   206   eIsMP3 mIsMP3;
   208 };
   210 }
   212 #endif

mercurial