OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 bitrate_allocator_(new BitrateAllocator()), | 146 bitrate_allocator_(new BitrateAllocator()), |
147 call_stats_(new CallStats()), | 147 call_stats_(new CallStats()), |
148 encoder_state_feedback_(new EncoderStateFeedback()), | 148 encoder_state_feedback_(new EncoderStateFeedback()), |
149 packet_router_(new PacketRouter()), | 149 packet_router_(new PacketRouter()), |
150 pacer_(new PacedSender(Clock::GetRealTimeClock(), | 150 pacer_(new PacedSender(Clock::GetRealTimeClock(), |
151 packet_router_.get(), | 151 packet_router_.get(), |
152 BitrateController::kDefaultStartBitrateKbps, | 152 BitrateController::kDefaultStartBitrateKbps, |
153 PacedSender::kDefaultPaceMultiplier * | 153 PacedSender::kDefaultPaceMultiplier * |
154 BitrateController::kDefaultStartBitrateKbps, | 154 BitrateController::kDefaultStartBitrateKbps, |
155 0)), | 155 0)), |
156 encoder_map_cs_(CriticalSectionWrapper::CreateCriticalSection()), | |
157 config_(new Config), | 156 config_(new Config), |
158 process_thread_(process_thread), | 157 process_thread_(process_thread), |
159 pacer_thread_(ProcessThread::Create()), | 158 pacer_thread_(ProcessThread::Create()), |
160 // Constructed last as this object calls the provided callback on | 159 // Constructed last as this object calls the provided callback on |
161 // construction. | 160 // construction. |
162 bitrate_controller_( | 161 bitrate_controller_( |
163 BitrateController::CreateBitrateController(Clock::GetRealTimeClock(), | 162 BitrateController::CreateBitrateController(Clock::GetRealTimeClock(), |
164 this)) { | 163 this)) { |
165 remote_bitrate_estimator_.reset(new WrappingBitrateEstimator( | 164 remote_bitrate_estimator_.reset(new WrappingBitrateEstimator( |
166 remb_.get(), Clock::GetRealTimeClock(), *config_.get())); | 165 remb_.get(), Clock::GetRealTimeClock(), *config_.get())); |
167 | 166 |
168 call_stats_->RegisterStatsObserver(remote_bitrate_estimator_.get()); | 167 call_stats_->RegisterStatsObserver(remote_bitrate_estimator_.get()); |
169 | 168 |
170 pacer_thread_->RegisterModule(pacer_.get()); | 169 pacer_thread_->RegisterModule(pacer_.get()); |
171 pacer_thread_->Start(); | 170 pacer_thread_->Start(); |
172 | 171 |
173 process_thread->RegisterModule(remote_bitrate_estimator_.get()); | 172 process_thread->RegisterModule(remote_bitrate_estimator_.get()); |
174 process_thread->RegisterModule(call_stats_.get()); | 173 process_thread->RegisterModule(call_stats_.get()); |
175 process_thread->RegisterModule(bitrate_controller_.get()); | 174 process_thread->RegisterModule(bitrate_controller_.get()); |
176 } | 175 } |
177 | 176 |
178 ChannelGroup::~ChannelGroup() { | 177 ChannelGroup::~ChannelGroup() { |
179 pacer_thread_->Stop(); | 178 pacer_thread_->Stop(); |
180 pacer_thread_->DeRegisterModule(pacer_.get()); | 179 pacer_thread_->DeRegisterModule(pacer_.get()); |
181 process_thread_->DeRegisterModule(bitrate_controller_.get()); | 180 process_thread_->DeRegisterModule(bitrate_controller_.get()); |
182 process_thread_->DeRegisterModule(call_stats_.get()); | 181 process_thread_->DeRegisterModule(call_stats_.get()); |
183 process_thread_->DeRegisterModule(remote_bitrate_estimator_.get()); | 182 process_thread_->DeRegisterModule(remote_bitrate_estimator_.get()); |
184 call_stats_->DeregisterStatsObserver(remote_bitrate_estimator_.get()); | 183 call_stats_->DeregisterStatsObserver(remote_bitrate_estimator_.get()); |
185 DCHECK(channels_.empty()); | |
186 DCHECK(channel_map_.empty()); | 184 DCHECK(channel_map_.empty()); |
187 DCHECK(!remb_->InUse()); | 185 DCHECK(!remb_->InUse()); |
188 DCHECK(vie_encoder_map_.empty()); | 186 DCHECK(vie_encoder_map_.empty()); |
189 DCHECK(send_encoders_.empty()); | |
190 } | 187 } |
191 | 188 |
192 bool ChannelGroup::CreateSendChannel(int channel_id, | 189 bool ChannelGroup::CreateSendChannel(int channel_id, |
193 int engine_id, | 190 int engine_id, |
194 Transport* transport, | 191 Transport* transport, |
195 int number_of_cores, | 192 int number_of_cores, |
196 const std::vector<uint32_t>& ssrcs, | 193 const std::vector<uint32_t>& ssrcs) { |
197 bool disable_default_encoder) { | |
198 // TODO(pbos): Remove checks for empty ssrcs and add this check when there's | 194 // TODO(pbos): Remove checks for empty ssrcs and add this check when there's |
199 // no base channel. | 195 // no base channel. |
200 // DCHECK(!ssrcs.empty()); | 196 // DCHECK(!ssrcs.empty()); |
201 rtc::scoped_ptr<ViEEncoder> vie_encoder( | 197 rtc::scoped_ptr<ViEEncoder> vie_encoder( |
202 new ViEEncoder(channel_id, number_of_cores, *config_.get(), | 198 new ViEEncoder(channel_id, number_of_cores, *config_.get(), |
203 *process_thread_, pacer_.get(), bitrate_allocator_.get(), | 199 *process_thread_, pacer_.get(), bitrate_allocator_.get(), |
204 bitrate_controller_.get(), false)); | 200 bitrate_controller_.get(), false)); |
205 if (!vie_encoder->Init()) { | 201 if (!vie_encoder->Init()) { |
206 return false; | 202 return false; |
207 } | 203 } |
208 ViEEncoder* encoder = vie_encoder.get(); | 204 ViEEncoder* encoder = vie_encoder.get(); |
209 if (!CreateChannel(channel_id, engine_id, transport, number_of_cores, | 205 if (!CreateChannel(channel_id, engine_id, transport, number_of_cores, |
210 vie_encoder.release(), ssrcs.empty() ? 1 : ssrcs.size(), | 206 vie_encoder.release(), ssrcs.empty() ? 1 : ssrcs.size(), |
211 true, disable_default_encoder)) { | 207 true)) { |
212 return false; | 208 return false; |
213 } | 209 } |
214 ViEChannel* channel = channel_map_[channel_id]; | 210 ViEChannel* channel = channel_map_[channel_id]; |
215 // Connect the encoder with the send packet router, to enable sending. | 211 // Connect the encoder with the send packet router, to enable sending. |
216 encoder->StartThreadsAndSetSharedMembers(channel->send_payload_router(), | 212 encoder->StartThreadsAndSetSharedMembers(channel->send_payload_router(), |
217 channel->vcm_protection_callback()); | 213 channel->vcm_protection_callback()); |
218 | 214 |
219 if (!ssrcs.empty()) { | 215 if (!ssrcs.empty()) { |
220 encoder_state_feedback_->AddEncoder(ssrcs, encoder); | 216 encoder_state_feedback_->AddEncoder(ssrcs, encoder); |
221 std::vector<uint32_t> first_ssrc(1, ssrcs[0]); | 217 std::vector<uint32_t> first_ssrc(1, ssrcs[0]); |
222 encoder->SetSsrcs(first_ssrc); | 218 encoder->SetSsrcs(first_ssrc); |
223 } | 219 } |
224 return true; | 220 return true; |
225 } | 221 } |
226 | 222 |
227 bool ChannelGroup::CreateReceiveChannel(int channel_id, | 223 bool ChannelGroup::CreateReceiveChannel(int channel_id, |
228 int engine_id, | 224 int engine_id, |
229 int base_channel_id, | |
230 Transport* transport, | 225 Transport* transport, |
231 int number_of_cores, | 226 int number_of_cores) { |
232 bool disable_default_encoder) { | |
233 ViEEncoder* encoder = GetEncoder(base_channel_id); | |
234 return CreateChannel(channel_id, engine_id, transport, number_of_cores, | 227 return CreateChannel(channel_id, engine_id, transport, number_of_cores, |
235 encoder, 1, false, disable_default_encoder); | 228 nullptr, 1, false); |
236 } | 229 } |
237 | 230 |
238 bool ChannelGroup::CreateChannel(int channel_id, | 231 bool ChannelGroup::CreateChannel(int channel_id, |
239 int engine_id, | 232 int engine_id, |
240 Transport* transport, | 233 Transport* transport, |
241 int number_of_cores, | 234 int number_of_cores, |
242 ViEEncoder* vie_encoder, | 235 ViEEncoder* vie_encoder, |
243 size_t max_rtp_streams, | 236 size_t max_rtp_streams, |
244 bool sender, | 237 bool sender) { |
245 bool disable_default_encoder) { | |
246 DCHECK(vie_encoder); | |
247 | |
248 rtc::scoped_ptr<ViEChannel> channel(new ViEChannel( | 238 rtc::scoped_ptr<ViEChannel> channel(new ViEChannel( |
249 channel_id, engine_id, number_of_cores, *config_.get(), transport, | 239 channel_id, engine_id, number_of_cores, *config_.get(), transport, |
250 process_thread_, encoder_state_feedback_->GetRtcpIntraFrameObserver(), | 240 process_thread_, encoder_state_feedback_->GetRtcpIntraFrameObserver(), |
251 bitrate_controller_->CreateRtcpBandwidthObserver(), | 241 bitrate_controller_->CreateRtcpBandwidthObserver(), |
252 remote_bitrate_estimator_.get(), call_stats_->rtcp_rtt_stats(), | 242 remote_bitrate_estimator_.get(), call_stats_->rtcp_rtt_stats(), |
253 pacer_.get(), packet_router_.get(), max_rtp_streams, sender, | 243 pacer_.get(), packet_router_.get(), max_rtp_streams, sender)); |
254 disable_default_encoder)); | |
255 if (channel->Init() != 0) { | 244 if (channel->Init() != 0) { |
256 return false; | 245 return false; |
257 } | 246 } |
258 if (!disable_default_encoder) { | |
259 VideoCodec encoder; | |
260 if (vie_encoder->GetEncoder(&encoder) != 0) { | |
261 return false; | |
262 } | |
263 if (sender && channel->SetSendCodec(encoder) != 0) { | |
264 return false; | |
265 } | |
266 } | |
267 | 247 |
268 // Register the channel to receive stats updates. | 248 // Register the channel to receive stats updates. |
269 call_stats_->RegisterStatsObserver(channel->GetStatsObserver()); | 249 call_stats_->RegisterStatsObserver(channel->GetStatsObserver()); |
270 | 250 |
271 // Store the channel, add it to the channel group and save the vie_encoder. | 251 // Store the channel, add it to the channel group and save the vie_encoder. |
272 channel_map_[channel_id] = channel.release(); | 252 channel_map_[channel_id] = channel.release(); |
273 { | 253 if (vie_encoder) { |
274 CriticalSectionScoped lock(encoder_map_cs_.get()); | 254 rtc::CritScope lock(&encoder_map_crit_); |
275 vie_encoder_map_[channel_id] = vie_encoder; | 255 vie_encoder_map_[channel_id] = vie_encoder; |
276 if (sender) | |
277 send_encoders_[channel_id] = vie_encoder; | |
278 } | 256 } |
279 | 257 |
280 return true; | 258 return true; |
281 } | 259 } |
282 | 260 |
283 void ChannelGroup::DeleteChannel(int channel_id) { | 261 void ChannelGroup::DeleteChannel(int channel_id) { |
284 ViEChannel* vie_channel = PopChannel(channel_id); | 262 ViEChannel* vie_channel = PopChannel(channel_id); |
285 | 263 |
286 ViEEncoder* vie_encoder = GetEncoder(channel_id); | 264 ViEEncoder* vie_encoder = GetEncoder(channel_id); |
287 DCHECK(vie_encoder != NULL); | |
288 | 265 |
289 call_stats_->DeregisterStatsObserver(vie_channel->GetStatsObserver()); | 266 call_stats_->DeregisterStatsObserver(vie_channel->GetStatsObserver()); |
290 SetChannelRembStatus(false, false, vie_channel); | 267 SetChannelRembStatus(false, false, vie_channel); |
291 | 268 |
292 // If we're owning the encoder, remove the feedback and stop all encoding | 269 // If we're a sender, remove the feedback and stop all encoding threads and |
293 // threads and processing. This must be done before deleting the channel. | 270 // processing. This must be done before deleting the channel. |
294 if (vie_encoder->channel_id() == channel_id) { | 271 if (vie_encoder) { |
295 encoder_state_feedback_->RemoveEncoder(vie_encoder); | 272 encoder_state_feedback_->RemoveEncoder(vie_encoder); |
296 vie_encoder->StopThreadsAndRemoveSharedMembers(); | 273 vie_encoder->StopThreadsAndRemoveSharedMembers(); |
297 } | 274 } |
298 | 275 |
299 unsigned int remote_ssrc = 0; | 276 unsigned int remote_ssrc = 0; |
300 vie_channel->GetRemoteSSRC(&remote_ssrc); | 277 vie_channel->GetRemoteSSRC(&remote_ssrc); |
301 RemoveChannel(channel_id); | 278 channel_map_.erase(channel_id); |
302 remote_bitrate_estimator_->RemoveStream(remote_ssrc); | 279 remote_bitrate_estimator_->RemoveStream(remote_ssrc); |
303 | 280 |
304 // Check if other channels are using the same encoder. | 281 delete vie_channel; |
305 if (OtherChannelsUsingEncoder(channel_id)) { | |
stefan-webrtc
2015/07/23 12:43:46
Can this no longer be the case? I guess it's not s
pbos-webrtc
2015/07/23 12:46:06
Yep, exactly. :)
| |
306 vie_encoder = NULL; | |
307 } else { | |
308 // Delete later when we've released the critsect. | |
309 } | |
310 | 282 |
311 // We can't erase the item before we've checked for other channels using | |
312 // same ViEEncoder. | |
313 PopEncoder(channel_id); | |
314 | |
315 delete vie_channel; | |
316 // Leave the write critsect before deleting the objects. | |
317 // Deleting a channel can cause other objects, such as renderers, to be | |
318 // deleted, which might take time. | |
319 // If statment just to show that this object is not always deleted. | |
320 if (vie_encoder) { | 283 if (vie_encoder) { |
321 LOG(LS_VERBOSE) << "ViEEncoder deleted for channel " << channel_id; | 284 { |
285 rtc::CritScope lock(&encoder_map_crit_); | |
286 vie_encoder_map_.erase(vie_encoder_map_.find(channel_id)); | |
287 } | |
322 delete vie_encoder; | 288 delete vie_encoder; |
323 } | 289 } |
324 | 290 |
325 LOG(LS_VERBOSE) << "Channel deleted " << channel_id; | 291 LOG(LS_VERBOSE) << "Channel deleted " << channel_id; |
326 } | 292 } |
327 | 293 |
328 void ChannelGroup::AddChannel(int channel_id) { | |
329 channels_.insert(channel_id); | |
330 } | |
331 | |
332 void ChannelGroup::RemoveChannel(int channel_id) { | |
333 channels_.erase(channel_id); | |
334 } | |
335 | |
336 bool ChannelGroup::HasChannel(int channel_id) const { | |
337 return channels_.find(channel_id) != channels_.end(); | |
338 } | |
339 | |
340 bool ChannelGroup::Empty() const { | |
341 return channels_.empty(); | |
342 } | |
343 | |
344 ViEChannel* ChannelGroup::GetChannel(int channel_id) const { | 294 ViEChannel* ChannelGroup::GetChannel(int channel_id) const { |
345 ChannelMap::const_iterator it = channel_map_.find(channel_id); | 295 ChannelMap::const_iterator it = channel_map_.find(channel_id); |
346 if (it == channel_map_.end()) { | 296 if (it == channel_map_.end()) { |
347 LOG(LS_ERROR) << "Channel doesn't exist " << channel_id; | 297 LOG(LS_ERROR) << "Channel doesn't exist " << channel_id; |
348 return NULL; | 298 return NULL; |
349 } | 299 } |
350 return it->second; | 300 return it->second; |
351 } | 301 } |
352 | 302 |
353 ViEEncoder* ChannelGroup::GetEncoder(int channel_id) const { | 303 ViEEncoder* ChannelGroup::GetEncoder(int channel_id) const { |
354 CriticalSectionScoped lock(encoder_map_cs_.get()); | 304 rtc::CritScope lock(&encoder_map_crit_); |
355 EncoderMap::const_iterator it = vie_encoder_map_.find(channel_id); | 305 EncoderMap::const_iterator it = vie_encoder_map_.find(channel_id); |
356 if (it == vie_encoder_map_.end()) { | 306 if (it == vie_encoder_map_.end()) |
357 return NULL; | 307 return nullptr; |
358 } | |
359 return it->second; | 308 return it->second; |
360 } | 309 } |
361 | 310 |
362 ViEChannel* ChannelGroup::PopChannel(int channel_id) { | 311 ViEChannel* ChannelGroup::PopChannel(int channel_id) { |
363 ChannelMap::iterator c_it = channel_map_.find(channel_id); | 312 ChannelMap::iterator c_it = channel_map_.find(channel_id); |
364 DCHECK(c_it != channel_map_.end()); | 313 DCHECK(c_it != channel_map_.end()); |
365 ViEChannel* channel = c_it->second; | 314 ViEChannel* channel = c_it->second; |
366 channel_map_.erase(c_it); | 315 channel_map_.erase(c_it); |
367 | 316 |
368 return channel; | 317 return channel; |
369 } | 318 } |
370 | 319 |
371 ViEEncoder* ChannelGroup::PopEncoder(int channel_id) { | 320 void ChannelGroup::SetSyncInterface(VoEVideoSync* sync_interface) { |
372 CriticalSectionScoped lock(encoder_map_cs_.get()); | |
373 auto it = vie_encoder_map_.find(channel_id); | |
374 DCHECK(it != vie_encoder_map_.end()); | |
375 ViEEncoder* encoder = it->second; | |
376 vie_encoder_map_.erase(it); | |
377 | |
378 it = send_encoders_.find(channel_id); | |
379 if (it != send_encoders_.end()) | |
380 send_encoders_.erase(it); | |
381 | |
382 return encoder; | |
383 } | |
384 | |
385 std::vector<int> ChannelGroup::GetChannelIds() const { | |
386 std::vector<int> ids; | |
387 for (auto channel : channel_map_) | 321 for (auto channel : channel_map_) |
388 ids.push_back(channel.first); | |
389 return ids; | |
390 } | |
391 | |
392 bool ChannelGroup::OtherChannelsUsingEncoder(int channel_id) const { | |
393 CriticalSectionScoped lock(encoder_map_cs_.get()); | |
394 EncoderMap::const_iterator orig_it = vie_encoder_map_.find(channel_id); | |
395 if (orig_it == vie_encoder_map_.end()) { | |
396 // No ViEEncoder for this channel. | |
397 return false; | |
398 } | |
399 | |
400 // Loop through all other channels to see if anyone points at the same | |
401 // ViEEncoder. | |
402 for (EncoderMap::const_iterator comp_it = vie_encoder_map_.begin(); | |
403 comp_it != vie_encoder_map_.end(); ++comp_it) { | |
404 // Make sure we're not comparing the same channel with itself. | |
405 if (comp_it->first != channel_id) { | |
406 if (comp_it->second == orig_it->second) { | |
407 return true; | |
408 } | |
409 } | |
410 } | |
411 return false; | |
412 } | |
413 | |
414 void ChannelGroup::SetSyncInterface(VoEVideoSync* sync_interface) { | |
415 for (auto channel : channel_map_) { | |
416 channel.second->SetVoiceChannel(-1, sync_interface); | 322 channel.second->SetVoiceChannel(-1, sync_interface); |
417 } | |
418 } | |
419 | |
420 void ChannelGroup::GetChannelsUsingEncoder(int channel_id, | |
421 ChannelList* channels) const { | |
422 CriticalSectionScoped lock(encoder_map_cs_.get()); | |
423 EncoderMap::const_iterator orig_it = vie_encoder_map_.find(channel_id); | |
424 | |
425 for (ChannelMap::const_iterator c_it = channel_map_.begin(); | |
426 c_it != channel_map_.end(); ++c_it) { | |
427 EncoderMap::const_iterator comp_it = vie_encoder_map_.find(c_it->first); | |
428 DCHECK(comp_it != vie_encoder_map_.end()); | |
429 if (comp_it->second == orig_it->second) { | |
430 channels->push_back(c_it->second); | |
431 } | |
432 } | |
433 } | 323 } |
434 | 324 |
435 BitrateController* ChannelGroup::GetBitrateController() const { | 325 BitrateController* ChannelGroup::GetBitrateController() const { |
436 return bitrate_controller_.get(); | 326 return bitrate_controller_.get(); |
437 } | 327 } |
438 | 328 |
439 RemoteBitrateEstimator* ChannelGroup::GetRemoteBitrateEstimator() const { | 329 RemoteBitrateEstimator* ChannelGroup::GetRemoteBitrateEstimator() const { |
440 return remote_bitrate_estimator_.get(); | 330 return remote_bitrate_estimator_.get(); |
441 } | 331 } |
442 | 332 |
(...skipping 27 matching lines...) Expand all Loading... | |
470 remb_->RemoveReceiveChannel(rtp_module); | 360 remb_->RemoveReceiveChannel(rtp_module); |
471 } | 361 } |
472 } | 362 } |
473 | 363 |
474 void ChannelGroup::OnNetworkChanged(uint32_t target_bitrate_bps, | 364 void ChannelGroup::OnNetworkChanged(uint32_t target_bitrate_bps, |
475 uint8_t fraction_loss, | 365 uint8_t fraction_loss, |
476 int64_t rtt) { | 366 int64_t rtt) { |
477 bitrate_allocator_->OnNetworkChanged(target_bitrate_bps, fraction_loss, rtt); | 367 bitrate_allocator_->OnNetworkChanged(target_bitrate_bps, fraction_loss, rtt); |
478 int pad_up_to_bitrate_bps = 0; | 368 int pad_up_to_bitrate_bps = 0; |
479 { | 369 { |
480 CriticalSectionScoped lock(encoder_map_cs_.get()); | 370 rtc::CritScope lock(&encoder_map_crit_); |
481 for (const auto& encoder : send_encoders_) | 371 for (const auto& encoder : vie_encoder_map_) |
482 pad_up_to_bitrate_bps += encoder.second->GetPaddingNeededBps(); | 372 pad_up_to_bitrate_bps += encoder.second->GetPaddingNeededBps(); |
483 } | 373 } |
484 pacer_->UpdateBitrate( | 374 pacer_->UpdateBitrate( |
485 target_bitrate_bps / 1000, | 375 target_bitrate_bps / 1000, |
486 PacedSender::kDefaultPaceMultiplier * target_bitrate_bps / 1000, | 376 PacedSender::kDefaultPaceMultiplier * target_bitrate_bps / 1000, |
487 pad_up_to_bitrate_bps / 1000); | 377 pad_up_to_bitrate_bps / 1000); |
488 } | 378 } |
489 } // namespace webrtc | 379 } // namespace webrtc |
OLD | NEW |