captured_audio_input.cc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2018 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.
  4. #include "components/mirroring/service/captured_audio_input.h"
  5. #include "base/check_op.h"
  6. #include "base/notreached.h"
  7. #include "media/mojo/common/input_error_code_converter.h"
  8. #include "media/mojo/mojom/audio_data_pipe.mojom.h"
  9. #include "mojo/public/cpp/system/platform_handle.h"
  10. namespace mirroring {
  11. CapturedAudioInput::CapturedAudioInput(StreamCreatorCallback callback)
  12. : stream_creator_callback_(std::move(callback)) {
  13. DETACH_FROM_SEQUENCE(sequence_checker_);
  14. DCHECK(!stream_creator_callback_.is_null());
  15. }
  16. CapturedAudioInput::~CapturedAudioInput() {}
  17. void CapturedAudioInput::CreateStream(media::AudioInputIPCDelegate* delegate,
  18. const media::AudioParameters& params,
  19. bool automatic_gain_control,
  20. uint32_t total_segments) {
  21. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  22. DCHECK(!automatic_gain_control); // Invalid to be true for screen capture.
  23. DCHECK(delegate);
  24. DCHECK(!delegate_);
  25. delegate_ = delegate;
  26. stream_creator_callback_.Run(
  27. stream_creator_client_receiver_.BindNewPipeAndPassRemote(), params,
  28. total_segments);
  29. }
  30. void CapturedAudioInput::RecordStream() {
  31. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  32. DCHECK(stream_.is_bound());
  33. stream_->Record();
  34. }
  35. void CapturedAudioInput::SetVolume(double volume) {
  36. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  37. DCHECK(stream_.is_bound());
  38. stream_->SetVolume(volume);
  39. }
  40. void CapturedAudioInput::CloseStream() {
  41. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  42. delegate_ = nullptr;
  43. stream_client_receiver_.reset();
  44. stream_.reset();
  45. }
  46. void CapturedAudioInput::SetOutputDeviceForAec(
  47. const std::string& output_device_id) {
  48. NOTREACHED();
  49. }
  50. void CapturedAudioInput::StreamCreated(
  51. mojo::PendingRemote<media::mojom::AudioInputStream> stream,
  52. mojo::PendingReceiver<media::mojom::AudioInputStreamClient> client_receiver,
  53. media::mojom::ReadOnlyAudioDataPipePtr data_pipe) {
  54. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  55. DCHECK(delegate_);
  56. DCHECK(!stream_);
  57. DCHECK(!stream_client_receiver_.is_bound());
  58. stream_.Bind(std::move(stream));
  59. stream_client_receiver_.Bind(std::move(client_receiver));
  60. DCHECK(data_pipe->socket.is_valid_platform_file());
  61. base::ScopedPlatformFile socket_handle = data_pipe->socket.TakePlatformFile();
  62. base::ReadOnlySharedMemoryRegion& shared_memory_region =
  63. data_pipe->shared_memory;
  64. DCHECK(shared_memory_region.IsValid());
  65. delegate_->OnStreamCreated(std::move(shared_memory_region),
  66. std::move(socket_handle),
  67. /* initally_muted */ false);
  68. }
  69. void CapturedAudioInput::OnError(media::mojom::InputStreamErrorCode code) {
  70. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  71. DCHECK(delegate_);
  72. delegate_->OnError(media::ConvertToCaptureCallbackCode(code));
  73. }
  74. void CapturedAudioInput::OnMutedStateChanged(bool is_muted) {
  75. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  76. DCHECK(delegate_);
  77. delegate_->OnMuted(is_muted);
  78. }
  79. } // namespace mirroring