Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / media / filters / decrypting_audio_decoder.h
blob67da6c0c7be6d0d6b9fa051d7ec4ed70c053ad59
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;
27 // Decryptor-based AudioDecoder implementation that can decrypt and decode
28 // encrypted audio buffers and return decrypted and decompressed audio frames.
29 // All public APIs and callbacks are trampolined to the |task_runner_| so
30 // that no locks are required for thread safety.
31 class MEDIA_EXPORT DecryptingAudioDecoder : public AudioDecoder {
32 public:
33 DecryptingAudioDecoder(
34 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
35 const SetDecryptorReadyCB& set_decryptor_ready_cb);
36 ~DecryptingAudioDecoder() override;
38 // AudioDecoder implementation.
39 std::string GetDisplayName() const override;
40 void Initialize(const AudioDecoderConfig& config,
41 const PipelineStatusCB& status_cb,
42 const OutputCB& output_cb) override;
43 void Decode(const scoped_refptr<DecoderBuffer>& buffer,
44 const DecodeCB& decode_cb) override;
45 void Reset(const base::Closure& closure) override;
47 private:
48 // For a detailed state diagram please see this link: http://goo.gl/8jAok
49 // TODO(xhwang): Add a ASCII state diagram in this file after this class
50 // stabilizes.
51 // TODO(xhwang): Update this diagram for DecryptingAudioDecoder.
52 enum State {
53 kUninitialized = 0,
54 kDecryptorRequested,
55 kPendingDecoderInit,
56 kIdle,
57 kPendingDecode,
58 kWaitingForKey,
59 kDecodeFinished,
60 kError
63 // Callback for DecryptorHost::RequestDecryptor(). |decryptor_attached_cb| is
64 // called when the decryptor has been completely attached to the pipeline.
65 void SetDecryptor(Decryptor* decryptor,
66 const DecryptorAttachedCB& decryptor_attached_cb);
68 // Initializes the audio decoder on the |decryptor_| with |config_|.
69 void InitializeDecoder();
71 // Callback for Decryptor::InitializeAudioDecoder() during initialization.
72 void FinishInitialization(bool success);
74 void DecodePendingBuffer();
76 // Callback for Decryptor::DecryptAndDecodeAudio().
77 void DeliverFrame(int buffer_size,
78 Decryptor::Status status,
79 const Decryptor::AudioFrames& frames);
81 // Callback for the |decryptor_| to notify this object that a new key has been
82 // added.
83 void OnKeyAdded();
85 // Resets decoder and calls |reset_cb_|.
86 void DoReset();
88 // Sets timestamps for |frames| and then passes them to |output_cb_|.
89 void ProcessDecodedFrames(const Decryptor::AudioFrames& frames);
91 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
93 State state_;
95 PipelineStatusCB init_cb_;
96 OutputCB output_cb_;
97 DecodeCB decode_cb_;
98 base::Closure reset_cb_;
100 // The current decoder configuration.
101 AudioDecoderConfig config_;
103 // Callback to request/cancel decryptor creation notification.
104 SetDecryptorReadyCB set_decryptor_ready_cb_;
106 Decryptor* decryptor_;
108 // The buffer that needs decrypting/decoding.
109 scoped_refptr<media::DecoderBuffer> pending_buffer_to_decode_;
111 // Indicates the situation where new key is added during pending decode
112 // (in other words, this variable can only be set in state kPendingDecode).
113 // If this variable is true and kNoKey is returned then we need to try
114 // decrypting/decoding again in case the newly added key is the correct
115 // decryption key.
116 bool key_added_while_decode_pending_;
118 scoped_ptr<AudioTimestampHelper> timestamp_helper_;
120 base::WeakPtr<DecryptingAudioDecoder> weak_this_;
121 base::WeakPtrFactory<DecryptingAudioDecoder> weak_factory_;
123 DISALLOW_COPY_AND_ASSIGN(DecryptingAudioDecoder);
126 } // namespace media
128 #endif // MEDIA_FILTERS_DECRYPTING_AUDIO_DECODER_H_