shogo4405 / HaishinKit.swift

Camera and Microphone streaming library via RTMP and SRT for iOS, macOS, tvOS and visionOS.
BSD 3-Clause "New" or "Revised" License
2.76k stars 612 forks source link

RTMP Status Listener not receiving `connectClosed` Event in Simulator #653

Closed alexthemitchell closed 4 years ago

alexthemitchell commented 4 years ago

Describe the bug Our RTMP status listening function is not getting a notification when we disconnect WiFi. After reviewing https://github.com/shogo4405/HaishinKit.swift/issues/489, we do not know why our listener is not receiving the .connectClosed event.

To Reproduce Steps to reproduce the behavior:

  1. Register self.rtmpStatusHandler as .rtmpStatus listener:
    self.rtmpConnection.addEventListener(.rtmpStatus, selector:#selector(self.rtmpStatusHandler(_:)), observer: self)
  2. Print all events received by rtmpStatusHandler:
    @objc func rtmpStatusHandler(_ notification:Notification) {
        print("rtmp got notification")
        let e:Event = Event.from(notification)
        print("connect---rtmp> \(Date()) event: \(e)")
    }
  3. Build app and begin stream in Simulator. Notice NetStream.Publish.Start and NetConnection.Connect.Success events logged in console:
    rtmp got notification
    connect---rtmp> 2020-03-12 17:19:52 +0000 event: Event{type: Name(rawValue: "rtmpStatus"),bubbles: false,data: Optional(Optional(["level": Optional("status"), "code": Optional("NetConnection.Connect.Success"), "clientid": Optional(<redacted>), "objectEncoding": Optional(0.0), "data": Optional([]), "description": Optional("Connection succeeded.")])),target: Optional(HaishinKit.RTMPConnection)}
    rtmp got notification
    connect---rtmp> 2020-03-12 17:19:58 +0000 event: Event{type: Name(rawValue: "rtmpStatus"),bubbles: false,data: Optional(Optional(["level": Optional("status"), "code": Optional("NetStream.Publish.Start"), "clientid": Optional(<redacted>), "description": Optional("FCPublish to stream videoName.")])),target: Optional(HaishinKit.RTMPConnection)}
    rtmp got notification
    connect---rtmp> 2020-03-12 17:19:58 +0000 event: Event{type: Name(rawValue: "rtmpStatus"),bubbles: false,data: Optional(Optional(["level": Optional("status"), "clientid": Optional(<redacted>), "code": Optional("NetStream.Publish.Start"), "description": Optional("Publishing <redacted>.")])),target: Optional(HaishinKit.RTMPConnection)}
  4. Disconnect WiFi from host computer; notice no logs from rtmpStatusHandler

Expected behavior

rtmpStatusHandler is called with .connectClosed or .connectNetworkChange event; in this case, printing logs with event info.

Smartphone (please complete the following information):

shogo4405 commented 4 years ago

It seemes works for me.

https://github.com/shogo4405/HaishinKit.swift/blob/master/Examples/iOS/LiveViewController.swift#L153

2020-15-03 22:36:58.024 [Warn] [com.haishinkit.HaishinKit] [RTMPConnection.swift:380] on(status:) > 
2020-15-03 22:36:58.032 [Info] [com.haishinkit.HaishinKit] [AudioConverter.swift:86] inSourceFormat > nil
2020-15-03 22:36:58.032 [Info] [com.haishinkit.Exsample.iOS] [LiveViewController.swift:155] rtmpStatusHandler(_:) > Event{type: Name(rawValue: "rtmpStatus"),bubbles: false,data: Optional(["description": Optional(""), "code": Optional("NetConnection.Connect.Closed"), "level": Optional("status")]),target: Optional(HaishinKit.RTMPConnection)}
2020-03-15 22:36:59.048072+0900 Example iOS[4538:391553] [] nw_connection_get_connected_socket [C2] Client called nw_connection_get_connected_socket on unconnected nw_connection
2020-03-15 22:36:59.048153+0900 Example iOS[4538:391553] TCP Conn 0x283a521c0 Failed : error 0:50 [50]
2020-15-03 22:36:59.049 [Info] [com.haishinkit.Exsample.iOS] [LiveViewController.swift:155] rtmpStatusHandler(_:) > Event{type: Name(rawValue: "rtmpStatus"),bubbles: false,data: Optional(["description": Optional(""), "code": Optional("NetConnection.Connect.Failed"), "level": Optional("error")]),target: Optional(HaishinKit.RTMPConnection)}

Simulator iPhone XR

It's a simulators problem?

alexthemitchell commented 4 years ago

@shogo4405 Thanks, you're right. It's a simulator problem; it emits the event as expected on a device.

This resolves my immediate issue, so feel free to close unless you can solve the problem in the simulator :)

allenlinli commented 4 years ago

Should RTMPConnection.Code.connectClosed.rawValue be called in rtmpStatusHandler when it does rtmpConnection.close()? I didn't receive the event connectClosed.

shogo4405 commented 4 years ago

It seemes that application can't get disconnect event from NSOutputStream, NSInputStream. I can't fix it. So I will close this issue.