Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / renderer_host / media / audio_sync_reader.h
blob58eb42ccab0a67d7bbe5a92e0465c92c7d75286d
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.
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_SYNC_READER_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_SYNC_READER_H_
8 #include "base/process/process.h"
9 #include "base/sync_socket.h"
10 #include "base/synchronization/lock.h"
11 #include "base/time/time.h"
12 #include "media/audio/audio_output_controller.h"
13 #include "media/base/audio_bus.h"
15 #if defined(OS_POSIX)
16 #include "base/file_descriptor_posix.h"
17 #endif
19 namespace base {
20 class SharedMemory;
23 namespace content {
25 // A AudioOutputController::SyncReader implementation using SyncSocket. This
26 // is used by AudioOutputController to provide a low latency data source for
27 // transmitting audio packets between the browser process and the renderer
28 // process.
29 class AudioSyncReader : public media::AudioOutputController::SyncReader {
30 public:
31 AudioSyncReader(base::SharedMemory* shared_memory,
32 const media::AudioParameters& params);
34 virtual ~AudioSyncReader();
36 // media::AudioOutputController::SyncReader implementations.
37 virtual void UpdatePendingBytes(uint32 bytes) OVERRIDE;
38 virtual void Read(const media::AudioBus* source,
39 media::AudioBus* dest) OVERRIDE;
40 virtual void Close() OVERRIDE;
42 bool Init();
43 bool PrepareForeignSocketHandle(base::ProcessHandle process_handle,
44 #if defined(OS_WIN)
45 base::SyncSocket::Handle* foreign_handle);
46 #else
47 base::FileDescriptor* foreign_handle);
48 #endif
50 private:
51 // Blocks until data is ready for reading or a timeout expires. Returns false
52 // if an error or timeout occurs.
53 bool WaitUntilDataIsReady();
55 const base::SharedMemory* const shared_memory_;
57 // Mutes all incoming samples. This is used to prevent audible sound
58 // during automated testing.
59 const bool mute_audio_;
61 // Socket for transmitting audio data.
62 scoped_ptr<base::CancelableSyncSocket> socket_;
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_;
68 // Shared memory wrapper used for transferring audio data to Read() callers.
69 scoped_ptr<media::AudioBus> output_bus_;
71 // Maximum amount of audio data which can be transferred in one Read() call.
72 const int packet_size_;
74 // Track the number of times the renderer missed its real-time deadline and
75 // report a UMA stat during destruction.
76 size_t renderer_callback_count_;
77 size_t renderer_missed_callback_count_;
79 // The maximum amount of time to wait for data from the renderer. Calculated
80 // from the parameters given at construction.
81 const base::TimeDelta maximum_wait_time_;
83 // The index of the audio buffer we're expecting to be sent from the renderer;
84 // used to block with timeout for audio data.
85 uint32 buffer_index_;
87 DISALLOW_COPY_AND_ASSIGN(AudioSyncReader);
90 } // namespace content
92 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_SYNC_READER_H_