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 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
|
/*
* Copyright 2018 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.
*/
#import "base/RTCVideoDecoderFactory.h"
#import "base/RTCVideoEncoderFactory.h"
#import "api/peerconnection/RTCAudioSource.h"
#import "api/peerconnection/RTCConfiguration.h"
#import "api/peerconnection/RTCDataChannel.h"
#import "api/peerconnection/RTCDataChannelConfiguration.h"
#import "api/peerconnection/RTCMediaConstraints.h"
#import "api/peerconnection/RTCMediaStreamTrack.h"
#import "api/peerconnection/RTCPeerConnection.h"
#import "api/peerconnection/RTCPeerConnectionFactory.h"
#import "api/peerconnection/RTCRtpCapabilities.h"
#import "api/peerconnection/RTCRtpCodecCapability.h"
#import "api/peerconnection/RTCRtpHeaderExtensionCapability.h"
#import "api/peerconnection/RTCRtpReceiver.h"
#import "api/peerconnection/RTCRtpSender.h"
#import "api/peerconnection/RTCRtpTransceiver.h"
#import "api/peerconnection/RTCSessionDescription.h"
#import "api/peerconnection/RTCVideoSource.h"
#import "rtc_base/system/unused.h"
#import <XCTest/XCTest.h>
@interface MockVideoEncoderDecoderFactory
: NSObject <RTC_OBJC_TYPE (RTCVideoEncoderFactory),
RTC_OBJC_TYPE (RTCVideoDecoderFactory)>
- (instancetype)initWithSupportedCodecs:
(nonnull NSArray<RTC_OBJC_TYPE(RTCVideoCodecInfo) *> *)supportedCodecs;
@end
@implementation MockVideoEncoderDecoderFactory {
NSArray<RTC_OBJC_TYPE(RTCVideoCodecInfo) *> *_supportedCodecs;
}
- (instancetype)initWithSupportedCodecs:
(nonnull NSArray<RTC_OBJC_TYPE(RTCVideoCodecInfo) *> *)supportedCodecs {
self = [super init];
if (self) {
_supportedCodecs = supportedCodecs;
}
return self;
}
- (nullable id<RTC_OBJC_TYPE(RTCVideoEncoder)>)createEncoder:
(nonnull RTC_OBJC_TYPE(RTCVideoCodecInfo) *)info {
return nil;
}
- (nullable id<RTC_OBJC_TYPE(RTCVideoDecoder)>)createDecoder:
(nonnull RTC_OBJC_TYPE(RTCVideoCodecInfo) *)info {
return nil;
}
- (nonnull NSArray<RTC_OBJC_TYPE(RTCVideoCodecInfo) *> *)supportedCodecs {
return _supportedCodecs;
}
@end
@interface RTCPeerConnectionFactoryTests : XCTestCase
@end
@implementation RTCPeerConnectionFactoryTests
- (void)testPeerConnectionLifetime {
@autoreleasepool {
RTC_OBJC_TYPE(RTCConfiguration) *config =
[[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
[[RTC_OBJC_TYPE(RTCMediaConstraints) alloc]
initWithMandatoryConstraints:@{}
optionalConstraints:nil];
RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
RTC_OBJC_TYPE(RTCPeerConnection) * peerConnection;
@autoreleasepool {
factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
peerConnection = [factory peerConnectionWithConfiguration:config
constraints:constraints
delegate:nil];
[peerConnection close];
factory = nil;
}
peerConnection = nil;
}
XCTAssertTrue(true, @"Expect test does not crash");
}
- (void)testMediaStreamLifetime {
@autoreleasepool {
RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
RTC_OBJC_TYPE(RTCMediaStream) * mediaStream;
@autoreleasepool {
factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
mediaStream = [factory mediaStreamWithStreamId:@"mediaStream"];
factory = nil;
}
mediaStream = nil;
RTC_UNUSED(mediaStream);
}
XCTAssertTrue(true, "Expect test does not crash");
}
- (void)testDataChannelLifetime {
@autoreleasepool {
RTC_OBJC_TYPE(RTCConfiguration) *config =
[[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
[[RTC_OBJC_TYPE(RTCMediaConstraints) alloc]
initWithMandatoryConstraints:@{}
optionalConstraints:nil];
RTC_OBJC_TYPE(RTCDataChannelConfiguration) *dataChannelConfig =
[[RTC_OBJC_TYPE(RTCDataChannelConfiguration) alloc] init];
RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
RTC_OBJC_TYPE(RTCPeerConnection) * peerConnection;
RTC_OBJC_TYPE(RTCDataChannel) * dataChannel;
@autoreleasepool {
factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
peerConnection = [factory peerConnectionWithConfiguration:config
constraints:constraints
delegate:nil];
dataChannel = [peerConnection dataChannelForLabel:@"test_channel"
configuration:dataChannelConfig];
XCTAssertNotNil(dataChannel);
[peerConnection close];
peerConnection = nil;
factory = nil;
}
dataChannel = nil;
}
XCTAssertTrue(true, "Expect test does not crash");
}
- (void)testRTCRtpTransceiverLifetime {
@autoreleasepool {
RTC_OBJC_TYPE(RTCConfiguration) *config =
[[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
[[RTC_OBJC_TYPE(RTCMediaConstraints) alloc]
initWithMandatoryConstraints:@{}
optionalConstraints:nil];
RTC_OBJC_TYPE(RTCRtpTransceiverInit) *init =
[[RTC_OBJC_TYPE(RTCRtpTransceiverInit) alloc] init];
RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
RTC_OBJC_TYPE(RTCPeerConnection) * peerConnection;
RTC_OBJC_TYPE(RTCRtpTransceiver) * tranceiver;
@autoreleasepool {
factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
peerConnection = [factory peerConnectionWithConfiguration:config
constraints:contraints
delegate:nil];
tranceiver = [peerConnection addTransceiverOfType:RTCRtpMediaTypeAudio
init:init];
XCTAssertNotNil(tranceiver);
[peerConnection close];
peerConnection = nil;
factory = nil;
}
tranceiver = nil;
}
XCTAssertTrue(true, "Expect test does not crash");
}
- (void)testRTCRtpSenderLifetime {
@autoreleasepool {
RTC_OBJC_TYPE(RTCConfiguration) *config =
[[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
config.sdpSemantics = RTCSdpSemanticsPlanB;
RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
[[RTC_OBJC_TYPE(RTCMediaConstraints) alloc]
initWithMandatoryConstraints:@{}
optionalConstraints:nil];
RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
RTC_OBJC_TYPE(RTCPeerConnection) * peerConnection;
RTC_OBJC_TYPE(RTCRtpSender) * sender;
@autoreleasepool {
factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
peerConnection = [factory peerConnectionWithConfiguration:config
constraints:constraints
delegate:nil];
sender = [peerConnection senderWithKind:kRTCMediaStreamTrackKindVideo
streamId:@"stream"];
XCTAssertNotNil(sender);
[peerConnection close];
peerConnection = nil;
factory = nil;
}
sender = nil;
}
XCTAssertTrue(true, "Expect test does not crash");
}
- (void)testRTCRtpReceiverLifetime {
@autoreleasepool {
RTC_OBJC_TYPE(RTCConfiguration) *config =
[[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
config.sdpSemantics = RTCSdpSemanticsPlanB;
RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
[[RTC_OBJC_TYPE(RTCMediaConstraints) alloc]
initWithMandatoryConstraints:@{}
optionalConstraints:nil];
RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
RTC_OBJC_TYPE(RTCPeerConnection) * pc1;
RTC_OBJC_TYPE(RTCPeerConnection) * pc2;
NSArray<RTC_OBJC_TYPE(RTCRtpReceiver) *> *receivers1;
NSArray<RTC_OBJC_TYPE(RTCRtpReceiver) *> *receivers2;
@autoreleasepool {
factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
pc1 = [factory peerConnectionWithConfiguration:config
constraints:constraints
delegate:nil];
[pc1 senderWithKind:kRTCMediaStreamTrackKindAudio streamId:@"stream"];
pc2 = [factory peerConnectionWithConfiguration:config
constraints:constraints
delegate:nil];
[pc2 senderWithKind:kRTCMediaStreamTrackKindAudio streamId:@"stream"];
NSTimeInterval negotiationTimeout = 15;
XCTAssertTrue([self negotiatePeerConnection:pc1
withPeerConnection:pc2
negotiationTimeout:negotiationTimeout]);
XCTAssertEqual(pc1.signalingState, RTCSignalingStateStable);
XCTAssertEqual(pc2.signalingState, RTCSignalingStateStable);
receivers1 = pc1.receivers;
receivers2 = pc2.receivers;
XCTAssertTrue(receivers1.count > 0);
XCTAssertTrue(receivers2.count > 0);
[pc1 close];
[pc2 close];
pc1 = nil;
pc2 = nil;
factory = nil;
}
receivers1 = nil;
receivers2 = nil;
}
XCTAssertTrue(true, "Expect test does not crash");
}
- (void)testAudioSourceLifetime {
@autoreleasepool {
RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
RTC_OBJC_TYPE(RTCAudioSource) * audioSource;
@autoreleasepool {
factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
audioSource = [factory audioSourceWithConstraints:nil];
XCTAssertNotNil(audioSource);
factory = nil;
}
audioSource = nil;
}
XCTAssertTrue(true, "Expect test does not crash");
}
- (void)testVideoSourceLifetime {
@autoreleasepool {
RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
RTC_OBJC_TYPE(RTCVideoSource) * videoSource;
@autoreleasepool {
factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
videoSource = [factory videoSource];
XCTAssertNotNil(videoSource);
factory = nil;
}
videoSource = nil;
}
XCTAssertTrue(true, "Expect test does not crash");
}
- (void)testAudioTrackLifetime {
@autoreleasepool {
RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
RTC_OBJC_TYPE(RTCAudioTrack) * audioTrack;
@autoreleasepool {
factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
audioTrack = [factory audioTrackWithTrackId:@"audioTrack"];
XCTAssertNotNil(audioTrack);
factory = nil;
}
audioTrack = nil;
}
XCTAssertTrue(true, "Expect test does not crash");
}
- (void)testVideoTrackLifetime {
@autoreleasepool {
RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
RTC_OBJC_TYPE(RTCVideoTrack) * videoTrack;
@autoreleasepool {
factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
videoTrack = [factory videoTrackWithSource:[factory videoSource]
trackId:@"videoTrack"];
XCTAssertNotNil(videoTrack);
factory = nil;
}
videoTrack = nil;
}
XCTAssertTrue(true, "Expect test does not crash");
}
- (void)testRollback {
@autoreleasepool {
RTC_OBJC_TYPE(RTCConfiguration) *config =
[[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
RTC_OBJC_TYPE(RTCMediaConstraints) *constraints = [[RTC_OBJC_TYPE(
RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{
kRTCMediaConstraintsOfferToReceiveAudio : kRTCMediaConstraintsValueTrue
}
optionalConstraints:nil];
__block RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
__block RTC_OBJC_TYPE(RTCPeerConnection) * pc1;
RTC_OBJC_TYPE(RTCSessionDescription) *rollback =
[[RTC_OBJC_TYPE(RTCSessionDescription) alloc]
initWithType:RTCSdpTypeRollback
sdp:@""];
@autoreleasepool {
factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
pc1 = [factory peerConnectionWithConfiguration:config
constraints:constraints
delegate:nil];
dispatch_semaphore_t negotiatedSem = dispatch_semaphore_create(0);
[pc1 offerForConstraints:constraints
completionHandler:^(RTC_OBJC_TYPE(RTCSessionDescription) * offer,
NSError * error) {
XCTAssertNil(error);
XCTAssertNotNil(offer);
__weak RTC_OBJC_TYPE(RTCPeerConnection) *weakPC1 = pc1;
[pc1 setLocalDescription:offer
completionHandler:^(NSError *error) {
XCTAssertNil(error);
[weakPC1 setLocalDescription:rollback
completionHandler:^(NSError *error) {
XCTAssertNil(error);
}];
}];
NSTimeInterval negotiationTimeout = 15;
dispatch_semaphore_wait(
negotiatedSem,
dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(negotiationTimeout * NSEC_PER_SEC)));
XCTAssertEqual(pc1.signalingState, RTCSignalingStateStable);
[pc1 close];
pc1 = nil;
factory = nil;
}];
}
XCTAssertTrue(true, "Expect test does not crash");
}
}
- (void)testSenderCapabilities {
@autoreleasepool {
RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
MockVideoEncoderDecoderFactory *encoder;
MockVideoEncoderDecoderFactory *decoder;
NSArray<RTC_OBJC_TYPE(RTCVideoCodecInfo) *> *supportedCodecs = @[
[[RTC_OBJC_TYPE(RTCVideoCodecInfo) alloc] initWithName:@"VP8"],
[[RTC_OBJC_TYPE(RTCVideoCodecInfo) alloc] initWithName:@"H264"]
];
encoder = [[MockVideoEncoderDecoderFactory alloc]
initWithSupportedCodecs:supportedCodecs];
decoder = [[MockVideoEncoderDecoderFactory alloc]
initWithSupportedCodecs:supportedCodecs];
factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc]
initWithEncoderFactory:encoder
decoderFactory:decoder];
RTC_OBJC_TYPE(RTCRtpCapabilities) *capabilities =
[factory rtpSenderCapabilitiesForKind:kRTCMediaStreamTrackKindVideo];
NSMutableArray<NSString *> *codecNames = [NSMutableArray new];
for (RTC_OBJC_TYPE(RTCRtpCodecCapability) * codec in capabilities.codecs) {
[codecNames addObject:codec.name];
}
XCTAssertTrue([codecNames containsObject:@"VP8"]);
XCTAssertTrue([codecNames containsObject:@"H264"]);
factory = nil;
}
}
- (void)testReceiverCapabilities {
@autoreleasepool {
RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
MockVideoEncoderDecoderFactory *encoder;
MockVideoEncoderDecoderFactory *decoder;
NSArray<RTC_OBJC_TYPE(RTCVideoCodecInfo) *> *supportedCodecs = @[
[[RTC_OBJC_TYPE(RTCVideoCodecInfo) alloc] initWithName:@"VP8"],
[[RTC_OBJC_TYPE(RTCVideoCodecInfo) alloc] initWithName:@"H264"]
];
encoder = [[MockVideoEncoderDecoderFactory alloc]
initWithSupportedCodecs:supportedCodecs];
decoder = [[MockVideoEncoderDecoderFactory alloc]
initWithSupportedCodecs:supportedCodecs];
factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc]
initWithEncoderFactory:encoder
decoderFactory:decoder];
RTC_OBJC_TYPE(RTCRtpCapabilities) *capabilities =
[factory rtpReceiverCapabilitiesForKind:kRTCMediaStreamTrackKindVideo];
NSMutableArray<NSString *> *codecNames = [NSMutableArray new];
for (RTC_OBJC_TYPE(RTCRtpCodecCapability) * codec in capabilities.codecs) {
[codecNames addObject:codec.name];
}
XCTAssertTrue([codecNames containsObject:@"VP8"]);
XCTAssertTrue([codecNames containsObject:@"H264"]);
factory = nil;
}
}
- (void)testSetCodecPreferences {
@autoreleasepool {
RTC_OBJC_TYPE(RTCConfiguration) *config =
[[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
[[RTC_OBJC_TYPE(RTCMediaConstraints) alloc]
initWithMandatoryConstraints:nil
optionalConstraints:nil];
RTC_OBJC_TYPE(RTCRtpTransceiverInit) *init =
[[RTC_OBJC_TYPE(RTCRtpTransceiverInit) alloc] init];
NSArray<RTC_OBJC_TYPE(RTCVideoCodecInfo) *> *supportedCodecs = @[
[[RTC_OBJC_TYPE(RTCVideoCodecInfo) alloc] initWithName:@"VP8"],
[[RTC_OBJC_TYPE(RTCVideoCodecInfo) alloc] initWithName:@"H264"]
];
MockVideoEncoderDecoderFactory *encoder =
[[MockVideoEncoderDecoderFactory alloc]
initWithSupportedCodecs:supportedCodecs];
MockVideoEncoderDecoderFactory *decoder =
[[MockVideoEncoderDecoderFactory alloc]
initWithSupportedCodecs:supportedCodecs];
RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
RTC_OBJC_TYPE(RTCPeerConnection) * peerConnection;
RTC_OBJC_TYPE(RTCRtpTransceiver) * tranceiver;
factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc]
initWithEncoderFactory:encoder
decoderFactory:decoder];
peerConnection = [factory peerConnectionWithConfiguration:config
constraints:constraints
delegate:nil];
tranceiver = [peerConnection addTransceiverOfType:RTCRtpMediaTypeVideo
init:init];
XCTAssertNotNil(tranceiver);
RTC_OBJC_TYPE(RTCRtpCapabilities) *capabilities =
[factory rtpReceiverCapabilitiesForKind:kRTCMediaStreamTrackKindVideo];
RTC_OBJC_TYPE(RTCRtpCodecCapability) * targetCodec;
for (RTC_OBJC_TYPE(RTCRtpCodecCapability) * codec in capabilities.codecs) {
if ([codec.name isEqual:@"VP8"]) {
targetCodec = codec;
break;
}
}
XCTAssertNotNil(targetCodec);
NSError *error = nil;
BOOL result = [tranceiver setCodecPreferences:@[ targetCodec ]
error:&error];
XCTAssertTrue(result);
XCTAssertNil(error);
@autoreleasepool {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block BOOL completed = NO;
[peerConnection
offerForConstraints:constraints
completionHandler:^(
RTC_OBJC_TYPE(RTCSessionDescription) *_Nullable sdp,
NSError *_Nullable error) {
XCTAssertNil(error);
XCTAssertNotNil(sdp);
NSArray<NSString *> *rtpMaps = [self rtpMapsFromSDP:sdp.sdp];
XCTAssertEqual(1, rtpMaps.count);
XCTAssertNotNil(targetCodec.preferredPayloadType);
XCTAssertNotNil(targetCodec.clockRate);
NSString *expected = [NSString
stringWithFormat:@"a=rtpmap:%i VP8/%i",
targetCodec.preferredPayloadType.intValue,
targetCodec.clockRate.intValue];
XCTAssertTrue([expected isEqualToString:rtpMaps[0]]);
completed = YES;
dispatch_semaphore_signal(semaphore);
}];
[peerConnection close];
peerConnection = nil;
factory = nil;
tranceiver = nil;
dispatch_semaphore_wait(
semaphore, dispatch_time(DISPATCH_TIME_NOW, 15.0 * NSEC_PER_SEC));
XCTAssertTrue(completed);
}
}
}
- (void)testSetHeaderExtensionsToNegotiate {
@autoreleasepool {
RTC_OBJC_TYPE(RTCConfiguration) *config =
[[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
[[RTC_OBJC_TYPE(RTCMediaConstraints) alloc]
initWithMandatoryConstraints:nil
optionalConstraints:nil];
RTC_OBJC_TYPE(RTCRtpTransceiverInit) *init =
[[RTC_OBJC_TYPE(RTCRtpTransceiverInit) alloc] init];
RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
RTC_OBJC_TYPE(RTCPeerConnection) * peerConnection;
RTC_OBJC_TYPE(RTCRtpTransceiver) * tranceiver;
factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
peerConnection = [factory peerConnectionWithConfiguration:config
constraints:constraints
delegate:nil];
tranceiver = [peerConnection addTransceiverOfType:RTCRtpMediaTypeVideo
init:init];
XCTAssertNotNil(tranceiver);
NSArray<RTC_OBJC_TYPE(RTCRtpHeaderExtensionCapability) *>
*headerExtensionsToNegotiate = tranceiver.headerExtensionsToNegotiate;
__block RTC_OBJC_TYPE(RTCRtpHeaderExtensionCapability) *targetExtension =
nil;
[headerExtensionsToNegotiate
enumerateObjectsUsingBlock:^(
RTC_OBJC_TYPE(RTCRtpHeaderExtensionCapability) * extension,
NSUInteger idx,
BOOL * stop) {
if ([extension.uri
isEqualToString:@"urn:ietf:params:rtp-hdrext:sdes:mid"]) {
targetExtension = extension;
} else {
extension.direction = RTCRtpTransceiverDirectionStopped;
}
}];
NSError *error = nil;
BOOL isOK =
[tranceiver setHeaderExtensionsToNegotiate:headerExtensionsToNegotiate
error:&error];
XCTAssertNil(error);
XCTAssertTrue(isOK);
@autoreleasepool {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block BOOL completed = NO;
[peerConnection
offerForConstraints:constraints
completionHandler:^(
RTC_OBJC_TYPE(RTCSessionDescription) *_Nullable sdp,
NSError *_Nullable error) {
XCTAssertNil(error);
XCTAssertNotNil(sdp);
NSArray<NSString *> *extMaps = [self extMapsFromSDP:sdp.sdp];
XCTAssertEqual(1, extMaps.count);
XCTAssertNotNil(targetExtension);
XCTAssertNotNil(targetExtension.preferredId);
NSString *expected = [NSString
stringWithFormat:@"a=extmap:%i %@",
targetExtension.preferredId.intValue,
targetExtension.uri];
XCTAssertTrue([expected isEqualToString:extMaps[0]]);
completed = YES;
dispatch_semaphore_signal(semaphore);
}];
[peerConnection close];
peerConnection = nil;
factory = nil;
tranceiver = nil;
dispatch_semaphore_wait(
semaphore, dispatch_time(DISPATCH_TIME_NOW, 15.0 * NSEC_PER_SEC));
XCTAssertTrue(completed);
}
}
}
- (void)testSetHeaderExtensionsToNegotiateError {
@autoreleasepool {
RTC_OBJC_TYPE(RTCConfiguration) *config =
[[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
[[RTC_OBJC_TYPE(RTCMediaConstraints) alloc]
initWithMandatoryConstraints:nil
optionalConstraints:nil];
RTC_OBJC_TYPE(RTCRtpTransceiverInit) *init =
[[RTC_OBJC_TYPE(RTCRtpTransceiverInit) alloc] init];
RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
RTC_OBJC_TYPE(RTCPeerConnection) * peerConnection;
RTC_OBJC_TYPE(RTCRtpTransceiver) * tranceiver;
factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
peerConnection = [factory peerConnectionWithConfiguration:config
constraints:constraints
delegate:nil];
tranceiver = [peerConnection addTransceiverOfType:RTCRtpMediaTypeVideo
init:init];
XCTAssertNotNil(tranceiver);
NSArray<RTC_OBJC_TYPE(RTCRtpHeaderExtensionCapability) *>
*headerExtensionsToNegotiate = tranceiver.headerExtensionsToNegotiate;
[headerExtensionsToNegotiate
enumerateObjectsUsingBlock:^(
RTC_OBJC_TYPE(RTCRtpHeaderExtensionCapability) * extension,
NSUInteger idx,
BOOL * stop) {
if ([extension.uri
isEqualToString:@"urn:ietf:params:rtp-hdrext:sdes:mid"]) {
extension.direction = RTCRtpTransceiverDirectionStopped;
}
}];
// Stopping a mandatory extension should yield an error
NSError *error = nil;
BOOL isOK =
[tranceiver setHeaderExtensionsToNegotiate:headerExtensionsToNegotiate
error:&error];
XCTAssertNotNil(error);
XCTAssertFalse(isOK);
[peerConnection close];
peerConnection = nil;
factory = nil;
tranceiver = nil;
}
}
- (bool)negotiatePeerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)pc1
withPeerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)pc2
negotiationTimeout:(NSTimeInterval)timeout {
__weak RTC_OBJC_TYPE(RTCPeerConnection) *weakPC1 = pc1;
__weak RTC_OBJC_TYPE(RTCPeerConnection) *weakPC2 = pc2;
RTC_OBJC_TYPE(RTCMediaConstraints) *sdpConstraints =
[[RTC_OBJC_TYPE(RTCMediaConstraints)
alloc] initWithMandatoryConstraints:@{
kRTCMediaConstraintsOfferToReceiveAudio : kRTCMediaConstraintsValueTrue
}
optionalConstraints:nil];
dispatch_semaphore_t negotiatedSem = dispatch_semaphore_create(0);
[weakPC1
offerForConstraints:sdpConstraints
completionHandler:^(RTC_OBJC_TYPE(RTCSessionDescription) * offer,
NSError * error) {
XCTAssertNil(error);
XCTAssertNotNil(offer);
[weakPC1
setLocalDescription:offer
completionHandler:^(NSError *error) {
XCTAssertNil(error);
[weakPC2
setRemoteDescription:offer
completionHandler:^(NSError *error) {
XCTAssertNil(error);
[weakPC2
answerForConstraints:sdpConstraints
completionHandler:^(
RTC_OBJC_TYPE(RTCSessionDescription) *
answer,
NSError * error) {
XCTAssertNil(error);
XCTAssertNotNil(answer);
[weakPC2
setLocalDescription:answer
completionHandler:^(NSError *error) {
XCTAssertNil(error);
[weakPC1
setRemoteDescription:answer
completionHandler:^(
NSError *error) {
XCTAssertNil(error);
dispatch_semaphore_signal(
negotiatedSem);
}];
}];
}];
}];
}];
}];
return 0 ==
dispatch_semaphore_wait(negotiatedSem,
dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(timeout * NSEC_PER_SEC)));
}
- (NSArray<NSString *> *)rtpMapsFromSDP:(NSString *)sdp {
NSMutableArray<NSString *> *rtpMaps = [NSMutableArray new];
NSArray *sdpLines =
[sdp componentsSeparatedByCharactersInSet:[NSCharacterSet
newlineCharacterSet]];
for (NSString *line in sdpLines) {
if ([line hasPrefix:@"a=rtpmap"]) {
[rtpMaps addObject:line];
}
}
return rtpMaps;
}
- (NSArray<NSString *> *)extMapsFromSDP:(NSString *)sdp {
NSMutableArray<NSString *> *extMaps = [NSMutableArray new];
NSArray *sdpLines =
[sdp componentsSeparatedByCharactersInSet:[NSCharacterSet
newlineCharacterSet]];
for (NSString *line in sdpLines) {
if ([line hasPrefix:@"a=extmap:"]) {
[extMaps addObject:line];
}
}
return extMaps;
}
@end
|