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

content / renderer / pepper / pepper_audio_encoder_host.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 CONTENT_RENDERER_PEPPER_PEPPER_AUDIO_ENCODER_HOST_H_
#define CONTENT_RENDERER_PEPPER_PEPPER_AUDIO_ENCODER_HOST_H_

#include <stdint.h>

#include <memory>

#include "base/memory/ref_counted.h"
#include "base/numerics/safe_math.h"
#include "base/task/single_thread_task_runner.h"
#include "ppapi/c/pp_codecs.h"
#include "ppapi/host/host_message_context.h"
#include "ppapi/host/resource_host.h"
#include "ppapi/proxy/resource_message_params.h"
#include "ppapi/proxy/serialized_structs.h"
#include "ppapi/shared_impl/media_stream_buffer_manager.h"

namespace content {

class RendererPpapiHost;

class PepperAudioEncoderHost
    : public ppapi::host::ResourceHost,
      public ppapi::MediaStreamBufferManager::Delegate {
 public:
  PepperAudioEncoderHost(RendererPpapiHost* host,
                         PP_Instance instance,
                         PP_Resource resource);

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

  ~PepperAudioEncoderHost() override;

 private:
  class AudioEncoderImpl;

  // ResourceHost implementation.
  int32_t OnResourceMessageReceived(
      const IPC::Message& msg,
      ppapi::host::HostMessageContext* context) override;

  int32_t OnHostMsgGetSupportedProfiles(
      ppapi::host::HostMessageContext* context);
  int32_t OnHostMsgInitialize(
      ppapi::host::HostMessageContext* context,
      const ppapi::proxy::PPB_AudioEncodeParameters& parameters);
  int32_t OnHostMsgGetAudioBuffers(ppapi::host::HostMessageContext* context);
  int32_t OnHostMsgEncode(ppapi::host::HostMessageContext* context,
                          int32_t buffer_id);
  int32_t OnHostMsgRecycleBitstreamBuffer(
      ppapi::host::HostMessageContext* context,
      int32_t buffer_id);
  int32_t OnHostMsgRequestBitrateChange(
      ppapi::host::HostMessageContext* context,
      uint32_t bitrate);
  int32_t OnHostMsgClose(ppapi::host::HostMessageContext* context);

  void GetSupportedProfiles(std::vector<PP_AudioProfileDescription>* profiles);
  bool IsInitializationValid(
      const ppapi::proxy::PPB_AudioEncodeParameters& parameters);
  bool AllocateBuffers(
      const ppapi::proxy::PPB_AudioEncodeParameters& parameters,
      int32_t samples_per_frame);
  void DoEncode();
  void BitstreamBufferReady(int32_t audio_buffer_id,
                            int32_t bitstream_buffer_id,
                            int32_t size);
  void NotifyPepperError(int32_t error);
  void Close();

  static void StopAudioEncoder(
      std::unique_ptr<AudioEncoderImpl> encoder,
      std::unique_ptr<ppapi::MediaStreamBufferManager> audio_buffer_manager,
      std::unique_ptr<ppapi::MediaStreamBufferManager>
          bitstream_buffer_manager);

  // Non-owning pointer.
  RendererPpapiHost* renderer_ppapi_host_;

  // Whether the encoder has been successfully initialized.
  bool initialized_;

  // Last error reported by the encoder.
  // This represents the current error state of the encoder, i.e. PP_OK
  // normally, or a Pepper error code if the encoder is uninitialized,
  // has been notified of an encoder error, has encountered some
  // other unrecoverable error, or has been closed by the plugin.
  // This field is checked in most message handlers to decide whether
  // operations should proceed or fail.
  int32_t encoder_last_error_;

  // Manages buffers containing audio samples from the plugin.
  std::unique_ptr<ppapi::MediaStreamBufferManager> audio_buffer_manager_;

  // Manages buffers containing encoded audio from the browser.
  std::unique_ptr<ppapi::MediaStreamBufferManager> bitstream_buffer_manager_;

  // Media task runner used to run the encoder.
  scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_;

  // The encoder actually doing the work.
  std::unique_ptr<AudioEncoderImpl> encoder_;

  base::WeakPtrFactory<PepperAudioEncoderHost> weak_ptr_factory_{this};
};

}  // namespace content

#endif  // CONTENT_RENDERER_PEPPER_PEPPER_AUDIO_ENCODER_HOST_H_