Fix leak when a PeerConnection offer or answer is created.
[chromium-blink-merge.git] / media / filters / decoder_selector.h
blob8107e724bae5efef0c3eded0fdc17478bb388b62
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.
5 #ifndef MEDIA_FILTERS_DECODER_SELECTOR_H_
6 #define MEDIA_FILTERS_DECODER_SELECTOR_H_
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"
17 namespace base {
18 class SingleThreadTaskRunner;
21 namespace media {
23 class DecoderBuffer;
24 class DecryptingDemuxerStream;
25 class Decryptor;
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;
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;
52 // |decoders| contains the Decoders to use when initializing.
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();
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);
69 // Aborts pending Decoder selection and fires |select_decoder_cb| with
70 // NULL and NULL immediately if it's pending.
71 void Abort();
73 private:
74 void DecryptingDecoderInitDone(PipelineStatus status);
75 void DecryptingDemuxerStreamInitDone(PipelineStatus status);
76 void InitializeDecoder();
77 void DecoderInitDone(PipelineStatus status);
78 void ReturnNullDecoder();
80 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
81 ScopedVector<Decoder> decoders_;
82 SetDecryptorReadyCB set_decryptor_ready_cb_;
84 DemuxerStream* input_stream_;
85 bool low_delay_;
86 SelectDecoderCB select_decoder_cb_;
88 scoped_ptr<Decoder> decoder_;
89 scoped_ptr<DecryptingDemuxerStream> decrypted_stream_;
91 // NOTE: Weak pointers must be invalidated before all other member variables.
92 base::WeakPtrFactory<DecoderSelector> weak_ptr_factory_;
94 DISALLOW_IMPLICIT_CONSTRUCTORS(DecoderSelector);
97 typedef DecoderSelector<DemuxerStream::VIDEO> VideoDecoderSelector;
98 typedef DecoderSelector<DemuxerStream::AUDIO> AudioDecoderSelector;
100 } // namespace media
102 #endif // MEDIA_FILTERS_DECODER_SELECTOR_H_