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

content / browser / renderer_host / media / audio_service_listener.cc [blame]

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

#include "content/browser/renderer_host/media/audio_service_listener.h"

#include <utility>

#include "base/feature_list.h"
#include "base/metrics/histogram_macros.h"
#include "base/process/process.h"
#include "base/time/default_tick_clock.h"
#include "content/browser/media/audio_log_factory.h"
#include "content/public/browser/audio_service.h"
#include "content/public/common/content_features.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "services/audio/public/mojom/audio_service.mojom.h"
#include "services/audio/public/mojom/log_factory_manager.mojom.h"

namespace content {

AudioServiceListener::AudioServiceListener() {
  ServiceProcessHost::AddObserver(this);
  Init(ServiceProcessHost::GetRunningProcessInfo());
}

AudioServiceListener::~AudioServiceListener() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(owning_sequence_);
  ServiceProcessHost::RemoveObserver(this);
}

base::Process AudioServiceListener::GetProcess() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(owning_sequence_);
  if (!audio_process_.IsValid())
    return base::Process();
  return audio_process_.Duplicate();
}

void AudioServiceListener::Init(
    std::vector<ServiceProcessInfo> running_service_processes) {
  for (const auto& info : running_service_processes) {
    if (info.IsService<audio::mojom::AudioService>()) {
      audio_process_ = info.GetProcess().Duplicate();
      MaybeSetLogFactory();
      break;
    }
  }
}

void AudioServiceListener::OnServiceProcessLaunched(
    const ServiceProcessInfo& info) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(owning_sequence_);
  if (!info.IsService<audio::mojom::AudioService>())
    return;

  audio_process_ = info.GetProcess().Duplicate();
  MaybeSetLogFactory();
}

void AudioServiceListener::OnServiceProcessTerminatedNormally(
    const ServiceProcessInfo& info) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(owning_sequence_);
  if (!info.IsService<audio::mojom::AudioService>())
    return;

  audio_process_ = base::Process();
  log_factory_is_set_ = false;
}

void AudioServiceListener::OnServiceProcessCrashed(
    const ServiceProcessInfo& info) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(owning_sequence_);
  if (!info.IsService<audio::mojom::AudioService>())
    return;

  audio_process_ = base::Process();
  log_factory_is_set_ = false;
}

void AudioServiceListener::MaybeSetLogFactory() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(owning_sequence_);
  if (!base::FeatureList::IsEnabled(features::kAudioServiceOutOfProcess) ||
      log_factory_is_set_)
    return;

  mojo::PendingRemote<media::mojom::AudioLogFactory> audio_log_factory;
  mojo::MakeSelfOwnedReceiver(
      std::make_unique<AudioLogFactory>(),
      audio_log_factory.InitWithNewPipeAndPassReceiver());
  mojo::Remote<audio::mojom::LogFactoryManager> log_factory_manager;
  GetAudioService().BindLogFactoryManager(
      log_factory_manager.BindNewPipeAndPassReceiver());
  log_factory_manager->SetLogFactory(std::move(audio_log_factory));
  log_factory_is_set_ = true;
}

}  // namespace content