Inheritance: IDisposable
示例#1
0
        public void Start(Peer peer, uint publisherId, FlowWriter controller)
        {
            if (PublisherId != 0)
            {
                if (controller != null)
                {
                    controller.WriteStatusResponse("Publish.BadName", Name + "is already published");
                }
            }
            PublisherId = publisherId;

            //string error;
            //if (!peer.OnPublish(this, out error))
            //{
            //    if (String.IsNullOrEmpty(error)) error = "Not allowed to publish " + Name;
            //}
            _publisher     = peer;
            _controller    = controller;
            _firstKeyFrame = false;
            foreach (var baseOutStream in OutStreams.OfType <IOutNetStream>())
            {
                baseOutStream.SendPublishNotify();
            }
            if (controller != null)
            {
                controller.WriteStatusResponse("Publish.Start", Name + "is now published");
            }
        }
示例#2
0
 public void Start(Peer peer, uint publisherId, FlowWriter controller)
 {
     if (PublisherId != 0)
     {
         if (controller != null)
         {
             controller.WriteStatusResponse("Publish.BadName", Name + "is already published");
         }
     }
     PublisherId = publisherId;
    
     string error;
     if (!peer.OnPublish(this, out error))
     {
         if (String.IsNullOrEmpty(error)) error = "Not allowed to publish " + Name;
     }
     _publisher = peer;
     _controller = controller;
     _firstKeyFrame = false;
     foreach (var listener in Listeners)
     {
         listener.Value.StartPublishing(Name);
     }
     Flush();
     if (controller != null)
     {
         controller.WriteStatusResponse("Publish.Start", Name + "is now published");
     }
 }
示例#3
0
 public Listener(uint id, Publication publication, FlowWriter writer, bool unbuffered)
 {
     Id          = id;
     Publication = publication;
     _writer     = writer;
     _unbuffered = unbuffered;
 }
示例#4
0
 public Listener(uint id, Publication publication, FlowWriter writer, bool unbuffered)
 {
     Id = id;
     Publication = publication;
     _writer = writer;
     _unbuffered = unbuffered;
 }
示例#5
0
 public FlowStream(ulong id, string signature, Peer peer, BaseRtmfpProtocol handler, Session band,FlowWriter localFlow)
     : base(id, signature, _Name, peer, handler, band, localFlow)
 {
     if (handler.StreamsManager?.StreamsByType.ContainsKey(StreamTypes.ST_IN_NET_RTMFP)??false)
     _publisher = handler.StreamsManager.StreamsByType[StreamTypes.ST_IN_NET_RTMFP].Select(x => x.Value as InNetRtmfpStream).SingleOrDefault(x=>x.PublisherId==StreamId);
     //_publication = handler.Publications.Values.SingleOrDefault(x => x.PublisherId == _index);
 }
示例#6
0
 public void PlayStream(string name,ulong flowId,uint streamId, OutFileRTMPFLVStream flvStream)
 {
     if (_flvStream != null) return;
     var flowStream = new FlowWriter(FlowStream.Signature + (char)streamId,this, flowId);
     flowStream.Play(name);
     _flvStream = flvStream;
     _flvStream.SignalAttachedToInStream();
 }
示例#7
0
        public Group JoinGroup(byte[] groupId, FlowWriter writer)
        {
            var group = new Group()
            {
                Id = groupId
            };

            Handler.Groups.Add(group.IdStr, group);
            JoinGroup(group, writer);
            return(group);
        }
示例#8
0
        public void JoinGroup(Group group, FlowWriter writer)
        {
            var count  = 5;
            var random = group.Count > 5?Utils.Random.Next(group.Count - 5):0;

            foreach (var peer in group)
            {
                if (count > 0)
                {
                    if (peer == this)
                    {
                        continue;
                    }
                    if (!WriteId(group, peer, writer))
                    {
                        continue;
                    }
                    --count;
                }
                else
                {
                    random--;
                    if (random < 0)
                    {
                        break;
                    }
                    if (random == 0)
                    {
                        WriteId(group, peer, writer);
                    }
                }
            }
            if (_groups.Count > 0 && _groups.Last().Key == group)
            {
                return;
            }
            var index = 0u;

            if (group.Peers.Count > 0)
            {
                index = group.Peers.Last().Key + 1;
                if (index < group.Peers.Last().Key)
                {
                    index = 0;
                    foreach (var item in group.Peers.Values.ToList())
                    {
                        group.Peers[index++] = item;
                    }
                }
            }
            group.Peers[index] = this;
            _groups[group]     = new Member(index, writer);
        }
