summaryrefslogtreecommitdiffstats
path: root/media/audio/fake_audio_input_stream.cc
blob: d631fb8b4d67eb69da60a0d3d8982d6e59d5f3eb (plain)
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
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/audio/fake_audio_input_stream.h"

using base::Time;
using base::TimeDelta;

AudioInputStream* FakeAudioInputStream::MakeFakeStream(int channels,
                                                       int bits_per_sample,
                                                       int sampling_rate,
                                                       int samples_per_packet) {
  return new FakeAudioInputStream(channels, bits_per_sample, sampling_rate,
                                  samples_per_packet);
}

FakeAudioInputStream::FakeAudioInputStream(int channels, int bits_per_sample,
                                           int sampling_rate,
                                           int samples_per_packet)
    : callback_(NULL),
      buffer_size_((channels * bits_per_sample * samples_per_packet) / 8),
      thread_("FakeAudioRecordingThread"),
      callback_interval_ms_((samples_per_packet * 1000) / sampling_rate) {
  // This object is ref counted (so that it can be used with Thread, PostTask)
  // but the caller expects a plain pointer. So we take a reference here and
  // will Release() ourselves in Close().
  AddRef();
}

bool FakeAudioInputStream::Open() {
  buffer_.reset(new uint8[buffer_size_]);
  memset(buffer_.get(), 0, buffer_size_);
  return true;
}

void FakeAudioInputStream::Start(AudioInputCallback* callback)  {
  DCHECK(!thread_.IsRunning());
  callback_ = callback;
  last_callback_time_ = Time::Now();
  thread_.Start();
  thread_.message_loop()->PostDelayedTask(
      FROM_HERE,
      NewRunnableMethod(this, &FakeAudioInputStream::DoCallback),
      callback_interval_ms_);
}

void FakeAudioInputStream::DoCallback() {
  DCHECK(callback_);
  callback_->OnData(this, buffer_.get(), buffer_size_);

  Time now = Time::Now();
  int64 next_callback_ms = (last_callback_time_ +
      TimeDelta::FromMilliseconds(callback_interval_ms_ * 2) -
      now).InMilliseconds();
  // If we are falling behind, try to catch up as much as we can in the next
  // callback.
  if (next_callback_ms < 0)
    next_callback_ms = 0;

  last_callback_time_ = now;
  thread_.message_loop()->PostDelayedTask(
      FROM_HERE,
      NewRunnableMethod(this, &FakeAudioInputStream::DoCallback),
      next_callback_ms);
}

void FakeAudioInputStream::Stop() {
  thread_.Stop();
}

void FakeAudioInputStream::Close() {
  if (callback_) {
    callback_->OnClose(this);
    callback_ = NULL;
  }
  Release();  // Destoys this object.
}