-
Notifications
You must be signed in to change notification settings - Fork 8
/
OhmSender.cpp
1799 lines (1424 loc) · 44.5 KB
/
OhmSender.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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "OhmSender.h"
#include <OpenHome/Net/Core/DvAvOpenhomeOrgSender1.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
namespace OpenHome {
namespace Av {
class ProviderSender : public Net::DvProviderAvOpenhomeOrgSender1
{
static const TUint kMaxMetadataBytes = 4096;
static const TUint kTimeoutAudioPresentMs = 1000;
public:
ProviderSender(Environment& aEnv, Net::DvDevice& aDevice);
void SetMetadata(const Brx& aValue);
void SetStatusEnabled();
void SetStatusDisabled();
void SetStatusBlocked();
void InformAudioPresent();
~ProviderSender() {}
private:
virtual void PresentationUrl(Net::IDvInvocation& aInvocation, Net::IDvInvocationResponseString& aValue);
virtual void Metadata(Net::IDvInvocation& aInvocation, Net::IDvInvocationResponseString& aValue);
virtual void Audio(Net::IDvInvocation& aInvocation, Net::IDvInvocationResponseBool& aValue);
virtual void Status(Net::IDvInvocation& aInvocation, Net::IDvInvocationResponseString& aValue);
virtual void Attributes(Net::IDvInvocation& aInvocation, Net::IDvInvocationResponseString& aValue);
void UpdateMetadata();
void TimerAudioPresentExpired();
private:
Bws<kMaxMetadataBytes> iMetadata;
mutable Mutex iMutex;
Timer iTimerAudioPresent;
};
class OhmSenderServer : IOhmSenderSessionData
{
public:
OhmSenderServer(Environment& aEnv, TIpAddress aInterface, const Brx& aImage, const Brx& aMimeType);
virtual ~OhmSenderServer();
void SetInterface(TIpAddress aInterface);
void AppendImageMetadata(Bwx& aMetadata);
virtual const Brx& Image() const;
virtual const Brx& MimeType() const;
private:
static const TUint kMaxMimeTypeBytes = 100;
Environment& iEnv;
TIpAddress iInterface;
Brh iImage;
Bws<kMaxMimeTypeBytes> iMimeType;
SocketTcpServer* iServer;
};
} // namespace Av
} // namespace OpenHome
using namespace OpenHome;
using namespace OpenHome::Net;
using namespace OpenHome::Av;
// ProviderSender
ProviderSender::ProviderSender(Environment& aEnv, Net::DvDevice& aDevice)
: DvProviderAvOpenhomeOrgSender1(aDevice)
, iMutex("PSND")
, iTimerAudioPresent(aEnv, MakeFunctor(*this, &ProviderSender::TimerAudioPresentExpired), "ProviderSender")
{
EnablePropertyPresentationUrl();
EnablePropertyMetadata();
EnablePropertyAudio();
EnablePropertyStatus();
EnablePropertyAttributes();
EnableActionPresentationUrl();
EnableActionMetadata();
EnableActionAudio();
EnableActionStatus();
EnableActionAttributes();
SetPropertyPresentationUrl(Brx::Empty());
SetPropertyMetadata(Brx::Empty());
SetPropertyAudio(false);
SetStatusDisabled();
SetPropertyAttributes(Brx::Empty());
}
void ProviderSender::PresentationUrl(Net::IDvInvocation& aInvocation, Net::IDvInvocationResponseString& aValue)
{
Brhz value;
GetPropertyPresentationUrl(value);
aInvocation.StartResponse();
aValue.Write(value);
aValue.WriteFlush();
aInvocation.EndResponse();
}
void ProviderSender::Metadata(Net::IDvInvocation& aInvocation, Net::IDvInvocationResponseString& aValue)
{
Brhz value;
GetPropertyMetadata(value);
aInvocation.StartResponse();
aValue.Write(value);
aValue.WriteFlush();
aInvocation.EndResponse();
}
void ProviderSender::Audio(Net::IDvInvocation& aInvocation, Net::IDvInvocationResponseBool& aValue)
{
TBool value;
GetPropertyAudio(value);
aInvocation.StartResponse();
aValue.Write(value);
aInvocation.EndResponse();
}
void ProviderSender::Status(Net::IDvInvocation& aInvocation, Net::IDvInvocationResponseString& aValue)
{
Brhz value;
GetPropertyStatus(value);
aInvocation.StartResponse();
aValue.Write(value);
aValue.WriteFlush();
aInvocation.EndResponse();
}
void ProviderSender::Attributes(Net::IDvInvocation& aInvocation, Net::IDvInvocationResponseString& aValue)
{
Brhz value;
GetPropertyAttributes(value);
aInvocation.StartResponse();
aValue.Write(value);
aValue.WriteFlush();
aInvocation.EndResponse();
}
void ProviderSender::SetMetadata(const Brx& aValue)
{
SetPropertyMetadata(aValue);
}
static const Brn kStatusEnabled("Enabled");
static const Brn kStatusDisabled("Disabled");
static const Brn kStatusBlocked("Blocked");
void ProviderSender::SetStatusEnabled()
{
SetPropertyStatus(kStatusEnabled);
}
void ProviderSender::SetStatusDisabled()
{
SetPropertyStatus(kStatusDisabled);
}
void ProviderSender::SetStatusBlocked()
{
SetPropertyStatus(kStatusBlocked);
}
void ProviderSender::InformAudioPresent()
{
SetPropertyAudio(true);
iTimerAudioPresent.FireIn(kTimeoutAudioPresentMs);
}
void ProviderSender::TimerAudioPresentExpired()
{
SetPropertyAudio(false);
}
// OhmSenderServer
OhmSenderServer::OhmSenderServer(Environment& aEnv, TIpAddress aInterface, const Brx& aImage, const Brx& aMimeType)
: iEnv(aEnv)
, iInterface(0)
, iImage(aImage)
, iMimeType(aMimeType)
, iServer(0)
{
SetInterface(aInterface);
};
OhmSenderServer::~OhmSenderServer()
{
if (iServer)
{
delete iServer;
iServer = 0;
}
}
void OhmSenderServer::SetInterface(TIpAddress aInterface)
{
if (aInterface != iInterface)
{
if (iServer)
{
delete iServer;
iServer = 0;
}
if (aInterface != 0)
{
iServer = new SocketTcpServer(iEnv, "OHMS", 0, aInterface);
iServer->Add("OHMS", new OhmSenderSession(iEnv, *this));
}
iInterface = aInterface;
}
};
void OhmSenderServer::AppendImageMetadata(Bwx& aMetadata)
{
if (iImage.Bytes() > 0 && iInterface != 0)
{
aMetadata.Append("<upnp:albumArtURI>");
aMetadata.Append("http://");
Endpoint(iServer->Port(), iInterface).AppendEndpoint(aMetadata);
aMetadata.Append("/icon");
aMetadata.Append("</upnp:albumArtURI>");
}
}
const Brx& OhmSenderServer::Image() const
{
return iImage;
}
const Brx& OhmSenderServer::MimeType() const
{
return iMimeType;
}
// OhmSenderDriver
OhmSenderDriver::OhmSenderDriver(Environment& aEnv)
: iMutex("OHMD")
, iEnabled(false)
, iActive(false)
, iSend(false)
, iFrame(0)
, iSamplesTotal(0)
, iSampleStart(0)
, iLatency(100)
, iSocket(aEnv)
, iFactory(100, 10, 10)
{
}
void OhmSenderDriver::SetAudioFormat(TUint aSampleRate, TUint aBitRate, TUint aChannels, TUint aBitDepth, TBool aLossless, const Brx& aCodecName)
{
AutoMutex mutex(iMutex);
iSampleRate = aSampleRate;
iBitRate = aBitRate;
iChannels = aChannels;
iBitDepth = aBitDepth;
iLossless = aLossless;
iCodecName.Replace(aCodecName);
}
void OhmSenderDriver::SendAudio(const TByte* aData, TUint aBytes)
{
AutoMutex mutex(iMutex);
TUint samples = aBytes * 8 / iChannels / iBitDepth;
if (!iSend) {
iSampleStart += samples;
return;
}
TUint multiplier = 48000 * 256;
if ((iSampleRate % 441) == 0)
{
multiplier = 44100 * 256;
}
TUint latency = iLatency * multiplier / 1000;
if (iFifoHistory.SlotsUsed() == kMaxHistoryFrames) {
iFifoHistory.Read()->RemoveRef();
}
OhmMsgAudio& msg = iFactory.CreateAudio(
false, // halt
iLossless,
false,
false,
samples,
iFrame,
0, // network timestamp
latency,
0,
iSampleStart,
iSamplesTotal,
iSampleRate,
iBitRate,
0, // volume offset
iBitDepth,
iChannels,
iCodecName,
Brn(aData, aBytes)
);
WriterBuffer writer(iBuffer);
writer.Flush();
msg.Externalise(writer);
msg.SetResent(true);
iFifoHistory.Write(&msg);
try {
iSocket.Send(iBuffer, iEndpoint);
}
catch (NetworkError&) {
}
iSampleStart += samples;
iFrame++;
}
// IOhmSenderDriver
void OhmSenderDriver::SetEnabled(TBool aValue)
{
AutoMutex mutex(iMutex);
iEnabled = aValue;
if (iSend) {
if (!aValue) { // turning off
ResetLocked();
}
}
else {
if (aValue && iActive) { // turning on
iSend = true;
}
}
}
void OhmSenderDriver::SetActive(TBool aValue)
{
AutoMutex mutex(iMutex);
iActive = aValue;
if (iSend) {
if (!aValue) { // turning off
ResetLocked();
}
}
else {
if (aValue && iEnabled) { // turning on
iSend = true;
}
}
}
void OhmSenderDriver::SetEndpoint(const Endpoint& aEndpoint, TIpAddress aAdapter)
{
AutoMutex mutex(iMutex);
iEndpoint.Replace(aEndpoint);
iAdapter = aAdapter;
}
void OhmSenderDriver::SetTtl(TUint aValue)
{
AutoMutex mutex(iMutex);
iSocket.SetTtl(aValue);
}
void OhmSenderDriver::SetLatency(TUint aValue)
{
AutoMutex mutex(iMutex);
iLatency = aValue;
}
void OhmSenderDriver::SetTrackPosition(TUint64 aSamplesTotal, TUint64 aSampleStart)
{
AutoMutex mutex(iMutex);
iSamplesTotal = aSamplesTotal;
iSampleStart = aSampleStart;
}
void OhmSenderDriver::Resend(OhmMsgAudio& aMsg)
{
WriterBuffer writer(iBuffer);
writer.Flush();
aMsg.Externalise(writer);
try {
iSocket.Send(iBuffer, iEndpoint);
}
catch (NetworkError&) {
}
}
void OhmSenderDriver::Resend(const Brx& aFrames)
{
AutoMutex mutex(iMutex);
printf("RESEND");
ReaderBuffer buffer(aFrames);
ReaderBinary reader(buffer);
TUint frames = aFrames.Bytes() / 4;
TUint frame = reader.ReadUintBe(4);
frames--;
printf(" %d", frame);
TBool found = false;
TUint count = iFifoHistory.SlotsUsed();
for (TUint i = 0; i < count; i++) {
OhmMsgAudio* msg = iFifoHistory.Read();
if (!found) {
TInt diff = frame - msg->Frame();
if (diff == 0) {
Resend(*msg);
if (frames-- > 0) {
frame = reader.ReadUintBe(4);
}
else {
found = true;
}
}
else {
while (diff < 0) {
if (frames-- > 0) {
frame = reader.ReadUintBe(4);
}
else {
found = true;
break;
}
diff = frame - msg->Frame();
if (diff == 0) {
Resend(*msg);
if (frames-- > 0) {
frame = reader.ReadUintBe(4);
}
else {
found = true;
}
break;
}
}
}
}
iFifoHistory.Write(msg);
}
printf("\n");
}
void OhmSenderDriver::ResetLocked()
{
iSend = false;
iFrame = 0;
TUint count = iFifoHistory.SlotsUsed();
for (TUint i = 0; i < count; i++) {
iFifoHistory.Read()->RemoveRef();
}
}
// OhmSender
OhmSender::OhmSender(Environment& aEnv, Net::DvDevice& aDevice, IOhmSenderDriver& aDriver, const Brx& aName, TUint aChannel, TIpAddress aInterface, TUint aTtl, TUint aLatency, TBool aMulticast, TBool aEnabled, const Brx& aImage, const Brx& aMimeType, TUint aPreset)
: iEnv(aEnv)
, iDevice(aDevice)
, iDriver(aDriver)
, iName(aName)
, iChannel(aChannel)
, iOhmInterface(aInterface)
, iOhzInterface(aInterface)
, iTtl(aTtl)
, iLatency(aLatency)
, iMulticast(aMulticast)
, iEnabled(false)
, iSocketOhm(aEnv)
, iSocketOhz(aEnv)
, iRxBuffer(iSocketOhm)
, iRxZone(iSocketOhz)
, iMutexStartStop("OHMS")
, iMutexActive("OHMA")
, iMutexZone("OHMZ")
, iNetworkDeactivated("OHDN", 0)
, iZoneDeactivated("OHDZ", 0)
, iStarted(false)
, iZoneStarted(false)
, iActive(false)
, iAliveJoined(false)
, iAliveBlocked(false)
, iTimerAliveJoin(aEnv, MakeFunctor(*this, &OhmSender::TimerAliveJoinExpired), "OhmSenderAliveJoin")
, iTimerAliveAudio(aEnv, MakeFunctor(*this, &OhmSender::TimerAliveAudioExpired), "OhmSenderAliveAudio")
, iTimerExpiry(aEnv, MakeFunctor(*this, &OhmSender::TimerExpiryExpired), "OhmSenderExpiry")
, iTimerZoneUri(aEnv, MakeFunctor(*this, &OhmSender::TimerZoneUriExpired), "OhmSenderZoneUri")
, iTimerPresetInfo(aEnv, MakeFunctor(*this, &OhmSender::TimerPresetInfoExpired), "OhmSenderPresetInfo")
, iSequenceTrack(0)
, iSequenceMetatext(0)
, iClientControllingTrackMetadata(false)
, iPreset(aPreset)
{
iProvider = new ProviderSender(aEnv, iDevice);
iDriver.SetTtl(iTtl);
LOG(kMedia, "OHM SENDER DRIVER TTL %d\n", iTtl);
iDriver.SetLatency(iLatency);
LOG(kMedia, "OHM SENDER DRIVER LATENCY %d\n", iLatency);
iThreadMulticast = new ThreadFunctor("MTXM", MakeFunctor(*this, &OhmSender::RunMulticast), kThreadPriorityNetwork, kThreadStackBytesNetwork);
iThreadMulticast->Start();
iThreadUnicast = new ThreadFunctor("MTXU", MakeFunctor(*this, &OhmSender::RunUnicast), kThreadPriorityNetwork, kThreadStackBytesNetwork);
iThreadUnicast->Start();
iThreadZone = new ThreadFunctor("MTXZ", MakeFunctor(*this, &OhmSender::RunZone), kThreadPriorityNetwork, kThreadStackBytesNetwork);
iThreadZone->Start();
iServer = new OhmSenderServer(aEnv, aInterface, aImage, aMimeType);
// scope for AutoMutex
{
AutoMutex mutex(iMutexStartStop);
StartZone(iOhzInterface);
}
UpdateChannel();
SetEnabled(aEnabled);
UpdateMetadata();
}
const Brx& OhmSender::SenderUri() const
{
return (iSenderUri.AbsoluteUri());
}
const Brx& OhmSender::SenderMetadata() const
{
return (iSenderMetadata);
}
void OhmSender::SetName(const Brx& aValue)
{
AutoMutex mutex(iMutexStartStop);
if (iName != aValue) {
iName.Replace(aValue);
UpdateMetadata();
}
}
void OhmSender::SetChannel(TUint aValue)
{
AutoMutex mutex(iMutexStartStop);
if (iChannel != aValue) {
if (iMulticast) {
if (iEnabled) {
Stop();
iChannel = aValue;
UpdateChannel();
Start(iOhmInterface);
}
else {
iChannel = aValue;
UpdateChannel();
UpdateUri();
}
}
else {
iChannel = aValue;
UpdateChannel();
}
}
}
void OhmSender::SetInterface(TIpAddress aValue)
{
AutoMutex mutex(iMutexStartStop);
// Zone is restarted before the ohm socket. This is not a particularly robust fix for the following bug:
// 1. SetInterface(0) called
// 2. Previously, Stop() and Start(0) where successfully called, meaning the ohm socket was started
// 3. Then, StopZone() is called successfully and StartZone(0) throws, meaning that this function exits
// leaving the ohm socket open and ohz socket closed.
// 4. The calls in step 2 call UpdateUri which ultimately sets the iTimerZoneUri to fire.
// 5. The timer thread fires which then tries to call iSocketOhz.Send which then asserts because the socket is closed.
// So, moving the Stop/StartZone to the top of this method avoids the problem but it doesn't seem robust
if (iOhzInterface != aValue)
{
StopZone();
StartZone(aValue);
}
if (iOhmInterface != aValue) {
if (iEnabled) {
Stop();
Start(aValue);
}
iOhmInterface = aValue;
}
iServer->SetInterface(aValue);
UpdateMetadata();
}
void OhmSender::SetTtl(TUint aValue)
{
AutoMutex mutex(iMutexStartStop);
if (iTtl != aValue) {
if (iEnabled) {
Stop();
iTtl = aValue;
iDriver.SetTtl(iTtl);
LOG(kMedia, "OHM SENDER DRIVER TTL %d\n", iTtl);
Start(iOhmInterface);
}
else {
iTtl = aValue;
iDriver.SetTtl(iTtl);
LOG(kMedia, "OHM SENDER DRIVER TTL %d\n", iTtl);
}
}
}
void OhmSender::SetLatency(TUint aValue)
{
AutoMutex mutex(iMutexStartStop);
if (iLatency != aValue) {
if (iEnabled) {
Stop();
iLatency = aValue;
iDriver.SetLatency(iLatency);
LOG(kMedia, "OHM SENDER DRIVER LATENCY %d\n", iLatency);
Start(iOhmInterface);
}
else {
iLatency = aValue;
iDriver.SetLatency(iLatency);
LOG(kMedia, "OHM SENDER DRIVER LATENCY %d\n", iLatency);
}
}
}
void OhmSender::SetMulticast(TBool aValue)
{
AutoMutex mutex(iMutexStartStop);
if (iMulticast != aValue) {
if (iEnabled) {
Stop();
iMulticast = aValue;
UpdateMetadata();
Start(iOhmInterface);
}
else {
iMulticast = aValue;
UpdateMetadata();
}
}
}
void OhmSender::SetEnabled(TBool aValue)
{
AutoMutex mutex(iMutexStartStop);
if (iEnabled != aValue) {
iEnabled = aValue;
iDriver.SetEnabled(iEnabled);
LOG(kMedia, "OHM SENDER DRIVER ENABLED %d\n", aValue);
if (iEnabled) {
iProvider->SetStatusEnabled();
Start(iOhmInterface);
}
else {
Stop();
iProvider->SetStatusDisabled();
}
}
}
// Start always called with the start/stop mutex locked
void OhmSender::Start(TIpAddress aValue)
{
if (!iStarted) {
if (aValue != 0)
{
if (iMulticast) {
iSocketOhm.OpenMulticast(aValue, iTtl, iMulticastEndpoint);
iTargetEndpoint.Replace(iMulticastEndpoint);
iTargetInterface = aValue;
iThreadMulticast->Signal();
}
else {
iSocketOhm.OpenUnicast(aValue, iTtl);
iTargetInterface = aValue;
iThreadUnicast->Signal();
}
iStarted = true;
}
iOhmInterface = aValue;
UpdateUri();
}
}
// Stop always called with the start/stop mutex locked
void OhmSender::Stop()
{
if (iStarted) {
iSocketOhm.ReadInterrupt();
iNetworkDeactivated.Wait();
iSocketOhm.Close();
iStarted = false;
UpdateUri();
}
}
void OhmSender::StopZone()
{
if (iZoneStarted)
{
iSocketOhz.ReadInterrupt();
iZoneDeactivated.Wait();
iTimerZoneUri.Cancel();
iTimerPresetInfo.Cancel();
iSocketOhz.Close();
iZoneStarted = false;
}
}
void OhmSender::StartZone(TIpAddress aValue)
{
if (!iZoneStarted)
{
if (aValue != 0)
{
iSocketOhz.Open(aValue, iTtl);
iThreadZone->Signal();
iZoneStarted = true;
}
iOhzInterface = aValue;
}
}
void OhmSender::SetTrack(const Brx& aUri, const Brx& aMetadata, TUint64 aSamplesTotal, TUint64 aSampleStart)
{
AutoMutex mutex(iMutexActive);
iClientControllingTrackMetadata = true;
iDriver.SetTrackPosition(aSamplesTotal, aSampleStart);
iSequenceTrack++;
iSequenceMetatext = 0;
iTrackUri.Replace(aUri);
iTrackMetadata.Replace(aMetadata);
iTrackMetatext.Replace(Brx::Empty());
if (iActive) {
SendTrack();
}
}
void OhmSender::SetMetatext(const Brx& aValue)
{
AutoMutex mutex(iMutexActive);
iSequenceMetatext++;
iTrackMetatext.Replace(aValue);
if (iActive) {
SendMetatext();
}
}
void OhmSender::SetPreset(TUint aValue)
{
AutoMutex mutex(iMutexZone);
iPreset = aValue;
}
OhmSender::~OhmSender()
{
LOG(kMedia, "OhmSender::~OhmSender\n");
iEnabled = false;
iDriver.SetEnabled(false);
LOG(kMedia, "OhmSender::~OhmSender disabled driver\n");
// scope for AutoMutex
{
AutoMutex mutex(iMutexStartStop);
Stop();
StopZone();
}
LOG(kMedia, "OhmSender::~OhmSender stopped\n");
delete iThreadZone;
LOG(kMedia, "OhmSender::~OhmSender deleted zone thread\n");
delete iServer;
LOG(kMedia, "OhmSender::~OhmSender deleted tcp server\n");
delete iThreadUnicast;
LOG(kMedia, "OhmSender::~OhmSender deleted unicast thread\n");
delete iThreadMulticast;
LOG(kMedia, "OhmSender::~OhmSender deleted multicast thread\n");
delete iProvider;
LOG(kMedia, "OhmSender::~OhmSender deleted provider\n");
}
// This runs a little state machine where the current state is reflected by:
//
// iAliveJoined: Indicates that someone is listening to us (we received a join recently)
// iAliveBlocked: Indicates that someone else is sending on our channel (we received audio recently)
//
// iActive, when true, causes pipeline audio to be sent out over the network
//
// The state machine ensures that iActive is only true when iAliveJoined is true and iAliveBlocked is false
//
void OhmSender::RunMulticast()
{
for (;;) {
LOG(kMedia, "OhmSender::RunMulticast wait\n");
iThreadMulticast->Wait();
LOG(kMedia, "OhmSender::RunMulticast go\n");
iDriver.SetEndpoint(iTargetEndpoint, iTargetInterface);
LOG(kMedia, "OHM SENDER DRIVER ENDPOINT %x:%d\n", iTargetEndpoint.Address(), iTargetEndpoint.Port());
try {
for (;;) {
try {
OhmHeader header;
header.Internalise(iRxBuffer);
if (header.MsgType() <= OhmHeader::kMsgTypeListen) {
LOG(kMedia, "OhmSender::RunMulticast join/listen received\n");
AutoMutex mutex(iMutexActive);
if (header.MsgType() == OhmHeader::kMsgTypeJoin) {
SendTrack();
SendMetatext();
}
if (!iActive) {
iActive = true;
iDriver.SetActive(true);
LOG(kMedia, "OHM SENDER DRIVER ACTIVE %d\n", iActive);
}
iAliveJoined = true;
iTimerAliveJoin.FireIn(kTimerAliveJoinTimeoutMs);
}
else if (header.MsgType() == OhmHeader::kMsgTypeResend) {
LOG(kMedia, "OhmSender::RunMulticast resend received\n");
OhmHeaderResend headerResend;
headerResend.Internalise(iRxBuffer, header);
TUint frames = headerResend.FramesCount();
if (frames > 0) {
iDriver.Resend(iRxBuffer.Read(frames * 4));
}
}
else if (header.MsgType() == OhmHeader::kMsgTypeAudio) {
// Check sender not us
Endpoint sender = iSocketOhm.Sender();
if (sender.Address() != iOhmInterface) {
LOG(kMedia, "OhmSender::RunMulticast audio received\n");
// The following randomisation prevents two senders from both sending,
// both seeing each other's audio, both backing off for the same amount of time,
// then both sending again, then both seeing each other's audio again,
// then both backing off for the same amount of time ...
TUint delay = iEnv.Random(kTimerAliveAudioTimeoutMs, kTimerAliveAudioTimeoutMs >> 1);
// scope for AutoMutex
{
AutoMutex mutex(iMutexActive);
if (iActive) {
iActive = false;
iDriver.SetActive(false);
LOG(kMedia, "OHM SENDER DRIVER ACTIVE %d\n", iActive);
}
iAliveBlocked = true;
iTimerAliveAudio.FireIn(delay);
}
LOG(kMedia, "OhmSender::RunMulticast blocked\n");
iProvider->SetStatusBlocked();
}
}
}
catch (OhmError&)
{
}
iRxBuffer.ReadFlush();
}
}
catch (ReaderError&) {
LOG(kMedia, "OhmSender::RunMulticast reader error\n");
}
iRxBuffer.ReadFlush();
iTimerAliveJoin.Cancel();
iTimerAliveAudio.Cancel();
// scope for AutoMutex
{
AutoMutex mutex(iMutexActive);
if (iActive) {
iActive = false;
iDriver.SetActive(false);
LOG(kMedia, "OHM SENDER DRIVER ACTIVE %d\n", iActive);