示例#9
0
 public Flow(ulong id, string signature, string name, Peer peer, BaseRtmfpProtocol handler, Session band,FlowWriter flowWriter)
 {
     Id = id;
     StreamId = signature.ToBytes().Read7BitValue(4);
     Peer = peer;
     Handler = handler;
     Band = band;
     if (flowWriter == null || flowWriter.Signature!=signature)
         Writer = new FlowWriter(signature, band, id) {Obj = name};
     else
     {
         Writer = flowWriter;
         Writer.FlowId = id;
     }
 }
示例#10
0
        public virtual Flow CreateFlow(ulong id, string signature, ulong assocFlowId)
        {
            if (IsEnqueueForDelete)
            {
            }
            if (Flows.ContainsKey(id))
            {
                return(Flows[id]);
            }

            Flow       flow      = null;
            FlowWriter localFlow = null;

            if (FlowWriters.ContainsKey(assocFlowId))
            {
                localFlow = FlowWriters[assocFlowId];
            }
            switch (signature)
            {
            case FlowConnection.Signature:
                Logger.Debug("New FlowConnection {0} on session {1}", id, Id);
                flow = new FlowConnection(id, Peer, Handler, this, localFlow);
                break;

            case FlowGroup.Signature:
                Logger.Debug("New FlowGroup {0} on session {1}", id, Id);
                flow = new FlowGroup(id, Peer, Handler, this, localFlow);
                break;

            default:
                if (signature.StartsWith(FlowStream.Signature))
                {
                    Logger.Debug("New FlowStream {0} on session {1}", id, Id);
                    flow = new FlowStream(id, signature, Peer, Handler, this, localFlow);
                }
                else
                {
                    Logger.FATAL("New unknown flow {0} on session {1}", signature, Id);
                }
                break;
            }
            if (flow != null && id != 0)
            {
                Flows[id] = flow;
            }
            return(flow);
        }
示例#11
0
        private bool WriteId(Group @group, Peer peer, FlowWriter writer)
        {
            H2NBinaryWriter response;

            if (writer != null)
            {
                response = writer.WriterRawMessage(true);
            }
            else
            {
                if (!peer._groups.ContainsKey(@group) || peer._groups[@group].Writer == null)
                {
                    return(false);
                }
                response = peer._groups[@group].Writer.WriterRawMessage(true);
            }
            response.Write((byte)0x0b);
            response.Write(peer.Id);
            return(true);
        }
示例#12
0
 public Listener AddListener(Peer peer,uint id,FlowWriter writer,bool unbuffered)
 {
     if (Listeners.ContainsKey(id))
         return Listeners[id];
     var listener = new Listener(id, this, writer, unbuffered);
     string error;
     if (peer.OnSubscribe(listener,out error))
     {
         Listeners[id] = listener;
         writer.WriteStatusResponse("Play.Reset", "Playing and resetting " + Name);
         writer.WriteStatusResponse("Play.Start", "Started playing " + Name);
         listener.Init(peer);
         return listener;
     }
     if (string.IsNullOrEmpty(error))
     {
         error = "Not authorized to play " + Name;
     }
     writer.WriteStatusResponse("Play.Failed", error);
     throw new Exception(error);
 }
示例#13
0
        public override InNetRtmfpStream PublishStream(Peer peer, uint id, string name, string type, FlowWriter writer)
        {
            var session = writer.Band as Session;

            if (!session.Application.StreamsManager.StreamNameAvailable(name))
            {
                WARN(
                    "Stream name {0} already occupied and application doesn't allow duplicated inbound network streams",
                    name);
                writer.WriteStatusResponse("Publish.BadName", name + "is not available");
                return(null);
            }
            var pInNetRtmpStream = new InNetRtmfpStream(session, session.Application.StreamsManager, name);

            session.Application.OnPublish(this, pInNetRtmpStream, type);
            pInNetRtmpStream.Start(peer, id, writer);
            return(pInNetRtmpStream);
        }
示例#14
0
 private void WriteBound(FlowWriter writer)
 {
     var data = writer.WriterRawMessage();
     data.Write((ushort)0x22);
     data.Write(_boundId);
     data.Write(3);
 }
示例#15
0
 public FlowConnection(ulong id, Peer peer, BaseRtmfpProtocol handler, Session band,FlowWriter flowWriter)
     : base(id, Signature, _Name, peer, handler, band, flowWriter)
 {
     Writer.Critical = true;
 }
