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 MessageLoopProxy
;
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 |message_loop_| so
28 // that no locks are required for thread safety.
29 class MEDIA_EXPORT DecryptingDemuxerStream
: public DemuxerStream
{
31 DecryptingDemuxerStream(
32 const scoped_refptr
<base::MessageLoopProxy
>& message_loop
,
33 const SetDecryptorReadyCB
& set_decryptor_ready_cb
);
34 virtual ~DecryptingDemuxerStream();
36 void Initialize(DemuxerStream
* stream
,
37 const PipelineStatusCB
& status_cb
);
38 void Reset(const base::Closure
& closure
);
40 // DemuxerStream implementation.
41 virtual void Read(const ReadCB
& read_cb
) OVERRIDE
;
42 virtual AudioDecoderConfig
audio_decoder_config() OVERRIDE
;
43 virtual VideoDecoderConfig
video_decoder_config() OVERRIDE
;
44 virtual Type
type() OVERRIDE
;
45 virtual void EnableBitstreamConverter() OVERRIDE
;
48 // For a detailed state diagram please see this link: http://goo.gl/8jAok
49 // TODO(xhwang): Add a ASCII state diagram in this file after this class
51 // TODO(xhwang): Update this diagram for DecryptingDemuxerStream.
61 // Callback for DecryptorHost::RequestDecryptor().
62 void SetDecryptor(Decryptor
* decryptor
);
64 // Callback for DemuxerStream::Read().
65 void DecryptBuffer(DemuxerStream::Status status
,
66 const scoped_refptr
<DecoderBuffer
>& buffer
);
68 void DecryptPendingBuffer();
70 // Callback for Decryptor::Decrypt().
71 void DeliverBuffer(Decryptor::Status status
,
72 const scoped_refptr
<DecoderBuffer
>& decrypted_buffer
);
74 // Callback for the |decryptor_| to notify this object that a new key has been
78 // Resets decoder and calls |reset_cb_|.
81 // Returns Decryptor::StreamType converted from |stream_type_|.
82 Decryptor::StreamType
GetDecryptorStreamType() const;
84 // Creates and initializes either |audio_config_| or |video_config_| based on
86 void InitializeDecoderConfig();
88 scoped_refptr
<base::MessageLoopProxy
> message_loop_
;
89 base::WeakPtrFactory
<DecryptingDemuxerStream
> weak_factory_
;
90 base::WeakPtr
<DecryptingDemuxerStream
> weak_this_
;
94 PipelineStatusCB init_cb_
;
96 base::Closure reset_cb_
;
98 // Pointer to the input demuxer stream that will feed us encrypted buffers.
99 DemuxerStream
* demuxer_stream_
;
101 AudioDecoderConfig audio_config_
;
102 VideoDecoderConfig video_config_
;
104 // Callback to request/cancel decryptor creation notification.
105 SetDecryptorReadyCB set_decryptor_ready_cb_
;
107 Decryptor
* decryptor_
;
109 // The buffer returned by the demuxer that needs to be decrypted.
110 scoped_refptr
<media::DecoderBuffer
> pending_buffer_to_decrypt_
;
112 // Indicates the situation where new key is added during pending decryption
113 // (in other words, this variable can only be set in state kPendingDecrypt).
114 // If this variable is true and kNoKey is returned then we need to try
115 // decrypting again in case the newly added key is the correct decryption key.
116 bool key_added_while_decrypt_pending_
;
118 DISALLOW_COPY_AND_ASSIGN(DecryptingDemuxerStream
);
123 #endif // MEDIA_FILTERS_DECRYPTING_DEMUXER_STREAM_H_