-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinputService_net.cpp
More file actions
192 lines (166 loc) · 5.57 KB
/
inputService_net.cpp
File metadata and controls
192 lines (166 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* Multi-channel Network TEXT input for Teensy Audio Library
* does NOT take update_responsibility
* Changes to this file should possibly be mirrored in inputMIDI_net.cpp
* Richard Palmer (C) 2024
*
* Released under GNU Affero General Public License v3.0 or later
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#ifndef _INPUT_SERVICE_NET_HPP_
#define _INPUT_SERVICE_NET_HPP_
#include <Arduino.h>
#include "inputService_net.h"
void AudioInputServiceNet::begin(void)
{
_currentPkt_I = 0; // initialize packet sequence
inputBegun = true;
#ifdef IS_DEBUG
Serial.printf("IN: inputServiceNet.begin() complete\n" );
#endif
}
/* ******* There is no regular update() function as this is not an AudioStream object ********
* all housekeeping is either in ce_transport::updateNet() or in function calls here
*/
// number of queued packects
bool AudioInputServiceNet::available(void)
{
if(etherTran.subsIn[_mySubI].streamID == EOQ) // don't provide data until subscription is active
return false;
return _myQueueI.size();
}
// without check - why?
int AudioInputServiceNet::getPktsInQueue()
{
//Serial.printf("IS: gPIQ Queued %i\n", _myQueueI.size());
return _myQueueI.size();
}
// assumes available(), size of next packet data (excluding headers)
int AudioInputServiceNet::dataSize(void)
{
return (_myQueueI.size() == 0) ? 0 : _myQueueI.front().samplesUsed;
}
queuePkt AudioInputServiceNet::getPkt()
{
static queuePkt _pkt; // buffer for popped incoming packet
//Serial.printf("+>+>+>++ Getting pkt, qptr %X\n", _myQueueI);
if(_myQueueI.size() == 0) // don't provide data until subscription is active
return _pkt; // Return rubbish. You should have checked getPktsInQueue() first!
//Serial.printf("+++++ Getting pkt, qptr %X\n", _myQueueI);
// extract data length from packet size
cli(); // not called by update()
_pkt = _myQueueI.front();
// memcpy((void*)&_pkt, (void*)&(_myQueueI.front()), sizeof(_pkt)); // whole packet
_myQueueI.pop();
sei();
#ifdef IS_DEBUG
// Serial.printf("Got a Service Pkt '%c', qptr %X\n", _pkt.c.content[0], _myQueueI);
#endif
return _pkt;
}
// There is no update() function
// subscribe to a stream from a (or any) host
// stream does not need to be active for subscription
// only 1 subscription per input_net object
// default registration
// register this object with subscriptions, hostname defaults to nullptr
int AudioInputServiceNet::subscribe(char * streamName, uint8_t sType, char * hostName)
{
if(_myStreamI != EOQ) // already subscribed
return _myStreamI;
int i;
int emptySlot = EOQ;
for (i = 0; i < MAX_SUBSCRIPTIONS; i++)
{
if(&_myQueueI == etherTran.subsIn[i].qPtr) // already subscribed
return i;
if(etherTran.subsIn[i].qPtr == nullptr && emptySlot == EOQ) // first empty slot
emptySlot = i;
}
if(emptySlot != EOQ) // there's space
{
// _myStreamI will be matched later
etherTran.subsIn[emptySlot].qPtr = &_myQueueI;
etherTran.subsIn[emptySlot].protocol = VBAN_SERVICE_SHIFTED;
etherTran.subsIn[emptySlot].serviceType = sType;
etherTran.subsIn[emptySlot].active = true;
strncpy(etherTran.subsIn[emptySlot].streamName, streamName, VBAN_STREAM_NAME_LENGTH-1);
if(hostName != nullptr)
strncpy(etherTran.subsIn[emptySlot].hostName, hostName, VBAN_HOSTNAME_LEN-1);
#ifdef IS_DEBUG
Serial.printf("--Subscribed to Service In stream '%s', host '%s', slot %i, queue 0x%04X\n", streamName, etherTran.subsIn[emptySlot].hostName, emptySlot,etherTran.subsIn[emptySlot].qPtr );
#endif
_mySubI = emptySlot;
return emptySlot;
// other particulars (e.g IP) will be filled in later
}
else
return EOQ;
}
// subscribe by name/IP
int AudioInputServiceNet::subscribe(char * streamName, uint8_t sType, IPAddress remoteIP)
{
if(_myStreamI != EOQ) // already subscribed
return _myStreamI;
int i;
int emptySlot = EOQ;
for (i = 0; i < MAX_SUBSCRIPTIONS; i++)
{
if(&_myQueueI == etherTran.subsIn[i].qPtr) // redundant(?) saftey check
return i;
if(etherTran.subsIn[i].qPtr == nullptr && emptySlot == EOQ) // first empty slot
emptySlot = i;
}
if(emptySlot != EOQ) // there's space
{
etherTran.subsIn[emptySlot].qPtr = &_myQueueI;
etherTran.subsIn[emptySlot].active = true;
etherTran.subsIn[emptySlot].protocol = VBAN_SERVICE_SHIFTED;
etherTran.subsIn[emptySlot].serviceType = sType;
strncpy(etherTran.subsIn[emptySlot].streamName, streamName, VBAN_STREAM_NAME_LENGTH-1);
etherTran.subsIn[emptySlot].ipAddress = remoteIP;
#ifdef IS_DEBUG
Serial.printf("Subscribed Service In to '%s', slot %i, IP ", streamName, emptySlot);
Serial.println(remoteIP);
#endif
_mySubI = emptySlot;
return emptySlot;
}
return EOQ;
}
void AudioInputServiceNet::unSubscribe(void) // release the subscribed stream.
{
if (_myStreamI >= 0)
{
etherTran.streamsIn[_myStreamI].subscription = EOQ;
etherTran.subsIn[_myStreamI].active = false;
}
_myStreamI = EOQ;
}
int AudioInputServiceNet::droppedFrames(bool reset)
{
int temp;
temp = framesDropped;
if(reset)
framesDropped = 0;
return temp;
}
void AudioInputServiceNet::printHdr(vban_header *hdr)
{
#ifdef IS_DEBUG
Serial.printf("ON: Hdr: '%c' proto+rate 0x%02X, samples %i, chans %i fmt_bit 0x%02X, '%s' fr %i\n", (char)hdr->vban, hdr->format_SR, hdr->format_nbs +1, hdr->format_nbc +1, hdr->format_bit, hdr->streamname, hdr->nuFrame);
#endif
}
void AudioInputServiceNet::printContent(uint8_t *buff, int len)
{
#ifdef IS_DEBUG
int i;
Serial.printf("Service pkt content len %i: ", len);
for(i=0; i < 6; i++)
Serial.printf("%02X ", buff[i]);
Serial.printf(" ... [%i] ", len);
for(i=len -6; i < len; i++)
Serial.printf("%02X ", buff[i]);
Serial.println();
#endif
}
#endif