Exemplo n.º 1
0
        async void EventReceivedCallback(object sender, EventReceivedEventArgs e)
        {
            if (e.Event == null)
            {
                return;
            }
            switch (e.Event.MessageType)
            {
            case MessageType.UserControlMessage:
                var m = (UserControlMessage)e.Event;
                if (m.EventType == UserControlMessageType.PingRequest)
                {
                    WriteProtocolControlMessage(new UserControlMessage(UserControlMessageType.PingResponse, m.Values));
                }
                break;

            case MessageType.DataAmf3:
#if DEBUG
                // Have no idea what the contents of these packets are.
                // Study these packets if we receive them.
                System.Diagnostics.Debugger.Break();
#endif
                break;

            case MessageType.CommandAmf3:
            case MessageType.DataAmf0:
            case MessageType.CommandAmf0:
                var command = (Command)e.Event;
                var call    = command.MethodCall;

                var param = call.Parameters.Length == 1 ? call.Parameters[0] : call.Parameters;
                if (call.Name == "_result")
                {
                    // unwrap Flex class, if present
                    var ack = param as AcknowledgeMessageExt;
                    callbackManager.SetResult(command.InvokeId, ack != null ? ack.Body : param);
                }
                else if (call.Name == "_error")
                {
                    // unwrap Flex class, if present
                    var error = param as ErrorMessage;
                    callbackManager.SetException(command.InvokeId, error != null ? new InvocationException(error) : new InvocationException());
                }
                else if (call.Name == "receive")
                {
                    var message = param as AsyncMessageExt;
                    if (message == null)
                    {
                        break;
                    }

                    object subtopicObject;
                    message.Headers.TryGetValue(AsyncMessageHeaders.Subtopic, out subtopicObject);

                    var dsSubtopic = subtopicObject as string;
                    var clientId   = message.ClientId;
                    var body       = message.Body;

                    WrapCallback(() =>
                    {
                        if (MessageReceived != null)
                        {
                            MessageReceived(this, new MessageReceivedEventArgs(clientId, dsSubtopic, body));
                        }
                    });
                }
                else if (call.Name == "onstatus")
                {
                    System.Diagnostics.Debug.Print("Received status.");
                }
                else if (call.Name == "connect")
                {
                    await ConnectResultInvokeAsync(call.Parameters);
                }
                else if (param is RemotingMessage)
                {
                    var message = param as RemotingMessage;

                    object endpoint;
                    message.Headers.TryGetValue(AsyncMessageHeaders.Endpoint, out endpoint);
                    object id;
                    message.Headers.TryGetValue(AsyncMessageHeaders.ID, out id);

                    WrapCallback(() =>
                    {
                        if (ServerMessageReceived != null)
                        {
                            ServerMessageReceived(this, new RemotingMessageReceivedEventArgs(message, message.Operation, endpoint as string, message.Destination, message.MessageId, message.Body, command.InvokeId));
                        }
                    });
                }
                else if (param is CommandMessage)
                {
                    var message = param as CommandMessage;

                    object endpoint;
                    message.Headers.TryGetValue(AsyncMessageHeaders.Endpoint, out endpoint);
                    object id;
                    message.Headers.TryGetValue(AsyncMessageHeaders.ID, out id);

                    WrapCallback(() =>
                    {
                        if (ServerCommandReceived != null)
                        {
                            ServerCommandReceived(this, new CommandMessageReceivedEventArgs(message, endpoint as string, id as string, command.InvokeId));
                        }
                    });
                }
                else
                {
#if DEBUG
                    System.Diagnostics.Debug.Print("Unknown RTMP Command: " + call.Name);
                    System.Diagnostics.Debugger.Break();
#endif
                }
                break;
            }
        }
