示例#1
0
        private void SendInvitationRejected(Bundle rinfo)
        {
            MIDIControl message = new MIDIControl();

            message.CreateInvitationRejected(this.initiator_token, MIDISession.GetInstance().ssrc, MIDISession.GetInstance().bonjourName);
            MIDISession.GetInstance().SendUDPMessage(message, rinfo);
        }
示例#2
0
 public void HandleInvitationRejected(MIDIControl control, Bundle rinfo)
 {
     connectFuture.Stop();
     connectFuture = null;
     MessagingCenter.Send <ConnectionFailedEvent>(new ConnectionFailedEvent(MIDIFailCode.REJECTED_INVITATION, rinfo, control.initiator_token), "ConnectionFailedEvent");
     MessagingCenter.Send <MIDIConnectionRequestRejectedEvent>(new MIDIConnectionRequestRejectedEvent(rinfo), "MIDIConnectionRequestRejectedEvent");
 }
示例#3
0
        private void HandleInvitationAccepted(MIDIControl control, Bundle rinfo)
        {
            if (DEBUG)
            {
                Log.Debug(TAG, "handleInvitationAccepted " + rinfo.ToString());
            }
            if (!this.isConnected && this.rinfo1 == null)
            {
                if (DEBUG)
                {
                    Log.Debug(TAG, "cancel connectFuture");
                }
                connectFuture.Stop();
                connectFuture = null;

                if (DEBUG)
                {
                    Log.Debug(TAG, "set rinfo1 " + rinfo.ToString());
                }
                rinfo1 = (Bundle)rinfo.Clone();
                rinfo1.PutString(midi.MIDIConstants.RINFO_NAME, control.name);
                rinfo.PutInt(midi.MIDIConstants.RINFO_PORT, rinfo.GetInt(midi.MIDIConstants.RINFO_PORT) + 1);
                connectTaskCount = 0;
                Connect(rinfo);
            }
            else
            if (!this.isConnected && rinfo2 == null)
            {
                if (DEBUG)
                {
                    Log.Debug(TAG, "cancel connectFuture");
                }
                connectFuture.Stop();
                connectFuture    = null;
                connectTaskCount = 0;

                this.isConnected = true;
                this.name        = control.name;
                this.ssrc        = control.ssrc;
                if (DEBUG)
                {
                    Log.Debug(TAG, "set rinfo2 " + rinfo.ToString());
                }
                rinfo2 = (Bundle)rinfo.Clone();
                MessagingCenter.Send <StreamConnectedEvent>(new StreamConnectedEvent(this.initiator_token), "StreamConnectedEvent");
                ResetSyncService(syncServicePrimaryFrequency);
            }
            else
            {
                if (DEBUG)
                {
                    Log.Debug("MIDI2Stream", "unhandled invitation accept");
                }
            }
        }
示例#4
0
        public void SendMidi(MIDIControl control, Bundle rinfo)
        {
//        Log.d("MIDIPort2","sendMidi(control)");
            if (!isListening)
            {
                if (DEBUG)
                {
                    Log.Debug(TAG, "not listening...");
                }
                return;
            }
            AddToOutboundQueue(control.GenerateBuffer(), rinfo);
        }
示例#5
0
        private void SendInvitationAccepted(Bundle rinfo)
        {
            MIDIControl message = new MIDIControl();

            message.CreateInvitationAccepted(initiator_token, MIDISession.GetInstance().ssrc, MIDISession.GetInstance().bonjourName);
            MIDISession.GetInstance().SendUDPMessage(message, rinfo);

            if (!syncStarted && rinfo2 != null)
            {
                syncStarted = true;
                MessagingCenter.Send <SyncronizeStartedEvent>(new SyncronizeStartedEvent(this.rinfo1), "SyncronizeStartedEvent");
            }
        }
示例#6
0
        public void SendInvitation(Bundle rinfo)
        {
            if (DEBUG)
            {
                Log.Debug("MIDIStream", "sendInvitation " + rinfo.GetString(MIDIConstants.RINFO_ADDR) + ":" + rinfo.GetInt(MIDIConstants.RINFO_PORT));
            }
            MIDIControl invite = new MIDIControl();

            invite.CreateInvitation(initiator_token, MIDISession.GetInstance().ssrc, MIDISession.GetInstance().bonjourName);
//        invite.dumppacket();
            MIDISession.GetInstance().SendUDPMessage(invite, rinfo);
            MessagingCenter.Send <MIDIConnectionSentRequestEvent>(new MIDIConnectionSentRequestEvent(rinfo), "MIDIConnectionSentRequestEvent");
        }
