Disable view source for Developer Tools.
[chromium-blink-merge.git] / media / filters / decrypting_demuxer_stream.h
blob4e4cca2237497dedcd737ad14fed7aaf6e3d800a
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"
17 namespace base {
18 class SingleThreadTaskRunner;
21 namespace media {
23 class DecoderBuffer;
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 {
30 public:
31 DecryptingDemuxerStream(
32 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
33 const SetDecryptorReadyCB& set_decryptor_ready_cb);
34 virtual ~DecryptingDemuxerStream();
36 void Initialize(DemuxerStream* stream,
37 const PipelineStatusCB& status_cb);
39 // Cancels all pending operations and fires all pending callbacks. Sets
40 // |this| to kUninitialized state if |this| hasn't been initialized, or to
41 // kIdle state otherwise.
42 void Reset(const base::Closure& closure);
44 // DemuxerStream implementation.
45 virtual void Read(const ReadCB& read_cb) OVERRIDE;
46 virtual AudioDecoderConfig audio_decoder_config() OVERRIDE;
47 virtual VideoDecoderConfig video_decoder_config() OVERRIDE;
48 virtual Type type() OVERRIDE;
49 virtual void EnableBitstreamConverter() OVERRIDE;
51 private:
52 // For a detailed state diagram please see this link: http://goo.gl/8jAok
53 // TODO(xhwang): Add a ASCII state diagram in this file after this class
54 // stabilizes.
55 // TODO(xhwang): Update this diagram for DecryptingDemuxerStream.
56 enum State {
57 kUninitialized = 0,
58 kDecryptorRequested,
59 kIdle,
60 kPendingDemuxerRead,
61 kPendingDecrypt,
62 kWaitingForKey,
65 // Callback for DecryptorHost::RequestDecryptor().
66 void SetDecryptor(Decryptor* decryptor);
68 // Callback for DemuxerStream::Read().
69 void DecryptBuffer(DemuxerStream::Status status,
70 const scoped_refptr<DecoderBuffer>& buffer);
72 void DecryptPendingBuffer();
74 // Callback for Decryptor::Decrypt().
75 void DeliverBuffer(Decryptor::Status status,
76 const scoped_refptr<DecoderBuffer>& decrypted_buffer);
78 // Callback for the |decryptor_| to notify this object that a new key has been
79 // added.
80 void OnKeyAdded();
82 // Resets decoder and calls |reset_cb_|.
83 void DoReset();
85 // Returns Decryptor::StreamType converted from |stream_type_|.
86 Decryptor::StreamType GetDecryptorStreamType() const;
88 // Creates and initializes either |audio_config_| or |video_config_| based on
89 // |demuxer_stream_|.
90 void InitializeDecoderConfig();
92 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
93 base::WeakPtrFactory<DecryptingDemuxerStream> weak_factory_;
94 base::WeakPtr<DecryptingDemuxerStream> weak_this_;
96 State state_;
98 PipelineStatusCB init_cb_;
99 ReadCB read_cb_;
100 base::Closure reset_cb_;
102 // Pointer to the input demuxer stream that will feed us encrypted buffers.
103 DemuxerStream* demuxer_stream_;
105 AudioDecoderConfig audio_config_;
106 VideoDecoderConfig video_config_;
108 // Callback to request/cancel decryptor creation notification.
109 SetDecryptorReadyCB set_decryptor_ready_cb_;
111 Decryptor* decryptor_;
113 // The buffer returned by the demuxer that needs to be decrypted.
114 scoped_refptr<media::DecoderBuffer> pending_buffer_to_decrypt_;
116 // Indicates the situation where new key is added during pending decryption
117 // (in other words, this variable can only be set in state kPendingDecrypt).
118 // If this variable is true and kNoKey is returned then we need to try
119 // decrypting again in case the newly added key is the correct decryption key.
120 bool key_added_while_decrypt_pending_;
122 DISALLOW_COPY_AND_ASSIGN(DecryptingDemuxerStream);
125 } // namespace media
127 #endif // MEDIA_FILTERS_DECRYPTING_DEMUXER_STREAM_H_