Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / media / filters / decrypting_demuxer_stream.h
blobec9f4b4663287238edd475ad43e5543b1222e17c
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;
58 virtual bool SupportsConfigChanges() OVERRIDE;
60 private:
61 // For a detailed state diagram please see this link: http://goo.gl/8jAok
62 // TODO(xhwang): Add a ASCII state diagram in this file after this class
63 // stabilizes.
64 // TODO(xhwang): Update this diagram for DecryptingDemuxerStream.
65 enum State {
66 kUninitialized = 0,
67 kDecryptorRequested,
68 kIdle,
69 kPendingDemuxerRead,
70 kPendingDecrypt,
71 kWaitingForKey,
72 kStopped
75 // Callback for DecryptorHost::RequestDecryptor().
76 void SetDecryptor(Decryptor* decryptor);
78 // Callback for DemuxerStream::Read().
79 void DecryptBuffer(DemuxerStream::Status status,
80 const scoped_refptr<DecoderBuffer>& buffer);
82 void DecryptPendingBuffer();
84 // Callback for Decryptor::Decrypt().
85 void DeliverBuffer(Decryptor::Status status,
86 const scoped_refptr<DecoderBuffer>& decrypted_buffer);
88 // Callback for the |decryptor_| to notify this object that a new key has been
89 // added.
90 void OnKeyAdded();
92 // Resets decoder and calls |reset_cb_|.
93 void DoReset();
95 // Returns Decryptor::StreamType converted from |stream_type_|.
96 Decryptor::StreamType GetDecryptorStreamType() const;
98 // Creates and initializes either |audio_config_| or |video_config_| based on
99 // |demuxer_stream_|.
100 void InitializeDecoderConfig();
102 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
104 State state_;
106 PipelineStatusCB init_cb_;
107 ReadCB read_cb_;
108 base::Closure reset_cb_;
110 // Pointer to the input demuxer stream that will feed us encrypted buffers.
111 DemuxerStream* demuxer_stream_;
113 AudioDecoderConfig audio_config_;
114 VideoDecoderConfig video_config_;
116 // Callback to request/cancel decryptor creation notification.
117 SetDecryptorReadyCB set_decryptor_ready_cb_;
119 Decryptor* decryptor_;
121 // The buffer returned by the demuxer that needs to be decrypted.
122 scoped_refptr<media::DecoderBuffer> pending_buffer_to_decrypt_;
124 // Indicates the situation where new key is added during pending decryption
125 // (in other words, this variable can only be set in state kPendingDecrypt).
126 // If this variable is true and kNoKey is returned then we need to try
127 // decrypting again in case the newly added key is the correct decryption key.
128 bool key_added_while_decrypt_pending_;
130 // NOTE: Weak pointers must be invalidated before all other member variables.
131 base::WeakPtrFactory<DecryptingDemuxerStream> weak_factory_;
132 base::WeakPtr<DecryptingDemuxerStream> weak_this_;
134 DISALLOW_COPY_AND_ASSIGN(DecryptingDemuxerStream);
137 } // namespace media
139 #endif // MEDIA_FILTERS_DECRYPTING_DEMUXER_STREAM_H_