示例#7
0
        public void HandleControlMessage(MIDIControl control, Bundle rinfo)
        {
            lastPacketReceivedTime = Common.CurrentTimeMillis();

            switch (control.command)
            {
            case AppleMIDICommand.INVITATION:
//                Log.d("MIDIStream","handle INVITATION");
                HandleInvitation(control, rinfo);
                ResetCheckConnectionService();
                break;

            case AppleMIDICommand.INVITATION_ACCEPTED:
//                Log.d("MIDIStream","handle INVITATION_ACCEPTED");
                HandleInvitationAccepted(control, rinfo);
                break;

            case AppleMIDICommand.INVITATION_REJECTED:
//                Log.d("MIDIStream","handle INVITATION_REJECTED");
                HandleInvitationRejected(control, rinfo);
                break;

            case AppleMIDICommand.END:
//                Log.d("MIDIStream","handle END");
                HandleEnd(control, rinfo);
                break;

            case AppleMIDICommand.SYNCHRONIZATION:
//                Log.d("MIDIStream","handle SYNCHRONIZATION");
                if (isInitiator)
                {
                    receivedSyncResponse = true;
                }
                SendSynchronization(control);
                break;

            case AppleMIDICommand.RECEIVER_FEEDBACK:
//                Log.d("MIDIStream","handle RECEIVER_FEEDBACK");
                break;

            case AppleMIDICommand.BITRATE_RECEIVE_LIMIT:
//                Log.d("MIDIStream","handle BITRATE_RECEIVE_LIMIT");
                break;

            default:
//                Log.d("MIDIStream", "unhandled command");
                break;
            }
        }
示例#8
0
        private void HandleInvitation(MIDIControl control, Bundle rinfo)
        {
            if (rinfo1 == null)
            {
                rinfo1 = (Bundle)rinfo.Clone();
                this.initiator_token = control.initiator_token;
                this.name            = control.name;
                this.ssrc            = control.ssrc;
                rinfo1.PutString(midi.MIDIConstants.RINFO_NAME, control.name);
                MessagingCenter.Send <MIDIConnectionRequestReceivedEvent>(new MIDIConnectionRequestReceivedEvent(rinfo1), "MIDIConnectionRequestReceivedEvent");
            }
            else
            if (rinfo.GetInt(midi.MIDIConstants.RINFO_PORT) == rinfo1.GetInt(midi.MIDIConstants.RINFO_PORT))
            {
                if (MIDISession.GetInstance().IsHostConnectionAllowed(rinfo))
                {
                    this.SendInvitationAccepted(rinfo);
                }
                else
                {
                    this.SendInvitationRejected(rinfo);
                }
                return;
            }
            else
            if (rinfo2 == null)
            {
                rinfo2           = (Bundle)rinfo.Clone();
                this.isConnected = true;
            }
            else
            {
                return;
            }

// this needs work...
            if (MIDISession.GetInstance().IsHostConnectionAllowed(rinfo))
            {
                this.SendInvitationAccepted(rinfo);
            }
            else
            {
                this.SendInvitationRejected(rinfo);
            }
        }
示例#9
0
        public void SendEnd(Bundle rinfo)
        {
            Log.Debug(TAG, "send end " + rinfo.ToString());
            MIDIControl message = new MIDIControl();

            message.CreateEnd(this.initiator_token, MIDISession.GetInstance().ssrc, MIDISession.GetInstance().bonjourName);
            MIDISession.GetInstance().SendUDPMessage(message, rinfo);

            if (isConnected)
            {
                MessagingCenter.Send <StreamDisconnectEvent>(new StreamDisconnectEvent(ssrc, (Bundle)this.rinfo1.Clone()), "StreamDisconnectEvent");
            }
            else
            {
                MessagingCenter.Send <StreamDisconnectEvent>(new StreamDisconnectEvent(ssrc, initiator_token, (Bundle)this.rinfo1.Clone()), "StreamDisconnectEvent");
            }
            isConnected = false;
        }