示例#16
0
        public override OutNetRtmfpStream SubScribeStream(Peer peer, uint id, string name, FlowWriter writer, double start, double length)
        {
            var session           = writer.Band as Session;
            var outNetRtmfpStream = new OutNetRtmfpStream(session, session.Application.StreamsManager, id, name)
            {
                Writer = writer, Unbuffered = start == -3000
            };

            outNetRtmfpStream.Init();
            var inBoundStream = session.Application.StreamsManager.FindByTypeByName(StreamTypes.ST_IN_NET, name, true, false).Select(x => x.Value).OfType <IInStream>().FirstOrDefault();

            switch ((int)start)
            {
            case -2000:
                if (inBoundStream != null)
                {
                    inBoundStream.Link(outNetRtmfpStream);
                }
                else
                {
                    goto default;
                }
                break;

            case -1000:
                if (inBoundStream != null)
                {
                    inBoundStream.Link(outNetRtmfpStream);
                }
                else
                {
                    goto case -999;
                }
                break;

            case -999:
                if (ClientApplicationManager.ClusterApplication != null)
                {
                    ClientApplicationManager.ClusterApplication.GetProtocolHandler <BaseClusterAppProtocolHandler>().PlayStream(session.Application.Id, name);
                }
                break;

            default:
                var metadata          = session.Application.StreamsManager.GetMetaData(name, true, session.Application.Configuration);
                var pRtmpInFileStream = InFileRTMPStream.GetInstance(session, session.Application.StreamsManager, metadata);
                if (pRtmpInFileStream == null)
                {
                    WARN("Unable to get file stream. Metadata:\n{0}", metadata.ToString());
                    goto case -999;
                }
                if (!pRtmpInFileStream.Initialize(metadata[Defines.CONF_APPLICATION_CLIENTSIDEBUFFER]))
                {
                    WARN("Unable to initialize file inbound stream");
                    pRtmpInFileStream.Dispose();
                    goto case -999;
                }
                if (!pRtmpInFileStream.Link(outNetRtmfpStream))
                {
                    goto case -999;
                }
                if (!pRtmpInFileStream.Play(start, length))
                {
                    FATAL("Unable to start the playback");
                    goto case -999;
                }
                break;
            }
            return(outNetRtmfpStream);
        }
 public override OutNetRtmfpStream SubScribeStream(Peer peer, uint id, string name, FlowWriter writer, double start, double length)
 {
     var session = writer.Band as Session;
     var outNetRtmfpStream = new OutNetRtmfpStream(session, session.Application.StreamsManager, id, name) { Writer = writer, Unbuffered = start == -3000 };
     outNetRtmfpStream.Init();
     var inBoundStream = session.Application.StreamsManager.FindByTypeByName(StreamTypes.ST_IN_NET, name, true, false).Select(x => x.Value).OfType<IInStream>().FirstOrDefault();
     
     switch ((int)start)
     {
         case -2000:
             if (inBoundStream != null)
             {
                 inBoundStream.Link(outNetRtmfpStream);
             }
             else
             {
                 goto default;
             }
             break;
         case -1000:
             if (inBoundStream != null)
             {
                 inBoundStream.Link(outNetRtmfpStream);
             }
             else
             {
                 goto case -999;
             }
             break;
         case -999:
             if (ClientApplicationManager.ClusterApplication != null)
             {
                 ClientApplicationManager.ClusterApplication.GetProtocolHandler<BaseClusterAppProtocolHandler>().PlayStream(session.Application.Id, name);
             }
             break;
         default:
             var metadata = session.Application.StreamsManager.GetMetaData(name, true, session.Application.Configuration);
             var pRtmpInFileStream = InFileRTMPStream.GetInstance(session, session.Application.StreamsManager, metadata);
                 if (pRtmpInFileStream == null)
                 {
                     WARN("Unable to get file stream. Metadata:\n{0}", metadata.ToString());
                     goto case -999;
                 }
                 if (!pRtmpInFileStream.Initialize(metadata[Defines.CONF_APPLICATION_CLIENTSIDEBUFFER]))
                 {
                     WARN("Unable to initialize file inbound stream");
                     pRtmpInFileStream.Dispose();
                     goto case -999;
                 }
                 if (!pRtmpInFileStream.Link(outNetRtmfpStream))
                 {
                     goto case -999;
                 }
                 if (!pRtmpInFileStream.Play(start, length))
                 {
                     FATAL("Unable to start the playback");
                     goto case -999;
                 }
             break;
     }
     return outNetRtmfpStream;
 }
 public virtual InNetRtmfpStream PublishStream(Peer peer, uint id, string name, string type, FlowWriter writer)
 {
     //throw new NotImplementedException();
     return(null);
 }
 public virtual OutNetRtmfpStream SubScribeStream(Peer peer, uint id, string name, FlowWriter writer, double start, double length)
 {
     //throw new NotImplementedException();
     return(null);
 }
