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

media / audio / audio_debug_recording_helper.cc [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.

#include "media/audio/audio_debug_recording_helper.h"

#include <memory>
#include <utility>

#include "base/files/file.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/sequence_checker.h"
#include "base/synchronization/lock.h"
#include "base/task/bind_post_task.h"
#include "media/audio/audio_debug_file_writer.h"
#include "media/base/audio_bus.h"

namespace media {

AudioDebugRecordingHelper::AudioDebugRecordingHelper(
    const AudioParameters& params,
    base::OnceClosure on_destruction_closure)
    : params_(params),
      file_writer_(nullptr, base::OnTaskRunnerDeleter(nullptr)),
      on_destruction_closure_(std::move(on_destruction_closure)) {
  DETACH_FROM_SEQUENCE(sequence_checker_);
}

AudioDebugRecordingHelper::~AudioDebugRecordingHelper() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (on_destruction_closure_)
    std::move(on_destruction_closure_).Run();
}

void AudioDebugRecordingHelper::EnableDebugRecording(
    AudioDebugRecordingStreamType stream_type,
    uint32_t id,
    CreateWavFileCallback create_file_callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  std::move(create_file_callback)
      .Run(stream_type, id,
           base::BindOnce(&AudioDebugRecordingHelper::StartDebugRecordingToFile,
                          weak_factory_.GetWeakPtr()));
}

void AudioDebugRecordingHelper::StartDebugRecordingToFile(base::File file) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  {
    base::AutoLock auto_lock(file_writer_lock_);

    if (!file.IsValid()) {
      PLOG(ERROR) << "Invalid debug recording file, error="
                  << file.error_details();
      file_writer_.reset();
      return;
    }

    file_writer_ = CreateAudioDebugFileWriter(params_, std::move(file));
  }
}

void AudioDebugRecordingHelper::DisableDebugRecording() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  {
    base::AutoLock auto_lock(file_writer_lock_);
    if (file_writer_) {
      WillDestroyAudioDebugFileWriter();
      file_writer_.reset();
    }
  }
}

void AudioDebugRecordingHelper::OnData(const AudioBus* source) {
  if (file_writer_lock_.Try()) {
    if (file_writer_) {
      file_writer_->Write(*source);
    }
    file_writer_lock_.Release();
  }
}

AudioDebugFileWriter::Ptr AudioDebugRecordingHelper::CreateAudioDebugFileWriter(
    const AudioParameters& params,
    base::File file) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  return AudioDebugFileWriter::Create(params, std::move(file));
}

void AudioDebugRecordingHelper::WillDestroyAudioDebugFileWriter() {
  // No special action required.
}

}  // namespace media