55
66BEGIN_APPLEMIDI_NAMESPACE
77
8+ // Read pending control UDP packets into the control buffer.
89template <class UdpClass , class Settings , class Platform >
910size_t AppleMIDISession<UdpClass, Settings, Platform>::readControlPackets()
1011{
@@ -24,6 +25,7 @@ size_t AppleMIDISession<UdpClass, Settings, Platform>::readControlPackets()
2425 return controlBuffer.size ();
2526}
2627
28+ // Parse buffered control packets and handle errors.
2729template <class UdpClass , class Settings , class Platform >
2830void AppleMIDISession<UdpClass, Settings, Platform>::parseControlPackets()
2931{
@@ -52,6 +54,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::parseControlPackets()
5254 }
5355}
5456
57+ // Read pending data UDP packets into the data buffer.
5558template <class UdpClass , class Settings , class Platform >
5659size_t AppleMIDISession<UdpClass, Settings, Platform>::readDataPackets()
5760{
@@ -71,6 +74,7 @@ size_t AppleMIDISession<UdpClass, Settings, Platform>::readDataPackets()
7174 return dataBuffer.size ();
7275}
7376
77+ // Parse buffered data packets using RTP-MIDI and AppleMIDI parsers.
7478template <class UdpClass , class Settings , class Platform >
7579void AppleMIDISession<UdpClass, Settings, Platform>::parseDataPackets()
7680{
@@ -104,6 +108,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::parseDataPackets()
104108 }
105109}
106110
111+ // Route an invitation based on the incoming port type.
107112template <class UdpClass , class Settings , class Platform >
108113void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedInvitation(AppleMIDI_Invitation_t &invitation, const amPortType &portType)
109114{
@@ -113,6 +118,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedInvitation(AppleMID
113118 ReceivedDataInvitation (invitation);
114119}
115120
121+ // Handle an incoming control invitation from a remote participant.
116122template <class UdpClass , class Settings , class Platform >
117123void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedControlInvitation(AppleMIDI_Invitation_t &invitation)
118124{
@@ -188,6 +194,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedControlInvitation(A
188194 writeInvitation (controlPort, participant.remoteIP , participant.remotePort , invitation, amInvitationAccepted);
189195}
190196
197+ // Handle an incoming data invitation for an existing participant.
191198template <class UdpClass , class Settings , class Platform >
192199void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedDataInvitation(AppleMIDI_Invitation &invitation)
193200{
@@ -233,12 +240,14 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedDataInvitation(Appl
233240 }
234241}
235242
243+ // Placeholder for bitrate receive limit messages (not used).
236244template <class UdpClass , class Settings , class Platform >
237245void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedBitrateReceiveLimit(AppleMIDI_BitrateReceiveLimit &)
238246{
239247}
240248
241249#ifdef APPLEMIDI_INITIATOR
250+ // Route accepted invitations based on the incoming port type.
242251template <class UdpClass , class Settings , class Platform >
243252void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedInvitationAccepted(AppleMIDI_InvitationAccepted_t &invitationAccepted, const amPortType &portType)
244253{
@@ -248,6 +257,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedInvitationAccepted(
248257 ReceivedDataInvitationAccepted (invitationAccepted);
249258}
250259
260+ // Update participant state after control invitation acceptance.
251261template <class UdpClass , class Settings , class Platform >
252262void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedControlInvitationAccepted(AppleMIDI_InvitationAccepted_t &invitationAccepted)
253263{
@@ -271,6 +281,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedControlInvitationAc
271281#endif
272282}
273283
284+ // Update participant state after data invitation acceptance.
274285template <class UdpClass , class Settings , class Platform >
275286void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedDataInvitationAccepted(AppleMIDI_InvitationAccepted_t &invitationAccepted)
276287{
@@ -287,6 +298,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedDataInvitationAccep
287298 pParticipant->invitationStatus = DataInvitationAccepted;
288299}
289300
301+ // Remove participant on invitation rejection.
290302template <class UdpClass , class Settings , class Platform >
291303void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedInvitationRejected(AppleMIDI_InvitationRejected_t & invitationRejected)
292304{
@@ -305,6 +317,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedInvitationRejected(
305317}
306318#endif
307319
320+ // Handle an incoming synchronization exchange packet.
308321/* ! \brief .
309322
310323From: http://en.wikipedia.org/wiki/RTP_MIDI
@@ -416,6 +429,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedSynchronization(App
416429// encapsulate only those changes to the MIDI stream state occurring after
417430// the specified packet number.
418431//
432+ // Process receiver feedback about last received sequence numbers.
419433template <class UdpClass , class Settings , class Platform >
420434void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedReceiverFeedback(AppleMIDI_ReceiverFeedback_t &receiverFeedback)
421435{
@@ -443,6 +457,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedReceiverFeedback(Ap
443457 }
444458}
445459
460+ // Handle end-session requests and notify callbacks.
446461template <class UdpClass , class Settings , class Platform >
447462void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedEndSession(AppleMIDI_EndSession_t &endSession)
448463{
@@ -471,6 +486,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedEndSession(AppleMID
471486}
472487
473488#ifdef USE_DIRECTORY
489+ // Check whether a remote IP is in the allowed directory list.
474490template <class UdpClass , class Settings , class Platform >
475491bool AppleMIDISession<UdpClass, Settings, Platform>::IsComputerInDirectory (IPAddress remoteIP) const
476492{
@@ -482,6 +498,7 @@ bool AppleMIDISession<UdpClass, Settings, Platform>::IsComputerInDirectory(IPAdd
482498#endif
483499
484500#ifndef ONE_PARTICIPANT
501+ // Find a participant by SSRC.
485502template <class UdpClass , class Settings , class Platform >
486503Participant<Settings>* AppleMIDISession<UdpClass, Settings, Platform>::getParticipantBySSRC (const ssrc_t & ssrc)
487504{
@@ -491,6 +508,7 @@ Participant<Settings>* AppleMIDISession<UdpClass, Settings, Platform>::getPartic
491508 return nullptr ;
492509}
493510
511+ // Find a participant by initiator token.
494512template <class UdpClass , class Settings , class Platform >
495513Participant<Settings>* AppleMIDISession<UdpClass, Settings, Platform>::getParticipantByInitiatorToken (const uint32_t & initiatorToken)
496514{
@@ -501,6 +519,7 @@ Participant<Settings>* AppleMIDISession<UdpClass, Settings, Platform>::getPartic
501519}
502520#endif
503521
522+ // Serialize and send an invitation packet on the given port.
504523template <class UdpClass , class Settings , class Platform >
505524void AppleMIDISession<UdpClass, Settings, Platform>::writeInvitation (UdpClass &port, const IPAddress& remoteIP, const uint16_t & remotePort, AppleMIDI_Invitation_t & invitation, const byte *command)
506525{
@@ -526,6 +545,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::writeInvitation(UdpClass &p
526545 port.flush ();
527546}
528547
548+ // Send receiver feedback on the control port.
529549template <class UdpClass , class Settings , class Platform >
530550void AppleMIDISession<UdpClass, Settings, Platform>::writeReceiverFeedback (const IPAddress& remoteIP, const uint16_t & remotePort, AppleMIDI_ReceiverFeedback_t & receiverFeedback)
531551{
@@ -551,6 +571,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::writeReceiverFeedback(const
551571 controlPort.flush ();
552572}
553573
574+ // Send a synchronization packet on the data port.
554575template <class UdpClass , class Settings , class Platform >
555576void AppleMIDISession<UdpClass, Settings, Platform>::writeSynchronization (const IPAddress& remoteIP, const uint16_t & remotePort, AppleMIDI_Synchronization_t &synchronization)
556577{
@@ -577,6 +598,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::writeSynchronization(const
577598 dataPort.flush ();
578599}
579600
601+ // Send an end-session packet on the control port.
580602template <class UdpClass , class Settings , class Platform >
581603void AppleMIDISession<UdpClass, Settings, Platform>::writeEndSession (const IPAddress& remoteIP, const uint16_t & remotePort, AppleMIDI_EndSession_t &endSession)
582604{
@@ -602,6 +624,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::writeEndSession(const IPAdd
602624 controlPort.flush ();
603625}
604626
627+ // Flush the outgoing MIDI buffer to all participants.
605628template <class UdpClass , class Settings , class Platform >
606629void AppleMIDISession<UdpClass, Settings, Platform>::writeRtpMidiToAllParticipants ()
607630{
@@ -618,6 +641,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::writeRtpMidiToAllParticipan
618641 outMidiBuffer.clear ();
619642}
620643
644+ // Build and send an RTP-MIDI packet for a participant.
621645template <class UdpClass , class Settings , class Platform >
622646void AppleMIDISession<UdpClass, Settings, Platform>::writeRtpMidiBuffer (Participant<Settings>* participant)
623647{
@@ -741,9 +765,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::writeRtpMidiBuffer(Particip
741765#endif
742766}
743767
744- //
745- //
746- //
768+ // Manage synchronization state for all active participants.
747769template <class UdpClass , class Settings , class Platform >
748770void AppleMIDISession<UdpClass, Settings, Platform>::manageSynchronization ()
749771{
@@ -807,13 +829,10 @@ void AppleMIDISession<UdpClass, Settings, Platform>::manageSynchronization()
807829
808830#ifdef APPLEMIDI_INITIATOR
809831
810- //
811- // The initiator of the session polls if remote station is still alive.
812- // (Initiators only)
832+ // Initiator heartbeat: schedule periodic synchronization exchanges.
813833//
814834// The initiator must initiate a new sync exchange at least once every 60 seconds;
815835// otherwise the responder may assume that the initiator has died and terminate the session.
816- //
817836template <class UdpClass , class Settings , class Platform >
818837void AppleMIDISession<UdpClass, Settings, Platform>::manageSynchronizationInitiatorHeartBeat (Participant<Settings>* pParticipant)
819838{
@@ -850,7 +869,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::manageSynchronizationInitia
850869 sendSynchronization (pParticipant);
851870}
852871
853- // checks for
872+ // Retry sync invitations while establishing synchronization.
854873template <class UdpClass , class Settings , class Platform >
855874void AppleMIDISession<UdpClass, Settings, Platform>::manageSynchronizationInitiatorInvites (size_t i)
856875{
@@ -880,6 +899,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::manageSynchronizationInitia
880899
881900#endif
882901
902+ // Send a CK0 synchronization message to a participant.
883903template <class UdpClass , class Settings , class Platform >
884904void AppleMIDISession<UdpClass, Settings, Platform>::sendSynchronization (Participant<Settings>* participant)
885905{
@@ -895,7 +915,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::sendSynchronization(Partici
895915 participant->lastInviteSentTime = now;
896916}
897917
898- // (Initiators only)
918+ // Manage invitation retries for session establishment (initiators only).
899919template <class UdpClass , class Settings , class Platform >
900920void AppleMIDISession<UdpClass, Settings, Platform>::manageSessionInvites ()
901921{
@@ -991,6 +1011,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::manageSessionInvites()
9911011 }
9921012}
9931013
1014+ // Periodically emit receiver feedback for active participants.
9941015// The recovery journal mechanism requires that the receiver
9951016// periodically inform the sender of the sequence number of the most
9961017// recently received packet. This allows the sender to reduce the size
@@ -1035,6 +1056,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::manageReceiverFeedback()
10351056
10361057#ifdef APPLEMIDI_INITIATOR
10371058
1059+ // Queue a new outgoing invitation for a remote endpoint.
10381060template <class UdpClass , class Settings , class Platform >
10391061bool AppleMIDISession<UdpClass, Settings, Platform>::sendInvite (IPAddress ip, uint16_t port)
10401062{
@@ -1067,6 +1089,7 @@ bool AppleMIDISession<UdpClass, Settings, Platform>::sendInvite(IPAddress ip, ui
10671089
10681090#endif
10691091
1092+ // Send end-session to all participants and clear them.
10701093template <class UdpClass , class Settings , class Platform >
10711094void AppleMIDISession<UdpClass, Settings, Platform>::sendEndSession ()
10721095{
@@ -1087,6 +1110,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::sendEndSession()
10871110#endif
10881111}
10891112
1113+ // Send end-session to a single participant and notify callbacks.
10901114template <class UdpClass , class Settings , class Platform >
10911115void AppleMIDISession<UdpClass, Settings, Platform>::sendEndSession (Participant<Settings>* participant)
10921116{
@@ -1099,6 +1123,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::sendEndSession(Participant<
10991123 _disconnectedCallback (participant->ssrc );
11001124}
11011125
1126+ // Handle an incoming RTP header and track latency/sequence.
11021127template <class UdpClass , class Settings , class Platform >
11031128void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedRtp (const Rtp_t& rtp)
11041129{
@@ -1139,6 +1164,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedRtp(const Rtp_t& rt
11391164 }
11401165}
11411166
1167+ // Notify that a MIDI byte stream has started.
11421168template <class UdpClass , class Settings , class Platform >
11431169void AppleMIDISession<UdpClass, Settings, Platform>::StartReceivedMidi ()
11441170{
@@ -1148,6 +1174,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::StartReceivedMidi()
11481174#endif
11491175}
11501176
1177+ // Handle a received MIDI byte and buffer it.
11511178template <class UdpClass , class Settings , class Platform >
11521179void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedMidi (byte value)
11531180{
@@ -1159,6 +1186,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedMidi(byte value)
11591186 inMidiBuffer.push_back (value);
11601187}
11611188
1189+ // Notify that a MIDI byte stream has ended.
11621190template <class UdpClass , class Settings , class Platform >
11631191void AppleMIDISession<UdpClass, Settings, Platform>::EndReceivedMidi ()
11641192{
0 commit comments