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

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

// Copyright 2016 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_AUDIO_DECODER_SERVICE_H_
#define MEDIA_MOJO_SERVICES_MOJO_AUDIO_DECODER_SERVICE_H_

#include <stdint.h>

#include <memory>
#include <optional>

#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "media/base/audio_decoder.h"
#include "media/base/cdm_context.h"
#include "media/base/status.h"
#include "media/mojo/mojom/audio_decoder.mojom.h"
#include "media/mojo/services/media_mojo_export.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "mojo/public/cpp/bindings/pending_associated_remote.h"

namespace base {
class SingleThreadTaskRunner;
}  // namespace base

namespace media {

class MojoCdmServiceContext;
class MojoDecoderBufferReader;
class MojoMediaClient;

class MEDIA_MOJO_EXPORT MojoAudioDecoderService final
    : public mojom::AudioDecoder {
 public:
  MojoAudioDecoderService(
      MojoMediaClient* mojo_media_client,
      MojoCdmServiceContext* mojo_cdm_service_context,
      scoped_refptr<base::SingleThreadTaskRunner> task_runner);

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

  ~MojoAudioDecoderService() final;

  // mojom::AudioDecoder implementation
  void GetSupportedConfigs(GetSupportedConfigsCallback callback) final;
  void Construct(
      mojo::PendingAssociatedRemote<mojom::AudioDecoderClient> client,
      mojo::PendingRemote<mojom::MediaLog> media_log) final;
  void Initialize(const AudioDecoderConfig& config,
                  const std::optional<base::UnguessableToken>& cdm_id,
                  InitializeCallback callback) final;

  void SetDataSource(mojo::ScopedDataPipeConsumerHandle receive_pipe) final;

  void Decode(mojom::DecoderBufferPtr buffer, DecodeCallback callback) final;

  void Reset(ResetCallback callback) final;

 private:
  // Called by |decoder_| upon finishing initialization.
  void OnInitialized(InitializeCallback callback, DecoderStatus status);

  // Called by |mojo_decoder_buffer_reader_| when read is finished.
  void OnReadDone(mojo::ReportBadMessageCallback bad_message_callback,
                  DecodeCallback callback,
                  scoped_refptr<DecoderBuffer> buffer);

  // Called by |mojo_decoder_buffer_reader_| when reset is finished.
  void OnReaderFlushDone(ResetCallback callback);

  // Called by |decoder_| when DecoderBuffer is accepted or rejected.
  void OnDecodeStatus(DecodeCallback callback, DecoderStatus status);

  // Called by |decoder_| when reset sequence is finished.
  void OnResetDone(ResetCallback callback);

  // Called by |decoder_| for each decoded buffer.
  void OnAudioBufferReady(scoped_refptr<AudioBuffer> audio_buffer);

  // Called by |decoder_| when it's waiting because of |reason|, e.g. waiting
  // for decryption key.
  void OnWaiting(WaitingReason reason);

  std::unique_ptr<MojoDecoderBufferReader> mojo_decoder_buffer_reader_;

  const raw_ptr<MojoMediaClient> mojo_media_client_;

  // A helper object required to get CDM from CDM id.
  const raw_ptr<MojoCdmServiceContext> mojo_cdm_service_context_ = nullptr;

  // The destination for the decoded buffers.
  mojo::AssociatedRemote<mojom::AudioDecoderClient> client_;

  // The CDM ID and the corresponding CdmContextRef, which must be held to keep
  // the CdmContext alive for the lifetime of the |decoder_|.
  std::optional<base::UnguessableToken> cdm_id_;
  std::unique_ptr<CdmContextRef> cdm_context_ref_;

  // The AudioDecoder that does actual decoding work.
  // This MUST be declared after |cdm_| to maintain correct destruction order.
  // The |decoder_| may need to access the CDM to do some clean up work in its
  // own destructor.
  std::unique_ptr<media::AudioDecoder> decoder_;

  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;

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

}  // namespace media

#endif  // MEDIA_MOJO_SERVICES_MOJO_AUDIO_DECODER_SERVICE_H_