OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_WEBRTC_WEBRTC_LOCAL_AUDIO_TRACK_ADAPTER_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_LOCAL_AUDIO_TRACK_ADAPTER_H_ |
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_LOCAL_AUDIO_TRACK_ADAPTER_H_ | 6 #define CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_LOCAL_AUDIO_TRACK_ADAPTER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
14 #include "base/threading/thread_checker.h" | |
15 #include "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
| 15 #include "content/renderer/media/media_stream_audio_level_calculator.h" |
16 #include "third_party/webrtc/api/mediastreamtrack.h" | 16 #include "third_party/webrtc/api/mediastreamtrack.h" |
17 #include "third_party/webrtc/media/base/audiorenderer.h" | 17 #include "third_party/webrtc/base/refcount.h" |
18 | 18 |
19 namespace cricket { | 19 namespace base { |
20 class AudioRenderer; | 20 class WaitableEvent; |
21 } | 21 } |
22 | 22 |
23 namespace webrtc { | 23 namespace webrtc { |
24 class AudioSourceInterface; | 24 class AudioSourceInterface; |
25 class AudioProcessorInterface; | 25 class AudioProcessorInterface; |
26 } | 26 } |
27 | 27 |
28 namespace content { | 28 namespace content { |
29 | 29 |
30 class MediaStreamAudioProcessor; | 30 class MediaStreamAudioProcessor; |
| 31 class MediaStreamAudioTrack; |
31 class WebRtcAudioSinkAdapter; | 32 class WebRtcAudioSinkAdapter; |
32 class WebRtcLocalAudioTrack; | |
33 | 33 |
| 34 // Provides an implementation of the webrtc::AudioTrackInterface that can be |
| 35 // bound/unbound to/from a MediaStreamAudioTrack. In other words, this is an |
| 36 // adapter that sits between the media stream object graph and WebRtc's object |
| 37 // graph and proxies between the two. |
| 38 // |
| 39 // TODO(miu): Rename to WebRtcAudioTrackAdapter since this can be used to proxy |
| 40 // between any kind (local or remote) of MediaStreamAudioTrack. |
34 class CONTENT_EXPORT WebRtcLocalAudioTrackAdapter | 41 class CONTENT_EXPORT WebRtcLocalAudioTrackAdapter |
35 : NON_EXPORTED_BASE( | 42 : NON_EXPORTED_BASE( |
36 public webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>) { | 43 public webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>) { |
37 public: | 44 public: |
38 static scoped_refptr<WebRtcLocalAudioTrackAdapter> Create( | 45 static scoped_refptr<WebRtcLocalAudioTrackAdapter> Create( |
39 const std::string& label, | 46 const std::string& label, |
40 webrtc::AudioSourceInterface* track_source); | 47 webrtc::AudioSourceInterface* track_source, |
| 48 const scoped_refptr<base::SingleThreadTaskRunner>& signaling_task_runner); |
41 | 49 |
42 WebRtcLocalAudioTrackAdapter( | 50 // Set the |track| that manages the MediaStreamAudioSinks. It is the client's |
43 const std::string& label, | 51 // responsibility to call this method with null before the track's destruction |
44 webrtc::AudioSourceInterface* track_source, | 52 // time. This is needed because WebRtcLocalAudioTrackAdapter is ref-counted |
45 const scoped_refptr<base::SingleThreadTaskRunner>& signaling_thread); | 53 // and could potentially out-live |track|. |
| 54 // |
| 55 // This method must only be called on the main thread. |
| 56 void SetMediaStreamAudioTrack(MediaStreamAudioTrack* track); |
46 | 57 |
47 ~WebRtcLocalAudioTrackAdapter() override; | 58 // Set the processor that applies signal processing on the data of the track. |
48 | |
49 void Initialize(WebRtcLocalAudioTrack* owner); | |
50 | |
51 // Called on the audio thread by the WebRtcLocalAudioTrack to set the signal | |
52 // level of the audio data. | |
53 void SetSignalLevel(int signal_level); | |
54 | |
55 // Method called by the WebRtcLocalAudioTrack to set the processor that | |
56 // applies signal processing on the data of the track. | |
57 // This class will keep a reference of the |processor|. | 59 // This class will keep a reference of the |processor|. |
58 // Called on the main render thread. | 60 // Called on the main render thread. |
59 void SetAudioProcessor( | 61 void SetAudioProcessor( |
60 const scoped_refptr<MediaStreamAudioProcessor>& processor); | 62 const scoped_refptr<MediaStreamAudioProcessor>& processor); |
61 | 63 |
| 64 // Set the object that provides shared access to the current audio signal |
| 65 // level. |
| 66 void SetReportedLevel( |
| 67 const scoped_refptr<MediaStreamAudioLevelCalculator::ReportedLevel>& |
| 68 reported_level); |
| 69 |
62 // webrtc::MediaStreamTrack implementation. | 70 // webrtc::MediaStreamTrack implementation. |
63 std::string kind() const override; | 71 std::string kind() const final; |
64 bool set_enabled(bool enable) override; | 72 bool set_enabled(bool enable) final; |
| 73 |
| 74 protected: |
| 75 friend class rtc::RefCountedObject<WebRtcLocalAudioTrackAdapter>; |
| 76 |
| 77 WebRtcLocalAudioTrackAdapter( |
| 78 const std::string& label, |
| 79 webrtc::AudioSourceInterface* track_source, |
| 80 const scoped_refptr<base::SingleThreadTaskRunner>& signaling_task_runner); |
| 81 |
| 82 ~WebRtcLocalAudioTrackAdapter() override; |
65 | 83 |
66 private: | 84 private: |
| 85 // Removes the |sink| from |track_| and then signals the |done_event| (if |
| 86 // provided). This is used by RemoveSink() to ensure the audio flow has |
| 87 // halted before it returns (on the signaling thread). |
| 88 void RemoveSinkOnMainThread(webrtc::AudioTrackSinkInterface* sink, |
| 89 base::WaitableEvent* done_event); |
| 90 |
67 // webrtc::AudioTrackInterface implementation. | 91 // webrtc::AudioTrackInterface implementation. |
68 void AddSink(webrtc::AudioTrackSinkInterface* sink) override; | 92 void AddSink(webrtc::AudioTrackSinkInterface* sink) final; |
69 void RemoveSink(webrtc::AudioTrackSinkInterface* sink) override; | 93 void RemoveSink(webrtc::AudioTrackSinkInterface* sink) final; |
70 bool GetSignalLevel(int* level) override; | 94 bool GetSignalLevel(int* level) final; |
71 rtc::scoped_refptr<webrtc::AudioProcessorInterface> GetAudioProcessor() | 95 rtc::scoped_refptr<webrtc::AudioProcessorInterface> GetAudioProcessor() final; |
72 override; | 96 webrtc::AudioSourceInterface* GetSource() const final; |
73 webrtc::AudioSourceInterface* GetSource() const override; | |
74 | |
75 // Weak reference. | |
76 WebRtcLocalAudioTrack* owner_; | |
77 | 97 |
78 // The source of the audio track which handles the audio constraints. | 98 // The source of the audio track which handles the audio constraints. |
79 // TODO(xians): merge |track_source_| to |capturer_| in WebRtcLocalAudioTrack. | 99 const rtc::scoped_refptr<webrtc::AudioSourceInterface> track_source_; |
80 rtc::scoped_refptr<webrtc::AudioSourceInterface> track_source_; | |
81 | 100 |
82 // Libjingle's signaling thread. | 101 // Task runner for operations that must be done on libjingle's signaling |
83 const scoped_refptr<base::SingleThreadTaskRunner> signaling_thread_; | 102 // thread. May be null for single-threaded unit tests. |
| 103 const scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner_; |
| 104 |
| 105 // Task runner for operations that must be done on the main thread. May be |
| 106 // null for single-threaded unit tests. |
| 107 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
| 108 |
| 109 // The track to add/remove sinks to/from. When the |
| 110 // webrtc::AudioTrackInterface::Add/RemoveSink() methods are called, they |
| 111 // create a proxy that implements the MediaStreamAudioSink interface to call |
| 112 // into the webrtc::AudioTrackSinkInterface. This must only be accessed on |
| 113 // the main thread. |
| 114 MediaStreamAudioTrack* track_; |
| 115 |
| 116 // A vector of the peer connection sink adapters which receive the audio data |
| 117 // from the audio track. This must only be accessed on the main thread. |
| 118 ScopedVector<WebRtcAudioSinkAdapter> sink_adapters_; |
| 119 |
| 120 // Protects |audio_processor_|, |voe_channels_|, and |signal_level_|. |
| 121 mutable base::Lock lock_; |
84 | 122 |
85 // The audio processsor that applies audio processing on the data of audio | 123 // The audio processsor that applies audio processing on the data of audio |
86 // track. | 124 // track. |
87 scoped_refptr<MediaStreamAudioProcessor> audio_processor_; | 125 scoped_refptr<MediaStreamAudioProcessor> audio_processor_; |
88 | 126 |
| 127 // Thread-safe accessor to current audio signal level. |
| 128 scoped_refptr<MediaStreamAudioLevelCalculator::ReportedLevel> reported_level_; |
| 129 |
89 // A vector of WebRtc VoE channels that the capturer sends data to. | 130 // A vector of WebRtc VoE channels that the capturer sends data to. |
90 std::vector<int> voe_channels_; | 131 std::vector<int> voe_channels_; |
91 | |
92 // A vector of the peer connection sink adapters which receive the audio data | |
93 // from the audio track. | |
94 ScopedVector<WebRtcAudioSinkAdapter> sink_adapters_; | |
95 | |
96 // The amplitude of the signal. | |
97 int signal_level_; | |
98 | |
99 // Thread checker for libjingle's signaling thread. | |
100 base::ThreadChecker signaling_thread_checker_; | |
101 base::ThreadChecker capture_thread_; | |
102 | |
103 // Protects |voe_channels_|, |audio_processor_| and |signal_level_|. | |
104 mutable base::Lock lock_; | |
105 }; | 132 }; |
106 | 133 |
107 } // namespace content | 134 } // namespace content |
108 | 135 |
109 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_LOCAL_AUDIO_TRACK_ADAPTER_H_ | 136 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_LOCAL_AUDIO_TRACK_ADAPTER_H_ |
OLD | NEW |