summaryrefslogtreecommitdiffstats
path: root/media/audio/null_audio_sink.cc
blob: c93ceb1efcbad9d434fe9d8ee01649bd94d2497d (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) 2012 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/null_audio_sink.h"

#include "base/bind.h"
#include "base/stringprintf.h"
#include "base/sys_byteorder.h"
#include "base/threading/platform_thread.h"

namespace media {

NullAudioSink::NullAudioSink()
    : initialized_(false),
      playing_(false),
      callback_(NULL),
      thread_("NullAudioThread"),
      hash_audio_for_testing_(false) {
}

void NullAudioSink::Initialize(const AudioParameters& params,
                               RenderCallback* callback) {
  DCHECK(!initialized_);
  params_ = params;

  audio_bus_ = AudioBus::Create(params_);

  if (hash_audio_for_testing_) {
    md5_channel_contexts_.reset(new base::MD5Context[params_.channels()]);
    for (int i = 0; i < params_.channels(); i++)
      base::MD5Init(&md5_channel_contexts_[i]);
  }

  callback_ = callback;
  initialized_ = true;
}

void NullAudioSink::Start() {
  if (!thread_.Start())
    return;

  thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
      &NullAudioSink::FillBufferTask, this));
}

void NullAudioSink::Stop() {
  SetPlaying(false);
  thread_.Stop();
}

void NullAudioSink::Play() {
  SetPlaying(true);
}

void NullAudioSink::Pause(bool /* flush */) {
  SetPlaying(false);
}

bool NullAudioSink::SetVolume(double volume) {
  // Audio is always muted.
  return volume == 0.0;
}

void NullAudioSink::SetPlaying(bool is_playing) {
  base::AutoLock auto_lock(lock_);
  playing_ = is_playing;
}

NullAudioSink::~NullAudioSink() {
  DCHECK(!thread_.IsRunning());
}

void NullAudioSink::FillBufferTask() {
  base::AutoLock auto_lock(lock_);

  base::TimeDelta delay;
  // Only consume buffers when actually playing.
  if (playing_)  {
    int frames_received = callback_->Render(audio_bus_.get(), 0);
    int frames_per_millisecond =
        params_.sample_rate() / base::Time::kMillisecondsPerSecond;

    if (hash_audio_for_testing_ && frames_received > 0) {
      DCHECK_EQ(sizeof(float), sizeof(uint32));
      int channels = audio_bus_->channels();
      for (int channel_idx = 0; channel_idx < channels; ++channel_idx) {
        float* channel = audio_bus_->channel(channel_idx);
        for (int frame_idx = 0; frame_idx < frames_received; frame_idx++) {
          // Convert float to uint32 w/o conversion loss.
          uint32 frame = base::ByteSwapToLE32(
              bit_cast<uint32>(channel[frame_idx]));
          base::MD5Update(
              &md5_channel_contexts_[channel_idx], base::StringPiece(
                  reinterpret_cast<char*>(&frame), sizeof(frame)));
        }
      }
    }

    // Calculate our sleep duration.
    delay = base::TimeDelta::FromMilliseconds(
        frames_received / frames_per_millisecond);
  } else {
    // If paused, sleep for 10 milliseconds before polling again.
    delay = base::TimeDelta::FromMilliseconds(10);
  }

  // Sleep for at least one millisecond so we don't spin the CPU.
  MessageLoop::current()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&NullAudioSink::FillBufferTask, this),
      std::max(delay, base::TimeDelta::FromMilliseconds(1)));
}

void NullAudioSink::StartAudioHashForTesting() {
  DCHECK(!initialized_);
  hash_audio_for_testing_ = true;
}

std::string NullAudioSink::GetAudioHashForTesting() {
  DCHECK(hash_audio_for_testing_);

  // If initialize failed or was never called, ensure we return an empty hash.
  int channels = 1;
  if (!initialized_) {
    md5_channel_contexts_.reset(new base::MD5Context[1]);
    base::MD5Init(&md5_channel_contexts_[0]);
  } else {
    channels = audio_bus_->channels();
  }

  // Hash all channels into the first channel.
  base::MD5Digest digest;
  for (int i = 1; i < channels; i++) {
    base::MD5Final(&digest, &md5_channel_contexts_[i]);
    base::MD5Update(&md5_channel_contexts_[0], base::StringPiece(
        reinterpret_cast<char*>(&digest), sizeof(base::MD5Digest)));
  }

  base::MD5Final(&digest, &md5_channel_contexts_[0]);
  return base::MD5DigestToBase16(digest);
}

}  // namespace media