summaryrefslogtreecommitdiffstats
path: root/chrome/browser/renderer_host/audio_renderer_host.cc
blob: fd798ac665eca632cf08266099f19d41e494cf33 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (c) 2006-2009 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 "base/message_loop.h"
#include "base/process.h"
#include "chrome/browser/renderer_host/audio_renderer_host.h"

AudioRendererHost::IPCAudioSource::IPCAudioSource(
    AudioRendererHost* host,
    int32 id,
    AudioOutputStream* stream,
    IPC::Message::Sender* sender,
    base::ProcessHandle process,
    size_t packet_size)
    : host_(host),
      id_(id),
      stream_(stream),
      sender_(sender) {
  // Make sure we can create and map the shared memory.
  DCHECK(shared_memory_.Create(L"", false, false, packet_size) &&
         shared_memory_.Map(packet_size));
  // Also make sure we can create the buffer and share to render process.
  DCHECK(shared_memory_.ShareToProcess(process, &foreign_memory_handle_));
}

AudioRendererHost::IPCAudioSource::~IPCAudioSource() {
}

size_t AudioRendererHost::IPCAudioSource::OnMoreData(AudioOutputStream* stream,
                                                     void* dest,
                                                     size_t max_size) {
  // TODO(hclam): send an IPC message to renderer provided with a
  // SharedMemoryHandle for audio renderer inside render process to fill in.
  // We should sleep until we receive a notification from render process that
  // the buffer is ready or this source is closed or an error is encountered.
  // Stuff do there here:
  // 1. Prepare the SharedMemory.
  // 2. Send an IPC.
  // 3. Wait until we receive notification.
  return 0;
}

void AudioRendererHost::IPCAudioSource::OnClose(AudioOutputStream* stream) {
  // TODO(hclam): should set a flag here and wakes up the thread waiting for
  // audio buffer. Should also make sure this call come from the thread as this
  // object is created.
  // Stuff to do here:
  // 1. Send an IPC to renderer about close complete.
  // 2. Remove this object from host.
  host_->DestroySource(id_);
}

void AudioRendererHost::IPCAudioSource::OnError(AudioOutputStream* stream,
                                                int code) {
  // TODO(hclam): make sure this method is received in the same thread as this
  // object is created and remove this object and from the map gracefully.
  // Stuff to do here:
  // 1. Send an IPC to renderer about the error.
  // 2. Close the stream so it would get closed.
  // TODO(cpu): make sure it is safe to close() in this method.
  stream_->Close();
}

void AudioRendererHost::IPCAudioSource::NotifyPacketReady() {
  // TODO(hclam): wake the thread waiting for buffer.
}

AudioRendererHost::AudioRendererHost(MessageLoop* message_loop)
    : next_id_(INVALID_ID+1),
      io_loop_(message_loop) {
  // Make sure we perform actual initialization operations in the thread where
  // this object should live.
  io_loop_->PostTask(FROM_HERE,
      NewRunnableMethod(this, &AudioRendererHost::OnInitialized));
}

AudioRendererHost::~AudioRendererHost() {
}

int AudioRendererHost::CreateStream(
    IPC::Message::Sender* sender, base::ProcessHandle handle,
    AudioManager::Format format, int channels, int sample_rate,
    int bits_per_sample, size_t packet_size) {
  DCHECK(MessageLoop::current() == io_loop_);

  // Create the stream in the first place.
  AudioOutputStream* stream = AudioManager::GetAudioManager()->MakeAudioStream(
      format, channels, sample_rate, bits_per_sample);
  if (!stream)
    return INVALID_ID;

  // Try to open the stream if we can create it.
  if (stream->Open(packet_size)) {
    // Create the containing IPCAudioSource and save it to the map.
    IPCAudioSource* source =
        new IPCAudioSource(this, next_id_++, stream, sender, handle,
                           packet_size);
    sources_.AddWithID(source, source->id());
    return source->id();
  }
  return INVALID_ID;
}

bool AudioRendererHost::Start(int stream_id) {
  DCHECK(MessageLoop::current() == io_loop_);
  IPCAudioSource* source = sources_.Lookup(stream_id);
  if (source) {
    source->stream()->Start(source);
    return true;
  }
  return false;
}

bool AudioRendererHost::Stop(int stream_id) {
  DCHECK(MessageLoop::current() == io_loop_);
  IPCAudioSource* source = sources_.Lookup(stream_id);
  if (source) {
    source->stream()->Stop();
    return true;
  }
  return false;
}

bool AudioRendererHost::Close(int stream_id) {
  DCHECK(MessageLoop::current() == io_loop_);
  IPCAudioSource* source = sources_.Lookup(stream_id);
  if (source) {
    source->stream()->Close();
    return true;
  }
  return false;
}

bool AudioRendererHost::SetVolume(
    int stream_id, double left_channel, double right_channel) {
  DCHECK(MessageLoop::current() == io_loop_);
  IPCAudioSource* source = sources_.Lookup(stream_id);
  if (source) {
    source->stream()->SetVolume(left_channel, right_channel);
  }
  return false;
}

bool AudioRendererHost::GetVolume(
    int stream_id, double* left_channel, double* right_channel) {
  DCHECK(MessageLoop::current() == io_loop_);
  IPCAudioSource* source = sources_.Lookup(stream_id);
  if (source) {
    source->stream()->GetVolume(left_channel, right_channel);
    return true;
  }
  return false;
}

void AudioRendererHost::NotifyPacketReady(int stream_id) {
  DCHECK(MessageLoop::current() == io_loop_);
  IPCAudioSource* source = sources_.Lookup(stream_id);
  if (source) {
    source->NotifyPacketReady();
  }
}

void AudioRendererHost::DestroyAllStreams() {
  DCHECK(MessageLoop::current() == io_loop_);
  // TODO(hclam): iterate on the map, close and delete every stream, and clear
  // the map.
}

void AudioRendererHost::DestroySource(int stream_id) {
  DCHECK(MessageLoop::current() == io_loop_);
  IPCAudioSource* source = sources_.Lookup(stream_id);
  if (source) {
    sources_.Remove(stream_id);
    delete source;
  }
}

void AudioRendererHost::Destroy() {
  // Post a message to the thread where this object should live and do the
  // actual operations there.
  io_loop_->PostTask(
      FROM_HERE, NewRunnableMethod(this, &AudioRendererHost::OnDestroyed));
}

void AudioRendererHost::OnInitialized() {
  DCHECK(MessageLoop::current() == io_loop_);

  // Increase the ref count of this object so it is active until we do
  // Release().
  AddRef();

  // Also create the AudioManager singleton in this thread.
  // TODO(hclam): figure out a better location to initialize the AudioManager
  // singleton.
  AudioManager::GetAudioManager();
}

void AudioRendererHost::OnDestroyed() {
  DCHECK(MessageLoop::current() == io_loop_);

  // Destroy audio streams only in the thread it should happen.
  // TODO(hclam): make sure we don't call IPC::Message::Sender inside
  // IPCAudioSource because it is most likely be destroyed.
  DestroyAllStreams();

  // Decrease the reference to this object, which may lead to self-destruction.
  Release();
}