otalk / TLKSimpleWebRTC

MIT License
28 stars 21 forks source link

BUG - Remote Video get frozen, audio session remain active #9

Open shamun opened 7 years ago

shamun commented 7 years ago

8 to 9 minute the Video session remain alive in iPhone 7 device. But after that the remote video get frozen. I tried to disable the NSNotificationCenter but that did not resolved it.

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(captureSessionDidStartRunning)
                                                 name:AVCaptureSessionDidStartRunningNotification
                                               object:nil];

- (void)captureSessionDidStartRunning {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self configureLocalPreview];
    });
}
shamun commented 7 years ago

Second problem, when i try to use second NSNotificationCenter the remote video freeze more earlier such as in 2 minute the video connection get frozen.

- (void)_socketEventReceived:(NSString*)eventName withData:(id)data {
    NSDictionary *dictionary = nil;
    NSDictionary *temporary = nil;

    if ([eventName isEqualToString:@"locked"]) {

        self.roomKey = (NSString*)[data objectAtIndex:0];
        if ([self.delegate respondsToSelector:@selector(socketIOSignaling:didChangeLock:)]) {
            [self.delegate socketIOSignaling:self didChangeLock:YES];
        }

    } else if ([eventName isEqualToString:@"unlocked"]) {

        self.roomKey = nil;
        if ([self.delegate respondsToSelector:@selector(socketIOSignaling:didChangeLock:)]) {
            [self.delegate socketIOSignaling:self didChangeLock:NO];
        }

    } else if ([eventName isEqualToString:@"passwordRequired"]) {

        if ([self.delegate respondsToSelector:@selector(socketIOSignalingRequiresServerPassword:)]) {
            [self.delegate socketIOSignalingRequiresServerPassword:self];
        }

    } else if ([eventName isEqualToString:@"stunservers"] || [eventName isEqualToString:@"turnservers"]) {

        NSArray *serverList = data[0];
        for (NSDictionary *info in serverList) {
            NSString *username = info[@"username"] ? info[@"username"] : @"";
            NSString *password = info[@"credential"] ? info[@"credential"] : @"";
            RTCICEServer *server = [[RTCICEServer alloc] initWithURI:[NSURL URLWithString:info[@"url"]] username:username password:password];
            [self.webRTC addICEServer:server];
        }
    } else if ([eventName isEqualToString:@"remove"]) {
      if( [self.vid_id count] > 0 ) {              
        NSLog(@"<<< CALLING OLD >>> : eventName = %@, type = %@, from = %@, to = %@, spdMid = %@ , sdpMLineIndex = %@, candidate = %@",
              self.vid_id[@"eventname"], self.vid_id[@"type"], self.vid_id[@"from"], self.vid_id[@"to"],
              self.vid_id[@"payload"][@"candidate"][@"sdpMid"],
              self.vid_id[@"payload"][@"candidate"][@"sdpMLineIndex"],
              self.vid_id[@"payload"][@"candidate"][@"candidate"]);

        RTCICECandidate* candidate = [[RTCICECandidate alloc] initWithMid:self.vid_id[@"payload"][@"candidate"][@"sdpMid"]
                                                                  index:[self.vid_id[@"payload"][@"candidate"][@"sdpMLineIndex"] integerValue]
                                                                    sdp:self.vid_id[@"payload"][@"candidate"][@"candidate"]];

        [self.webRTC addICECandidate:candidate forPeerWithID:self.vid_id[@"from"] ];

      }

    } else {

        dictionary = data[0];

        if (![dictionary isKindOfClass:[NSDictionary class]]) {
            dictionary = nil;
        }

    }

    NSLog(@"eventName = %@, type = %@, from = %@, to = %@",eventName, dictionary[@"type"], dictionary[@"from"], dictionary[@"to"]);

    id my_event_name = eventName.length ? eventName : [NSNull null];

    NSString *value = (NSString*)dictionary[@"type"];
    id my_type = value.length ? value : [NSNull null];

    NSString *value1 = (NSString*)dictionary[@"from"];
    id my_from= value1.length ? value1 : [NSNull null];

    NSString *value2 = (NSString*)dictionary[@"to"];
    id my_to= value2.length ? value2 : [NSNull null];

    temporary = @{@"eventname": my_event_name,
                @"type": my_type,
                @"from"  : my_from,
                @"to"  : my_to};

    if (![temporary isKindOfClass:[NSDictionary class]]) {
      temporary = nil;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"loginComplete" object:dictionary];

    }
    else {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"loginComplete" object:temporary];
    }

    if ([dictionary[@"type"] isEqualToString:@"iceFailed"]) {

        [[[UIAlertView alloc] initWithTitle:@"Connection Failed" message:@"Talky could not establish a connection to a participant in this chat. Please try again later." delegate:nil cancelButtonTitle:@"Continue" otherButtonTitles:nil] show];

    } else if ([dictionary[@"type"] isEqualToString:@"candidate"]) {

      //NSLog(@">>> OLD vid_id: %@", self.vid_id);
      if ( [self.vid_id count] <= 0 ) {
        NSLog(@"<<< vid_id - 1");

      id my_event_name = @"message";

      NSString *value = (NSString*)dictionary[@"type"];
      id my_type = value.length ? value : [NSNull null];

        NSLog(@"<<< vid_id - 2");

      NSString *value1 = (NSString*)dictionary[@"from"];
      id my_from= value1.length ? value1 : [NSNull null];

        NSLog(@"<<< vid_id - 3");

      NSString *value2 = (NSString*)dictionary[@"to"];
      id my_to= value2.length ? value2 : [NSNull null];

        NSLog(@"<<< vid_id - 4");

      // NEW
      NSString *ns_spdMid = (NSString*)dictionary[@"payload"][@"candidate"][@"sdpMid"];
      id my_sdpMid = ns_spdMid.length ? ns_spdMid : [NSNull null];

        NSLog(@"<<< vid_id - 5");

      NSNumber *ns_sdpMLineIndex = (NSNumber*)dictionary[@"payload"][@"candidate"][@"sdpMLineIndex"];
      id my_sdpMLineIndex = ns_sdpMLineIndex ? ns_sdpMLineIndex : [NSNull null];

        NSLog(@"<<< vid_id - 6");

      NSString *ns_candidate = (NSString*)dictionary[@"payload"][@"candidate"][@"candidate"];
      id my_candidate = ns_candidate.length ? ns_candidate : [NSNull null];

        NSLog(@"<<< vid_id - last");

      self.vid_id = @{@"eventname": my_event_name,
                    @"type": my_type,
                    @"from"  : my_from,
                    @"to"  : my_to,
                    @"payload":
                        @{@"candidate": @{@"spdMid": my_sdpMid,
                                       @"sdpMLineIndex": my_sdpMLineIndex,
                                       @"candidate": my_candidate}}
                    };

        NSLog(@"<<< OLD: eventName = %@, type = %@, from = %@, to = %@, spdMid = %@ , sdpMLineIndex = %@, candidate = %@",
              self.vid_id[@"eventname"], self.vid_id[@"type"], self.vid_id[@"from"], self.vid_id[@"to"],
              self.vid_id[@"payload"][@"candidate"][@"sdpMid"],
              self.vid_id[@"payload"][@"candidate"][@"sdpMLineIndex"],
              self.vid_id[@"payload"][@"candidate"][@"candidate"]);

      }
      else {
        NSLog(@"<<< vid_id >>> ignore to insert more.......");
      }

      NSLog(@"<<< CANDIDATE >>>: eventName = %@, type = %@, from = %@, to = %@, spdMid = %@ , sdpMLineIndex = %@, candidate = %@",
            @"CANDIDATE", dictionary[@"type"], dictionary[@"from"], dictionary[@"to"],
            dictionary[@"payload"][@"candidate"][@"sdpMid"],
            dictionary[@"payload"][@"candidate"][@"sdpMLineIndex"],
            dictionary[@"payload"][@"candidate"][@"candidate"]);
      RTCICECandidate* candidate = [[RTCICECandidate alloc] initWithMid:dictionary[@"payload"][@"candidate"][@"sdpMid"]
                                                                    index:[dictionary[@"payload"][@"candidate"][@"sdpMLineIndex"] integerValue]
                                                                      sdp:dictionary[@"payload"][@"candidate"][@"candidate"]];

      [self.webRTC addICECandidate:candidate forPeerWithID:dictionary[@"from"]];

    } else if ([dictionary[@"type"] isEqualToString:@"answer"]) {

        RTCSessionDescription* remoteSDP = [[RTCSessionDescription alloc] initWithType:dictionary[@"payload"][@"type"]
                                                                                   sdp:dictionary[@"payload"][@"sdp"]];

        [self.webRTC setRemoteDescription:remoteSDP forPeerWithID:dictionary[@"from"] receiver:NO];

    } else if ([dictionary[@"type"] isEqualToString:@"offer"]) {

        [self.webRTC addPeerConnectionForID:dictionary[@"from"]];
        [self.currentClients addObject:dictionary[@"from"]];

        // Fix for browser-to-app connection crash using beta API.
        NSString* origSDP = dictionary[@"payload"][@"sdp"];
        NSError* error;
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"m=application \\d+ DTLS/SCTP 5000 *"
                                                                               options:0
                                                                                 error:&error];

        NSString* sdp = [regex stringByReplacingMatchesInString:origSDP options:0 range:NSMakeRange(0, [origSDP length]) withTemplate:@"m=application 0 DTLS/SCTP 5000"];

        RTCSessionDescription* remoteSDP = [[RTCSessionDescription alloc] initWithType:dictionary[@"payload"][@"type"]
                                                                                   sdp:sdp];

        [self.webRTC setRemoteDescription:remoteSDP forPeerWithID:dictionary[@"from"] receiver:YES];

    } else if ([eventName isEqualToString:@"remove"]) {

        [self.webRTC removePeerConnectionForID:dictionary[@"id"]];
        [self _peerDisconnectedForIdentifier:dictionary[@"id"]];

        [self.currentClients removeObject:dictionary[@"id"]];

    } else if ([dictionary[@"payload"][@"name"] isEqualToString:@"audio"]) {

        TLKMediaStream *stream = [self _streamForPeerIdentifier:dictionary[@"from"]];
        stream.audioMuted = [dictionary[@"type"] isEqualToString:@"mute"];
        if([self.delegate respondsToSelector:@selector(socketIOSignaling:peer:toggledAudioMute:)]) {
            [self.delegate socketIOSignaling:self peer:dictionary[@"from"] toggledAudioMute:stream.audioMuted];
        }

    } else if ([dictionary[@"payload"][@"name"] isEqualToString:@"video"]) {

        TLKMediaStream *stream = [self _streamForPeerIdentifier:dictionary[@"from"]];
        stream.videoMuted = [dictionary[@"type"] isEqualToString:@"mute"];
        if([self.delegate respondsToSelector:@selector(socketIOSignaling:peer:toggledVideoMute:)]) {
            [self.delegate socketIOSignaling:self peer:dictionary[@"from"] toggledVideoMute:stream.videoMuted];
        }

    }
}