/// <summary> /// Make connection with a RTMP server like Wowza, red5 or FMS /// </summary> /// <returns></returns> public void Connect(string connectUrl, NC_ResultCallBackConnect resultCallBackConnect) { ServerLink serverLink = new ServerLink(connectUrl); Connect(serverLink, resultCallBackConnect); }
public void Connect(ServerLink serverLink, NC_ResultCallBackConnect resultCallBackConnect) { Connect(serverLink, resultCallBackConnect, null); }
/// <summary> /// Fires an NC_ResultCallBackConnect on the correct thread /// if SynchronizationContext is filled. /// </summary> private void DoNC_ResultCallBackConnectEvent(NC_ResultCallBackConnect onResult, bool success) { // ----------------------------------------------------------------------------------------- SendOrPostCallback callback = new SendOrPostCallback(delegate(object state) { // This is specified other thread (state as State_NC_ResultCallBackConnect).OnResult((state as State_NC_ResultCallBackConnect).thisObject, (state as State_NC_ResultCallBackConnect).Success); }); // ----------------------------------------------------------------------------------------- State_NC_ResultCallBackConnect callbackState = new State_NC_ResultCallBackConnect(); callbackState.thisObject = this; callbackState.OnResult = onResult; callbackState.Success = success; SynchronizationContext sc; lock (lockVAR) { sc = synchronizationContext; } //lock if (sc != null) { switch (synchronizationContextMethod) { case SynchronizationContextMethod.Post: sc.Post(callback, callbackState); break; case SynchronizationContextMethod.Send: sc.Send(callback, callbackState); break; } //switch } else { callback(callbackState); } }
/// <summary> /// Make connection with a RTMP server like Wowza, red5 or FMS /// </summary> /// <returns></returns> public void Connect(ServerLink serverLink, NC_ResultCallBackConnect resultCallBackConnect, params AMFObjectProperty[] amfProperties) { // First check if there isn't a build connection in the message queue lock (lockVAR) { if (messageQueue != null) { foreach (MQ_RTMPMessage msg in messageQueue) { if (msg.MethodCall == MethodCall.ConnectRTMPServer) { // cancel, we're already trying to do it! return; } } //foreach } } //lock netConnectionConnectInfo.Clear(); MQ_RTMPMessage message = new MQ_RTMPMessage(); message.MethodCall = MethodCall.ConnectRTMPServer; message.Params = new object[] { serverLink, resultCallBackConnect, amfProperties }; AddMessageToPump(message); }
private void MQ_Connect(ServerLink sl, NC_ResultCallBackConnect onResult, params AMFObjectProperty[] amfProperties) { MQ_Close(); netConnectionState = NetConnectionState.Connecting; serverLink = sl; bool success = MQInternal_MakeConnection(amfProperties); if (success) { netConnectionState = NetConnectionState.Connected; } if (success) { success = false; // Now we have to wait for the "onConnect" event to be send by the wowza server, before we can say the // connection was successful. We do this for a max of 7 seconds, if no "onConnect" then // disconnect and say failed DateTime timeStamp = DateTime.Now.AddMilliseconds(onConnectWaitTimeoutMS); while (!success && (timeStamp - DateTime.Now).TotalMilliseconds > 0) { if (netConnectionState == NetConnectionState.Connected) { if (MQInternal_IsConnected) { if (MQInternal_DataInSocket) { RTMPPacket packet = null; ReadPacket(out packet); // When we get an Invoke while connecting this is the "connect" event! // where we have been waiting for. if (packet.PacketType == PacketType.Invoke) { success = true; } HandleClientPacket(packet); } } else { // There is no connection anymore break; } } // don't use cpu 100% if (!success) { Thread.Sleep(10); } } //while // Close connection when failed! if (!success || (DateTime.Now - timeStamp).TotalMilliseconds > 0) { #if SSL if (sslStream != null) { try { sslStream.Close(); sslStream.Dispose(); } catch { } } sslStream = null; #endif if (tcpSocket != null) { try { tcpSocket.Shutdown(SocketShutdown.Both); tcpSocket.Close(); } catch { } } tcpSocket = null; // temporary before we set the state to disconnected! netConnectionState = NetConnectionState.Connecting; dtNetConnectionKeepAlive = DateTime.Now; } } if (onResult != null) { // this is delegate given when connecting (used by Mediaplayer class) // makes event OnConnect not needed DoNC_ResultCallBackConnectEvent(onResult, success); } // Now we do a "global" onConnect if (success && OnConnect != null) { MQ_RTMPMessage message = new MQ_RTMPMessage(); message.MethodCall = MethodCall.OnEventCallUserCode; message.Params = new object[] { OnConnect, this }; AddOnEventUserCallCodeToPump(message); } if (!success) { // Set state netConnectionState = NetConnectionState.Disconnected; } }