1 // Copyright (c) 2012 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_DECRYPTING_DEMUXER_STREAM_H_
6 #define MEDIA_FILTERS_DECRYPTING_DEMUXER_STREAM_H_
8 #include "base/callback.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/weak_ptr.h"
11 #include "media/base/audio_decoder_config.h"
12 #include "media/base/decryptor.h"
13 #include "media/base/demuxer_stream.h"
14 #include "media/base/pipeline_status.h"
15 #include "media/base/video_decoder_config.h"
18 class SingleThreadTaskRunner
;
25 // Decryptor-based DemuxerStream implementation that converts a potentially
26 // encrypted demuxer stream to a clear demuxer stream.
27 // All public APIs and callbacks are trampolined to the |task_runner_| so
28 // that no locks are required for thread safety.
29 class MEDIA_EXPORT DecryptingDemuxerStream
: public DemuxerStream
{
31 DecryptingDemuxerStream(
32 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
33 const SetDecryptorReadyCB
& set_decryptor_ready_cb
);
35 // Cancels all pending operations immediately and fires all pending callbacks.
36 ~DecryptingDemuxerStream() override
;
38 void Initialize(DemuxerStream
* stream
,
39 const PipelineStatusCB
& status_cb
);
41 // Cancels all pending operations and fires all pending callbacks. If in
42 // kPendingDemuxerRead or kPendingDecrypt state, waits for the pending
43 // operation to finish before satisfying |closure|. Sets the state to
44 // kUninitialized if |this| hasn't been initialized, or to kIdle otherwise.
45 void Reset(const base::Closure
& closure
);
47 // DemuxerStream implementation.
48 void Read(const ReadCB
& read_cb
) override
;
49 AudioDecoderConfig
audio_decoder_config() override
;
50 VideoDecoderConfig
video_decoder_config() override
;
51 Type
type() const override
;
52 Liveness
liveness() const override
;
53 void EnableBitstreamConverter() override
;
54 bool SupportsConfigChanges() override
;
55 VideoRotation
video_rotation() override
;
58 // For a detailed state diagram please see this link: http://goo.gl/8jAok
59 // TODO(xhwang): Add a ASCII state diagram in this file after this class
61 // TODO(xhwang): Update this diagram for DecryptingDemuxerStream.
71 // Callback for DecryptorHost::RequestDecryptor(). |decryptor_attached_cb| is
72 // called when the decryptor has been completely attached to the pipeline.
73 void SetDecryptor(Decryptor
* decryptor
,
74 const DecryptorAttachedCB
& decryptor_attached_cb
);
76 // Callback for DemuxerStream::Read().
77 void DecryptBuffer(DemuxerStream::Status status
,
78 const scoped_refptr
<DecoderBuffer
>& buffer
);
80 void DecryptPendingBuffer();
82 // Callback for Decryptor::Decrypt().
83 void DeliverBuffer(Decryptor::Status status
,
84 const scoped_refptr
<DecoderBuffer
>& decrypted_buffer
);
86 // Callback for the |decryptor_| to notify this object that a new key has been
90 // Resets decoder and calls |reset_cb_|.
93 // Returns Decryptor::StreamType converted from |stream_type_|.
94 Decryptor::StreamType
GetDecryptorStreamType() const;
96 // Creates and initializes either |audio_config_| or |video_config_| based on
98 void InitializeDecoderConfig();
100 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
104 PipelineStatusCB init_cb_
;
106 base::Closure reset_cb_
;
108 // Pointer to the input demuxer stream that will feed us encrypted buffers.
109 DemuxerStream
* demuxer_stream_
;
111 AudioDecoderConfig audio_config_
;
112 VideoDecoderConfig video_config_
;
114 // Callback to request/cancel decryptor creation notification.
115 SetDecryptorReadyCB set_decryptor_ready_cb_
;
117 Decryptor
* decryptor_
;
119 // The buffer returned by the demuxer that needs to be decrypted.
120 scoped_refptr
<media::DecoderBuffer
> pending_buffer_to_decrypt_
;
122 // Indicates the situation where new key is added during pending decryption
123 // (in other words, this variable can only be set in state kPendingDecrypt).
124 // If this variable is true and kNoKey is returned then we need to try
125 // decrypting again in case the newly added key is the correct decryption key.
126 bool key_added_while_decrypt_pending_
;
128 base::WeakPtr
<DecryptingDemuxerStream
> weak_this_
;
129 base::WeakPtrFactory
<DecryptingDemuxerStream
> weak_factory_
;
131 DISALLOW_COPY_AND_ASSIGN(DecryptingDemuxerStream
);
136 #endif // MEDIA_FILTERS_DECRYPTING_DEMUXER_STREAM_H_