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"
16 #include "base/file_descriptor_posix.h"
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
29 class AudioSyncReader
: public media::AudioOutputController::SyncReader
{
31 AudioSyncReader(base::SharedMemory
* shared_memory
,
32 const media::AudioParameters
& params
);
34 ~AudioSyncReader() override
;
36 // media::AudioOutputController::SyncReader implementations.
37 void UpdatePendingBytes(uint32 bytes
) override
;
38 void Read(media::AudioBus
* dest
) override
;
39 void Close() override
;
42 bool PrepareForeignSocket(base::ProcessHandle process_handle
,
43 base::SyncSocket::TransitDescriptor
* descriptor
);
46 // Blocks until data is ready for reading or a timeout expires. Returns false
47 // if an error or timeout occurs.
48 bool WaitUntilDataIsReady();
50 const base::SharedMemory
* const shared_memory_
;
52 // Mutes all incoming samples. This is used to prevent audible sound
53 // during automated testing.
54 const bool mute_audio_
;
56 // Socket for transmitting audio data.
57 scoped_ptr
<base::CancelableSyncSocket
> socket_
;
59 // Socket to be used by the renderer. The reference is released after
60 // PrepareForeignSocketHandle() is called and ran successfully.
61 scoped_ptr
<base::CancelableSyncSocket
> foreign_socket_
;
63 // Shared memory wrapper used for transferring audio data to Read() callers.
64 scoped_ptr
<media::AudioBus
> output_bus_
;
66 // Maximum amount of audio data which can be transferred in one Read() call.
67 const int packet_size_
;
69 // Track the number of times the renderer missed its real-time deadline and
70 // report a UMA stat during destruction.
71 size_t renderer_callback_count_
;
72 size_t renderer_missed_callback_count_
;
74 // The maximum amount of time to wait for data from the renderer. Calculated
75 // from the parameters given at construction.
76 const base::TimeDelta maximum_wait_time_
;
78 // The index of the audio buffer we're expecting to be sent from the renderer;
79 // used to block with timeout for audio data.
82 DISALLOW_COPY_AND_ASSIGN(AudioSyncReader
);
85 } // namespace content
87 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_SYNC_READER_H_