Commit message: None
CVE: None
Vulnerability description: None

Project: Chrome

static bool CheckParameters(AudioParameters params) {
  if (!params.IsValid())
    return false;
  return true;
}

Project: Chrome

void AudioOutputController::Close(Task* closed_task) {
  DCHECK(closed_task);
  DCHECK(message_loop_);
  message_loop_->PostTask(
      FROM_HERE,
      NewRunnableMethod(this, &AudioOutputController::DoClose, closed_task));
}

Project: Chrome

scoped_refptr<AudioOutputController> AudioOutputController::CreateLowLatency(
    EventHandler* event_handler,
    AudioParameters params,
    SyncReader* sync_reader) {

  DCHECK(sync_reader);

  if (!CheckParameters(params))
    return NULL;

  if (!AudioManager::GetAudioManager())
    return NULL;

  scoped_refptr<AudioOutputController> controller(new AudioOutputController(
      event_handler, 0, sync_reader));

  controller->message_loop_ =
      AudioManager::GetAudioManager()->GetMessageLoop();
  controller->message_loop_->PostTask(
      FROM_HERE,
      NewRunnableMethod(controller.get(), &AudioOutputController::DoCreate,
                        params));
  return controller;
}

Project: Chrome

void AudioOutputController::DoClose(Task* closed_task) {
  DCHECK_EQ(message_loop_, MessageLoop::current());

  if (state_ != kClosed) {
    if (stream_) {
      stream_->Stop();
      stream_->Close();
      stream_ = NULL;
    }

    if (LowLatencyMode()) {
      sync_reader_->Close();
    }

    state_ = kClosed;
  }

  closed_task->Run();
  delete closed_task;
}

Project: Chrome

void AudioOutputController::DoCreate(AudioParameters params) {
  DCHECK_EQ(message_loop_, MessageLoop::current());

  if (state_ == kClosed)
    return;
  DCHECK(state_ == kEmpty);

  if (!AudioManager::GetAudioManager())
    return;

  stream_ = AudioManager::GetAudioManager()->MakeAudioOutputStreamProxy(params);
  if (!stream_) {
    handler_->OnError(this, 0);
    return;
  }

  if (!stream_->Open()) {
    stream_->Close();
    stream_ = NULL;

    handler_->OnError(this, 0);
    return;
  }

  stream_->SetVolume(volume_);

  state_ = kCreated;

  handler_->OnCreated(this);

  if (!LowLatencyMode()) {
    AutoLock auto_lock(lock_);
    SubmitOnMoreData_Locked();
  }
}

Project: Chrome

void AudioOutputController::DoPause() {
  DCHECK_EQ(message_loop_, MessageLoop::current());

  if (state_ != kPlaying)
    return;
  state_ = kPaused;

  stream_->Stop();

  if (LowLatencyMode()) {
    sync_reader_->UpdatePendingBytes(kPauseMark);
  }

  handler_->OnPaused(this);
}

Project: Chrome

void AudioOutputController::DoReportError(int code) {
  DCHECK_EQ(message_loop_, MessageLoop::current());
  if (state_ != kClosed)
    handler_->OnError(this, code);
}

Project: Chrome

void AudioOutputController::DoSetVolume(double volume) {
  DCHECK_EQ(message_loop_, MessageLoop::current());

  volume_ = volume;

  if (state_ != kPlaying && state_ != kPaused && state_ != kCreated)
    return;

  stream_->SetVolume(volume_);
}

Project: Chrome

void AudioOutputController::EnqueueData(const uint8* data, uint32 size) {
  AutoLock auto_lock(lock_);
  pending_request_ = false;
  if (size) {
    buffer_.Append(data, size);
    SubmitOnMoreData_Locked();
  }
}

Project: Chrome

void AudioOutputController::Flush() {
  DCHECK(message_loop_);
  message_loop_->PostTask(
      FROM_HERE,
      NewRunnableMethod(this, &AudioOutputController::DoFlush));
}

Project: Chrome

void AudioOutputController::OnError(AudioOutputStream* stream, int code) {
  message_loop_->PostTask(
      FROM_HERE,
      NewRunnableMethod(this, &AudioOutputController::DoReportError, code));
}

Project: Chrome

uint32 AudioOutputController::OnMoreData(
    AudioOutputStream* stream, uint8* dest,
    uint32 max_size, AudioBuffersState buffers_state) {
  if (!sync_reader_) {
    AutoLock auto_lock(lock_);

    buffers_state_ = buffers_state;

    if (state_ != kPlaying) {
      return 0;
    }

    uint32 size = buffer_.Read(dest, max_size);
    buffers_state_.pending_bytes += size;
    SubmitOnMoreData_Locked();
    return size;
  }

  uint32 size =  sync_reader_->Read(dest, max_size);
  sync_reader_->UpdatePendingBytes(buffers_state.total_bytes() + size);
  return size;
}

Project: Chrome

void AudioOutputController::Pause() {
  DCHECK(message_loop_);
  message_loop_->PostTask(
      FROM_HERE,
      NewRunnableMethod(this, &AudioOutputController::DoPause));
}

Project: Chrome

void AudioOutputController::SubmitOnMoreData_Locked() {
  lock_.AssertAcquired();

  if (buffer_.forward_bytes() > buffer_.forward_capacity())
    return;

  if (pending_request_)
    return;
  pending_request_ = true;

  AudioBuffersState buffers_state = buffers_state_;
  buffers_state.pending_bytes += buffer_.forward_bytes();

  AutoUnlock auto_unlock(lock_);
  handler_->OnMoreData(this, buffers_state);
}