示例#20
0
 public override Session CreateSession(Peer peer, Cookie cookie)
 {
     var connection = new FlowWriter(FlowConnection.Signature,DownloadSession,0);
     var connectArgs = Variant.Get();
     connectArgs["app"] = AppName;
     connectArgs["tcUrl"] = TcUrl;
     connectArgs["objectEncoding"] = 3.0;
     connectArgs["flashVer"] = "WIN 17,0,0,134";
     connectArgs["fpad"] = false;
     connectArgs["capabilities"] = 235.0;
     connectArgs["audioCodecs"] = 3575.0;
     connectArgs["videoCodecs"] = 252.0;
     connectArgs["videoFunction"] = 1.0;
     connectArgs["swfUrl"] = Variant.Get();
     connectArgs["pageUrl"] = Variant.Get();
     connection.Connect(connectArgs, (f1, message) =>
     {
         Log += message[1]["code"];
         if (message[1]["code"] == "NetConnection.Connect.Success")
         {
             //connection.SetPeerInfo(FarProtocol.IOHandler.Socket.LocalEndPoint as IPEndPoint);
             connection.CreateStream((f2, o) =>
             {
                 DownloadSession.PlayStream(StreamName, f2.Id, o[1],
                     new OutFileRTMPFLVStream(this, StreamsManager,
                         FilePath ?? Url.Substring(8).Replace('/', '_').Replace(':', '_') + ".flv", StreamName));
                 Status = "正在下载";
                 StartTime = DateTime.Now;
             });
         }
         else
         {
             Status = "连接失败";
         }
     });
     return DownloadSession;
 }
示例#21
0
 public FlowStream(ulong id, string signature, Peer peer, BaseRtmfpProtocol handler, Session band, FlowWriter localFlow)
     : base(id, signature, _Name, peer, handler, band, localFlow)
 {
     if (handler.StreamsManager?.StreamsByType.ContainsKey(StreamTypes.ST_IN_NET_RTMFP) ?? false)
     {
         _publisher = handler.StreamsManager.StreamsByType[StreamTypes.ST_IN_NET_RTMFP].Select(x => x.Value as InNetRtmfpStream).SingleOrDefault(x => x.PublisherId == StreamId);
     }
     //_publication = handler.Publications.Values.SingleOrDefault(x => x.PublisherId == _index);
 }
示例#22
0
 public virtual OutNetRtmfpStream SubScribeStream(Peer peer, uint id, string name, FlowWriter writer, double start, double length)
 {
     //throw new NotImplementedException();
     return null;
 }
示例#23
0
 public virtual InNetRtmfpStream PublishStream(Peer peer, uint id, string name, string type, FlowWriter writer)
 {
     //throw new NotImplementedException();
     return null;
 }
示例#24
0
 public void Stop(Peer peer, uint publisherId)
 {
     if (publisherId == 0) return;
     if (PublisherId != publisherId)
     {
         Logger.WARN("Unpublish '{0}' operation with a {1} id different than its publisher {2} id", Name, publisherId, PublisherId);
         return;
     }
     foreach (var listener in Listeners)
     {
         listener.Value.StopPublishing(Name);
     }
     Flush();
     peer.OnUnpublish(this);
     VideoQOS.Reset();
     AudioQOS.Reset();
     PublisherId = 0;
     _publisher = null;
     _controller = null;
 }
示例#25
0
 public FlowWriter(FlowWriter flowWriter)
 {
     Id = flowWriter.Id;
     _stage = flowWriter._stage;
     _stageAck = flowWriter._stageAck;
     _ackCount = flowWriter._ackCount;
     _lostCount = flowWriter._lostCount;
     Reliable = flowWriter.Reliable;
     Band = flowWriter.Band;
     FlowId = flowWriter.FlowId;
     Signature = flowWriter.Signature;
     Close();
 }
 public override InNetRtmfpStream PublishStream(Peer peer, uint id, string name, string type, FlowWriter writer)
 {
     var session = writer.Band as Session;
     if (!session.Application.StreamsManager.StreamNameAvailable(name))
     {
         WARN(
             "Stream name {0} already occupied and application doesn't allow duplicated inbound network streams",
             name);
         writer.WriteStatusResponse("Publish.BadName", name + "is not available");
         return null;
     }
     var pInNetRtmpStream = new InNetRtmfpStream(session, session.Application.StreamsManager, name);
     session.Application.OnPublish(this, pInNetRtmpStream, type);
     pInNetRtmpStream.Start(peer, id, writer);
     return pInNetRtmpStream;
 }
示例#27
0
 public Member(uint index, FlowWriter writer)
 {
     Index  = index;
     Writer = writer;
 }
示例#28
0
 public FlowGroup(ulong id, Peer peer, BaseRtmfpProtocol handler, Session band, FlowWriter flowWriter)
     : base(id, Signature, _Name, peer, handler, band, flowWriter)
 {
 }