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

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

// Copyright 2017 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_INPUT_STREAM_H_
#define MEDIA_MOJO_SERVICES_MOJO_AUDIO_INPUT_STREAM_H_

#include <memory>
#include <string>

#include "base/sequence_checker.h"
#include "media/audio/audio_input_delegate.h"
#include "media/mojo/mojom/audio_data_pipe.mojom.h"
#include "media/mojo/mojom/audio_input_stream.mojom.h"
#include "media/mojo/services/media_mojo_export.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"

namespace media {

// This class handles IPC for single audio input stream by delegating method
// calls to its AudioInputDelegate.
class MEDIA_MOJO_EXPORT MojoAudioInputStream
    : public mojom::AudioInputStream,
      public AudioInputDelegate::EventHandler {
 public:
  using StreamCreatedCallback =
      base::OnceCallback<void(mojom::ReadWriteAudioDataPipePtr, bool)>;
  using CreateDelegateCallback =
      base::OnceCallback<std::unique_ptr<AudioInputDelegate>(
          AudioInputDelegate::EventHandler*)>;

  // |create_delegate_callback| is used to obtain an AudioInputDelegate for the
  // stream in the constructor. |stream_created_callback| is called when the
  // stream has been initialized. |deleter_callback| is called when this class
  // should be removed (stream ended/error). |deleter_callback| is required to
  // destroy |this| synchronously.
  MojoAudioInputStream(
      mojo::PendingReceiver<mojom::AudioInputStream> receiver,
      mojo::PendingRemote<mojom::AudioInputStreamClient> client,
      CreateDelegateCallback create_delegate_callback,
      StreamCreatedCallback stream_created_callback,
      base::OnceClosure deleter_callback);

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

  ~MojoAudioInputStream() override;

  void SetOutputDeviceForAec(const std::string& raw_output_device_id);

 private:
  // mojom::AudioInputStream implementation.
  void Record() override;
  void SetVolume(double volume) override;

  // AudioInputDelegate::EventHandler implementation.
  void OnStreamCreated(
      int stream_id,
      base::UnsafeSharedMemoryRegion shared_memory_region,
      std::unique_ptr<base::CancelableSyncSocket> foreign_socket,
      bool initially_muted) override;
  void OnStreamError(int stream_id) override;

  // Closes connection to client and notifies owner.
  void OnError();

  SEQUENCE_CHECKER(sequence_checker_);

  StreamCreatedCallback stream_created_callback_;
  base::OnceClosure deleter_callback_;
  mojo::Receiver<AudioInputStream> receiver_;
  mojo::Remote<mojom::AudioInputStreamClient> client_;
  std::unique_ptr<AudioInputDelegate> delegate_;
  base::WeakPtrFactory<MojoAudioInputStream> weak_factory_{this};
};

}  // namespace media

#endif  // MEDIA_MOJO_SERVICES_MOJO_AUDIO_INPUT_STREAM_H_