1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120

media / mojo / services / mojo_decryptor_service.h [blame]

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_MOJO_SERVICES_MOJO_DECRYPTOR_SERVICE_H_
#define MEDIA_MOJO_SERVICES_MOJO_DECRYPTOR_SERVICE_H_

#include <stddef.h>
#include <stdint.h>

#include "base/compiler_specific.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "media/base/cdm_context.h"
#include "media/base/decryptor.h"
#include "media/mojo/mojom/decryptor.mojom.h"
#include "media/mojo/services/media_mojo_export.h"

namespace media {

class DecoderBuffer;
class MojoDecoderBufferReader;
class MojoDecoderBufferWriter;

// A mojom::Decryptor implementation that proxies decryptor calls to a
// media::Decryptor.
class MEDIA_MOJO_EXPORT MojoDecryptorService final : public mojom::Decryptor {
 public:
  using StreamType = media::Decryptor::StreamType;
  using Status = media::Decryptor::Status;

  // If |cdm_context_ref| is null, caller must ensure that |decryptor| outlives
  // |this|. Otherwise, |decryptor| is guaranteed to be valid as long as
  // |cdm_context_ref| is held.
  MojoDecryptorService(media::Decryptor* decryptor,
                       std::unique_ptr<CdmContextRef> cdm_context_ref);

  MojoDecryptorService(const MojoDecryptorService&) = delete;
  MojoDecryptorService& operator=(const MojoDecryptorService&) = delete;

  ~MojoDecryptorService() final;

  // mojom::Decryptor implementation.
  void Initialize(mojo::ScopedDataPipeConsumerHandle audio_pipe,
                  mojo::ScopedDataPipeConsumerHandle video_pipe,
                  mojo::ScopedDataPipeConsumerHandle decrypt_pipe,
                  mojo::ScopedDataPipeProducerHandle decrypted_pipe) final;
  void Decrypt(StreamType stream_type,
               mojom::DecoderBufferPtr encrypted,
               DecryptCallback callback) final;
  void CancelDecrypt(StreamType stream_type) final;
  void InitializeAudioDecoder(const AudioDecoderConfig& config,
                              InitializeAudioDecoderCallback callback) final;
  void InitializeVideoDecoder(const VideoDecoderConfig& config,
                              InitializeVideoDecoderCallback callback) final;
  void DecryptAndDecodeAudio(mojom::DecoderBufferPtr encrypted,
                             DecryptAndDecodeAudioCallback callback) final;
  void DecryptAndDecodeVideo(mojom::DecoderBufferPtr encrypted,
                             DecryptAndDecodeVideoCallback callback) final;
  void ResetDecoder(StreamType stream_type) final;
  void DeinitializeDecoder(StreamType stream_type) final;

 private:
  void OnReadDone(StreamType stream_type,
                  DecryptCallback callback,
                  scoped_refptr<DecoderBuffer> buffer);

  // Callback executed once Decrypt() is done.
  void OnDecryptDone(DecryptCallback callback,
                     Status status,
                     scoped_refptr<DecoderBuffer> buffer);

  // Callbacks executed once decoder initialized.
  void OnAudioDecoderInitialized(InitializeAudioDecoderCallback callback,
                                 bool success);
  void OnVideoDecoderInitialized(InitializeVideoDecoderCallback callback,
                                 bool success);

  void OnAudioRead(DecryptAndDecodeAudioCallback callback,
                   scoped_refptr<DecoderBuffer> buffer);
  void OnVideoRead(DecryptAndDecodeVideoCallback callback,
                   scoped_refptr<DecoderBuffer> buffer);
  void OnReaderFlushDone(StreamType stream_type);

  // Callbacks executed when DecryptAndDecode are done.
  void OnAudioDecoded(DecryptAndDecodeAudioCallback callback,
                      Status status,
                      const media::Decryptor::AudioFrames& frames);
  void OnVideoDecoded(DecryptAndDecodeVideoCallback callback,
                      Status status,
                      scoped_refptr<VideoFrame> frame);

  // Returns audio/video buffer reader according to the |stream_type|.
  MojoDecoderBufferReader* GetBufferReader(StreamType stream_type) const;

  bool has_initialize_been_called_ = false;

  // Helper classes to receive encrypted DecoderBuffer from the client.
  std::unique_ptr<MojoDecoderBufferReader> audio_buffer_reader_;
  std::unique_ptr<MojoDecoderBufferReader> video_buffer_reader_;
  std::unique_ptr<MojoDecoderBufferReader> decrypt_buffer_reader_;

  // Helper class to send decrypted DecoderBuffer to the client.
  std::unique_ptr<MojoDecoderBufferWriter> decrypted_buffer_writer_;

  raw_ptr<media::Decryptor, DanglingUntriaged> decryptor_;

  // Holds the CdmContextRef to keep the CdmContext alive for the lifetime of
  // the |decryptor_|.
  std::unique_ptr<CdmContextRef> cdm_context_ref_;

  base::WeakPtr<MojoDecryptorService> weak_this_;
  base::WeakPtrFactory<MojoDecryptorService> weak_factory_{this};
};

}  // namespace media

#endif  // MEDIA_MOJO_SERVICES_MOJO_DECRYPTOR_SERVICE_H_