summaryrefslogtreecommitdiffstats
path: root/chromecast/renderer/media/audio_pipeline_proxy.cc
blob: 30297a8a549b2c1c696653b563cc444b57745b94 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright 2014 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 "chromecast/renderer/media/audio_pipeline_proxy.h"

#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/memory/shared_memory.h"
#include "base/message_loop/message_loop.h"
#include "base/threading/thread_checker.h"
#include "chromecast/common/media/cma_messages.h"
#include "chromecast/common/media/shared_memory_chunk.h"
#include "chromecast/media/cma/base/buffering_defs.h"
#include "chromecast/media/cma/base/cma_logging.h"
#include "chromecast/media/cma/base/coded_frame_provider.h"
#include "chromecast/media/cma/ipc/media_message_fifo.h"
#include "chromecast/media/cma/ipc_streamer/av_streamer_proxy.h"
#include "chromecast/media/cma/pipeline/av_pipeline_client.h"
#include "chromecast/renderer/media/cma_message_filter_proxy.h"
#include "chromecast/renderer/media/media_channel_proxy.h"
#include "media/base/bind_to_current_loop.h"
#include "media/base/pipeline_status.h"

namespace chromecast {
namespace media {

namespace {

void IgnoreResult() {
}

}  // namespace

// AudioPipelineProxyInternal -
// This class is not thread safe and should run on the same thread
// as the media channel proxy.
class AudioPipelineProxyInternal {
 public:
  typedef base::Callback<void(scoped_ptr<base::SharedMemory>)> SharedMemCB;

  static void Release(scoped_ptr<AudioPipelineProxyInternal> proxy);

  explicit AudioPipelineProxyInternal(
      scoped_refptr<MediaChannelProxy> media_channel_proxy);
  virtual ~AudioPipelineProxyInternal();

  // Notify the other side (browser process) of some activity on the audio pipe.
  // TODO(erickung): either send an IPC message or write a byte on the
  // SyncSocket.
  void NotifyPipeWrite();

  // These functions are almost a one to one correspondence with AudioPipeline
  // but this is an internal class and there is no reason to derive from
  // AudioPipeline.
  void SetClient(const base::Closure& pipe_read_cb,
                 const AvPipelineClient& client);
  void CreateAvPipe(const SharedMemCB& shared_mem_cb);
  void Initialize(const ::media::AudioDecoderConfig& config,
                  const ::media::PipelineStatusCB& status_cb);
  void SetVolume(float volume);

 private:
  void Shutdown();

  // Callbacks for CmaMessageFilterHost::AudioDelegate.
  void OnAvPipeCreated(bool status,
                       base::SharedMemoryHandle shared_mem_handle,
                       base::FileDescriptor socket);
  void OnStateChanged(::media::PipelineStatus status);

  base::ThreadChecker thread_checker_;

  scoped_refptr<MediaChannelProxy> media_channel_proxy_;

  // Store the callback for a pending state transition.
  ::media::PipelineStatusCB status_cb_;

  SharedMemCB shared_mem_cb_;

