An RTCSessionDescription will now return nil from its initializer if the SDP passed to it is invalid.
Bug: webrtc:13022
Change-Id: I2f2ad96884cf2f43f5ea95c1210470dd6aa5c919
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/226980
Commit-Queue: Jake Bromberg <jakebromberg@google.com>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#34607}
diff --git a/sdk/objc/api/peerconnection/RTCSessionDescription.h b/sdk/objc/api/peerconnection/RTCSessionDescription.h
index 8a9479d..0ef60cc 100644
--- a/sdk/objc/api/peerconnection/RTCSessionDescription.h
+++ b/sdk/objc/api/peerconnection/RTCSessionDescription.h
@@ -36,8 +36,10 @@
- (instancetype)init NS_UNAVAILABLE;
-/** Initialize a session description with a type and SDP string. */
-- (instancetype)initWithType:(RTCSdpType)type sdp:(NSString *)sdp NS_DESIGNATED_INITIALIZER;
+/** Initialize a session description with a type and SDP string. Returns nil if the sdp is invalid.
+ */
+- (nullable instancetype)initWithType:(RTCSdpType)type
+ sdp:(NSString *)sdp NS_DESIGNATED_INITIALIZER;
+ (NSString *)stringForType:(RTCSdpType)type;
diff --git a/sdk/objc/api/peerconnection/RTCSessionDescription.mm b/sdk/objc/api/peerconnection/RTCSessionDescription.mm
index 9fd97fe..3e235b1 100644
--- a/sdk/objc/api/peerconnection/RTCSessionDescription.mm
+++ b/sdk/objc/api/peerconnection/RTCSessionDescription.mm
@@ -17,23 +17,45 @@
@implementation RTC_OBJC_TYPE (RTCSessionDescription)
+@synthesize nativeDescription = _nativeDescription;
@synthesize type = _type;
@synthesize sdp = _sdp;
+ (NSString *)stringForType:(RTCSdpType)type {
- std::string string = [[self class] stdStringForType:type];
+ std::string string = [self stdStringForType:type];
return [NSString stringForStdString:string];
}
+ (RTCSdpType)typeForString:(NSString *)string {
std::string typeString = string.stdString;
- return [[self class] typeForStdString:typeString];
+ return [self typeForStdString:typeString];
}
-- (instancetype)initWithType:(RTCSdpType)type sdp:(NSString *)sdp {
++ (webrtc::SessionDescriptionInterface *)nativeDescriptionForString:(NSString *)sdp
+ type:(RTCSdpType)type {
+ webrtc::SdpParseError error;
+
+ webrtc::SessionDescriptionInterface *description =
+ webrtc::CreateSessionDescription([self stdStringForType:type], sdp.stdString, &error);
+
+ if (!description) {
+ RTCLogError(@"Failed to create session description: %s\nline: %s",
+ error.description.c_str(),
+ error.line.c_str());
+ }
+
+ return description;
+}
+
+- (nullable instancetype)initWithType:(RTCSdpType)type sdp:(NSString *)sdp {
if (self = [super init]) {
_type = type;
_sdp = [sdp copy];
+ _nativeDescription = [[self class] nativeDescriptionForString:_sdp type:_type];
+
+ if (_nativeDescription == nil) {
+ return nil;
+ }
}
return self;
}
@@ -46,23 +68,6 @@
#pragma mark - Private
-- (webrtc::SessionDescriptionInterface *)nativeDescription {
- webrtc::SdpParseError error;
-
- webrtc::SessionDescriptionInterface *description =
- webrtc::CreateSessionDescription([[self class] stdStringForType:_type],
- _sdp.stdString,
- &error);
-
- if (!description) {
- RTCLogError(@"Failed to create session description: %s\nline: %s",
- error.description.c_str(),
- error.line.c_str());
- }
-
- return description;
-}
-
- (instancetype)initWithNativeDescription:
(const webrtc::SessionDescriptionInterface *)nativeDescription {
NSParameterAssert(nativeDescription);
diff --git a/sdk/objc/unittests/RTCSessionDescriptionTest.mm b/sdk/objc/unittests/RTCSessionDescriptionTest.mm
index ee65649..1f8a75d 100644
--- a/sdk/objc/unittests/RTCSessionDescriptionTest.mm
+++ b/sdk/objc/unittests/RTCSessionDescriptionTest.mm
@@ -42,6 +42,13 @@
EXPECT_EQ([self sdp].stdString, sdp);
}
+- (void)testInvalidSessionDescriptionConversion {
+ RTC_OBJC_TYPE(RTCSessionDescription) *description =
+ [[RTC_OBJC_TYPE(RTCSessionDescription) alloc] initWithType:RTCSdpTypeAnswer sdp:@"invalid"];
+
+ EXPECT_EQ(nil, description);
+}
+
- (void)testInitFromNativeSessionDescription {
webrtc::SessionDescriptionInterface *nativeDescription;
@@ -135,6 +142,13 @@
}
}
+TEST(RTCSessionDescriptionTest, InvalidSessionDescriptionConversionTest) {
+ @autoreleasepool {
+ RTCSessionDescriptionTest *test = [[RTCSessionDescriptionTest alloc] init];
+ [test testInvalidSessionDescriptionConversion];
+ }
+}
+
TEST(RTCSessionDescriptionTest, InitFromSessionDescriptionTest) {
@autoreleasepool {
RTCSessionDescriptionTest *test = [[RTCSessionDescriptionTest alloc] init];