You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

642 lines
21 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2015 The WebRTC project authors. All Rights Reserved.
  3. *
  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
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "pc/rtp_sender.h"
  11. #include <atomic>
  12. #include <utility>
  13. #include <vector>
  14. #include "api/audio_options.h"
  15. #include "api/media_stream_interface.h"
  16. #include "media/base/media_engine.h"
  17. #include "pc/peer_connection.h"
  18. #include "pc/stats_collector.h"
  19. #include "rtc_base/checks.h"
  20. #include "rtc_base/helpers.h"
  21. #include "rtc_base/location.h"
  22. #include "rtc_base/logging.h"
  23. #include "rtc_base/trace_event.h"
  24. namespace webrtc {
  25. namespace {
  26. // This function is only expected to be called on the signaling thread.
  27. // On the other hand, some test or even production setups may use
  28. // several signaling threads.
  29. int GenerateUniqueId() {
  30. static std::atomic<int> g_unique_id{0};
  31. return ++g_unique_id;
  32. }
  33. // Returns an true if any RtpEncodingParameters member that isn't implemented
  34. // contains a value.
  35. bool UnimplementedRtpEncodingParameterHasValue(
  36. const RtpEncodingParameters& encoding_params) {
  37. if (encoding_params.codec_payload_type.has_value() ||
  38. encoding_params.fec.has_value() || encoding_params.rtx.has_value() ||
  39. encoding_params.dtx.has_value() || encoding_params.ptime.has_value() ||
  40. encoding_params.scale_framerate_down_by.has_value() ||
  41. !encoding_params.dependency_rids.empty()) {
  42. return true;
  43. }
  44. return false;
  45. }
  46. // Returns true if a "per-sender" encoding parameter contains a value that isn't
  47. // its default. Currently max_bitrate_bps and bitrate_priority both are
  48. // implemented "per-sender," meaning that these encoding parameters
  49. // are used for the RtpSender as a whole, not for a specific encoding layer.
  50. // This is done by setting these encoding parameters at index 0 of
  51. // RtpParameters.encodings. This function can be used to check if these
  52. // parameters are set at any index other than 0 of RtpParameters.encodings,
  53. // because they are currently unimplemented to be used for a specific encoding
  54. // layer.
  55. bool PerSenderRtpEncodingParameterHasValue(
  56. const RtpEncodingParameters& encoding_params) {
  57. if (encoding_params.bitrate_priority != kDefaultBitratePriority ||
  58. encoding_params.network_priority != kDefaultBitratePriority) {
  59. return true;
  60. }
  61. return false;
  62. }
  63. void RemoveEncodingLayers(const std::vector<std::string>& rids,
  64. std::vector<RtpEncodingParameters>* encodings) {
  65. RTC_DCHECK(encodings);
  66. encodings->erase(
  67. std::remove_if(encodings->begin(), encodings->end(),
  68. [&rids](const RtpEncodingParameters& encoding) {
  69. return absl::c_linear_search(rids, encoding.rid);
  70. }),
  71. encodings->end());
  72. }
  73. RtpParameters RestoreEncodingLayers(
  74. const RtpParameters& parameters,
  75. const std::vector<std::string>& removed_rids,
  76. const std::vector<RtpEncodingParameters>& all_layers) {
  77. RTC_DCHECK_EQ(parameters.encodings.size() + removed_rids.size(),
  78. all_layers.size());
  79. RtpParameters result(parameters);
  80. result.encodings.clear();
  81. size_t index = 0;
  82. for (const RtpEncodingParameters& encoding : all_layers) {
  83. if (absl::c_linear_search(removed_rids, encoding.rid)) {
  84. result.encodings.push_back(encoding);
  85. continue;
  86. }
  87. result.encodings.push_back(parameters.encodings[index++]);
  88. }
  89. return result;
  90. }
  91. } // namespace
  92. // Returns true if any RtpParameters member that isn't implemented contains a
  93. // value.
  94. bool UnimplementedRtpParameterHasValue(const RtpParameters& parameters) {
  95. if (!parameters.mid.empty()) {
  96. return true;
  97. }
  98. for (size_t i = 0; i < parameters.encodings.size(); ++i) {
  99. if (UnimplementedRtpEncodingParameterHasValue(parameters.encodings[i])) {
  100. return true;
  101. }
  102. // Encoding parameters that are per-sender should only contain value at
  103. // index 0.
  104. if (i != 0 &&
  105. PerSenderRtpEncodingParameterHasValue(parameters.encodings[i])) {
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. RtpSenderBase::RtpSenderBase(rtc::Thread* worker_thread,
  112. const std::string& id,
  113. SetStreamsObserver* set_streams_observer)
  114. : worker_thread_(worker_thread),
  115. id_(id),
  116. set_streams_observer_(set_streams_observer) {
  117. RTC_DCHECK(worker_thread);
  118. init_parameters_.encodings.emplace_back();
  119. }
  120. void RtpSenderBase::SetFrameEncryptor(
  121. rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) {
  122. frame_encryptor_ = std::move(frame_encryptor);
  123. // Special Case: Set the frame encryptor to any value on any existing channel.
  124. if (media_channel_ && ssrc_ && !stopped_) {
  125. worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
  126. media_channel_->SetFrameEncryptor(ssrc_, frame_encryptor_);
  127. });
  128. }
  129. }
  130. void RtpSenderBase::SetMediaChannel(cricket::MediaChannel* media_channel) {
  131. RTC_DCHECK(media_channel == nullptr ||
  132. media_channel->media_type() == media_type());
  133. media_channel_ = media_channel;
  134. }
  135. RtpParameters RtpSenderBase::GetParametersInternal() const {
  136. if (stopped_) {
  137. return RtpParameters();
  138. }
  139. if (!media_channel_ || !ssrc_) {
  140. return init_parameters_;
  141. }
  142. return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] {
  143. RtpParameters result = media_channel_->GetRtpSendParameters(ssrc_);
  144. RemoveEncodingLayers(disabled_rids_, &result.encodings);
  145. return result;
  146. });
  147. }
  148. RtpParameters RtpSenderBase::GetParameters() const {
  149. RtpParameters result = GetParametersInternal();
  150. last_transaction_id_ = rtc::CreateRandomUuid();
  151. result.transaction_id = last_transaction_id_.value();
  152. return result;
  153. }
  154. RTCError RtpSenderBase::SetParametersInternal(const RtpParameters& parameters) {
  155. RTC_DCHECK(!stopped_);
  156. if (UnimplementedRtpParameterHasValue(parameters)) {
  157. LOG_AND_RETURN_ERROR(
  158. RTCErrorType::UNSUPPORTED_PARAMETER,
  159. "Attempted to set an unimplemented parameter of RtpParameters.");
  160. }
  161. if (!media_channel_ || !ssrc_) {
  162. auto result = cricket::CheckRtpParametersInvalidModificationAndValues(
  163. init_parameters_, parameters);
  164. if (result.ok()) {
  165. init_parameters_ = parameters;
  166. }
  167. return result;
  168. }
  169. return worker_thread_->Invoke<RTCError>(RTC_FROM_HERE, [&] {
  170. RtpParameters rtp_parameters = parameters;
  171. if (!disabled_rids_.empty()) {
  172. // Need to add the inactive layers.
  173. RtpParameters old_parameters =
  174. media_channel_->GetRtpSendParameters(ssrc_);
  175. rtp_parameters = RestoreEncodingLayers(parameters, disabled_rids_,
  176. old_parameters.encodings);
  177. }
  178. return media_channel_->SetRtpSendParameters(ssrc_, rtp_parameters);
  179. });
  180. }
  181. RTCError RtpSenderBase::SetParameters(const RtpParameters& parameters) {
  182. TRACE_EVENT0("webrtc", "RtpSenderBase::SetParameters");
  183. if (stopped_) {
  184. LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
  185. "Cannot set parameters on a stopped sender.");
  186. }
  187. if (!last_transaction_id_) {
  188. LOG_AND_RETURN_ERROR(
  189. RTCErrorType::INVALID_STATE,
  190. "Failed to set parameters since getParameters() has never been called"
  191. " on this sender");
  192. }
  193. if (last_transaction_id_ != parameters.transaction_id) {
  194. LOG_AND_RETURN_ERROR(
  195. RTCErrorType::INVALID_MODIFICATION,
  196. "Failed to set parameters since the transaction_id doesn't match"
  197. " the last value returned from getParameters()");
  198. }
  199. RTCError result = SetParametersInternal(parameters);
  200. last_transaction_id_.reset();
  201. return result;
  202. }
  203. void RtpSenderBase::SetStreams(const std::vector<std::string>& stream_ids) {
  204. set_stream_ids(stream_ids);
  205. if (set_streams_observer_)
  206. set_streams_observer_->OnSetStreams();
  207. }
  208. bool RtpSenderBase::SetTrack(MediaStreamTrackInterface* track) {
  209. TRACE_EVENT0("webrtc", "RtpSenderBase::SetTrack");
  210. if (stopped_) {
  211. RTC_LOG(LS_ERROR) << "SetTrack can't be called on a stopped RtpSender.";
  212. return false;
  213. }
  214. if (track && track->kind() != track_kind()) {
  215. RTC_LOG(LS_ERROR) << "SetTrack with " << track->kind()
  216. << " called on RtpSender with " << track_kind()
  217. << " track.";
  218. return false;
  219. }
  220. // Detach from old track.
  221. if (track_) {
  222. DetachTrack();
  223. track_->UnregisterObserver(this);
  224. RemoveTrackFromStats();
  225. }
  226. // Attach to new track.
  227. bool prev_can_send_track = can_send_track();
  228. // Keep a reference to the old track to keep it alive until we call SetSend.
  229. rtc::scoped_refptr<MediaStreamTrackInterface> old_track = track_;
  230. track_ = track;
  231. if (track_) {
  232. track_->RegisterObserver(this);
  233. AttachTrack();
  234. }
  235. // Update channel.
  236. if (can_send_track()) {
  237. SetSend();
  238. AddTrackToStats();
  239. } else if (prev_can_send_track) {
  240. ClearSend();
  241. }
  242. attachment_id_ = (track_ ? GenerateUniqueId() : 0);
  243. return true;
  244. }
  245. void RtpSenderBase::SetSsrc(uint32_t ssrc) {
  246. TRACE_EVENT0("webrtc", "RtpSenderBase::SetSsrc");
  247. if (stopped_ || ssrc == ssrc_) {
  248. return;
  249. }
  250. // If we are already sending with a particular SSRC, stop sending.
  251. if (can_send_track()) {
  252. ClearSend();
  253. RemoveTrackFromStats();
  254. }
  255. ssrc_ = ssrc;
  256. if (can_send_track()) {
  257. SetSend();
  258. AddTrackToStats();
  259. }
  260. if (!init_parameters_.encodings.empty()) {
  261. worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
  262. RTC_DCHECK(media_channel_);
  263. // Get the current parameters, which are constructed from the SDP.
  264. // The number of layers in the SDP is currently authoritative to support
  265. // SDP munging for Plan-B simulcast with "a=ssrc-group:SIM <ssrc-id>..."
  266. // lines as described in RFC 5576.
  267. // All fields should be default constructed and the SSRC field set, which
  268. // we need to copy.
  269. RtpParameters current_parameters =
  270. media_channel_->GetRtpSendParameters(ssrc_);
  271. RTC_DCHECK_GE(current_parameters.encodings.size(),
  272. init_parameters_.encodings.size());
  273. for (size_t i = 0; i < init_parameters_.encodings.size(); ++i) {
  274. init_parameters_.encodings[i].ssrc =
  275. current_parameters.encodings[i].ssrc;
  276. init_parameters_.encodings[i].rid = current_parameters.encodings[i].rid;
  277. current_parameters.encodings[i] = init_parameters_.encodings[i];
  278. }
  279. current_parameters.degradation_preference =
  280. init_parameters_.degradation_preference;
  281. media_channel_->SetRtpSendParameters(ssrc_, current_parameters);
  282. init_parameters_.encodings.clear();
  283. });
  284. }
  285. // Attempt to attach the frame decryptor to the current media channel.
  286. if (frame_encryptor_) {
  287. SetFrameEncryptor(frame_encryptor_);
  288. }
  289. }
  290. void RtpSenderBase::Stop() {
  291. TRACE_EVENT0("webrtc", "RtpSenderBase::Stop");
  292. // TODO(deadbeef): Need to do more here to fully stop sending packets.
  293. if (stopped_) {
  294. return;
  295. }
  296. if (track_) {
  297. DetachTrack();
  298. track_->UnregisterObserver(this);
  299. }
  300. if (can_send_track()) {
  301. ClearSend();
  302. RemoveTrackFromStats();
  303. }
  304. media_channel_ = nullptr;
  305. set_streams_observer_ = nullptr;
  306. stopped_ = true;
  307. }
  308. RTCError RtpSenderBase::DisableEncodingLayers(
  309. const std::vector<std::string>& rids) {
  310. if (stopped_) {
  311. LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
  312. "Cannot disable encodings on a stopped sender.");
  313. }
  314. if (rids.empty()) {
  315. return RTCError::OK();
  316. }
  317. // Check that all the specified layers exist and disable them in the channel.
  318. RtpParameters parameters = GetParametersInternal();
  319. for (const std::string& rid : rids) {
  320. if (absl::c_none_of(parameters.encodings,
  321. [&rid](const RtpEncodingParameters& encoding) {
  322. return encoding.rid == rid;
  323. })) {
  324. LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
  325. "RID: " + rid + " does not refer to a valid layer.");
  326. }
  327. }
  328. if (!media_channel_ || !ssrc_) {
  329. RemoveEncodingLayers(rids, &init_parameters_.encodings);
  330. // Invalidate any transaction upon success.
  331. last_transaction_id_.reset();
  332. return RTCError::OK();
  333. }
  334. for (RtpEncodingParameters& encoding : parameters.encodings) {
  335. // Remain active if not in the disable list.
  336. encoding.active &= absl::c_none_of(
  337. rids,
  338. [&encoding](const std::string& rid) { return encoding.rid == rid; });
  339. }
  340. RTCError result = SetParametersInternal(parameters);
  341. if (result.ok()) {
  342. disabled_rids_.insert(disabled_rids_.end(), rids.begin(), rids.end());
  343. // Invalidate any transaction upon success.
  344. last_transaction_id_.reset();
  345. }
  346. return result;
  347. }
  348. LocalAudioSinkAdapter::LocalAudioSinkAdapter() : sink_(nullptr) {}
  349. LocalAudioSinkAdapter::~LocalAudioSinkAdapter() {
  350. rtc::CritScope lock(&lock_);
  351. if (sink_)
  352. sink_->OnClose();
  353. }
  354. void LocalAudioSinkAdapter::OnData(const void* audio_data,
  355. int bits_per_sample,
  356. int sample_rate,
  357. size_t number_of_channels,
  358. size_t number_of_frames) {
  359. rtc::CritScope lock(&lock_);
  360. if (sink_) {
  361. sink_->OnData(audio_data, bits_per_sample, sample_rate, number_of_channels,
  362. number_of_frames);
  363. }
  364. }
  365. void LocalAudioSinkAdapter::SetSink(cricket::AudioSource::Sink* sink) {
  366. rtc::CritScope lock(&lock_);
  367. RTC_DCHECK(!sink || !sink_);
  368. sink_ = sink;
  369. }
  370. rtc::scoped_refptr<AudioRtpSender> AudioRtpSender::Create(
  371. rtc::Thread* worker_thread,
  372. const std::string& id,
  373. StatsCollector* stats,
  374. SetStreamsObserver* set_streams_observer) {
  375. return rtc::scoped_refptr<AudioRtpSender>(
  376. new rtc::RefCountedObject<AudioRtpSender>(worker_thread, id, stats,
  377. set_streams_observer));
  378. }
  379. AudioRtpSender::AudioRtpSender(rtc::Thread* worker_thread,
  380. const std::string& id,
  381. StatsCollector* stats,
  382. SetStreamsObserver* set_streams_observer)
  383. : RtpSenderBase(worker_thread, id, set_streams_observer),
  384. stats_(stats),
  385. dtmf_sender_proxy_(DtmfSenderProxy::Create(
  386. rtc::Thread::Current(),
  387. DtmfSender::Create(rtc::Thread::Current(), this))),
  388. sink_adapter_(new LocalAudioSinkAdapter()) {}
  389. AudioRtpSender::~AudioRtpSender() {
  390. // For DtmfSender.
  391. SignalDestroyed();
  392. Stop();
  393. }
  394. bool AudioRtpSender::CanInsertDtmf() {
  395. if (!media_channel_) {
  396. RTC_LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
  397. return false;
  398. }
  399. // Check that this RTP sender is active (description has been applied that
  400. // matches an SSRC to its ID).
  401. if (!ssrc_) {
  402. RTC_LOG(LS_ERROR) << "CanInsertDtmf: Sender does not have SSRC.";
  403. return false;
  404. }
  405. return worker_thread_->Invoke<bool>(
  406. RTC_FROM_HERE, [&] { return voice_media_channel()->CanInsertDtmf(); });
  407. }
  408. bool AudioRtpSender::InsertDtmf(int code, int duration) {
  409. if (!media_channel_) {
  410. RTC_LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
  411. return false;
  412. }
  413. if (!ssrc_) {
  414. RTC_LOG(LS_ERROR) << "InsertDtmf: Sender does not have SSRC.";
  415. return false;
  416. }
  417. bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
  418. return voice_media_channel()->InsertDtmf(ssrc_, code, duration);
  419. });
  420. if (!success) {
  421. RTC_LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
  422. }
  423. return success;
  424. }
  425. sigslot::signal0<>* AudioRtpSender::GetOnDestroyedSignal() {
  426. return &SignalDestroyed;
  427. }
  428. void AudioRtpSender::OnChanged() {
  429. TRACE_EVENT0("webrtc", "AudioRtpSender::OnChanged");
  430. RTC_DCHECK(!stopped_);
  431. if (cached_track_enabled_ != track_->enabled()) {
  432. cached_track_enabled_ = track_->enabled();
  433. if (can_send_track()) {
  434. SetSend();
  435. }
  436. }
  437. }
  438. void AudioRtpSender::DetachTrack() {
  439. RTC_DCHECK(track_);
  440. audio_track()->RemoveSink(sink_adapter_.get());
  441. }
  442. void AudioRtpSender::AttachTrack() {
  443. RTC_DCHECK(track_);
  444. cached_track_enabled_ = track_->enabled();
  445. audio_track()->AddSink(sink_adapter_.get());
  446. }
  447. void AudioRtpSender::AddTrackToStats() {
  448. if (can_send_track() && stats_) {
  449. stats_->AddLocalAudioTrack(audio_track().get(), ssrc_);
  450. }
  451. }
  452. void AudioRtpSender::RemoveTrackFromStats() {
  453. if (can_send_track() && stats_) {
  454. stats_->RemoveLocalAudioTrack(audio_track().get(), ssrc_);
  455. }
  456. }
  457. rtc::scoped_refptr<DtmfSenderInterface> AudioRtpSender::GetDtmfSender() const {
  458. return dtmf_sender_proxy_;
  459. }
  460. void AudioRtpSender::SetSend() {
  461. RTC_DCHECK(!stopped_);
  462. RTC_DCHECK(can_send_track());
  463. if (!media_channel_) {
  464. RTC_LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
  465. return;
  466. }
  467. cricket::AudioOptions options;
  468. #if !defined(WEBRTC_CHROMIUM_BUILD) && !defined(WEBRTC_WEBKIT_BUILD)
  469. // TODO(tommi): Remove this hack when we move CreateAudioSource out of
  470. // PeerConnection. This is a bit of a strange way to apply local audio
  471. // options since it is also applied to all streams/channels, local or remote.
  472. if (track_->enabled() && audio_track()->GetSource() &&
  473. !audio_track()->GetSource()->remote()) {
  474. options = audio_track()->GetSource()->options();
  475. }
  476. #endif
  477. // |track_->enabled()| hops to the signaling thread, so call it before we hop
  478. // to the worker thread or else it will deadlock.
  479. bool track_enabled = track_->enabled();
  480. bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
  481. return voice_media_channel()->SetAudioSend(ssrc_, track_enabled, &options,
  482. sink_adapter_.get());
  483. });
  484. if (!success) {
  485. RTC_LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc_;
  486. }
  487. }
  488. void AudioRtpSender::ClearSend() {
  489. RTC_DCHECK(ssrc_ != 0);
  490. RTC_DCHECK(!stopped_);
  491. if (!media_channel_) {
  492. RTC_LOG(LS_WARNING) << "ClearAudioSend: No audio channel exists.";
  493. return;
  494. }
  495. cricket::AudioOptions options;
  496. bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
  497. return voice_media_channel()->SetAudioSend(ssrc_, false, &options, nullptr);
  498. });
  499. if (!success) {
  500. RTC_LOG(LS_WARNING) << "ClearAudioSend: ssrc is incorrect: " << ssrc_;
  501. }
  502. }
  503. rtc::scoped_refptr<VideoRtpSender> VideoRtpSender::Create(
  504. rtc::Thread* worker_thread,
  505. const std::string& id,
  506. SetStreamsObserver* set_streams_observer) {
  507. return rtc::scoped_refptr<VideoRtpSender>(
  508. new rtc::RefCountedObject<VideoRtpSender>(worker_thread, id,
  509. set_streams_observer));
  510. }
  511. VideoRtpSender::VideoRtpSender(rtc::Thread* worker_thread,
  512. const std::string& id,
  513. SetStreamsObserver* set_streams_observer)
  514. : RtpSenderBase(worker_thread, id, set_streams_observer) {}
  515. VideoRtpSender::~VideoRtpSender() {
  516. Stop();
  517. }
  518. void VideoRtpSender::OnChanged() {
  519. TRACE_EVENT0("webrtc", "VideoRtpSender::OnChanged");
  520. RTC_DCHECK(!stopped_);
  521. if (cached_track_content_hint_ != video_track()->content_hint()) {
  522. cached_track_content_hint_ = video_track()->content_hint();
  523. if (can_send_track()) {
  524. SetSend();
  525. }
  526. }
  527. }
  528. void VideoRtpSender::AttachTrack() {
  529. RTC_DCHECK(track_);
  530. cached_track_content_hint_ = video_track()->content_hint();
  531. }
  532. rtc::scoped_refptr<DtmfSenderInterface> VideoRtpSender::GetDtmfSender() const {
  533. RTC_LOG(LS_ERROR) << "Tried to get DTMF sender from video sender.";
  534. return nullptr;
  535. }
  536. void VideoRtpSender::SetSend() {
  537. RTC_DCHECK(!stopped_);
  538. RTC_DCHECK(can_send_track());
  539. if (!media_channel_) {
  540. RTC_LOG(LS_ERROR) << "SetVideoSend: No video channel exists.";
  541. return;
  542. }
  543. cricket::VideoOptions options;
  544. VideoTrackSourceInterface* source = video_track()->GetSource();
  545. if (source) {
  546. options.is_screencast = source->is_screencast();
  547. options.video_noise_reduction = source->needs_denoising();
  548. }
  549. switch (cached_track_content_hint_) {
  550. case VideoTrackInterface::ContentHint::kNone:
  551. break;
  552. case VideoTrackInterface::ContentHint::kFluid:
  553. options.is_screencast = false;
  554. break;
  555. case VideoTrackInterface::ContentHint::kDetailed:
  556. case VideoTrackInterface::ContentHint::kText:
  557. options.is_screencast = true;
  558. break;
  559. }
  560. bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
  561. return video_media_channel()->SetVideoSend(ssrc_, &options, video_track());
  562. });
  563. RTC_DCHECK(success);
  564. }
  565. void VideoRtpSender::ClearSend() {
  566. RTC_DCHECK(ssrc_ != 0);
  567. RTC_DCHECK(!stopped_);
  568. if (!media_channel_) {
  569. RTC_LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
  570. return;
  571. }
  572. // Allow SetVideoSend to fail since |enable| is false and |source| is null.
  573. // This the normal case when the underlying media channel has already been
  574. // deleted.
  575. worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
  576. return video_media_channel()->SetVideoSend(ssrc_, nullptr, nullptr);
  577. });
  578. }
  579. } // namespace webrtc