-
Notifications
You must be signed in to change notification settings - Fork 8
/
OhmReceiver.cpp
884 lines (680 loc) · 16.4 KB
/
OhmReceiver.cpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
#include "OhmReceiver.h"
#include <OpenHome/Private/Ascii.h>
#include <OpenHome/Private/Arch.h>
#include <OpenHome/Private/Debug.h>
#include <OpenHome/Private/Env.h>
#include "Debug.h"
#include <stdio.h>
#ifdef _WIN32
# pragma warning(disable:4355) // use of 'this' in ctor lists safe in this case
#endif
using namespace OpenHome;
using namespace OpenHome::Net;
using namespace OpenHome::Av;
OhmReceiver::OhmReceiver(Environment& aEnv, TIpAddress aInterface, TUint aTtl, IOhmReceiverDriver& aDriver)
: iEnv(aEnv)
, iInterface(aInterface)
, iTtl(aTtl)
, iDriver(&aDriver)
, iMutexMode("OHRM")
, iMutexTransport("OHRT")
, iPlaying("OHRP", 0)
, iZoning("OHRZ", 0)
, iStopped("OHRS", 0)
, iNullStop("OHRN", 0)
, iLatency(0)
, iTransportState(eStopped)
, iPlayMode(eNone)
, iZoneMode(false)
, iTerminating(false)
, iEndpointNull(0, Brn("0.0.0.0"))
, iSocketZone(aEnv)
, iRxZone(iSocketZone)
, iTimerZoneQuery(aEnv, MakeFunctor(*this, &OhmReceiver::SendZoneQuery), "OhmReceiverZoneQuery")
, iFactory(500, 10, 10)
, iRepairing(false)
, iTimerRepair(aEnv, MakeFunctor(*this, &OhmReceiver::TimerRepairExpired), "OhmReceiverRepair")
{
iProtocolMulticast = new OhmProtocolMulticast(aEnv, *this, iFactory);
iProtocolUnicast = new OhmProtocolUnicast(aEnv, *this, iFactory);
iThread = new ThreadFunctor("OHRT", MakeFunctor(*this, &OhmReceiver::Run), kThreadPriority, kThreadStackBytes);
iThread->Start();
iThreadZone = new ThreadFunctor("OHRZ", MakeFunctor(*this, &OhmReceiver::RunZone), kThreadZonePriority, kThreadZoneStackBytes);
iThreadZone->Start();
}
OhmReceiver::~OhmReceiver()
{
iMutexTransport.Wait();
StopLocked();
iTerminating = true;
iThread->Signal();
iThreadZone->Signal();
delete (iThread);
delete (iThreadZone);
delete (iProtocolMulticast);
delete (iProtocolUnicast);
iMutexTransport.Signal();
}
TUint OhmReceiver::Ttl() const
{
iMutexTransport.Wait();
TUint ttl = iTtl;
iMutexTransport.Signal();
return (ttl);
}
TIpAddress OhmReceiver::Interface() const
{
iMutexTransport.Wait();
TIpAddress iface = iInterface;
iMutexTransport.Signal();
return (iface);
}
void OhmReceiver::SetTtl(TUint aValue)
{
iMutexTransport.Wait();
iMutexMode.Wait();
if (iTransportState != eStopped) {
switch (iPlayMode)
{
case eNone:
iTtl = aValue;
break;
case eMulticast:
iProtocolMulticast->Stop();
iStopped.Wait();
iTtl = aValue;
iTransportState = eDisconnected;
iDriver->Disconnected();
iThread->Signal();
iPlaying.Wait();
break;
case eUnicast:
iProtocolUnicast->Stop();
iStopped.Wait();
iTtl = aValue;
iTransportState = eDisconnected;
iDriver->Disconnected();
iThread->Signal();
iPlaying.Wait();
break;
case eNull:
iTtl = aValue;
break;
}
}
else {
iTtl = aValue;
}
iMutexMode.Signal();
iMutexTransport.Signal();
}
void OhmReceiver::SetInterface(TIpAddress aValue)
{
iMutexTransport.Wait();
iMutexMode.Wait();
if (iTransportState != eStopped) {
switch (iPlayMode)
{
case eNone:
iInterface = aValue;
break;
case eMulticast:
iProtocolMulticast->Stop();
iStopped.Wait();
iTransportState = eDisconnected;
iDriver->Disconnected();
iInterface = aValue;
iThread->Signal();
iPlaying.Wait();
break;
case eUnicast:
iProtocolUnicast->Stop();
iStopped.Wait();
iTransportState = eDisconnected;
iDriver->Disconnected();
iInterface = aValue;
iThread->Signal();
iPlaying.Wait();
break;
case eNull:
iInterface = aValue;
break;
}
}
else {
iInterface = aValue;
}
iMutexMode.Signal();
iMutexTransport.Signal();
}
void OhmReceiver::Play(const Brx& aUri)
{
iMutexTransport.Wait();
StopLocked();
iTransportState = eStarted;
iDriver->Started();
OpenHome::Uri uri;
try {
uri.Replace(aUri);
}
catch (UriError&) {
}
iMutexMode.Wait();
iEndpoint.Replace(Endpoint(uri.Port(), uri.Host()));
if (iEndpoint.Equals(iEndpointNull))
{
iZoneMode = false;
iPlayMode = eNull;
iTransportState = eConnected;
iDriver->Connected();
iThread->Signal();
iPlaying.Wait();
}
else if (uri.Scheme() == Brn("ohz") && iEndpoint.Equals(iSocketZone.This())) {
iZoneMode = true;
iPlayMode = eNone;
iZone.Replace(uri.PathAndQuery().Split(1));
iThreadZone->Signal();
iZoning.Wait();
}
else if (uri.Scheme() == Brn("ohm")) {
iZoneMode = false;
iPlayMode = eMulticast;
iThread->Signal();
iPlaying.Wait();
}
else if (uri.Scheme() == Brn("ohu")) {
iZoneMode = false;
iPlayMode = eUnicast;
iThread->Signal();
iPlaying.Wait();
}
else {
iZoneMode = false;
iPlayMode = eNull;
iTransportState = eConnected;
iDriver->Connected();
iThread->Signal();
iPlaying.Wait();
}
iMutexMode.Signal();
iMutexTransport.Signal();
}
void OhmReceiver::PlayZoneMode(const Brx& aUri)
{
iMutexTransport.Wait();
OpenHome::Uri uri;
try {
uri.Replace(aUri);
}
catch (UriError&) {
uri.Replace(Brn("ohm://0.0.0.0:0"));
}
Endpoint endpoint(uri.Port(), uri.Host());
iMutexTransport.Signal();
iMutexMode.Wait();
if (iEndpoint.Equals(endpoint)) {
iMutexMode.Signal();
return;
}
switch (iPlayMode)
{
case eNone:
break;
case eMulticast:
iProtocolMulticast->Stop();
iStopped.Wait();
break;
case eUnicast:
iProtocolUnicast->Stop();
iStopped.Wait();
break;
case eNull:
iNullStop.Signal();
iStopped.Wait();
break;
}
iMutexTransport.Wait();
Reset();
if (iTransportState != eStarted) {
iDriver->Started();
}
iEndpoint.Replace(endpoint);
if (iEndpoint.Equals(iEndpointNull))
{
iPlayMode = eNull;
iTransportState = eConnected;
iDriver->Connected();
iThread->Signal();
iPlaying.Wait();
}
else if (uri.Scheme() == Brn("ohm")) {
iPlayMode = eMulticast;
iTransportState = eStarted;
iThread->Signal();
iPlaying.Wait();
}
else if (uri.Scheme() == Brn("ohu")) {
iPlayMode = eUnicast;
iTransportState = eStarted;
iThread->Signal();
iPlaying.Wait();
}
else {
iPlayMode = eNull;
iTransportState = eConnected;
iDriver->Connected();
iThread->Signal();
iPlaying.Wait();
}
iMutexMode.Signal();
iMutexTransport.Signal();
}
void OhmReceiver::Stop()
{
iMutexTransport.Wait();
StopLocked();
iMutexTransport.Signal();
}
void OhmReceiver::StopLocked()
{
if (iTransportState == eStopped)
{
return;
}
iMutexMode.Wait();
iMutexTransport.Signal();
if (iZoneMode)
{
iSocketZone.ReadInterrupt();
iStopped.Wait();
}
switch (iPlayMode)
{
case eNone:
break;
case eMulticast:
iProtocolMulticast->Stop();
iStopped.Wait();
break;
case eUnicast:
iProtocolUnicast->Stop();
iStopped.Wait();
break;
case eNull:
iNullStop.Signal();
iStopped.Wait();
break;
}
iMutexTransport.Wait();
Reset();
iTransportState = eStopped;
iDriver->Stopped();
iMutexMode.Signal();
}
void OhmReceiver::Run()
{
for (;;) {
iThread->Wait();
if (iTerminating) {
break;
}
TUint ttl(iTtl);
TIpAddress iface(iInterface);
Endpoint endpoint(iEndpoint);
switch (iPlayMode) {
case eNone:
ASSERTS();
case eMulticast:
iPlaying.Signal();
iProtocolMulticast->Play(iface, ttl, endpoint);
break;
case eUnicast:
iPlaying.Signal();
iProtocolUnicast->Play(iface, ttl, endpoint);
break;
case eNull:
iPlaying.Signal();
iNullStop.Wait();
break;
}
iStopped.Signal();
}
}
void OhmReceiver::SendZoneQuery()
{
OhzHeaderZoneQuery headerZoneQuery(iZone);
OhzHeader header(OhzHeader::kMsgTypeZoneQuery, headerZoneQuery.MsgBytes());
WriterBuffer writer(iTxZone);
writer.Flush();
header.Externalise(writer);
headerZoneQuery.Externalise(writer);
writer.Write(iZone);
iSocketZone.Send(iTxZone);
iTimerZoneQuery.FireIn(kTimerZoneQueryDelayMs);
}
void OhmReceiver::RunZone()
{
for (;;) {
iThreadZone->Wait();
if (iTerminating) {
break;
}
iSocketZone.Open(iInterface, iTtl);
iZoning.Signal();
SendZoneQuery();
try {
for (;;) {
OhzHeader header;
for (;;) {
try {
header.Internalise(iRxZone);
break;
}
catch (OhzError&) {
iRxZone.ReadFlush();
}
}
if (header.MsgType() == OhzHeader::kMsgTypeZoneUri) {
OhzHeaderZoneUri headerZoneUri;
headerZoneUri.Internalise(iRxZone, header);
Brn msgZone = iRxZone.Read(headerZoneUri.ZoneBytes());
Brn msgUri = iRxZone.Read(headerZoneUri.UriBytes());
if (msgZone == iZone)
{
iTimerZoneQuery.Cancel();
PlayZoneMode(msgUri);
}
}
iRxZone.ReadFlush();
}
}
catch (ReaderError&) {
}
iRxZone.ReadFlush();
iTimerZoneQuery.Cancel();
iSocketZone.Close();
iStopped.Signal();
}
}
TUint OhmReceiver::Latency(OhmMsgAudio& aMsg)
{
TUint latency = aMsg.MediaLatency();
if (latency != 0) {
TUint multiplier = 48000 * 256;
if ((aMsg.SampleRate() % 441) == 0)
{
multiplier = 44100 * 256;
}
latency = latency * 1000 / multiplier;
LOG(kMedia, "LATENCY %d\n", latency);
return (latency);
}
LOG(kMedia, "LATENCY %d\n", kDefaultLatency);
return (kDefaultLatency);
}
void OhmReceiver::Reset()
{
iTimerRepair.Cancel();
if (iRepairing) {
iRepairFirst->RemoveRef();
while (iFifoRepair.SlotsUsed() > 0) {
iFifoRepair.Read()->RemoveRef();
}
}
iRepairing = false;
iLatency = 0;
}
TBool OhmReceiver::RepairBegin(OhmMsgAudio& aMsg)
{
LOG(kMedia, "BEGIN ON %d\n", aMsg.Frame());
iRepairFirst = &aMsg;
iTimerRepair.FireIn(iEnv.Random(kInitialRepairTimeoutMs));
return (true);
}
void OhmReceiver::RepairReset()
{
LOG(kMedia, "RESET\n");
iTimerRepair.Cancel();
iRepairFirst->RemoveRef();
while (iFifoRepair.SlotsUsed() > 0) {
iFifoRepair.Read()->RemoveRef();
}
iTransportState = eDisconnected;
iDriver->Disconnected();
iLatency = 0;
}
TBool OhmReceiver::Repair(OhmMsgAudio& aMsg)
{
// get the incoming frame number
TUint frame = aMsg.Frame();
LOG(kMedia, "GOT %d\n", frame);
// get difference between this and the last frame send down the pipeline
TInt diff = frame - iFrame;
if (diff == 1) {
// incoming frame is one greater than the last frame sent down the pipeline, so send this ...
iFrame++;
iDriver->Add(aMsg);
// ... and see if the current first waiting frame is now also ready to be sent
while (iRepairFirst->Frame() == iFrame + 1) {
// ... yes, it is, so send it
iFrame++;
iDriver->Add(*iRepairFirst);
// ... and see if there are further messages waiting in the fifo
if (iFifoRepair.SlotsUsed() == 0) {
// ... no, so we have completed the repair
LOG(kMedia, "END\n");
return (false);
}
// ... yes, so update the current first waiting frame and continue testing to see if this can also be sent
iRepairFirst = iFifoRepair.Read();
}
// ... we're done
return (true);
}
if (diff < 1) {
// incoming frames is equal to or earlier than the last frame sent down the pipeline
// in other words, it's a duplicate, so so discard it and continue
aMsg.RemoveRef();
return (true);
}
// Ok, its a frame that needs to be put into the backlog, but where?
// compare it to the current first waiting frame
diff = frame - iRepairFirst->Frame();
if (diff < 0) {
// it's earlier than the current first waiting message, so it should become the new current first waiting frame
// and the old first waiting frame needs to be injected into the start of the backlog, so inject it into the end
// and rotate the others (if there is space to add another frame)
TUint count = iFifoRepair.SlotsUsed();
if (count == kMaxRepairBacklogFrames) {
// can't put another frame into the backlog
RepairReset();
return (false);
}
iFifoRepair.Write(iRepairFirst);
if (count == 0) {
iRepairLast = iRepairFirst->Frame();
}
for (TUint i = 0; i < count; i++) {
iFifoRepair.Write(iFifoRepair.Read());
}
// replace the currently waiting message with this new one
iRepairFirst = &aMsg;
return (true);
}
if (diff == 0) {
// it's equal to the currently first waiting frame, so discard it - it's a duplicate
aMsg.RemoveRef();
return (true);
}
// ok, it's after the currently first waiting frame, so it needs to go into the backlog
// first check if the backlog is empty
if (iFifoRepair.SlotsUsed() == 0) {
// ... yes, so just inject it
iFifoRepair.Write(&aMsg);
// ... and always keep track of the latest frame in the backlog
iRepairLast = frame;
return (true);
}
// ok, so the backlog is not empty
// is the incoming frame later than the last one currently in the backlog?
diff = frame - iRepairLast;
if (diff > 0) {
// ... yes, so, again, just inject it (if there is space)
TUint count = iFifoRepair.SlotsUsed();
if (count == kMaxRepairBacklogFrames) {
// can't put another frame into the backlog
RepairReset();
return (false);
}
iFifoRepair.Write(&aMsg);
// ... and update the record of the last frame in the backlog
iRepairLast = frame;
return (true);
}
// is it a duplicate of the last frame in the backlog?
if (diff == 0) {
// ... yes, so discard
aMsg.RemoveRef();
return (true);
}
// ... no, so it has to go somewhere in the middle of the backlog, so iterate through and inject it at the right place (if there is space)
TUint count = iFifoRepair.SlotsUsed();
TBool found = false;
for (TUint i = 0; i < count; i++) {
OhmMsgAudio* msg = iFifoRepair.Read();
if (!found) {
diff = frame - msg->Frame();
if (diff < 0) {
if (count == kMaxRepairBacklogFrames) {
// can't put another frame into the backlog
iFifoRepair.Write(&aMsg);
RepairReset();
return (false);
}
iFifoRepair.Write(&aMsg);
found = true;
}
else if (diff == 0) {
aMsg.RemoveRef();
found = true;
}
}
iFifoRepair.Write(msg);
}
return (true);
}
void OhmReceiver::TimerRepairExpired()
{
iMutexTransport.Wait();
if (iRepairing) {
LOG(kMedia, "REQUEST RESEND");
Bws<kMaxRepairMissedFrames * 4> missed;
WriterBuffer buffer(missed);
WriterBinary writer(buffer);
TUint count = 0;
TUint start = iFrame + 1;
TUint end = iRepairFirst->Frame();
// phase 1 - request the frames between the last sent down the pipeline and the first waiting frame
for (TUint i = start; i < end; i++) {
writer.WriteUint32Be(i);
LOG(kMedia, " %d", i);
if (++count == kMaxRepairMissedFrames) {
break;
}
}
// phase 2 - if there is room add the missing frames in the backlog
if (count < kMaxRepairMissedFrames) {
TUint slots = iFifoRepair.SlotsUsed();
for (TUint j = 0; j < slots; j++) {
OhmMsgAudio* msg = iFifoRepair.Read();
if (count < kMaxRepairMissedFrames) {
start = end + 1;
end = msg->Frame();
for (TUint i = start; i < end; i++) {
writer.WriteUint32Be(i);
LOG(kMedia, " %d", i);
if (++count == kMaxRepairMissedFrames) {
break;
}
}
}
iFifoRepair.Write(msg);
}
}
LOG(kMedia, "\n");
switch (iPlayMode) {
case eMulticast:
iProtocolMulticast->RequestResend(missed);
break;
case eUnicast:
iProtocolUnicast->RequestResend(missed);
break;
default:
break;
}
// iTimerRepair.FireIn(iEnv.Random(iLatency >> 1)); // check again a random time 1/2 of the audio latency
iTimerRepair.FireIn(kSubsequentRepairTimeoutMs); // check again a random time 1/2 of the audio latency
}
iMutexTransport.Signal();
}
// IOhmReceiver
void OhmReceiver::Add(OhmMsg& aMsg)
{
iDriver->Timestamp(aMsg);
iMutexTransport.Wait();
aMsg.Process(*this);
iMutexTransport.Signal();
}
void OhmReceiver::ResendSeen()
{
iMutexTransport.Wait();
if (iRepairing) {
// iTimerRepair.FireIn(iEnv.Random(iLatency >> 2)); // delay repair timer by a random time between 0 and 1/4 of the audio latency
iTimerRepair.FireIn(kSubsequentRepairTimeoutMs); // delay repair timer by a random time between 0 and 1/4 of the audio latency
}
iMutexTransport.Signal();
}
// IOhmMsgProcessor
void OhmReceiver::Process(OhmMsgAudio& aMsg)
{
if (iLatency == 0) {
iFrame = aMsg.Frame();
iLatency = Latency(aMsg);
iTransportState = ePlaying;
iDriver->Playing();
iDriver->Add(aMsg);
return;
}
if (iRepairing) {
iRepairing = Repair(aMsg);
return;
}
TInt diff = aMsg.Frame() - iFrame;
if (diff == 1) {
iFrame++;
iDriver->Add(aMsg);
}
else if (diff < 1) {
aMsg.RemoveRef();
}
else {
iRepairing = RepairBegin(aMsg);
}
}
void OhmReceiver::Process(OhmMsgTrack& aMsg)
{
if (iTransportState == eStarted) {
iTransportState = eConnected;
iDriver->Connected();
}
iDriver->Add(aMsg);
}
void OhmReceiver::Process(OhmMsgMetatext& aMsg)
{
if (iTransportState == eStarted) {
iTransportState = eConnected;
iDriver->Connected();
}
iDriver->Add(aMsg);
}