[MD settings] moving attached() code
[chromium-blink-merge.git] / media / filters / decrypting_audio_decoder.h
blob79191723e9d060128808cf8f4fd6d68e487721eb
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_AUDIO_DECODER_H_
6 #define MEDIA_FILTERS_DECRYPTING_AUDIO_DECODER_H_
8 #include "base/callback.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/time/time.h"
13 #include "media/base/audio_decoder.h"
14 #include "media/base/decryptor.h"
15 #include "media/base/demuxer_stream.h"
17 namespace base {
18 class SingleThreadTaskRunner;
21 namespace media {
23 class AudioTimestampHelper;
24 class DecoderBuffer;
25 class Decryptor;
26 class MediaLog;
28 // Decryptor-based AudioDecoder implementation that can decrypt and decode
29 // encrypted audio buffers and return decrypted and decompressed audio frames.
30 // All public APIs and callbacks are trampolined to the |task_runner_| so
31 // that no locks are required for thread safety.
32 class MEDIA_EXPORT DecryptingAudioDecoder : public AudioDecoder {
33 public:
34 DecryptingAudioDecoder(
35 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
36 const scoped_refptr<MediaLog>& media_log,
37 const SetDecryptorReadyCB& set_decryptor_ready_cb,
38 const base::Closure& waiting_for_decryption_key_cb);
39 ~DecryptingAudioDecoder() override;
41 // AudioDecoder implementation.
42 std::string GetDisplayName() const override;
43 void Initialize(const AudioDecoderConfig& config,
44 const InitCB& init_cb,
45 const OutputCB& output_cb) override;
46 void Decode(const scoped_refptr<DecoderBuffer>& buffer,
47 const DecodeCB& decode_cb) override;
48 void Reset(const base::Closure& closure) override;
50 private:
51 // For a detailed state diagram please see this link: http://goo.gl/8jAok
52 // TODO(xhwang): Add a ASCII state diagram in this file after this class
53 // stabilizes.
54 // TODO(xhwang): Update this diagram for DecryptingAudioDecoder.
55 enum State {
56 kUninitialized = 0,
57 kDecryptorRequested,
58 kPendingDecoderInit,
59 kIdle,
60 kPendingDecode,
61 kWaitingForKey,
62 kDecodeFinished,
63 kError
66 // Callback for DecryptorHost::RequestDecryptor(). |decryptor_attached_cb| is
67 // called when the decryptor has been completely attached to the pipeline.
68 void SetDecryptor(Decryptor* decryptor,
69 const DecryptorAttachedCB& decryptor_attached_cb);
71 // Initializes the audio decoder on the |decryptor_| with |config_|.
72 void InitializeDecoder();
74 // Callback for Decryptor::InitializeAudioDecoder() during initialization.
75 void FinishInitialization(bool success);
77 void DecodePendingBuffer();
79 // Callback for Decryptor::DecryptAndDecodeAudio().
80 void DeliverFrame(int buffer_size,
81 Decryptor::Status status,
82 const Decryptor::AudioFrames& frames);
84 // Callback for the |decryptor_| to notify this object that a new key has been
85 // added.
86 void OnKeyAdded();
88 // Resets decoder and calls |reset_cb_|.
89 void DoReset();
91 // Sets timestamps for |frames| and then passes them to |output_cb_|.
92 void ProcessDecodedFrames(const Decryptor::AudioFrames& frames);
94 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
96 scoped_refptr<MediaLog> media_log_;
98 State state_;
100 InitCB init_cb_;
101 OutputCB output_cb_;
102 DecodeCB decode_cb_;
103 base::Closure reset_cb_;
104 base::Closure waiting_for_decryption_key_cb_;
106 // The current decoder configuration.
107 AudioDecoderConfig config_;
109 // Callback to request/cancel decryptor creation notification.
110 SetDecryptorReadyCB set_decryptor_ready_cb_;
112 Decryptor* decryptor_;
114 // The buffer that needs decrypting/decoding.
115 scoped_refptr<media::DecoderBuffer> pending_buffer_to_decode_;
117 // Indicates the situation where new key is added during pending decode
118 // (in other words, this variable can only be set in state kPendingDecode).
119 // If this variable is true and kNoKey is returned then we need to try
120 // decrypting/decoding again in case the newly added key is the correct
121 // decryption key.
122 bool key_added_while_decode_pending_;
124 scoped_ptr<AudioTimestampHelper> timestamp_helper_;
126 base::WeakPtr<DecryptingAudioDecoder> weak_this_;
127 base::WeakPtrFactory<DecryptingAudioDecoder> weak_factory_;
129 DISALLOW_COPY_AND_ASSIGN(DecryptingAudioDecoder);
132 } // namespace media
134 #endif // MEDIA_FILTERS_DECRYPTING_AUDIO_DECODER_H_