1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef MEDIA_FILTERS_DECODER_SELECTOR_H_ 6 #define MEDIA_FILTERS_DECODER_SELECTOR_H_ 7 8 #include "base/callback.h" 9 #include "base/memory/ref_counted.h" 10 #include "base/memory/scoped_vector.h" 11 #include "base/memory/weak_ptr.h" 12 #include "media/base/decryptor.h" 13 #include "media/base/demuxer_stream.h" 14 #include "media/base/pipeline_status.h" 15 #include "media/filters/decoder_stream_traits.h" 16 17 namespace base { 18 class SingleThreadTaskRunner; 19 } 20 21 namespace media { 22 23 class DecoderBuffer; 24 class DecryptingDemuxerStream; 25 class Decryptor; 26 27 // DecoderSelector (creates if necessary and) initializes the proper 28 // Decoder for a given DemuxerStream. If the given DemuxerStream is 29 // encrypted, a DecryptingDemuxerStream may also be created. 30 // The template parameter |StreamType| is the type of stream we will be 31 // selecting a decoder for. 32 template<DemuxerStream::Type StreamType> 33 class MEDIA_EXPORT DecoderSelector { 34 public: 35 typedef DecoderStreamTraits<StreamType> StreamTraits; 36 typedef typename StreamTraits::DecoderType Decoder; 37 38 // Indicates completion of Decoder selection. 39 // - First parameter: The initialized Decoder. If it's set to NULL, then 40 // Decoder initialization failed. 41 // - Second parameter: The initialized DecryptingDemuxerStream. If it's not 42 // NULL, then a DecryptingDemuxerStream is created and initialized to do 43 // decryption for the initialized Decoder. 44 // Note: The caller owns selected Decoder and DecryptingDemuxerStream. 45 // The caller should call DecryptingDemuxerStream::Reset() before 46 // calling Decoder::Reset() to release any pending decryption or read. 47 typedef base::Callback< 48 void(scoped_ptr<Decoder>, 49 scoped_ptr<DecryptingDemuxerStream>)> 50 SelectDecoderCB; 51 52 // |decoders| contains the Decoders to use when initializing. 53 // 54 // |set_decryptor_ready_cb| is optional. If |set_decryptor_ready_cb| is null, 55 // no decryptor will be available to perform decryption. 56 DecoderSelector( 57 const scoped_refptr<base::SingleThreadTaskRunner>& message_loop, 58 ScopedVector<Decoder> decoders, 59 const SetDecryptorReadyCB& set_decryptor_ready_cb); 60 ~DecoderSelector(); 61 62 // Initializes and selects a Decoder that can decode the |stream|. 63 // Selected Decoder (and DecryptingDemuxerStream) is returned via 64 // the |select_decoder_cb|. 65 void SelectDecoder(DemuxerStream* stream, 66 bool low_delay, 67 const SelectDecoderCB& select_decoder_cb, 68 const typename Decoder::OutputCB& output_cb); 69 70 // Aborts pending Decoder selection and fires |select_decoder_cb| with 71 // NULL and NULL immediately if it's pending. 72 void Abort(); 73 74 private: 75 void DecryptingDecoderInitDone(PipelineStatus status); 76 void DecryptingDemuxerStreamInitDone(PipelineStatus status); 77 void InitializeDecoder(); 78 void DecoderInitDone(PipelineStatus status); 79 void ReturnNullDecoder(); 80 81 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 82 ScopedVector<Decoder> decoders_; 83 SetDecryptorReadyCB set_decryptor_ready_cb_; 84 85 DemuxerStream* input_stream_; 86 bool low_delay_; 87 SelectDecoderCB select_decoder_cb_; 88 typename Decoder::OutputCB output_cb_; 89 90 scoped_ptr<Decoder> decoder_; 91 scoped_ptr<DecryptingDemuxerStream> decrypted_stream_; 92 93 // NOTE: Weak pointers must be invalidated before all other member variables. 94 base::WeakPtrFactory<DecoderSelector> weak_ptr_factory_; 95 96 DISALLOW_IMPLICIT_CONSTRUCTORS(DecoderSelector); 97 }; 98 99 typedef DecoderSelector<DemuxerStream::VIDEO> VideoDecoderSelector; 100 typedef DecoderSelector<DemuxerStream::AUDIO> AudioDecoderSelector; 101 102 } // namespace media 103 104 #endif // MEDIA_FILTERS_DECODER_SELECTOR_H_ 105