Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(603)

Side by Side Diff: webrtc/modules/rtp_rtcp/source/rtp_receiver_impl.cc

Issue 2770233003: Implemented the GetSources() in native code. (Closed)
Patch Set: Try to fix the build failure on the bots. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
11 #include "webrtc/modules/rtp_rtcp/source/rtp_receiver_impl.h" 11 #include "webrtc/modules/rtp_rtcp/source/rtp_receiver_impl.h"
12 12
13 #include <assert.h> 13 #include <assert.h>
14 #include <math.h> 14 #include <math.h>
15 #include <stdlib.h> 15 #include <stdlib.h>
16 #include <string.h> 16 #include <string.h>
17 17
18 #include <set>
19 #include <vector>
20
18 #include "webrtc/base/logging.h" 21 #include "webrtc/base/logging.h"
19 #include "webrtc/common_types.h" 22 #include "webrtc/common_types.h"
20 #include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h" 23 #include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
21 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" 24 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
22 #include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h" 25 #include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
23 26
24 namespace webrtc { 27 namespace webrtc {
25 28
29 // Only return the contribuing sources in the last 10 seconds.
30 static const int64_t kContributingSourcesTimeoutMs = 10000;
31
32 // The maximum size of the contributing sources lists.
33 static const int kMaxContributingSourceListsSize = 100;
34
26 using RtpUtility::Payload; 35 using RtpUtility::Payload;
27 36
28 RtpReceiver* RtpReceiver::CreateVideoReceiver( 37 RtpReceiver* RtpReceiver::CreateVideoReceiver(
29 Clock* clock, 38 Clock* clock,
30 RtpData* incoming_payload_callback, 39 RtpData* incoming_payload_callback,
31 RtpFeedback* incoming_messages_callback, 40 RtpFeedback* incoming_messages_callback,
32 RTPPayloadRegistry* rtp_payload_registry) { 41 RTPPayloadRegistry* rtp_payload_registry) {
33 if (!incoming_payload_callback) 42 if (!incoming_payload_callback)
34 incoming_payload_callback = NullObjectRtpData(); 43 incoming_payload_callback = NullObjectRtpData();
35 if (!incoming_messages_callback) 44 if (!incoming_messages_callback)
(...skipping 10 matching lines...) Expand all
46 RTPPayloadRegistry* rtp_payload_registry) { 55 RTPPayloadRegistry* rtp_payload_registry) {
47 if (!incoming_payload_callback) 56 if (!incoming_payload_callback)
48 incoming_payload_callback = NullObjectRtpData(); 57 incoming_payload_callback = NullObjectRtpData();
49 if (!incoming_messages_callback) 58 if (!incoming_messages_callback)
50 incoming_messages_callback = NullObjectRtpFeedback(); 59 incoming_messages_callback = NullObjectRtpFeedback();
51 return new RtpReceiverImpl( 60 return new RtpReceiverImpl(
52 clock, incoming_messages_callback, rtp_payload_registry, 61 clock, incoming_messages_callback, rtp_payload_registry,
53 RTPReceiverStrategy::CreateAudioStrategy(incoming_payload_callback)); 62 RTPReceiverStrategy::CreateAudioStrategy(incoming_payload_callback));
54 } 63 }
55 64
56 RtpReceiverImpl::RtpReceiverImpl( 65 RtpReceiverImpl::RtpReceiverImpl(Clock* clock,
57 Clock* clock, 66 RtpFeedback* incoming_messages_callback,
58 RtpFeedback* incoming_messages_callback, 67 RTPPayloadRegistry* rtp_payload_registry,
59 RTPPayloadRegistry* rtp_payload_registry, 68 RTPReceiverStrategy* rtp_media_receiver)
60 RTPReceiverStrategy* rtp_media_receiver)
61 : clock_(clock), 69 : clock_(clock),
62 rtp_payload_registry_(rtp_payload_registry), 70 rtp_payload_registry_(rtp_payload_registry),
63 rtp_media_receiver_(rtp_media_receiver), 71 rtp_media_receiver_(rtp_media_receiver),
64 cb_rtp_feedback_(incoming_messages_callback), 72 cb_rtp_feedback_(incoming_messages_callback),
65 last_receive_time_(0), 73 last_receive_time_(0),
66 last_received_payload_length_(0), 74 last_received_payload_length_(0),
67 ssrc_(0), 75 ssrc_(0),
68 num_csrcs_(0), 76 num_csrcs_(0),
69 current_remote_csrc_(), 77 current_remote_csrc_(),
70 last_received_timestamp_(0), 78 last_received_timestamp_(0),
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 } 161 }
154 LOG(LS_WARNING) << "Receiving invalid payload type."; 162 LOG(LS_WARNING) << "Receiving invalid payload type.";
155 return false; 163 return false;
156 } 164 }
157 165
158 WebRtcRTPHeader webrtc_rtp_header; 166 WebRtcRTPHeader webrtc_rtp_header;
159 memset(&webrtc_rtp_header, 0, sizeof(webrtc_rtp_header)); 167 memset(&webrtc_rtp_header, 0, sizeof(webrtc_rtp_header));
160 webrtc_rtp_header.header = rtp_header; 168 webrtc_rtp_header.header = rtp_header;
161 CheckCSRC(webrtc_rtp_header); 169 CheckCSRC(webrtc_rtp_header);
162 170
171 UpdateContributingSource();
172
163 size_t payload_data_length = payload_length - rtp_header.paddingLength; 173 size_t payload_data_length = payload_length - rtp_header.paddingLength;
164 174
165 bool is_first_packet_in_frame = false; 175 bool is_first_packet_in_frame = false;
166 { 176 {
167 rtc::CritScope lock(&critical_section_rtp_receiver_); 177 rtc::CritScope lock(&critical_section_rtp_receiver_);
168 if (HaveReceivedFrame()) { 178 if (HaveReceivedFrame()) {
169 is_first_packet_in_frame = 179 is_first_packet_in_frame =
170 last_received_sequence_number_ + 1 == rtp_header.sequenceNumber && 180 last_received_sequence_number_ + 1 == rtp_header.sequenceNumber &&
171 last_received_timestamp_ != rtp_header.timestamp; 181 last_received_timestamp_ != rtp_header.timestamp;
172 } else { 182 } else {
(...skipping 23 matching lines...) Expand all
196 last_received_sequence_number_ = rtp_header.sequenceNumber; 206 last_received_sequence_number_ = rtp_header.sequenceNumber;
197 } 207 }
198 } 208 }
199 return true; 209 return true;
200 } 210 }
201 211
202 TelephoneEventHandler* RtpReceiverImpl::GetTelephoneEventHandler() { 212 TelephoneEventHandler* RtpReceiverImpl::GetTelephoneEventHandler() {
203 return rtp_media_receiver_->GetTelephoneEventHandler(); 213 return rtp_media_receiver_->GetTelephoneEventHandler();
204 } 214 }
205 215
216 std::vector<RtpContributingSource> RtpReceiverImpl::GetContributingSources() {
217 int64_t now = clock_->TimeInMilliseconds();
218 std::vector<RtpContributingSource> contributing_sources;
219
220 {
221 rtc::CritScope lock(&critical_section_rtp_receiver_);
222
223 for (auto rit = csrc_source_list_.rbegin(); rit != csrc_source_list_.rend();
224 ++rit) {
225 if (now - (*rit).timestamp() > kContributingSourcesTimeoutMs) {
226 break;
227 }
228 contributing_sources.push_back(*rit);
229 }
230
231 // Add the contributing sources that use the SSRC.
232 std::set<uint32_t> selected_ssrcs;
233 for (auto rit = ssrc_source_list_.rbegin(); rit != ssrc_source_list_.rend();
234 ++rit) {
235 if (now - (*rit).timestamp() > kContributingSourcesTimeoutMs) {
236 break;
237 }
238 if (selected_ssrcs.find((*rit).ssrc()) == selected_ssrcs.end()) {
239 selected_ssrcs.insert((*rit).ssrc());
240 contributing_sources.push_back(*rit);
241 }
242 }
243 } // End critsect.
244
245 return contributing_sources;
246 }
247
206 bool RtpReceiverImpl::Timestamp(uint32_t* timestamp) const { 248 bool RtpReceiverImpl::Timestamp(uint32_t* timestamp) const {
207 rtc::CritScope lock(&critical_section_rtp_receiver_); 249 rtc::CritScope lock(&critical_section_rtp_receiver_);
208 if (!HaveReceivedFrame()) 250 if (!HaveReceivedFrame())
209 return false; 251 return false;
210 *timestamp = last_received_timestamp_; 252 *timestamp = last_received_timestamp_;
211 return true; 253 return true;
212 } 254 }
213 255
214 bool RtpReceiverImpl::LastReceivedTimeMs(int64_t* receive_time_ms) const { 256 bool RtpReceiverImpl::LastReceivedTimeMs(int64_t* receive_time_ms) const {
215 rtc::CritScope lock(&critical_section_rtp_receiver_); 257 rtc::CritScope lock(&critical_section_rtp_receiver_);
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
454 // Using CSRC 0 to signal this event, not interop safe, other 496 // Using CSRC 0 to signal this event, not interop safe, other
455 // implementations might have CSRC 0 as a valid value. 497 // implementations might have CSRC 0 as a valid value.
456 if (num_csrcs_diff > 0) { 498 if (num_csrcs_diff > 0) {
457 cb_rtp_feedback_->OnIncomingCSRCChanged(0, true); 499 cb_rtp_feedback_->OnIncomingCSRCChanged(0, true);
458 } else if (num_csrcs_diff < 0) { 500 } else if (num_csrcs_diff < 0) {
459 cb_rtp_feedback_->OnIncomingCSRCChanged(0, false); 501 cb_rtp_feedback_->OnIncomingCSRCChanged(0, false);
460 } 502 }
461 } 503 }
462 } 504 }
463 505
506 void RtpReceiverImpl::UpdateContributingSource() {
507 rtc::CritScope lock(&critical_section_rtp_receiver_);
508 int64_t now = clock_->TimeInMilliseconds();
509
510 for (size_t i = 0; i < num_csrcs_; ++i) {
511 auto map_it = iterator_by_csrc_.find(current_remote_csrc_[i]);
512 // If it is a new CSRC, append a new object to the end of the list.
513 if (map_it == iterator_by_csrc_.end()) {
514 RtpContributingSource contributing_source;
515 contributing_source.set_timestamp(now);
516 contributing_source.set_csrc(current_remote_csrc_[i]);
517 csrc_source_list_.push_back(contributing_source);
518 } else { // Move the object to the end of the list.
519 auto list_it = map_it->second;
520 (*list_it).set_timestamp(now);
521 (*list_it).set_csrc(current_remote_csrc_[i]);
522 csrc_source_list_.splice(csrc_source_list_.end(), csrc_source_list_,
523 list_it);
524 }
525 // Update the unordered_map.
526 auto new_list_it = std::prev(csrc_source_list_.end());
527 iterator_by_csrc_[current_remote_csrc_[i]] = new_list_it;
528
529 // Remove the out of date objects if the lists are too large.
530 if (csrc_source_list_.size() + ssrc_source_list_.size() >
531 kMaxContributingSourceListsSize) {
532 UpdateSourceLists(now);
533 }
534 }
535
536 // If this is the first packet or the SSRC is changed, insert a new
537 // contributing source that uses the SSRC.
538 if (ssrc_source_list_.size() == 0 ||
539 ssrc_source_list_[ssrc_source_list_.size() - 1].ssrc() != ssrc_) {
540 RtpContributingSource ssrc_source;
541 ssrc_source.set_timestamp(now);
542 ssrc_source.set_ssrc(ssrc_);
543 ssrc_source_list_.push_back(ssrc_source);
544 } else {
545 ssrc_source_list_[ssrc_source_list_.size() - 1].set_timestamp(now);
546 }
547 }
548
549 // Update the lists and remove the out of date objects.
550 void RtpReceiverImpl::UpdateSourceLists(int64_t now) {
551 for (auto it = csrc_source_list_.begin(); it != csrc_source_list_.end();
552 ++it) {
553 if (now - (*it).timestamp() <= kContributingSourcesTimeoutMs) {
554 break;
555 }
556 iterator_by_csrc_.erase((*it).source());
557 csrc_source_list_.erase(it);
558 }
559
560 for (auto it = ssrc_source_list_.begin(); it != ssrc_source_list_.end();
561 ++it) {
562 if (now - (*it).timestamp() <= kContributingSourcesTimeoutMs) {
563 break;
564 }
565 ssrc_source_list_.erase(it);
566 }
567 }
568
464 } // namespace webrtc 569 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698