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 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: #if !defined(DirectShowReader_h_) michael@0: #define DirectShowReader_h_ michael@0: michael@0: #include "windows.h" // HRESULT, DWORD michael@0: #include "MediaDecoderReader.h" michael@0: #include "mozilla/RefPtr.h" michael@0: #include "MP3FrameParser.h" michael@0: michael@0: class IGraphBuilder; michael@0: class IMediaControl; michael@0: class IMediaSeeking; michael@0: class IMediaEventEx; michael@0: michael@0: namespace mozilla { michael@0: michael@0: class AudioSinkFilter; michael@0: class SourceFilter; michael@0: michael@0: namespace dom { michael@0: class TimeRanges; michael@0: } michael@0: michael@0: // Decoder backend for decoding MP3 using DirectShow. DirectShow operates as michael@0: // a filter graph. The basic design of the DirectShowReader is that we have michael@0: // a SourceFilter that wraps the MediaResource that connects to the michael@0: // MP3 decoder filter. The MP3 decoder filter "pulls" data as it requires it michael@0: // downstream on its own thread. When the MP3 decoder has produced a block of michael@0: // decoded samples, its thread calls downstream into our AudioSinkFilter, michael@0: // passing the decoded buffer in. The AudioSinkFilter inserts the samples into michael@0: // a SampleSink object. The SampleSink blocks the MP3 decoder's thread until michael@0: // the decode thread calls DecodeAudioData(), whereupon the SampleSink michael@0: // releases the decoded samples to the decode thread, and unblocks the MP3 michael@0: // decoder's thread. The MP3 decoder can then request more data from the michael@0: // SourceFilter, and decode more data. If the decode thread calls michael@0: // DecodeAudioData() and there's no decoded samples waiting to be extracted michael@0: // in the SampleSink, the SampleSink blocks the decode thread until the MP3 michael@0: // decoder produces a decoded sample. michael@0: class DirectShowReader : public MediaDecoderReader michael@0: { michael@0: public: michael@0: DirectShowReader(AbstractMediaDecoder* aDecoder); michael@0: michael@0: virtual ~DirectShowReader(); michael@0: michael@0: nsresult Init(MediaDecoderReader* aCloneDonor) MOZ_OVERRIDE; michael@0: michael@0: bool DecodeAudioData() MOZ_OVERRIDE; michael@0: bool DecodeVideoFrame(bool &aKeyframeSkip, michael@0: int64_t aTimeThreshold) MOZ_OVERRIDE; michael@0: michael@0: bool HasAudio() MOZ_OVERRIDE; michael@0: bool HasVideo() MOZ_OVERRIDE; michael@0: michael@0: nsresult ReadMetadata(MediaInfo* aInfo, michael@0: MetadataTags** aTags) MOZ_OVERRIDE; michael@0: michael@0: nsresult Seek(int64_t aTime, michael@0: int64_t aStartTime, michael@0: int64_t aEndTime, michael@0: int64_t aCurrentTime) MOZ_OVERRIDE; michael@0: michael@0: void NotifyDataArrived(const char* aBuffer, michael@0: uint32_t aLength, michael@0: int64_t aOffset) MOZ_OVERRIDE; michael@0: michael@0: private: michael@0: michael@0: // Notifies the filter graph that playback is complete. aStatus is michael@0: // the code to send to the filter graph. Always returns false, so michael@0: // that we can just "return Finish()" from DecodeAudioData(). michael@0: bool Finish(HRESULT aStatus); michael@0: michael@0: // DirectShow filter graph, and associated playback and seeking michael@0: // control interfaces. michael@0: RefPtr mGraph; michael@0: RefPtr mControl; michael@0: RefPtr mMediaSeeking; michael@0: michael@0: // Wraps the MediaResource, and feeds undecoded data into the filter graph. michael@0: RefPtr mSourceFilter; michael@0: michael@0: // Sits at the end of the graph, removing decoded samples from the graph. michael@0: // The graph will block while this is blocked, i.e. it will pause decoding. michael@0: RefPtr mAudioSinkFilter; michael@0: michael@0: // Some MP3s are variable bitrate, so DirectShow's duration estimation michael@0: // can make its duration estimation based on the wrong bitrate. So we parse michael@0: // the MP3 frames to get a more accuate estimate of the duration. michael@0: MP3FrameParser mMP3FrameParser; michael@0: michael@0: #ifdef DEBUG michael@0: // Used to add/remove the filter graph to the Running Object Table. You can michael@0: // connect GraphEdit/GraphStudio to the graph to observe and/or debug its michael@0: // topology and state. michael@0: DWORD mRotRegister; michael@0: #endif michael@0: michael@0: // Number of channels in the audio stream. michael@0: uint32_t mNumChannels; michael@0: michael@0: // Samples per second in the audio stream. michael@0: uint32_t mAudioRate; michael@0: michael@0: // Number of bytes per sample. Can be either 1 or 2. michael@0: uint32_t mBytesPerSample; michael@0: michael@0: // Duration of the stream, in microseconds. michael@0: int64_t mDuration; michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif