Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / media / filters / decrypting_demuxer_stream.h
blobe8d82224a10c809bfad183d8b1098f353c24678e
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 const base::Closure& waiting_for_decryption_key_cb);
36 // Cancels all pending operations immediately and fires all pending callbacks.
37 ~DecryptingDemuxerStream() override;
39 void Initialize(DemuxerStream* stream,
40 const PipelineStatusCB& status_cb);
42 // Cancels all pending operations and fires all pending callbacks. If in
43 // kPendingDemuxerRead or kPendingDecrypt state, waits for the pending
44 // operation to finish before satisfying |closure|. Sets the state to
45 // kUninitialized if |this| hasn't been initialized, or to kIdle otherwise.
46 void Reset(const base::Closure& closure);
48 // DemuxerStream implementation.
49 void Read(const ReadCB& read_cb) override;
50 AudioDecoderConfig audio_decoder_config() override;
51 VideoDecoderConfig video_decoder_config() override;
52 Type type() const override;
53 Liveness liveness() const override;
54 void EnableBitstreamConverter() override;
55 bool SupportsConfigChanges() override;
56 VideoRotation video_rotation() override;
58 private:
59 // For a detailed state diagram please see this link: http://goo.gl/8jAok
60 // TODO(xhwang): Add a ASCII state diagram in this file after this class
61 // stabilizes.
62 // TODO(xhwang): Update this diagram for DecryptingDemuxerStream.
63 enum State {
64 kUninitialized = 0,
65 kDecryptorRequested,
66 kIdle,
67 kPendingDemuxerRead,
68 kPendingDecrypt,
69 kWaitingForKey
72 // Callback for DecryptorHost::RequestDecryptor(). |decryptor_attached_cb| is
73 // called when the decryptor has been completely attached to the pipeline.
74 void SetDecryptor(Decryptor* decryptor,
75 const DecryptorAttachedCB& decryptor_attached_cb);
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_;
103 State state_;
105 PipelineStatusCB init_cb_;
106 ReadCB read_cb_;
107 base::Closure reset_cb_;
108 base::Closure waiting_for_decryption_key_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 base::WeakPtr<DecryptingDemuxerStream> weak_this_;
131 base::WeakPtrFactory<DecryptingDemuxerStream> weak_factory_;
133 DISALLOW_COPY_AND_ASSIGN(DecryptingDemuxerStream);
136 } // namespace media
138 #endif // MEDIA_FILTERS_DECRYPTING_DEMUXER_STREAM_H_