示例#10
0
        private void HandleEnd(MIDIControl control, Bundle rinfo)
        {
            if (rinfo != rinfo1)
            {
                MessagingCenter.Send <ConnectionFailedEvent>(new ConnectionFailedEvent(MIDIFailCode.REJECTED_INVITATION, rinfo), "ConnectionFailedEvent");
                return;
            }

            this.isConnected = false;
            // shutdown sync
            if (connectFuture != null)
            {
                connectFuture.Stop();
                connectFuture = null;
            }
            if (syncFuture != null)
            {
                syncFuture.Stop();
                syncFuture = null;
            }
            if (checkConnectionFuture != null)
            {
                checkConnectionFuture.Stop();
                checkConnectionFuture = null;
            }
            //        if(MIDISession.getInstance().getAutoReconnect()) {
            //            Bundle rinfo = (Bundle) rinfo1.clone();
            //            rinfo1 = rinfo2 = null;
            //            sendInvitation(rinfo);
            //            syncStarted = false;
            //            syncFailCount = 0;
            //            syncCount = 0;
            //            this.isInitiator = false;
            //            connectFuture = null;
            //            checkConnectionFuture = null;
            //            syncFuture = null;
            //            primarySyncComplete = false;
            //            return;
            //        }
            //        EventBus.getDefault().post(new MIDIConnectionEndEvent(this.rinfo1));
            MessagingCenter.Send <StreamDisconnectEvent>(new StreamDisconnectEvent(ssrc, (Bundle)this.rinfo1.Clone()), "StreamDisconnectEvent");
        }
示例#11
0
        public void SendSynchronization(MIDIControl inboundSyncMessage)
        {
            long        now = MIDISession.GetInstance().GetNow();
            MIDIControl outboundSyncMessage;
            int         count = (inboundSyncMessage != null) ? inboundSyncMessage.count : -1;

            if (count == 3)
            {
                count = -1;
            }
            outboundSyncMessage = new MIDIControl();
            outboundSyncMessage.CreateSyncronization(
                MIDISession.GetInstance().ssrc,
                count + 1,
                (count != -1) ? inboundSyncMessage.timestamp1 : 0,
                (count != -1) ? inboundSyncMessage.timestamp2 : 0,
                (count != -1) ? inboundSyncMessage.timestamp3 : 0);

            switch (count)
            {
            case -1: // send count:0
                outboundSyncMessage.timestamp1 = now;
                outboundSyncMessage.timestamp2 = (timeDifference != 0L) ? now - timeDifference : 0L;
                outboundSyncMessage.timestamp3 = (timeDifference != 0L) ? now + timeDifference : 0L;
                break;

            case 0: // received count:0, respond count:1
                outboundSyncMessage.timestamp2 = now;
                outboundSyncMessage.timestamp3 = now - timeDifference;
                break;

            case 1: // received count:1, respond count:2
//                outboundSyncMessage.sync_timestamp3 = now;
//                latency = inboundSyncMessage.sync_timestamp3 - inboundSyncMessage.sync_timestamp1;
//                timeDifference = Math.round(inboundSyncMessage.sync_timestamp3-inboundSyncMessage.sync_timestamp2) - latency;

                timeDifference = (inboundSyncMessage.timestamp3 - inboundSyncMessage.timestamp1) / 2;
                timeDifference = inboundSyncMessage.timestamp3 + timeDifference - now;
                outboundSyncMessage.timestamp3 = now;
                break;

            case 2:  // received count:2, sending nothing
                     /* compute media delay */
//                diff = ( command->data.sync.timestamp3 - command->data.sync.timestamp1 ) / 2;
                /* approximate time difference between peer and self */
//                diff = command->data.sync.timestamp3 + diff - timestamp;
                timeDifference = (inboundSyncMessage.timestamp3 - inboundSyncMessage.timestamp1) / 2;
                timeDifference = inboundSyncMessage.timestamp3 + timeDifference - now;
                break;

            default: break;
            }
            if (outboundSyncMessage.count < 3)
            {
                MIDISession.GetInstance().SendUDPMessage(outboundSyncMessage, rinfo2);
                if (!primarySyncComplete && ++syncCount > 3)
                {
                    primarySyncComplete = true;
                    MessagingCenter.Send <SyncronizeStoppedEvent>(new SyncronizeStoppedEvent(this.rinfo1), "SyncronizeStoppedEvent");
                    MessagingCenter.Send <ConnectionEstablishedEvent>(new ConnectionEstablishedEvent(rinfo1), "ConnectionEstablishedEvent");
                }
            }
        }