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/weak_ptr.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 "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
15 #include "content/renderer/media/media_stream_audio_level_calculator.h" | 15 #include "content/renderer/media/media_stream_audio_level_calculator.h" |
16 #include "content/renderer/media/media_stream_audio_processor.h" | 16 #include "content/renderer/media/media_stream_audio_processor.h" |
17 #include "third_party/webrtc/api/mediastreamtrack.h" | 17 #include "third_party/webrtc/api/mediastreamtrack.h" |
18 #include "third_party/webrtc/media/base/audiorenderer.h" | 18 #include "third_party/webrtc/media/base/audiorenderer.h" |
19 | 19 |
20 namespace cricket { | 20 namespace base { |
21 class AudioRenderer; | 21 class WaitableEvent; |
22 } | 22 } |
23 | 23 |
24 namespace webrtc { | 24 namespace webrtc { |
25 class AudioSourceInterface; | 25 class AudioSourceInterface; |
26 class AudioProcessorInterface; | 26 class AudioProcessorInterface; |
27 } | 27 } |
28 | 28 |
29 namespace content { | 29 namespace content { |
30 | 30 |
31 class MediaStreamAudioProcessor; | 31 class MediaStreamAudioProcessor; |
| 32 class MediaStreamAudioTrack; |
32 class WebRtcAudioSinkAdapter; | 33 class WebRtcAudioSinkAdapter; |
33 class WebRtcLocalAudioTrack; | |
34 | 34 |
35 // Provides an implementation of the webrtc::AudioTrackInterface that can be | 35 // Provides an implementation of the webrtc::AudioTrackInterface that can be |
36 // bound/unbound to/from a MediaStreamAudioTrack. In other words, this is an | 36 // bound/unbound to/from a MediaStreamAudioTrack. In other words, this is an |
37 // adapter that sits between the media stream object graph and WebRtc's object | 37 // adapter that sits between the media stream object graph and WebRtc's object |
38 // graph and proxies between the two. | 38 // graph and proxies between the two. |
39 class CONTENT_EXPORT WebRtcLocalAudioTrackAdapter | 39 class CONTENT_EXPORT WebRtcLocalAudioTrackAdapter |
40 : NON_EXPORTED_BASE( | 40 : NON_EXPORTED_BASE( |
41 public webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>) { | 41 public webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>) { |
42 public: | 42 public: |
43 static scoped_refptr<WebRtcLocalAudioTrackAdapter> Create( | 43 static scoped_refptr<WebRtcLocalAudioTrackAdapter> Create( |
44 const std::string& label, | 44 const std::string& label, |
45 webrtc::AudioSourceInterface* track_source); | 45 webrtc::AudioSourceInterface* track_source); |
46 | 46 |
47 WebRtcLocalAudioTrackAdapter( | 47 WebRtcLocalAudioTrackAdapter( |
48 const std::string& label, | 48 const std::string& label, |
49 webrtc::AudioSourceInterface* track_source, | 49 webrtc::AudioSourceInterface* track_source, |
50 scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner); | 50 scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner); |
51 | 51 |
52 ~WebRtcLocalAudioTrackAdapter() override; | 52 ~WebRtcLocalAudioTrackAdapter() override; |
53 | 53 |
54 void Initialize(WebRtcLocalAudioTrack* owner); | 54 // Set the |track| that manages the MediaStreamAudioSinks. The WeakPtr will |
| 55 // only be dereferenced on the main thread. This method must only be called |
| 56 // on the main thread. |
| 57 void SetMediaStreamAudioTrack(base::WeakPtr<MediaStreamAudioTrack> track); |
55 | 58 |
56 // Set the object that provides shared access to the current audio signal | 59 // Set the object that provides shared access to the current audio signal |
57 // level. This method may only be called once, before the audio data flow | 60 // level. This method may only be called once, before the audio data flow |
58 // starts, and before any calls to GetSignalLevel() might be made. | 61 // starts, and before any calls to GetSignalLevel() might be made. |
59 void SetLevel(scoped_refptr<MediaStreamAudioLevelCalculator::Level> level); | 62 void SetLevel(scoped_refptr<MediaStreamAudioLevelCalculator::Level> level); |
60 | 63 |
61 // Method called by the WebRtcLocalAudioTrack to set the processor that | 64 // Method called by the WebRtcLocalAudioTrack to set the processor that |
62 // applies signal processing on the data of the track. | 65 // applies signal processing on the data of the track. |
63 // This class will keep a reference of the |processor|. | 66 // This class will keep a reference of the |processor|. |
64 // Called on the main render thread. | 67 // Called on the main render thread. |
65 // This method may only be called once, before the audio data flow starts, and | 68 // This method may only be called once, before the audio data flow starts, and |
66 // before any calls to GetAudioProcessor() might be made. | 69 // before any calls to GetAudioProcessor() might be made. |
67 void SetAudioProcessor(scoped_refptr<MediaStreamAudioProcessor> processor); | 70 void SetAudioProcessor(scoped_refptr<MediaStreamAudioProcessor> processor); |
68 | 71 |
69 // webrtc::MediaStreamTrack implementation. | 72 // webrtc::MediaStreamTrack implementation. |
70 std::string kind() const override; | 73 std::string kind() const override; |
71 bool set_enabled(bool enable) override; | 74 bool set_enabled(bool enable) override; |
72 | 75 |
73 private: | 76 private: |
74 // webrtc::AudioTrackInterface implementation. | 77 // webrtc::AudioTrackInterface implementation. |
75 void AddSink(webrtc::AudioTrackSinkInterface* sink) override; | 78 void AddSink(webrtc::AudioTrackSinkInterface* sink) override; |
76 void RemoveSink(webrtc::AudioTrackSinkInterface* sink) override; | 79 void RemoveSink(webrtc::AudioTrackSinkInterface* sink) override; |
77 bool GetSignalLevel(int* level) override; | 80 bool GetSignalLevel(int* level) override; |
78 rtc::scoped_refptr<webrtc::AudioProcessorInterface> GetAudioProcessor() | 81 rtc::scoped_refptr<webrtc::AudioProcessorInterface> GetAudioProcessor() |
79 override; | 82 override; |
80 webrtc::AudioSourceInterface* GetSource() const override; | 83 webrtc::AudioSourceInterface* GetSource() const override; |
81 | 84 |
82 // Weak reference. | 85 // Removes the |sink| from |track_| and then signals the |done_event| (if |
83 WebRtcLocalAudioTrack* owner_; | 86 // provided). This is used by RemoveSink() to ensure the audio flow has |
| 87 // halted before it returns. |
| 88 void RemoveSinkOnMainThread(webrtc::AudioTrackSinkInterface* sink, |
| 89 base::WaitableEvent* done_event); |
84 | 90 |
85 // The source of the audio track which handles the audio constraints. | 91 // The source of the audio track which handles the audio constraints. |
86 // TODO(xians): merge |track_source_| to |capturer_| in WebRtcLocalAudioTrack. | 92 const rtc::scoped_refptr<webrtc::AudioSourceInterface> track_source_; |
87 rtc::scoped_refptr<webrtc::AudioSourceInterface> track_source_; | |
88 | 93 |
89 // Libjingle's signaling thread. | 94 // Task runner for operations that must be done on libjingle's signaling |
| 95 // thread. May be null for single-threaded unit tests. |
90 const scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner_; | 96 const scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner_; |
91 | 97 |
| 98 // Task runner for operations that must be done on the main thread. May be |
| 99 // null for single-threaded unit tests. |
| 100 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
| 101 |
| 102 // The track to add/remove sinks to/from. When the |
| 103 // webrtc::AudioTrackInterface::Add/RemoveSink() methods are called, they |
| 104 // create a proxy (WebRtcAudioSinkAdapter) that implements the |
| 105 // MediaStreamAudioSink interface to call into the |
| 106 // webrtc::AudioTrackSinkInterface. This must only be dereferenced on the |
| 107 // main thread. |
| 108 base::WeakPtr<MediaStreamAudioTrack> track_; |
| 109 |
92 // The audio processsor that applies audio processing on the data of audio | 110 // The audio processsor that applies audio processing on the data of audio |
93 // track. This must be set before calls to GetAudioProcessor() are made. | 111 // track. This must be set before calls to GetAudioProcessor() are made. |
94 scoped_refptr<MediaStreamAudioProcessor> audio_processor_; | 112 scoped_refptr<MediaStreamAudioProcessor> audio_processor_; |
95 | 113 |
96 // A vector of the peer connection sink adapters which receive the audio data | 114 // A vector of the peer connection sink adapters which receive the audio data |
97 // from the audio track. | 115 // from the audio track. |
98 ScopedVector<WebRtcAudioSinkAdapter> sink_adapters_; | 116 std::vector<scoped_ptr<WebRtcAudioSinkAdapter>> sink_adapters_; |
99 | 117 |
100 // Thread-safe accessor to current audio signal level. This must be set | 118 // Thread-safe accessor to current audio signal level. This must be set |
101 // before calls to GetSignalLevel() are made. | 119 // before calls to GetSignalLevel() are made. |
102 scoped_refptr<MediaStreamAudioLevelCalculator::Level> level_; | 120 scoped_refptr<MediaStreamAudioLevelCalculator::Level> level_; |
103 }; | 121 }; |
104 | 122 |
105 } // namespace content | 123 } // namespace content |
106 | 124 |
107 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_LOCAL_AUDIO_TRACK_ADAPTER_H_ | 125 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_LOCAL_AUDIO_TRACK_ADAPTER_H_ |
OLD | NEW |