OLD | NEW |
| (Empty) |
1 /* | |
2 * libjingle | |
3 * Copyright 2004 Google Inc. | |
4 * | |
5 * Redistribution and use in source and binary forms, with or without | |
6 * modification, are permitted provided that the following conditions are met: | |
7 * | |
8 * 1. Redistributions of source code must retain the above copyright notice, | |
9 * this list of conditions and the following disclaimer. | |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | |
11 * this list of conditions and the following disclaimer in the documentation | |
12 * and/or other materials provided with the distribution. | |
13 * 3. The name of the author may not be used to endorse or promote products | |
14 * derived from this software without specific prior written permission. | |
15 * | |
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
26 */ | |
27 | |
28 #ifndef TALK_MEDIA_WEBRTCVOICEENGINE_H_ | |
29 #define TALK_MEDIA_WEBRTCVOICEENGINE_H_ | |
30 | |
31 #include <map> | |
32 #include <string> | |
33 #include <vector> | |
34 | |
35 #include "talk/media/base/rtputils.h" | |
36 #include "talk/media/webrtc/webrtccommon.h" | |
37 #include "talk/media/webrtc/webrtcvoe.h" | |
38 #include "talk/session/media/channel.h" | |
39 #include "webrtc/audio_state.h" | |
40 #include "webrtc/base/buffer.h" | |
41 #include "webrtc/base/scoped_ptr.h" | |
42 #include "webrtc/base/stream.h" | |
43 #include "webrtc/base/thread_checker.h" | |
44 #include "webrtc/call.h" | |
45 #include "webrtc/common.h" | |
46 #include "webrtc/config.h" | |
47 | |
48 namespace cricket { | |
49 | |
50 class AudioDeviceModule; | |
51 class AudioRenderer; | |
52 class VoEWrapper; | |
53 class WebRtcVoiceMediaChannel; | |
54 | |
55 // WebRtcVoiceEngine is a class to be used with CompositeMediaEngine. | |
56 // It uses the WebRtc VoiceEngine library for audio handling. | |
57 class WebRtcVoiceEngine final : public webrtc::TraceCallback { | |
58 friend class WebRtcVoiceMediaChannel; | |
59 public: | |
60 // Exposed for the WVoE/MC unit test. | |
61 static bool ToCodecInst(const AudioCodec& in, webrtc::CodecInst* out); | |
62 | |
63 WebRtcVoiceEngine(); | |
64 // Dependency injection for testing. | |
65 explicit WebRtcVoiceEngine(VoEWrapper* voe_wrapper); | |
66 ~WebRtcVoiceEngine(); | |
67 bool Init(rtc::Thread* worker_thread); | |
68 void Terminate(); | |
69 | |
70 rtc::scoped_refptr<webrtc::AudioState> GetAudioState() const; | |
71 VoiceMediaChannel* CreateChannel(webrtc::Call* call, | |
72 const AudioOptions& options); | |
73 | |
74 bool GetOutputVolume(int* level); | |
75 bool SetOutputVolume(int level); | |
76 int GetInputLevel(); | |
77 | |
78 const std::vector<AudioCodec>& codecs(); | |
79 RtpCapabilities GetCapabilities() const; | |
80 | |
81 // For tracking WebRtc channels. Needed because we have to pause them | |
82 // all when switching devices. | |
83 // May only be called by WebRtcVoiceMediaChannel. | |
84 void RegisterChannel(WebRtcVoiceMediaChannel* channel); | |
85 void UnregisterChannel(WebRtcVoiceMediaChannel* channel); | |
86 | |
87 // Called by WebRtcVoiceMediaChannel to set a gain offset from | |
88 // the default AGC target level. | |
89 bool AdjustAgcLevel(int delta); | |
90 | |
91 VoEWrapper* voe() { return voe_wrapper_.get(); } | |
92 int GetLastEngineError(); | |
93 | |
94 // Set the external ADM. This can only be called before Init. | |
95 bool SetAudioDeviceModule(webrtc::AudioDeviceModule* adm); | |
96 | |
97 // Starts AEC dump using an existing file. A maximum file size in bytes can be | |
98 // specified. When the maximum file size is reached, logging is stopped and | |
99 // the file is closed. If max_size_bytes is set to <= 0, no limit will be | |
100 // used. | |
101 bool StartAecDump(rtc::PlatformFile file, int64_t max_size_bytes); | |
102 | |
103 // Stops AEC dump. | |
104 void StopAecDump(); | |
105 | |
106 // Starts recording an RtcEventLog using an existing file until 10 minutes | |
107 // pass or the StopRtcEventLog function is called. | |
108 bool StartRtcEventLog(rtc::PlatformFile file); | |
109 | |
110 // Stops recording the RtcEventLog. | |
111 void StopRtcEventLog(); | |
112 | |
113 private: | |
114 void Construct(); | |
115 bool InitInternal(); | |
116 // Every option that is "set" will be applied. Every option not "set" will be | |
117 // ignored. This allows us to selectively turn on and off different options | |
118 // easily at any time. | |
119 bool ApplyOptions(const AudioOptions& options); | |
120 void SetDefaultDevices(); | |
121 | |
122 // webrtc::TraceCallback: | |
123 void Print(webrtc::TraceLevel level, const char* trace, int length) override; | |
124 | |
125 void StartAecDump(const std::string& filename); | |
126 int CreateVoEChannel(); | |
127 | |
128 rtc::ThreadChecker signal_thread_checker_; | |
129 rtc::ThreadChecker worker_thread_checker_; | |
130 | |
131 // The primary instance of WebRtc VoiceEngine. | |
132 rtc::scoped_ptr<VoEWrapper> voe_wrapper_; | |
133 rtc::scoped_refptr<webrtc::AudioState> audio_state_; | |
134 // The external audio device manager | |
135 webrtc::AudioDeviceModule* adm_ = nullptr; | |
136 std::vector<AudioCodec> codecs_; | |
137 std::vector<WebRtcVoiceMediaChannel*> channels_; | |
138 webrtc::Config voe_config_; | |
139 bool initialized_ = false; | |
140 bool is_dumping_aec_ = false; | |
141 | |
142 webrtc::AgcConfig default_agc_config_; | |
143 // Cache received extended_filter_aec, delay_agnostic_aec and experimental_ns | |
144 // values, and apply them in case they are missing in the audio options. We | |
145 // need to do this because SetExtraOptions() will revert to defaults for | |
146 // options which are not provided. | |
147 rtc::Optional<bool> extended_filter_aec_; | |
148 rtc::Optional<bool> delay_agnostic_aec_; | |
149 rtc::Optional<bool> experimental_ns_; | |
150 | |
151 RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcVoiceEngine); | |
152 }; | |
153 | |
154 // WebRtcVoiceMediaChannel is an implementation of VoiceMediaChannel that uses | |
155 // WebRtc Voice Engine. | |
156 class WebRtcVoiceMediaChannel final : public VoiceMediaChannel, | |
157 public webrtc::Transport { | |
158 public: | |
159 WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine, | |
160 const AudioOptions& options, | |
161 webrtc::Call* call); | |
162 ~WebRtcVoiceMediaChannel() override; | |
163 | |
164 const AudioOptions& options() const { return options_; } | |
165 | |
166 bool SetSendParameters(const AudioSendParameters& params) override; | |
167 bool SetRecvParameters(const AudioRecvParameters& params) override; | |
168 bool SetPlayout(bool playout) override; | |
169 bool PausePlayout(); | |
170 bool ResumePlayout(); | |
171 bool SetSend(SendFlags send) override; | |
172 bool PauseSend(); | |
173 bool ResumeSend(); | |
174 bool SetAudioSend(uint32_t ssrc, | |
175 bool enable, | |
176 const AudioOptions* options, | |
177 AudioRenderer* renderer) override; | |
178 bool AddSendStream(const StreamParams& sp) override; | |
179 bool RemoveSendStream(uint32_t ssrc) override; | |
180 bool AddRecvStream(const StreamParams& sp) override; | |
181 bool RemoveRecvStream(uint32_t ssrc) override; | |
182 bool GetActiveStreams(AudioInfo::StreamList* actives) override; | |
183 int GetOutputLevel() override; | |
184 int GetTimeSinceLastTyping() override; | |
185 void SetTypingDetectionParameters(int time_window, | |
186 int cost_per_typing, | |
187 int reporting_threshold, | |
188 int penalty_decay, | |
189 int type_event_delay) override; | |
190 bool SetOutputVolume(uint32_t ssrc, double volume) override; | |
191 | |
192 bool CanInsertDtmf() override; | |
193 bool InsertDtmf(uint32_t ssrc, int event, int duration) override; | |
194 | |
195 void OnPacketReceived(rtc::Buffer* packet, | |
196 const rtc::PacketTime& packet_time) override; | |
197 void OnRtcpReceived(rtc::Buffer* packet, | |
198 const rtc::PacketTime& packet_time) override; | |
199 void OnReadyToSend(bool ready) override {} | |
200 bool GetStats(VoiceMediaInfo* info) override; | |
201 | |
202 void SetRawAudioSink( | |
203 uint32_t ssrc, | |
204 rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) override; | |
205 | |
206 // implements Transport interface | |
207 bool SendRtp(const uint8_t* data, | |
208 size_t len, | |
209 const webrtc::PacketOptions& options) override { | |
210 rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len, | |
211 kMaxRtpPacketLen); | |
212 rtc::PacketOptions rtc_options; | |
213 rtc_options.packet_id = options.packet_id; | |
214 return VoiceMediaChannel::SendPacket(&packet, rtc_options); | |
215 } | |
216 | |
217 bool SendRtcp(const uint8_t* data, size_t len) override { | |
218 rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len, | |
219 kMaxRtpPacketLen); | |
220 return VoiceMediaChannel::SendRtcp(&packet, rtc::PacketOptions()); | |
221 } | |
222 | |
223 int GetReceiveChannelId(uint32_t ssrc) const; | |
224 int GetSendChannelId(uint32_t ssrc) const; | |
225 | |
226 private: | |
227 bool SetSendCodecs(const std::vector<AudioCodec>& codecs); | |
228 bool SetOptions(const AudioOptions& options); | |
229 bool SetMaxSendBandwidth(int bps); | |
230 bool SetRecvCodecs(const std::vector<AudioCodec>& codecs); | |
231 bool SetLocalRenderer(uint32_t ssrc, AudioRenderer* renderer); | |
232 bool MuteStream(uint32_t ssrc, bool mute); | |
233 | |
234 WebRtcVoiceEngine* engine() { return engine_; } | |
235 int GetLastEngineError() { return engine()->GetLastEngineError(); } | |
236 int GetOutputLevel(int channel); | |
237 bool GetRedSendCodec(const AudioCodec& red_codec, | |
238 const std::vector<AudioCodec>& all_codecs, | |
239 webrtc::CodecInst* send_codec); | |
240 bool SetPlayout(int channel, bool playout); | |
241 void SetNack(int channel, bool nack_enabled); | |
242 bool SetSendCodec(int channel, const webrtc::CodecInst& send_codec); | |
243 bool ChangePlayout(bool playout); | |
244 bool ChangeSend(SendFlags send); | |
245 bool ChangeSend(int channel, SendFlags send); | |
246 int CreateVoEChannel(); | |
247 bool DeleteVoEChannel(int channel); | |
248 bool IsDefaultRecvStream(uint32_t ssrc) { | |
249 return default_recv_ssrc_ == static_cast<int64_t>(ssrc); | |
250 } | |
251 bool SetSendCodecs(int channel, const std::vector<AudioCodec>& codecs); | |
252 bool SetSendBitrateInternal(int bps); | |
253 | |
254 rtc::ThreadChecker worker_thread_checker_; | |
255 | |
256 WebRtcVoiceEngine* const engine_ = nullptr; | |
257 std::vector<AudioCodec> recv_codecs_; | |
258 std::vector<AudioCodec> send_codecs_; | |
259 rtc::scoped_ptr<webrtc::CodecInst> send_codec_; | |
260 bool send_bitrate_setting_ = false; | |
261 int send_bitrate_bps_ = 0; | |
262 AudioOptions options_; | |
263 rtc::Optional<int> dtmf_payload_type_; | |
264 bool desired_playout_ = false; | |
265 bool nack_enabled_ = false; | |
266 bool playout_ = false; | |
267 SendFlags desired_send_ = SEND_NOTHING; | |
268 SendFlags send_ = SEND_NOTHING; | |
269 webrtc::Call* const call_ = nullptr; | |
270 | |
271 // SSRC of unsignalled receive stream, or -1 if there isn't one. | |
272 int64_t default_recv_ssrc_ = -1; | |
273 // Volume for unsignalled stream, which may be set before the stream exists. | |
274 double default_recv_volume_ = 1.0; | |
275 // Sink for unsignalled stream, which may be set before the stream exists. | |
276 rtc::scoped_ptr<webrtc::AudioSinkInterface> default_sink_; | |
277 // Default SSRC to use for RTCP receiver reports in case of no signaled | |
278 // send streams. See: https://code.google.com/p/webrtc/issues/detail?id=4740 | |
279 // and https://code.google.com/p/chromium/issues/detail?id=547661 | |
280 uint32_t receiver_reports_ssrc_ = 0xFA17FA17u; | |
281 | |
282 class WebRtcAudioSendStream; | |
283 std::map<uint32_t, WebRtcAudioSendStream*> send_streams_; | |
284 std::vector<webrtc::RtpExtension> send_rtp_extensions_; | |
285 | |
286 class WebRtcAudioReceiveStream; | |
287 std::map<uint32_t, WebRtcAudioReceiveStream*> recv_streams_; | |
288 std::vector<webrtc::RtpExtension> recv_rtp_extensions_; | |
289 | |
290 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcVoiceMediaChannel); | |
291 }; | |
292 } // namespace cricket | |
293 | |
294 #endif // TALK_MEDIA_WEBRTCVOICEENGINE_H_ | |
OLD | NEW |