Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(413)

Side by Side Diff: content/renderer/media/webaudio_media_stream_source.h

Issue 1834323002: MediaStream audio: Refactor 3 separate "glue" implementations into one. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments about PS1 addressed. Fixed is_stopped_/StopSource() foo. REBASE Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CONTENT_RENDERER_MEDIA_WEBAUDIO_CAPTURER_SOURCE_H_ 5 #ifndef CONTENT_RENDERER_MEDIA_WEBAUDIO_MEDIA_STREAM_SOURCE_H_
6 #define CONTENT_RENDERER_MEDIA_WEBAUDIO_CAPTURER_SOURCE_H_ 6 #define CONTENT_RENDERER_MEDIA_WEBAUDIO_MEDIA_STREAM_SOURCE_H_
7 7
8 #include <stddef.h> 8 #include "base/memory/scoped_ptr.h"
9
10 #include "base/macros.h"
11 #include "base/synchronization/lock.h"
12 #include "base/threading/thread_checker.h"
13 #include "base/time/time.h" 9 #include "base/time/time.h"
14 #include "media/audio/audio_parameters.h" 10 #include "content/renderer/media/media_stream_audio_source.h"
15 #include "media/base/audio_bus.h" 11 #include "media/base/audio_bus.h"
16 #include "media/base/audio_capturer_source.h"
17 #include "media/base/audio_push_fifo.h" 12 #include "media/base/audio_push_fifo.h"
18 #include "third_party/WebKit/public/platform/WebAudioDestinationConsumer.h" 13 #include "third_party/WebKit/public/platform/WebAudioDestinationConsumer.h"
19 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" 14 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
20 #include "third_party/WebKit/public/platform/WebVector.h" 15 #include "third_party/WebKit/public/platform/WebVector.h"
21 16
22 namespace content { 17 namespace content {
23 18
24 class WebRtcLocalAudioTrack; 19 // Implements the WebAudioDestinationConsumer interface to provide a source of
20 // audio data (i.e., the output from a graph of WebAudio nodes) to one or more
21 // MediaStreamAudioTracks. Audio data is transported directly to the tracks in
22 // 10 ms chunks.
23 class WebAudioMediaStreamSource final
24 : NON_EXPORTED_BASE(public MediaStreamAudioSource),
25 public blink::WebAudioDestinationConsumer {
26 public:
27 explicit WebAudioMediaStreamSource(blink::WebMediaStreamSource* blink_source);
25 28
26 // WebAudioCapturerSource is the missing link between 29 ~WebAudioMediaStreamSource() override;
27 // WebAudio's MediaStreamAudioDestinationNode and WebRtcLocalAudioTrack.
28 //
29 // 1. WebKit calls the setFormat() method setting up the basic stream format
30 // (channels, and sample-rate).
31 // 2. consumeAudio() is called periodically by WebKit which dispatches the
32 // audio stream to the WebRtcLocalAudioTrack::Capture() method.
33 class WebAudioCapturerSource : public blink::WebAudioDestinationConsumer {
34 public:
35 explicit WebAudioCapturerSource(blink::WebMediaStreamSource* blink_source);
36 30
37 ~WebAudioCapturerSource() override; 31 private:
38
39 // WebAudioDestinationConsumer implementation. 32 // WebAudioDestinationConsumer implementation.
40 // setFormat() is called early on, so that we can configure the audio track. 33 //
34 // Note: Blink ensures setFormat() and consumeAudio() are not called
35 // concurrently across threads, but these methods could be called on any
36 // thread.
41 void setFormat(size_t number_of_channels, float sample_rate) override; 37 void setFormat(size_t number_of_channels, float sample_rate) override;
42 // MediaStreamAudioDestinationNode periodically calls consumeAudio().
43 // Called on the WebAudio audio thread.
44 void consumeAudio(const blink::WebVector<const float*>& audio_data, 38 void consumeAudio(const blink::WebVector<const float*>& audio_data,
45 size_t number_of_frames) override; 39 size_t number_of_frames) override;
46 40
47 // Called when the WebAudioCapturerSource is hooking to a media audio track.
48 // |track| is the sink of the data flow and must remain alive until Stop() is
49 // called.
50 void Start(WebRtcLocalAudioTrack* track);
51
52 // Called when the media audio track is stopping.
53 void Stop();
54
55 private:
56 // Called by AudioPushFifo zero or more times during the call to 41 // Called by AudioPushFifo zero or more times during the call to
57 // consumeAudio(). Delivers audio data with the required buffer size to the 42 // consumeAudio(). Delivers audio data with the required buffer size to the
58 // track. 43 // tracks.
59 void DeliverRebufferedAudio(const media::AudioBus& audio_bus, 44 void DeliverRebufferedAudio(const media::AudioBus& audio_bus,
60 int frame_delay); 45 int frame_delay);
61 46
62 // Deregisters this object from its blink::WebMediaStreamSource. 47 // MediaStreamAudioSource implementation.
63 void DeregisterFromBlinkSource(); 48 bool EnsureSourceIsStarted() final;
49 void EnsureSourceIsStopped() final;
64 50
65 // Used to DCHECK that some methods are called on the correct thread. 51 // In debug builds, check that all methods that could cause object graph
52 // or data flow changes are being called on the main thread.
66 base::ThreadChecker thread_checker_; 53 base::ThreadChecker thread_checker_;
67 54
68 // The audio track this WebAudioCapturerSource is feeding data to. 55 // True while this WebAudioMediaStreamSource is registered with
69 WebRtcLocalAudioTrack* track_; 56 // |blink_source_| and is consuming audio.
70 57 bool is_registered_consumer_;
71 media::AudioParameters params_;
72
73 // Flag to help notify the |track_| when the audio format has changed.
74 bool audio_format_changed_;
75 58
76 // A wrapper used for providing audio to |fifo_|. 59 // A wrapper used for providing audio to |fifo_|.
77 scoped_ptr<media::AudioBus> wrapper_bus_; 60 scoped_ptr<media::AudioBus> wrapper_bus_;
78 61
79 // Takes in the audio data passed to consumeAudio() and re-buffers it into 10 62 // Takes in the audio data passed to consumeAudio() and re-buffers it into 10
80 // ms chunks for the track. This ensures each chunk of audio delivered to the 63 // ms chunks for the tracks. This ensures each chunk of audio delivered to
81 // track has the required buffer size, regardless of the amount of audio 64 // the tracks has the required buffer size, regardless of the amount of audio
82 // provided via each consumeAudio() call. 65 // provided via each consumeAudio() call.
83 media::AudioPushFifo fifo_; 66 media::AudioPushFifo fifo_;
84 67
85 // Used to pass the reference timestamp between DeliverDecodedAudio() and 68 // Used to pass the reference timestamp between DeliverDecodedAudio() and
86 // DeliverRebufferedAudio(). 69 // DeliverRebufferedAudio().
87 base::TimeTicks current_reference_time_; 70 base::TimeTicks current_reference_time_;
88 71
89 // Synchronizes HandleCapture() with AudioCapturerSource calls.
90 base::Lock lock_;
91 bool started_;
92
93 // This object registers with a blink::WebMediaStreamSource. We keep track of 72 // This object registers with a blink::WebMediaStreamSource. We keep track of
94 // that in order to be able to deregister before stopping the audio track. 73 // that in order to be able to deregister before stopping this source.
95 blink::WebMediaStreamSource blink_source_; 74 blink::WebMediaStreamSource blink_source_;
96 75
97 DISALLOW_COPY_AND_ASSIGN(WebAudioCapturerSource); 76 DISALLOW_COPY_AND_ASSIGN(WebAudioMediaStreamSource);
98 }; 77 };
99 78
100 } // namespace content 79 } // namespace content
101 80
102 #endif // CONTENT_RENDERER_MEDIA_WEBAUDIO_CAPTURER_SOURCE_H_ 81 #endif // CONTENT_RENDERER_MEDIA_WEBAUDIO_MEDIA_STREAM_SOURCE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698