Exemplo n.º 2
0
        void EventReceivedCallback(object sender, EventReceivedEventArgs e)
        {
            switch (e.Event.MessageType)
            {
            case MessageType.UserControlMessage:
                var m = (UserControlMessage)e.Event;
                if (m.EventType == UserControlMessageType.PingRequest)
                {
                    Console.WriteLine("Client Ping Request");
                    WriteProtocolControlMessage(new UserControlMessage(UserControlMessageType.PingResponse, m.Values));
                }
                else if (m.EventType == UserControlMessageType.SetBufferLength)
                {
                    Console.WriteLine("Set Buffer Length");
                    // TODO
                }
                else if (m.EventType == UserControlMessageType.PingResponse)
                {
                    Console.WriteLine("Ping Response");
                    var message = m as UserControlMessage;
                    callbackManager.SetResult(message.Values[0], null);
                }
                break;

            case MessageType.DataAmf3:
#if DEBUG
                System.Diagnostics.Debugger.Break();
#endif
                break;

            case MessageType.CommandAmf3:
            case MessageType.DataAmf0:
            case MessageType.CommandAmf0:
                var command = (Command)e.Event;
                var call    = command.MethodCall;
                var param   = call.Parameters.Length == 1 ? call.Parameters[0] : call.Parameters;
                switch (call.Name)
                {
                case "connect":
                    Console.WriteLine("Connect");
                    StreamId = server.RequestStreamId();
                    HandleConnectInvokeAsync(command);
                    HasConnected = true;
                    break;

                case "_result":
                    // unwrap Flex class, if present
                    var ack = param as AcknowledgeMessage;
                    callbackManager.SetResult(command.InvokeId, ack != null ? ack.Body : param);
                    break;

                case "_error":
                    // unwrap Flex class, if present
                    var error = param as ErrorMessage;
                    callbackManager.SetException(command.InvokeId, error != null ? new InvocationException(error) : new InvocationException());
                    break;

                case "receiveAudio":
                    // TODO
                    break;

                case "releaseStream":
                    Console.WriteLine("ReleaseStream");
                    // TODO
                    break;

                case "publish":
                    Console.WriteLine("publish");
                    HandlePublish(command);
                    break;

                case "unpublish":
                    HandleUnpublish(command);
                    break;

                case "FCpublish":
                case "FCPublish":
                    Console.WriteLine("FCPublish");
                    // TODO
                    break;

                case "FCUnpublish":
                case "FCunPublish":
                    Console.WriteLine("FCUnpublish");
                    HandleUnpublish(command);
                    break;

                case "createStream":
                    SetResultValInvoke(StreamId, command.InvokeId);
                    Console.WriteLine("create stream");
                    // TODO
                    break;

                case "play":
                    Console.WriteLine("play");
                    // TODO
                    HandlePlay(command);
                    break;

                case "deleteStream":
                    Console.WriteLine("deleteStream");
                    // TODO
                    break;

                case "@setDataFrame":
                    Console.WriteLine("SetDataFrame");
                    SetDataFrame(command);
                    // TODO
                    break;

                default:
#if DEBUG
                    System.Diagnostics.Debug.Print($"unknown rtmp command: {call.Name}");
                    System.Diagnostics.Debugger.Break();
#endif
                    break;
                }
                break;

            case MessageType.WindowAcknowledgementSize:
                var msg = (WindowAcknowledgementSize)e.Event;
                break;

            case MessageType.Video:
                var video_data = e.Event as VideoData;
                if (is_not_set_video_config && video_data.Data.Length >= 2 && video_data.Data[1] == 0)
                {
                    is_not_set_video_config = false;
                    AvCConfigureRecord      = video_data;
                }
                if (ChannelDataReceived != null)
                {
                    ChannelDataReceived(this, new ChannelDataReceivedEventArgs(ChannelType.Video, e.Event));
                }
                break;

            case MessageType.Audio:
                var audio_data = e.Event as AudioData;
                if (is_not_set_auido_config && audio_data.Data.Length >= 2 && audio_data.Data[1] == 0)
                {
                    is_not_set_auido_config = false;
                    AACConfigureRecord      = audio_data;
                }
                if (ChannelDataReceived != null)
                {
                    ChannelDataReceived(this, new ChannelDataReceivedEventArgs(ChannelType.Audio, e.Event));
                }
                break;

            case MessageType.Acknowledgement:
                break;

            default:
                Console.WriteLine(string.Format("Unknown message type {0}", e.Event.MessageType));
                break;
            }
        }
Exemplo n.º 3
0
        private void EventReceivedCallback(object sender, EventReceivedEventArgs e)
        {
            try
            {
                switch (e.Event.MessageType)
                {
                case MessageType.UserControlMessage:
                    var m = (UserControlMessage)e.Event;
                    if (m.EventType == UserControlMessageType.PingRequest)
                    {
                        WriteProtocolControlMessage(new UserControlMessage(UserControlMessageType.PingResponse,
                                                                           m.Values));
                    }
                    break;

                case MessageType.DataAmf3:
#if DEBUG
                    // Have no idea what the contents of these packets are.
                    // Study these packets if we receive them.
                    Debugger.Break();
#endif
                    break;

                case MessageType.CommandAmf3:
                case MessageType.DataAmf0:
                case MessageType.CommandAmf0:
                    var command = (Command)e.Event;
                    var call    = command.MethodCall;

                    var param = call.Parameters.Length == 1 ? call.Parameters[0] : call.Parameters;
                    if (call.Name == "_result" && !(param is AcknowledgeMessageExt))
                    {
                        //wrap the parameter
                        var ack = new AcknowledgeMessageExt {
                            Body = param
                        };
                        callbackManager.SetResult(command.InvokeId, ack);
                    }
                    else if (call.Name == "_result")
                    {
                        var ack = (AcknowledgeMessageExt)param;
                        callbackManager.SetResult(command.InvokeId, ack);
                    }
                    else if (call.Name == "_error")
                    {
                        // unwrap Flex class, if present
                        var error = (ErrorMessage)param;
                        callbackManager.SetException(command.InvokeId,
                                                     error != null ? new InvocationException(error) : new InvocationException());
                    }
                    else if (call.Name == "receive")
                    {
                        var message = (AsyncMessageExt)param;
                        if (message == null)
                        {
                            break;
                        }

                        object subtopicObject;
                        message.Headers.TryGetValue(AsyncMessageHeaders.Subtopic, out subtopicObject);

                        var dsSubtopic = subtopicObject as string;
                        var clientId   = message.ClientId;

                        WrapCallback(() =>
                        {
                            if (MessageReceived != null)
                            {
                                MessageReceived(this, new MessageReceivedEventArgs(clientId, dsSubtopic, message));
                            }
                        });
                    }
                    else if (call.Name == "onstatus")
                    {
                        Debug.Print("Received status.");
                    }
                    else
                    {
#if DEBUG
                        Debug.Print("Unknown RTMP Command: " + call.Name);
                        Debugger.Break();
#endif
                    }
                    break;
                }
            }
            catch (ClientDisconnectedException)
            {
                Close();
            }
        }