Exemplo n.º 1
0
        public async Task <bool> Publish(StreamName key, AbstractRtmpMessage message)
        {
            var mediaStreams = _mediaStreamProvider.GetMediaStream();

            mediaStreams.TryGetValue(key, out MediaStream mediaStream);
            if (mediaStream == null)
            {
                mediaStream = new MediaStream(key);
                mediaStreams.TryAdd(key, mediaStream);
            }
            if (message is RtmpCommandMessage)
            {
                var msg         = message as RtmpCommandMessage;
                var command     = msg.Command;
                var commandName = (String)command[0].ToString();
                switch (commandName)
                {
                case "publish":
                    Publish(mediaStreams, msg);
                    break;

                case "closeStream":
                    await mediaStream.SendEofToAllSubscriberAndClose();

                    break;

                default:
                    break;
                }
            }
            if (message is AbstractRtmpMediaMessage)
            {
                mediaStream.AddContent((AbstractRtmpMediaMessage)message, true);
            }
            return(await Task.FromResult(true));
        }
        public static AbstractRtmpMessage Decode(RtmpHeader header, IByteBuffer payload)
        {
            AbstractRtmpMessage result = null;
            short messageTypeId        = header.MessageTypeId;

            switch (messageTypeId)
            {
            case Constants.MSG_SET_CHUNK_SIZE:
            {
                int          readInt      = payload.ReadInt();
                SetChunkSize setChunkSize = new SetChunkSize();
                setChunkSize.ChunkSize = readInt;
                result = setChunkSize;
            }
            break;

            case Constants.MSG_ABORT_MESSAGE:
            {
                int   csid  = payload.ReadInt();
                Abort abort = new Abort(csid);
                result = abort;
            }

            break;

            case Constants.MSG_ACKNOWLEDGEMENT:
            {
                int ack = payload.ReadInt();
                result = new Acknowledgement(ack);
            }
            break;

            case Constants.MSG_WINDOW_ACKNOWLEDGEMENT_SIZE:
            {
                int size = payload.ReadInt();
                result = new WindowAcknowledgementSize(size);
            }
            break;

            case Constants.MSG_SET_PEER_BANDWIDTH:
            {
                int ackSize = payload.ReadInt();
                int type    = payload.ReadByte();
                result = new SetPeerBandWidth(ackSize, type);
            }
            break;

            case Constants.MSG_TYPE_COMMAND_AMF0:
            {
                List <Object> decode = AMF0.DecodeAll(payload);
                result = new RtmpCommandMessage(decode);
            }
            break;

            case Constants.MSG_USER_CONTROL_MESSAGE_EVENTS:
            {
                short readShort = payload.ReadShort();
                int   data      = payload.ReadInt();
                result = new UserControlMessageEvent(readShort, data);
            }
            break;

            case Constants.MSG_TYPE_AUDIO_MESSAGE:
            {
                AudioMessage am = new AudioMessage();

                byte[] data = ReadAll(payload);
                am.AudioData = data;

                if (header.Fmt == Constants.CHUNK_FMT_0)
                {
                    am.Timestamp = header.Timestamp;
                }
                else if (header.Fmt == Constants.CHUNK_FMT_1 || header.Fmt == Constants.CHUNK_FMT_2)
                {
                    am.TimestampDelta = header.TimestampDelta;
                }
                result = am;
            }
            break;

            case Constants.MSG_TYPE_VIDEO_MESSAGE:
            {
                VideoMessage vm   = new VideoMessage();
                byte[]       data = ReadAll(payload);

                vm.VideoData = data;

                if (header.Fmt == Constants.CHUNK_FMT_0)
                {
                    vm.Timestamp = header.Timestamp;
                }
                else if (header.Fmt == Constants.CHUNK_FMT_1 || header.Fmt == Constants.CHUNK_FMT_2)
                {
                    vm.TimestampDelta = header.TimestampDelta;
                }
                result = vm;
            }
            break;

            case Constants.MSG_TYPE_DATA_MESSAGE_AMF0:
            {
                result = new RtmpDataMessage(AMF0.DecodeAll(payload));
            }
            break;

            default:
                break;
            }
            if (result != null)
            {
                result.InboundBodyLength   = header.MessageLength;
                result.InboundHeaderLength = header.HeaderLength;

                return(result);
            }
            else
            {
                return(null);
            }
        }