2010-06-08 16:31:27 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is the Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Chris Double <chris.double@double.co.nz>
|
|
|
|
* Chris Pearce <chris@pearce.org.nz>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
#if !defined(nsWebMReader_h_)
|
|
|
|
#define nsWebMReader_h_
|
|
|
|
|
|
|
|
#include "nsDeque.h"
|
|
|
|
#include "nsBuiltinDecoderReader.h"
|
2010-09-13 01:45:50 -07:00
|
|
|
#include "nsWebMBufferedParser.h"
|
2010-10-13 18:50:20 -07:00
|
|
|
#include "nsAutoRef.h"
|
2010-06-08 16:31:27 -07:00
|
|
|
#include "nestegg/nestegg.h"
|
|
|
|
#include "vpx/vpx_decoder.h"
|
|
|
|
#include "vpx/vp8dx.h"
|
2010-10-18 19:55:45 -07:00
|
|
|
#ifdef MOZ_TREMOR
|
|
|
|
#include "tremor/ivorbiscodec.h"
|
|
|
|
#else
|
2010-06-08 16:31:27 -07:00
|
|
|
#include "vorbis/codec.h"
|
2010-10-18 19:55:45 -07:00
|
|
|
#endif
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
class nsMediaDecoder;
|
|
|
|
|
2010-11-28 12:06:38 -08:00
|
|
|
// Holds a nestegg_packet, and its file offset. This is needed so we
|
|
|
|
// know the offset in the file we've played up to, in order to calculate
|
|
|
|
// whether it's likely we can play through to the end without needing
|
|
|
|
// to stop to buffer, given the current download rate.
|
|
|
|
class NesteggPacketHolder {
|
|
|
|
public:
|
|
|
|
NesteggPacketHolder(nestegg_packet* aPacket, PRInt64 aOffset)
|
|
|
|
: mPacket(aPacket), mOffset(aOffset)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(NesteggPacketHolder);
|
|
|
|
}
|
|
|
|
~NesteggPacketHolder() {
|
|
|
|
MOZ_COUNT_DTOR(NesteggPacketHolder);
|
|
|
|
nestegg_free_packet(mPacket);
|
|
|
|
}
|
|
|
|
nestegg_packet* mPacket;
|
|
|
|
// Offset in bytes. This is the offset of the end of the Block
|
|
|
|
// which contains the packet.
|
|
|
|
PRInt64 mOffset;
|
|
|
|
private:
|
|
|
|
// Copy constructor and assignment operator not implemented. Don't use them!
|
|
|
|
NesteggPacketHolder(const NesteggPacketHolder &aOther);
|
|
|
|
NesteggPacketHolder& operator= (NesteggPacketHolder const& aOther);
|
|
|
|
};
|
|
|
|
|
2010-06-08 16:31:27 -07:00
|
|
|
// Thread and type safe wrapper around nsDeque.
|
|
|
|
class PacketQueueDeallocator : public nsDequeFunctor {
|
|
|
|
virtual void* operator() (void* anObject) {
|
2010-11-28 12:06:38 -08:00
|
|
|
delete static_cast<NesteggPacketHolder*>(anObject);
|
2010-06-08 16:31:27 -07:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Typesafe queue for holding nestegg packets. It has
|
|
|
|
// ownership of the items in the queue and will free them
|
|
|
|
// when destroyed.
|
|
|
|
class PacketQueue : private nsDeque {
|
|
|
|
public:
|
|
|
|
PacketQueue()
|
|
|
|
: nsDeque(new PacketQueueDeallocator())
|
|
|
|
{}
|
|
|
|
|
|
|
|
~PacketQueue() {
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline PRInt32 GetSize() {
|
|
|
|
return nsDeque::GetSize();
|
|
|
|
}
|
|
|
|
|
2010-11-28 12:06:38 -08:00
|
|
|
inline void Push(NesteggPacketHolder* aItem) {
|
2010-06-10 15:59:15 -07:00
|
|
|
NS_ASSERTION(aItem, "NULL pushed to PacketQueue");
|
2010-06-08 16:31:27 -07:00
|
|
|
nsDeque::Push(aItem);
|
|
|
|
}
|
|
|
|
|
2010-11-28 12:06:38 -08:00
|
|
|
inline void PushFront(NesteggPacketHolder* aItem) {
|
2010-06-10 15:59:15 -07:00
|
|
|
NS_ASSERTION(aItem, "NULL pushed to PacketQueue");
|
2010-05-30 21:02:00 -07:00
|
|
|
nsDeque::PushFront(aItem);
|
|
|
|
}
|
|
|
|
|
2010-11-28 12:06:38 -08:00
|
|
|
inline NesteggPacketHolder* PopFront() {
|
|
|
|
return static_cast<NesteggPacketHolder*>(nsDeque::PopFront());
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void Reset() {
|
|
|
|
while (GetSize() > 0) {
|
2010-11-28 12:06:38 -08:00
|
|
|
delete PopFront();
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class nsWebMReader : public nsBuiltinDecoderReader
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsWebMReader(nsBuiltinDecoder* aDecoder);
|
|
|
|
~nsWebMReader();
|
|
|
|
|
2010-09-20 17:49:50 -07:00
|
|
|
virtual nsresult Init(nsBuiltinDecoderReader* aCloneDonor);
|
2010-06-08 16:31:27 -07:00
|
|
|
virtual nsresult ResetDecode();
|
|
|
|
virtual PRBool DecodeAudioData();
|
|
|
|
|
|
|
|
// If the Theora granulepos has not been captured, it may read several packets
|
|
|
|
// until one with a granulepos has been captured, to ensure that all packets
|
|
|
|
// read have valid time info.
|
|
|
|
virtual PRBool DecodeVideoFrame(PRBool &aKeyframeSkip,
|
|
|
|
PRInt64 aTimeThreshold);
|
|
|
|
|
|
|
|
virtual PRBool HasAudio()
|
|
|
|
{
|
|
|
|
mozilla::MonitorAutoEnter mon(mMonitor);
|
|
|
|
return mHasAudio;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual PRBool HasVideo()
|
|
|
|
{
|
|
|
|
mozilla::MonitorAutoEnter mon(mMonitor);
|
|
|
|
return mHasVideo;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual nsresult ReadMetadata();
|
2010-08-19 15:50:37 -07:00
|
|
|
virtual nsresult Seek(PRInt64 aTime, PRInt64 aStartTime, PRInt64 aEndTime, PRInt64 aCurrentTime);
|
2010-08-25 01:43:00 -07:00
|
|
|
virtual nsresult GetBuffered(nsTimeRanges* aBuffered, PRInt64 aStartTime);
|
2010-09-13 01:45:50 -07:00
|
|
|
virtual void NotifyDataArrived(const char* aBuffer, PRUint32 aLength, PRUint32 aOffset);
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Value passed to NextPacket to determine if we are reading a video or an
|
|
|
|
// audio packet.
|
|
|
|
enum TrackType {
|
|
|
|
VIDEO = 0,
|
|
|
|
AUDIO = 1
|
|
|
|
};
|
|
|
|
|
|
|
|
// Read a packet from the nestegg file. Returns NULL if all packets for
|
|
|
|
// the particular track have been read. Pass VIDEO or AUDIO to indicate the
|
|
|
|
// type of the packet we want to read.
|
2010-11-28 12:06:38 -08:00
|
|
|
nsReturnRef<NesteggPacketHolder> NextPacket(TrackType aTrackType);
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
// Returns an initialized ogg packet with data obtained from the WebM container.
|
|
|
|
ogg_packet InitOggPacket(unsigned char* aData,
|
|
|
|
size_t aLength,
|
|
|
|
PRBool aBOS,
|
|
|
|
PRBool aEOS,
|
|
|
|
PRInt64 aGranulepos);
|
|
|
|
|
|
|
|
// Decode a nestegg packet of audio data. Push the audio data on the
|
|
|
|
// audio queue. Returns PR_TRUE when there's more audio to decode,
|
|
|
|
// PR_FALSE if the audio is finished, end of file has been reached,
|
|
|
|
// or an un-recoverable read error has occured. The reader's monitor
|
|
|
|
// must be held during this call. This function will free the packet
|
|
|
|
// so the caller must not use the packet after calling.
|
2010-11-28 12:06:38 -08:00
|
|
|
PRBool DecodeAudioPacket(nestegg_packet* aPacket, PRInt64 aOffset);
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
// Release context and set to null. Called when an error occurs during
|
|
|
|
// reading metadata or destruction of the reader itself.
|
|
|
|
void Cleanup();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// libnestegg context for webm container. Access on state machine thread
|
|
|
|
// or decoder thread only.
|
|
|
|
nestegg* mContext;
|
|
|
|
|
|
|
|
// VP8 decoder state
|
|
|
|
vpx_codec_ctx_t mVP8;
|
|
|
|
|
|
|
|
// Vorbis decoder state
|
|
|
|
vorbis_info mVorbisInfo;
|
|
|
|
vorbis_comment mVorbisComment;
|
|
|
|
vorbis_dsp_state mVorbisDsp;
|
|
|
|
vorbis_block mVorbisBlock;
|
|
|
|
PRUint32 mPacketCount;
|
|
|
|
PRUint32 mChannels;
|
|
|
|
|
|
|
|
// Queue of video and audio packets that have been read but not decoded. These
|
|
|
|
// must only be accessed from the state machine thread.
|
|
|
|
PacketQueue mVideoPackets;
|
|
|
|
PacketQueue mAudioPackets;
|
|
|
|
|
|
|
|
// Index of video and audio track to play
|
|
|
|
PRUint32 mVideoTrack;
|
|
|
|
PRUint32 mAudioTrack;
|
|
|
|
|
2010-08-12 19:28:15 -07:00
|
|
|
// Time in ms of the start of the first audio sample we've decoded.
|
|
|
|
PRInt64 mAudioStartMs;
|
|
|
|
|
|
|
|
// Number of samples we've decoded since decoding began at mAudioStartMs.
|
|
|
|
PRUint64 mAudioSamples;
|
|
|
|
|
2010-09-20 17:49:50 -07:00
|
|
|
// Parser state and computed offset-time mappings. Shared by multiple
|
|
|
|
// readers when decoder has been cloned. Main thread only.
|
|
|
|
nsRefPtr<nsWebMBufferedState> mBufferedState;
|
2010-09-13 01:45:50 -07:00
|
|
|
|
2010-06-08 16:31:27 -07:00
|
|
|
// Booleans to indicate if we have audio and/or video data
|
|
|
|
PRPackedBool mHasVideo;
|
|
|
|
PRPackedBool mHasAudio;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|