  DISALLOW_COPY_AND_ASSIGN(AudioPipelineProxyInternal);
};

// static
void AudioPipelineProxyInternal::Release(
    scoped_ptr<AudioPipelineProxyInternal> proxy) {
  proxy->Shutdown();
}

AudioPipelineProxyInternal::AudioPipelineProxyInternal(
    scoped_refptr<MediaChannelProxy> media_channel_proxy)
    : media_channel_proxy_(media_channel_proxy) {
  DCHECK(media_channel_proxy.get());

  // Creation can be done on a different thread.
  thread_checker_.DetachFromThread();
}

AudioPipelineProxyInternal::~AudioPipelineProxyInternal() {
}

void AudioPipelineProxyInternal::Shutdown() {
  DCHECK(thread_checker_.CalledOnValidThread());

  // Remove any callback on AudioPipelineProxyInternal.
  media_channel_proxy_->SetAudioDelegate(
      CmaMessageFilterProxy::AudioDelegate());
}

void AudioPipelineProxyInternal::NotifyPipeWrite() {
  DCHECK(thread_checker_.CalledOnValidThread());

  // TODO(erickung): An alternative way would be to use a dedicated socket for
  // this event.
  bool success = media_channel_proxy_->Send(scoped_ptr<IPC::Message>(
      new CmaHostMsg_NotifyPipeWrite(
          media_channel_proxy_->GetId(), kAudioTrackId)));
  VLOG_IF(4, !success) << "Sending msg failed";
}

void AudioPipelineProxyInternal::SetClient(
    const base::Closure& pipe_read_cb,
    const AvPipelineClient& client) {
  DCHECK(thread_checker_.CalledOnValidThread());

  CmaMessageFilterProxy::AudioDelegate delegate;
  delegate.av_pipe_cb =
      base::Bind(&AudioPipelineProxyInternal::OnAvPipeCreated,
                 base::Unretained(this));
  delegate.state_changed_cb =
      base::Bind(&AudioPipelineProxyInternal::OnStateChanged,
                 base::Unretained(this));
  delegate.pipe_read_cb = pipe_read_cb;
  delegate.client = client;
  bool success = media_channel_proxy_->SetAudioDelegate(delegate);
  CHECK(success);
}

void AudioPipelineProxyInternal::CreateAvPipe(
    const SharedMemCB& shared_mem_cb) {
  DCHECK(thread_checker_.CalledOnValidThread());
  DCHECK(shared_mem_cb_.is_null());
  bool success = media_channel_proxy_->Send(scoped_ptr<IPC::Message>(
      new CmaHostMsg_CreateAvPipe(
          media_channel_proxy_->GetId(), kAudioTrackId, kAppAudioBufferSize)));
  if (!success) {
    shared_mem_cb.Run(scoped_ptr<base::SharedMemory>());
    return;
  }
  shared_mem_cb_ = shared_mem_cb;
}

void AudioPipelineProxyInternal::OnAvPipeCreated(
    bool success,
    base::SharedMemoryHandle shared_mem_handle,
    base::FileDescriptor socket) {
  DCHECK(thread_checker_.CalledOnValidThread());
  DCHECK(!shared_mem_cb_.is_null());
  if (!success) {
    shared_mem_cb_.Run(scoped_ptr<base::SharedMemory>());
    return;
  }

  CHECK(base::SharedMemory::IsHandleValid(shared_mem_handle));
  shared_mem_cb_.Run(scoped_ptr<base::SharedMemory>(
      new base::SharedMemory(shared_mem_handle, false)));
}

void AudioPipelineProxyInternal::Initialize(
    const ::media::AudioDecoderConfig& config,
    const ::media::PipelineStatusCB& status_cb) {
  DCHECK(thread_checker_.CalledOnValidThread());
  bool success = media_channel_proxy_->Send(scoped_ptr<IPC::Message>(
      new CmaHostMsg_AudioInitialize(
          media_channel_proxy_->GetId(), kAudioTrackId, config)));
  if (!success) {
    status_cb.Run( ::media::PIPELINE_ERROR_INITIALIZATION_FAILED);
    return;
  }
  DCHECK(status_cb_.is_null());
  status_cb_ = status_cb;
}

void AudioPipelineProxyInternal::SetVolume(float volume) {
  DCHECK(thread_checker_.CalledOnValidThread());
  media_channel_proxy_->Send(scoped_ptr<IPC::Message>(
      new CmaHostMsg_SetVolume(media_channel_proxy_->GetId(),
                               kAudioTrackId, volume)));
}

void AudioPipelineProxyInternal::OnStateChanged(
    ::media::PipelineStatus status) {
  DCHECK(thread_checker_.CalledOnValidThread());
  DCHECK(!status_cb_.is_null());
  base::ResetAndReturn(&status_cb_).Run(status);
}


// A macro runs current member function on |io_message_loop_proxy_| thread.
#define FORWARD_ON_IO_THREAD(param_fn, ...) \
  io_message_loop_proxy_->PostTask( \
      FROM_HERE, \
      base::Bind(&AudioPipelineProxyInternal::param_fn, \
                 base::Unretained(proxy_.get()), ##__VA_ARGS__))

AudioPipelineProxy::AudioPipelineProxy(
    scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy,
    scoped_refptr<MediaChannelProxy> media_channel_proxy)
    : io_message_loop_proxy_(io_message_loop_proxy),
      proxy_(new AudioPipelineProxyInternal(media_channel_proxy)),
      audio_streamer_(new AvStreamerProxy()),
      weak_factory_(this) {
  DCHECK(io_message_loop_proxy_.get());
  weak_this_ = weak_factory_.GetWeakPtr();
  thread_checker_.DetachFromThread();
}

AudioPipelineProxy::~AudioPipelineProxy() {
  DCHECK(thread_checker_.CalledOnValidThread());
  // Release the underlying object on the right thread.
  io_message_loop_proxy_->PostTask(
      FROM_HERE,
      base::Bind(&AudioPipelineProxyInternal::Release, base::Passed(&proxy_)));
}

void AudioPipelineProxy::SetClient(
    const AvPipelineClient& client) {
  DCHECK(thread_checker_.CalledOnValidThread());
  base::Closure pipe_read_cb = ::media::BindToCurrentLoop(
      base::Bind(&AudioPipelineProxy::OnPipeRead, weak_this_));
  FORWARD_ON_IO_THREAD(SetClient, pipe_read_cb, client);
}

void AudioPipelineProxy::Initialize(
    const ::media::AudioDecoderConfig& config,
    scoped_ptr<CodedFrameProvider> frame_provider,
    const ::media::PipelineStatusCB& status_cb) {
  CMALOG(kLogControl) << "AudioPipelineProxy::Initialize";
  DCHECK(thread_checker_.CalledOnValidThread());
  audio_streamer_->SetCodedFrameProvider(frame_provider.Pass());

  AudioPipelineProxyInternal::SharedMemCB shared_mem_cb =
      ::media::BindToCurrentLoop(base::Bind(
          &AudioPipelineProxy::OnAvPipeCreated, weak_this_,
          config, status_cb));
  FORWARD_ON_IO_THREAD(CreateAvPipe, shared_mem_cb);
}

void AudioPipelineProxy::OnAvPipeCreated(
    const ::media::AudioDecoderConfig& config,
    const ::media::PipelineStatusCB& status_cb,
    scoped_ptr<base::SharedMemory> shared_memory) {
  CMALOG(kLogControl) << "AudioPipelineProxy::OnAvPipeCreated";
  DCHECK(thread_checker_.CalledOnValidThread());
  if (!shared_memory ||
      !shared_memory->Map(kAppAudioBufferSize)) {
    status_cb.Run(::media::PIPELINE_ERROR_INITIALIZATION_FAILED);
    return;
  }
  CHECK(shared_memory->memory());

  scoped_ptr<MediaMemoryChunk> shared_memory_chunk(
      new SharedMemoryChunk(shared_memory.Pass(), kAppAudioBufferSize));
  scoped_ptr<MediaMessageFifo> audio_pipe(
      new MediaMessageFifo(shared_memory_chunk.Pass(), false));
  audio_pipe->ObserveWriteActivity(
      base::Bind(&AudioPipelineProxy::OnPipeWrite, weak_this_));

  audio_streamer_->SetMediaMessageFifo(audio_pipe.Pass());

  // Now proceed to the decoder/renderer initialization.
  FORWARD_ON_IO_THREAD(Initialize, config, status_cb);
}

void AudioPipelineProxy::StartFeeding() {
  DCHECK(thread_checker_.CalledOnValidThread());
  DCHECK(audio_streamer_);
  audio_streamer_->Start();
}

void AudioPipelineProxy::Flush(const base::Closure& done_cb) {
  DCHECK(thread_checker_.CalledOnValidThread());
  DCHECK(audio_streamer_);
  audio_streamer_->StopAndFlush(done_cb);
}

void AudioPipelineProxy::Stop() {
  DCHECK(thread_checker_.CalledOnValidThread());
  if (!audio_streamer_)
    return;
  audio_streamer_->StopAndFlush(base::Bind(&IgnoreResult));
}

void AudioPipelineProxy::SetVolume(float volume) {
  DCHECK(thread_checker_.CalledOnValidThread());
  FORWARD_ON_IO_THREAD(SetVolume, volume);
}

void AudioPipelineProxy::OnPipeWrite() {
  DCHECK(thread_checker_.CalledOnValidThread());
  FORWARD_ON_IO_THREAD(NotifyPipeWrite);
}

void AudioPipelineProxy::OnPipeRead() {
  DCHECK(thread_checker_.CalledOnValidThread());
  if (audio_streamer_)
    audio_streamer_->OnFifoReadEvent();
}

}  // namespace cma
}  // namespace chromecast