CSharpRTMP.Core.Protocols.Rtmp.BaseRTMPAppProtocolHandler.ProcessInvokeOnStatus C# (CSharp) Method

ProcessInvokeOnStatus() private method

private ProcessInvokeOnStatus ( BaseRTMPProtocol pFrom, AmfMessage request ) : bool
pFrom BaseRTMPProtocol
request AmfMessage
return bool
        private bool ProcessInvokeOnStatus(BaseRTMPProtocol pFrom, AmfMessage request)
        {
            if ((!NeedsToPullExternalStream(pFrom))
            && (!NeedsToPushLocalStream(pFrom)))
            {
                Logger.WARN("Default implementation of ProcessInvokeOnStatus in application {0}: Request:\n{1}",
                        Application.Name,
                        request.ToString());
                return true;
            }

            //1. Test and see if this connection is an outbound RTMP connection
            //and get a pointer to it
            if (pFrom.Type != ProtocolTypes.PT_OUTBOUND_RTMP)
            {
                Logger.FATAL("This is not an outbound connection");
                return false;
            }
            var pProtocol = (OutboundRTMPProtocol)pFrom;

            //2. Validate the request
            if (request.InvokeParam[1]["code"] != VariantType.String)
            {
                Logger.FATAL("invalid onStatus:\n{0}", request.Body.ToString());
                return false;
            }


            //6. Get our hands on streaming parameters

            var path = NeedsToPullExternalStream(pFrom) ? "externalStreamConfig" : "localStreamConfig";
            Variant parameters = pFrom.CustomParameters["customParameters"][path];

            if (NeedsToPullExternalStream(pFrom))
            {
                if (request.InvokeParam[1]["code"] != "NetStream.Play.Start")
                {
                    Logger.WARN("onStatus message ignored:\n{0}", request.Body.ToString());
                    return true;
                }
                if (!Application.StreamNameAvailable(parameters["localStreamName"],pProtocol))    
                {
                    Logger.WARN("Stream name {0} already occupied and application doesn't allow duplicated inbound network streams",
                            parameters["localStreamName"]);
                    return false;
                }
                var pStream = pProtocol.CreateINS(request.ChannelId,request.StreamId, parameters["localStreamName"]);
                if (pStream == null)
                {
                    Logger.FATAL("Unable to create stream");
                    return false;
                }

                var waitingSubscribers =
                        Application.StreamsManager.GetWaitingSubscribers(pStream.Name, pStream.Type);
                foreach (var waitingSubscriber in waitingSubscribers)
                {
                    pStream.Link(waitingSubscriber);
                }

            }
            else
            {
                if (request.InvokeParam[1]["code"] != "NetStream.Publish.Start")
                {
                    Logger.WARN("onStatus message ignored:\n{0}", request.ToString());
                    return true;
                }

                var pBaseInStream =
                        (IInStream)Application.StreamsManager.FindByUniqueId(parameters["localUniqueStreamId"]);

                if (pBaseInStream == null)
                {
                    Logger.FATAL("Unable to find the inbound stream with id {0}",
                             parameters["localUniqueStreamId"]);
                    return false;
                }

                //5. Create the network outbound stream
                var pBaseOutNetRTMPStream = pProtocol.CreateONS(
                    request.StreamId,
                    pBaseInStream.Name,
                    pBaseInStream.Type);
                if (pBaseOutNetRTMPStream == null)
                {
                    Logger.FATAL("Unable to create outbound stream");
                    return false;
                }
                pBaseOutNetRTMPStream.SendOnStatusPlayMessages = false;

                //6. Link and return
                if (!pBaseInStream.Link(pBaseOutNetRTMPStream))
                {
                    Logger.FATAL("Unable to link streams");
                    return false;
                }
            }

            return true;
        }