NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / media / filters / decrypting_demuxer_stream.h
blobaa9e897f226b0194492b3583cee03a5b333a1a21
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. If in
40 // kPendingDemuxerRead or kPendingDecrypt state, waits for the pending
41 // operation to finish before satisfying |closure|. Sets the state to
42 // kUninitialized if |this| hasn't been initialized, or to kIdle otherwise.
43 void Reset(const base::Closure& closure);
45 // Cancels all pending operations immediately and fires all pending callbacks
46 // and sets the state to kStopped. Does NOT wait for any pending operations.
47 // Note: During the teardown process, media pipeline will be waiting on the
48 // render main thread. If a Decryptor depends on the render main thread
49 // (e.g. PpapiDecryptor), the pending DecryptCB would not be satisfied.
50 void Stop(const base::Closure& closure);
52 // DemuxerStream implementation.
53 virtual void Read(const ReadCB& read_cb) OVERRIDE;
54 virtual AudioDecoderConfig audio_decoder_config() OVERRIDE;
55 virtual VideoDecoderConfig video_decoder_config() OVERRIDE;
56 virtual Type type() OVERRIDE;
57 virtual void EnableBitstreamConverter() OVERRIDE;
59 private:
60 // For a detailed state diagram please see this link: http://goo.gl/8jAok
61 // TODO(xhwang): Add a ASCII state diagram in this file after this class
62 // stabilizes.
63 // TODO(xhwang): Update this diagram for DecryptingDemuxerStream.
64 enum State {
65 kUninitialized = 0,
66 kDecryptorRequested,
67 kIdle,
68 kPendingDemuxerRead,
69 kPendingDecrypt,
70 kWaitingForKey,
71 kStopped
74 // Callback for DecryptorHost::RequestDecryptor().
75 void SetDecryptor(Decryptor* decryptor);
77 // Callback for DemuxerStream::Read().
78 void DecryptBuffer(DemuxerStream::Status status,
79 const scoped_refptr<DecoderBuffer>& buffer);
81 void DecryptPendingBuffer();
83 // Callback for Decryptor::Decrypt().
84 void DeliverBuffer(Decryptor::Status status,
85 const scoped_refptr<DecoderBuffer>& decrypted_buffer);
87 // Callback for the |decryptor_| to notify this object that a new key has been
88 // added.
89 void OnKeyAdded();
91 // Resets decoder and calls |reset_cb_|.
92 void DoReset();
94 // Returns Decryptor::StreamType converted from |stream_type_|.
95 Decryptor::StreamType GetDecryptorStreamType() const;
97 // Creates and initializes either |audio_config_| or |video_config_| based on
98 // |demuxer_stream_|.
99 void InitializeDecoderConfig();
101 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
102 base::WeakPtrFactory<DecryptingDemuxerStream> weak_factory_;
103 base::WeakPtr<DecryptingDemuxerStream> weak_this_;
105 State state_;
107 PipelineStatusCB init_cb_;
108 ReadCB read_cb_;
109 base::Closure reset_cb_;
111 // Pointer to the input demuxer stream that will feed us encrypted buffers.
112 DemuxerStream* demuxer_stream_;
114 AudioDecoderConfig audio_config_;
115 VideoDecoderConfig video_config_;
117 // Callback to request/cancel decryptor creation notification.
118 SetDecryptorReadyCB set_decryptor_ready_cb_;
120 Decryptor* decryptor_;
122 // The buffer returned by the demuxer that needs to be decrypted.
123 scoped_refptr<media::DecoderBuffer> pending_buffer_to_decrypt_;
125 // Indicates the situation where new key is added during pending decryption
126 // (in other words, this variable can only be set in state kPendingDecrypt).
127 // If this variable is true and kNoKey is returned then we need to try
128 // decrypting again in case the newly added key is the correct decryption key.
129 bool key_added_while_decrypt_pending_;
131 DISALLOW_COPY_AND_ASSIGN(DecryptingDemuxerStream);
134 } // namespace media
136 #endif // MEDIA_FILTERS_DECRYPTING_DEMUXER_STREAM_H_