1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/directshow/DirectShowReader.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,119 @@ 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 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 +#if !defined(DirectShowReader_h_) 1.11 +#define DirectShowReader_h_ 1.12 + 1.13 +#include "windows.h" // HRESULT, DWORD 1.14 +#include "MediaDecoderReader.h" 1.15 +#include "mozilla/RefPtr.h" 1.16 +#include "MP3FrameParser.h" 1.17 + 1.18 +class IGraphBuilder; 1.19 +class IMediaControl; 1.20 +class IMediaSeeking; 1.21 +class IMediaEventEx; 1.22 + 1.23 +namespace mozilla { 1.24 + 1.25 +class AudioSinkFilter; 1.26 +class SourceFilter; 1.27 + 1.28 +namespace dom { 1.29 +class TimeRanges; 1.30 +} 1.31 + 1.32 +// Decoder backend for decoding MP3 using DirectShow. DirectShow operates as 1.33 +// a filter graph. The basic design of the DirectShowReader is that we have 1.34 +// a SourceFilter that wraps the MediaResource that connects to the 1.35 +// MP3 decoder filter. The MP3 decoder filter "pulls" data as it requires it 1.36 +// downstream on its own thread. When the MP3 decoder has produced a block of 1.37 +// decoded samples, its thread calls downstream into our AudioSinkFilter, 1.38 +// passing the decoded buffer in. The AudioSinkFilter inserts the samples into 1.39 +// a SampleSink object. The SampleSink blocks the MP3 decoder's thread until 1.40 +// the decode thread calls DecodeAudioData(), whereupon the SampleSink 1.41 +// releases the decoded samples to the decode thread, and unblocks the MP3 1.42 +// decoder's thread. The MP3 decoder can then request more data from the 1.43 +// SourceFilter, and decode more data. If the decode thread calls 1.44 +// DecodeAudioData() and there's no decoded samples waiting to be extracted 1.45 +// in the SampleSink, the SampleSink blocks the decode thread until the MP3 1.46 +// decoder produces a decoded sample. 1.47 +class DirectShowReader : public MediaDecoderReader 1.48 +{ 1.49 +public: 1.50 + DirectShowReader(AbstractMediaDecoder* aDecoder); 1.51 + 1.52 + virtual ~DirectShowReader(); 1.53 + 1.54 + nsresult Init(MediaDecoderReader* aCloneDonor) MOZ_OVERRIDE; 1.55 + 1.56 + bool DecodeAudioData() MOZ_OVERRIDE; 1.57 + bool DecodeVideoFrame(bool &aKeyframeSkip, 1.58 + int64_t aTimeThreshold) MOZ_OVERRIDE; 1.59 + 1.60 + bool HasAudio() MOZ_OVERRIDE; 1.61 + bool HasVideo() MOZ_OVERRIDE; 1.62 + 1.63 + nsresult ReadMetadata(MediaInfo* aInfo, 1.64 + MetadataTags** aTags) MOZ_OVERRIDE; 1.65 + 1.66 + nsresult Seek(int64_t aTime, 1.67 + int64_t aStartTime, 1.68 + int64_t aEndTime, 1.69 + int64_t aCurrentTime) MOZ_OVERRIDE; 1.70 + 1.71 + void NotifyDataArrived(const char* aBuffer, 1.72 + uint32_t aLength, 1.73 + int64_t aOffset) MOZ_OVERRIDE; 1.74 + 1.75 +private: 1.76 + 1.77 + // Notifies the filter graph that playback is complete. aStatus is 1.78 + // the code to send to the filter graph. Always returns false, so 1.79 + // that we can just "return Finish()" from DecodeAudioData(). 1.80 + bool Finish(HRESULT aStatus); 1.81 + 1.82 + // DirectShow filter graph, and associated playback and seeking 1.83 + // control interfaces. 1.84 + RefPtr<IGraphBuilder> mGraph; 1.85 + RefPtr<IMediaControl> mControl; 1.86 + RefPtr<IMediaSeeking> mMediaSeeking; 1.87 + 1.88 + // Wraps the MediaResource, and feeds undecoded data into the filter graph. 1.89 + RefPtr<SourceFilter> mSourceFilter; 1.90 + 1.91 + // Sits at the end of the graph, removing decoded samples from the graph. 1.92 + // The graph will block while this is blocked, i.e. it will pause decoding. 1.93 + RefPtr<AudioSinkFilter> mAudioSinkFilter; 1.94 + 1.95 + // Some MP3s are variable bitrate, so DirectShow's duration estimation 1.96 + // can make its duration estimation based on the wrong bitrate. So we parse 1.97 + // the MP3 frames to get a more accuate estimate of the duration. 1.98 + MP3FrameParser mMP3FrameParser; 1.99 + 1.100 +#ifdef DEBUG 1.101 + // Used to add/remove the filter graph to the Running Object Table. You can 1.102 + // connect GraphEdit/GraphStudio to the graph to observe and/or debug its 1.103 + // topology and state. 1.104 + DWORD mRotRegister; 1.105 +#endif 1.106 + 1.107 + // Number of channels in the audio stream. 1.108 + uint32_t mNumChannels; 1.109 + 1.110 + // Samples per second in the audio stream. 1.111 + uint32_t mAudioRate; 1.112 + 1.113 + // Number of bytes per sample. Can be either 1 or 2. 1.114 + uint32_t mBytesPerSample; 1.115 + 1.116 + // Duration of the stream, in microseconds. 1.117 + int64_t mDuration; 1.118 +}; 1.119 + 1.120 +} // namespace mozilla 1.121 + 1.122 +#endif