audio_sync_reader.h revision a3f7b4e666c476898878fa745f637129375cd889
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_SYNC_READER_H_
6#define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_SYNC_READER_H_
7
8#include "base/file_descriptor_posix.h"
9#include "base/process/process.h"
10#include "base/sync_socket.h"
11#include "base/synchronization/lock.h"
12#include "base/time/time.h"
13#include "media/audio/audio_output_controller.h"
14#include "media/base/audio_bus.h"
15
16namespace base {
17class SharedMemory;
18}
19
20namespace content {
21
22// A AudioOutputController::SyncReader implementation using SyncSocket. This
23// is used by AudioOutputController to provide a low latency data source for
24// transmitting audio packets between the browser process and the renderer
25// process.
26class AudioSyncReader : public media::AudioOutputController::SyncReader {
27 public:
28  AudioSyncReader(base::SharedMemory* shared_memory,
29                  const media::AudioParameters& params,
30                  int input_channels);
31
32  virtual ~AudioSyncReader();
33
34  // media::AudioOutputController::SyncReader implementations.
35  virtual void UpdatePendingBytes(uint32 bytes) OVERRIDE;
36  virtual int Read(bool block,
37                   const media::AudioBus* source,
38                   media::AudioBus* dest) OVERRIDE;
39  virtual void Close() OVERRIDE;
40
41  bool Init();
42  bool PrepareForeignSocketHandle(base::ProcessHandle process_handle,
43#if defined(OS_WIN)
44                                  base::SyncSocket::Handle* foreign_handle);
45#else
46                                  base::FileDescriptor* foreign_handle);
47#endif
48
49 private:
50  // Indicates whether the renderer has data available for reading.
51  bool DataReady();
52
53  // Blocks until DataReady() is true or a timeout expires.
54  void WaitTillDataReady();
55
56  base::SharedMemory* shared_memory_;
57
58  // Number of input channels for synchronized I/O.
59  int input_channels_;
60
61  // Socket for transmitting audio data.
62  scoped_ptr<base::CancelableSyncSocket> socket_;
63
64  // Socket to be used by the renderer. The reference is released after
65  // PrepareForeignSocketHandle() is called and ran successfully.
66  scoped_ptr<base::CancelableSyncSocket> foreign_socket_;
67
68  // Shared memory wrapper used for transferring audio data to Read() callers.
69  scoped_ptr<media::AudioBus> output_bus_;
70
71  // Shared memory wrapper used for transferring audio data from Read() callers.
72  scoped_ptr<media::AudioBus> input_bus_;
73
74  // Maximum amount of audio data which can be transferred in one Read() call.
75  int packet_size_;
76
77  // Track the number of times the renderer missed its real-time deadline and
78  // report a UMA stat during destruction.
79  size_t renderer_callback_count_;
80  size_t renderer_missed_callback_count_;
81
82  DISALLOW_COPY_AND_ASSIGN(AudioSyncReader);
83};
84
85}  // namespace content
86
87#endif  // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_SYNC_READER_H_
88