1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
|
/*
* Copyright 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
// This file contains mock implementations of observers used in PeerConnection.
// TODO(steveanton): These aren't really mocks and should be renamed.
#ifndef PC_TEST_MOCK_PEER_CONNECTION_OBSERVERS_H_
#define PC_TEST_MOCK_PEER_CONNECTION_OBSERVERS_H_
#include <cstddef>
#include <cstdint>
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "api/candidate.h"
#include "api/data_channel_interface.h"
#include "api/jsep.h"
#include "api/jsep_ice_candidate.h"
#include "api/legacy_stats_types.h"
#include "api/make_ref_counted.h"
#include "api/media_stream_interface.h"
#include "api/peer_connection_interface.h"
#include "api/rtc_error.h"
#include "api/rtp_receiver_interface.h"
#include "api/rtp_transceiver_interface.h"
#include "api/scoped_refptr.h"
#include "api/set_local_description_observer_interface.h"
#include "api/set_remote_description_observer_interface.h"
#include "api/stats/rtc_stats_collector_callback.h"
#include "api/stats/rtc_stats_report.h"
#include "pc/stream_collection.h"
#include "rtc_base/checks.h"
#include "rtc_base/string_encode.h"
#include "rtc_base/synchronization/mutex.h"
#include "rtc_base/thread_annotations.h"
namespace webrtc {
class MockPeerConnectionObserver : public PeerConnectionObserver {
public:
struct AddTrackEvent {
explicit AddTrackEvent(
scoped_refptr<RtpReceiverInterface> event_receiver,
std::vector<scoped_refptr<MediaStreamInterface>> event_streams)
: receiver(std::move(event_receiver)),
streams(std::move(event_streams)) {
for (auto stream : streams) {
std::vector<scoped_refptr<MediaStreamTrackInterface>> tracks;
for (auto audio_track : stream->GetAudioTracks()) {
tracks.push_back(audio_track);
}
for (auto video_track : stream->GetVideoTracks()) {
tracks.push_back(video_track);
}
snapshotted_stream_tracks[stream] = tracks;
}
}
scoped_refptr<RtpReceiverInterface> receiver;
std::vector<scoped_refptr<MediaStreamInterface>> streams;
// This map records the tracks present in each stream at the time the
// OnAddTrack callback was issued.
std::map<scoped_refptr<MediaStreamInterface>,
std::vector<scoped_refptr<MediaStreamTrackInterface>>>
snapshotted_stream_tracks;
};
MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
virtual ~MockPeerConnectionObserver() {}
void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
pc_ = pc;
if (pc) {
state_ = pc_->signaling_state();
}
}
void OnSignalingChange(
PeerConnectionInterface::SignalingState new_state) override {
RTC_DCHECK(pc_);
RTC_DCHECK(pc_->signaling_state() == new_state);
state_ = new_state;
}
MediaStreamInterface* RemoteStream(const std::string& label) {
return remote_streams_->find(label);
}
StreamCollectionInterface* remote_streams() const {
return remote_streams_.get();
}
void OnAddStream(scoped_refptr<MediaStreamInterface> stream) override {
last_added_stream_ = stream;
remote_streams_->AddStream(stream);
}
void OnRemoveStream(scoped_refptr<MediaStreamInterface> stream) override {
last_removed_stream_ = stream;
remote_streams_->RemoveStream(stream.get());
}
void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
void OnNegotiationNeededEvent(uint32_t event_id) override {
latest_negotiation_needed_event_ = event_id;
}
void OnDataChannel(
scoped_refptr<DataChannelInterface> data_channel) override {
last_datachannel_ = data_channel;
}
void OnIceConnectionChange(
PeerConnectionInterface::IceConnectionState new_state) override {
RTC_DCHECK(pc_);
RTC_DCHECK(pc_->ice_connection_state() == new_state);
// When ICE is finished, the caller will get to a kIceConnectionCompleted
// state, because it has the ICE controlling role, while the callee
// will get to a kIceConnectionConnected state. This means that both ICE
// and DTLS are connected.
ice_connected_ =
(new_state == PeerConnectionInterface::kIceConnectionConnected) ||
(new_state == PeerConnectionInterface::kIceConnectionCompleted);
callback_triggered_ = true;
}
void OnIceGatheringChange(
PeerConnectionInterface::IceGatheringState new_state) override {
RTC_DCHECK(pc_);
RTC_DCHECK(pc_->ice_gathering_state() == new_state);
ice_gathering_complete_ =
new_state == PeerConnectionInterface::kIceGatheringComplete;
callback_triggered_ = true;
}
void OnIceCandidate(const IceCandidateInterface* candidate) override {
RTC_DCHECK(pc_);
candidates_.push_back(std::make_unique<JsepIceCandidate>(
candidate->sdp_mid(), candidate->sdp_mline_index(),
candidate->candidate()));
callback_triggered_ = true;
}
void OnIceCandidatesRemoved(
const std::vector<Candidate>& candidates) override {
num_candidates_removed_++;
callback_triggered_ = true;
}
void OnIceConnectionReceivingChange(bool receiving) override {
callback_triggered_ = true;
}
void OnAddTrack(scoped_refptr<RtpReceiverInterface> receiver,
const std::vector<scoped_refptr<MediaStreamInterface>>&
streams) override {
RTC_DCHECK(receiver);
num_added_tracks_++;
last_added_track_label_ = receiver->id();
add_track_events_.push_back(AddTrackEvent(receiver, streams));
}
void OnTrack(scoped_refptr<RtpTransceiverInterface> transceiver) override {
on_track_transceivers_.push_back(transceiver);
}
void OnRemoveTrack(scoped_refptr<RtpReceiverInterface> receiver) override {
remove_track_events_.push_back(receiver);
}
std::vector<scoped_refptr<RtpReceiverInterface>> GetAddTrackReceivers() {
std::vector<scoped_refptr<RtpReceiverInterface>> receivers;
for (const AddTrackEvent& event : add_track_events_) {
receivers.push_back(event.receiver);
}
return receivers;
}
int CountAddTrackEventsForStream(const std::string& stream_id) {
int found_tracks = 0;
for (const AddTrackEvent& event : add_track_events_) {
bool has_stream_id = false;
for (auto stream : event.streams) {
if (stream->id() == stream_id) {
has_stream_id = true;
break;
}
}
if (has_stream_id) {
++found_tracks;
}
}
return found_tracks;
}
// Returns the id of the last added stream.
// Empty string if no stream have been added.
std::string GetLastAddedStreamId() {
if (last_added_stream_.get())
return last_added_stream_->id();
return "";
}
std::string GetLastRemovedStreamId() {
if (last_removed_stream_.get())
return last_removed_stream_->id();
return "";
}
IceCandidateInterface* last_candidate() {
if (candidates_.empty()) {
return nullptr;
} else {
return candidates_.back().get();
}
}
std::vector<const IceCandidateInterface*> GetAllCandidates() {
std::vector<const IceCandidateInterface*> candidates;
for (const auto& candidate : candidates_) {
candidates.push_back(candidate.get());
}
return candidates;
}
std::vector<IceCandidateInterface*> GetCandidatesByMline(int mline_index) {
std::vector<IceCandidateInterface*> candidates;
for (const auto& candidate : candidates_) {
if (candidate->sdp_mline_index() == mline_index) {
candidates.push_back(candidate.get());
}
}
return candidates;
}
bool legacy_renegotiation_needed() const { return renegotiation_needed_; }
void clear_legacy_renegotiation_needed() { renegotiation_needed_ = false; }
bool has_negotiation_needed_event() {
return latest_negotiation_needed_event_.has_value();
}
uint32_t latest_negotiation_needed_event() {
return latest_negotiation_needed_event_.value_or(0u);
}
void clear_latest_negotiation_needed_event() {
latest_negotiation_needed_event_ = std::nullopt;
}
scoped_refptr<PeerConnectionInterface> pc_;
PeerConnectionInterface::SignalingState state_;
std::vector<std::unique_ptr<IceCandidateInterface>> candidates_;
scoped_refptr<DataChannelInterface> last_datachannel_;
scoped_refptr<StreamCollection> remote_streams_;
bool renegotiation_needed_ = false;
std::optional<uint32_t> latest_negotiation_needed_event_;
bool ice_gathering_complete_ = false;
bool ice_connected_ = false;
bool callback_triggered_ = false;
int num_added_tracks_ = 0;
std::string last_added_track_label_;
std::vector<AddTrackEvent> add_track_events_;
std::vector<scoped_refptr<RtpReceiverInterface>> remove_track_events_;
std::vector<scoped_refptr<RtpTransceiverInterface>> on_track_transceivers_;
int num_candidates_removed_ = 0;
private:
scoped_refptr<MediaStreamInterface> last_added_stream_;
scoped_refptr<MediaStreamInterface> last_removed_stream_;
};
class MockCreateSessionDescriptionObserver
: public CreateSessionDescriptionObserver {
public:
MockCreateSessionDescriptionObserver()
: called_(false),
error_("MockCreateSessionDescriptionObserver not called") {}
virtual ~MockCreateSessionDescriptionObserver() {}
void OnSuccess(SessionDescriptionInterface* desc) override {
MutexLock lock(&mutex_);
called_ = true;
error_ = "";
desc_.reset(desc);
}
void OnFailure(RTCError error) override {
MutexLock lock(&mutex_);
called_ = true;
error_ = error.message();
}
bool called() const {
MutexLock lock(&mutex_);
return called_;
}
bool result() const {
MutexLock lock(&mutex_);
return error_.empty();
}
const std::string& error() const {
MutexLock lock(&mutex_);
return error_;
}
std::unique_ptr<SessionDescriptionInterface> MoveDescription() {
MutexLock lock(&mutex_);
return std::move(desc_);
}
private:
mutable Mutex mutex_;
bool called_ RTC_GUARDED_BY(mutex_);
std::string error_ RTC_GUARDED_BY(mutex_);
std::unique_ptr<SessionDescriptionInterface> desc_ RTC_GUARDED_BY(mutex_);
};
class MockSetSessionDescriptionObserver : public SetSessionDescriptionObserver {
public:
static scoped_refptr<MockSetSessionDescriptionObserver> Create() {
return make_ref_counted<MockSetSessionDescriptionObserver>();
}
MockSetSessionDescriptionObserver()
: called_(false),
error_("MockSetSessionDescriptionObserver not called") {}
~MockSetSessionDescriptionObserver() override {}
void OnSuccess() override {
MutexLock lock(&mutex_);
called_ = true;
error_ = "";
}
void OnFailure(RTCError error) override {
MutexLock lock(&mutex_);
called_ = true;
error_ = error.message();
}
bool called() const {
MutexLock lock(&mutex_);
return called_;
}
bool result() const {
MutexLock lock(&mutex_);
return error_.empty();
}
const std::string& error() const {
MutexLock lock(&mutex_);
return error_;
}
private:
mutable Mutex mutex_;
bool called_;
std::string error_;
};
class FakeSetLocalDescriptionObserver
: public SetLocalDescriptionObserverInterface {
public:
bool called() const { return error_.has_value(); }
RTCError& error() {
RTC_DCHECK(error_.has_value());
return *error_;
}
// SetLocalDescriptionObserverInterface implementation.
void OnSetLocalDescriptionComplete(RTCError error) override {
error_ = std::move(error);
}
private:
// Set on complete, on success this is set to an RTCError::OK() error.
std::optional<RTCError> error_;
};
class FakeSetRemoteDescriptionObserver
: public SetRemoteDescriptionObserverInterface {
public:
bool called() const { return error_.has_value(); }
RTCError& error() {
RTC_DCHECK(error_.has_value());
return *error_;
}
// SetRemoteDescriptionObserverInterface implementation.
void OnSetRemoteDescriptionComplete(RTCError error) override {
error_ = std::move(error);
}
private:
// Set on complete, on success this is set to an RTCError::OK() error.
std::optional<RTCError> error_;
};
class MockDataChannelObserver : public DataChannelObserver {
public:
struct Message {
std::string data;
bool binary;
};
explicit MockDataChannelObserver(DataChannelInterface* channel)
: channel_(channel) {
channel_->RegisterObserver(this);
states_.push_back(channel_->state());
}
virtual ~MockDataChannelObserver() { channel_->UnregisterObserver(); }
void OnBufferedAmountChange(uint64_t previous_amount) override {}
void OnStateChange() override {
states_.push_back(channel_->state());
if (state_change_callback_) {
state_change_callback_(states_.back());
}
}
void OnMessage(const DataBuffer& buffer) override {
messages_.push_back(
{std::string(buffer.data.data<char>(), buffer.data.size()),
buffer.binary});
}
bool IsOpen() const { return state() == DataChannelInterface::kOpen; }
std::vector<Message> messages() const { return messages_; }
std::string last_message() const {
if (messages_.empty())
return {};
return messages_.back().data;
}
bool last_message_is_binary() const {
if (messages_.empty())
return false;
return messages_.back().binary;
}
size_t received_message_count() const { return messages_.size(); }
DataChannelInterface::DataState state() const { return states_.back(); }
const std::vector<DataChannelInterface::DataState>& states() const {
return states_;
}
void set_state_change_callback(
std::function<void(DataChannelInterface::DataState)> func) {
state_change_callback_ = std::move(func);
}
private:
scoped_refptr<DataChannelInterface> channel_;
std::vector<DataChannelInterface::DataState> states_;
std::vector<Message> messages_;
std::function<void(DataChannelInterface::DataState)> state_change_callback_;
};
class MockStatsObserver : public StatsObserver {
public:
MockStatsObserver() : called_(false), stats_() {}
virtual ~MockStatsObserver() {}
virtual void OnComplete(const StatsReports& reports) {
RTC_CHECK(!called_);
called_ = true;
stats_.Clear();
stats_.number_of_reports = reports.size();
for (const auto* r : reports) {
if (r->type() == StatsReport::kStatsReportTypeSsrc) {
stats_.timestamp = r->timestamp();
GetIntValue(r, StatsReport::kStatsValueNameAudioOutputLevel,
&stats_.audio_output_level);
GetIntValue(r, StatsReport::kStatsValueNameAudioInputLevel,
&stats_.audio_input_level);
GetIntValue(r, StatsReport::kStatsValueNameBytesReceived,
&stats_.bytes_received);
GetIntValue(r, StatsReport::kStatsValueNameBytesSent,
&stats_.bytes_sent);
GetInt64Value(r, StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
&stats_.capture_start_ntp_time);
stats_.track_ids.emplace_back();
GetStringValue(r, StatsReport::kStatsValueNameTrackId,
&stats_.track_ids.back());
} else if (r->type() == StatsReport::kStatsReportTypeBwe) {
stats_.timestamp = r->timestamp();
GetIntValue(r, StatsReport::kStatsValueNameAvailableReceiveBandwidth,
&stats_.available_receive_bandwidth);
} else if (r->type() == StatsReport::kStatsReportTypeComponent) {
stats_.timestamp = r->timestamp();
GetStringValue(r, StatsReport::kStatsValueNameDtlsCipher,
&stats_.dtls_cipher);
GetStringValue(r, StatsReport::kStatsValueNameSrtpCipher,
&stats_.srtp_cipher);
}
}
}
bool called() const { return called_; }
size_t number_of_reports() const { return stats_.number_of_reports; }
double timestamp() const { return stats_.timestamp; }
int AudioOutputLevel() const {
RTC_CHECK(called_);
return stats_.audio_output_level;
}
int AudioInputLevel() const {
RTC_CHECK(called_);
return stats_.audio_input_level;
}
int BytesReceived() const {
RTC_CHECK(called_);
return stats_.bytes_received;
}
int BytesSent() const {
RTC_CHECK(called_);
return stats_.bytes_sent;
}
int64_t CaptureStartNtpTime() const {
RTC_CHECK(called_);
return stats_.capture_start_ntp_time;
}
int AvailableReceiveBandwidth() const {
RTC_CHECK(called_);
return stats_.available_receive_bandwidth;
}
std::string DtlsCipher() const {
RTC_CHECK(called_);
return stats_.dtls_cipher;
}
std::string SrtpCipher() const {
RTC_CHECK(called_);
return stats_.srtp_cipher;
}
std::vector<std::string> TrackIds() const {
RTC_CHECK(called_);
return stats_.track_ids;
}
private:
bool GetIntValue(const StatsReport* report,
StatsReport::StatsValueName name,
int* value) {
const StatsReport::Value* v = report->FindValue(name);
if (v) {
// TODO(tommi): We should really just be using an int here :-/
*value = FromString<int>(v->ToString());
}
return v != nullptr;
}
bool GetInt64Value(const StatsReport* report,
StatsReport::StatsValueName name,
int64_t* value) {
const StatsReport::Value* v = report->FindValue(name);
if (v) {
// TODO(tommi): We should really just be using an int here :-/
*value = FromString<int64_t>(v->ToString());
}
return v != nullptr;
}
bool GetStringValue(const StatsReport* report,
StatsReport::StatsValueName name,
std::string* value) {
const StatsReport::Value* v = report->FindValue(name);
if (v)
*value = v->ToString();
return v != nullptr;
}
bool called_;
struct {
void Clear() {
number_of_reports = 0;
timestamp = 0;
audio_output_level = 0;
audio_input_level = 0;
bytes_received = 0;
bytes_sent = 0;
capture_start_ntp_time = 0;
available_receive_bandwidth = 0;
dtls_cipher.clear();
srtp_cipher.clear();
track_ids.clear();
}
size_t number_of_reports;
double timestamp;
int audio_output_level;
int audio_input_level;
int bytes_received;
int bytes_sent;
int64_t capture_start_ntp_time;
int available_receive_bandwidth;
std::string dtls_cipher;
std::string srtp_cipher;
std::vector<std::string> track_ids;
} stats_;
};
// Helper class that just stores the report from the callback.
class MockRTCStatsCollectorCallback : public RTCStatsCollectorCallback {
public:
scoped_refptr<const RTCStatsReport> report() { return report_; }
bool called() const { return called_; }
protected:
void OnStatsDelivered(
const scoped_refptr<const RTCStatsReport>& report) override {
report_ = report;
called_ = true;
}
private:
bool called_ = false;
scoped_refptr<const RTCStatsReport> report_;
};
} // namespace webrtc
#endif // PC_TEST_MOCK_PEER_CONNECTION_OBSERVERS_H_
|