Skip to content

Commit 6e5e75c

Browse files
committed
added comments
1 parent 45b6eb6 commit 6e5e75c

1 file changed

Lines changed: 37 additions & 9 deletions

File tree

src/AppleMIDI.hpp

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
BEGIN_APPLEMIDI_NAMESPACE
77

8+
// Read pending control UDP packets into the control buffer.
89
template <class UdpClass, class Settings, class Platform>
910
size_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.
2729
template <class UdpClass, class Settings, class Platform>
2830
void 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.
5558
template <class UdpClass, class Settings, class Platform>
5659
size_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.
7478
template <class UdpClass, class Settings, class Platform>
7579
void 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.
107112
template <class UdpClass, class Settings, class Platform>
108113
void 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.
116122
template <class UdpClass, class Settings, class Platform>
117123
void 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.
191198
template <class UdpClass, class Settings, class Platform>
192199
void 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).
236244
template <class UdpClass, class Settings, class Platform>
237245
void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedBitrateReceiveLimit(AppleMIDI_BitrateReceiveLimit &)
238246
{
239247
}
240248

241249
#ifdef APPLEMIDI_INITIATOR
250+
// Route accepted invitations based on the incoming port type.
242251
template <class UdpClass, class Settings, class Platform>
243252
void 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.
251261
template <class UdpClass, class Settings, class Platform>
252262
void 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.
274285
template <class UdpClass, class Settings, class Platform>
275286
void 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.
290302
template <class UdpClass, class Settings, class Platform>
291303
void 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
310323
From: 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.
419433
template <class UdpClass, class Settings, class Platform>
420434
void 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.
446461
template <class UdpClass, class Settings, class Platform>
447462
void 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.
474490
template <class UdpClass, class Settings, class Platform>
475491
bool 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.
485502
template <class UdpClass, class Settings, class Platform>
486503
Participant<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.
494512
template <class UdpClass, class Settings, class Platform>
495513
Participant<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.
504523
template <class UdpClass, class Settings, class Platform>
505524
void 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.
529549
template <class UdpClass, class Settings, class Platform>
530550
void 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.
554575
template <class UdpClass, class Settings, class Platform>
555576
void 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.
580602
template <class UdpClass, class Settings, class Platform>
581603
void 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.
605628
template <class UdpClass, class Settings, class Platform>
606629
void 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.
621645
template <class UdpClass, class Settings, class Platform>
622646
void 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.
747769
template <class UdpClass, class Settings, class Platform>
748770
void 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-
//
817836
template <class UdpClass, class Settings, class Platform>
818837
void 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.
854873
template <class UdpClass, class Settings, class Platform>
855874
void 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.
883903
template <class UdpClass, class Settings, class Platform>
884904
void 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).
899919
template <class UdpClass, class Settings, class Platform>
900920
void 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.
10381060
template <class UdpClass, class Settings, class Platform>
10391061
bool 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.
10701093
template <class UdpClass, class Settings, class Platform>
10711094
void 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.
10901114
template <class UdpClass, class Settings, class Platform>
10911115
void 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.
11021127
template <class UdpClass, class Settings, class Platform>
11031128
void 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.
11421168
template <class UdpClass, class Settings, class Platform>
11431169
void 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.
11511178
template <class UdpClass, class Settings, class Platform>
11521179
void 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.
11621190
template <class UdpClass, class Settings, class Platform>
11631191
void AppleMIDISession<UdpClass, Settings, Platform>::EndReceivedMidi()
11641192
{

0 commit comments

Comments
 (0)