-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathOhmSender.h
243 lines (219 loc) · 7.1 KB
/
OhmSender.h
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
#ifndef HEADER_OHM_SENDER
#define HEADER_OHM_SENDER
#include <OpenHome/OhNetTypes.h>
#include <OpenHome/Buffer.h>
#include <OpenHome/Net/Core/DvDevice.h>
#include <OpenHome/Private/Thread.h>
#include <OpenHome/Private/Timer.h>
#include <OpenHome/Private/Fifo.h>
#include <OpenHome/Private/Http.h>
#include "Ohm.h"
#include "OhmMsg.h"
#include "OhmSocket.h"
#include "OhmSenderDriver.h"
namespace OpenHome {
class Environment;
namespace Av {
class ProviderSender;
class OhmSenderServer;
class OhmSenderDriver : public IOhmSenderDriver
{
static const TUint kMaxAudioFrameBytes = 16 * 1024;
static const TUint kMaxHistoryFrames = 100;
public:
OhmSenderDriver(Environment& aEnv);
void SetAudioFormat(TUint aSampleRate, TUint aBitRate, TUint aChannels, TUint aBitDepth, TBool aLossless, const Brx& aCodecName);
void SendAudio(const TByte* aData, TUint aBytes);
private:
// IOhmSenderDriver
virtual void SetEnabled(TBool aValue);
virtual void SetActive(TBool aValue);
virtual void SetEndpoint(const Endpoint& aEndpoint, TIpAddress aAdapter);
virtual void SetTtl(TUint aValue);
virtual void SetLatency(TUint aValue);
virtual void SetTrackPosition(TUint64 aSampleStart, TUint64 aSamplesTotal);
virtual void Resend(const Brx& aFrames);
private:
void ResetLocked();
void Resend(OhmMsgAudio& aMsg);
private:
Mutex iMutex;
TBool iEnabled;
TBool iActive;
TBool iSend;
Endpoint iEndpoint;
TIpAddress iAdapter;
Bws<kMaxAudioFrameBytes> iBuffer;
TUint iFrame;
TUint iSampleRate;
TUint iBitRate;
TUint iChannels;
TUint iBitDepth;
TBool iLossless;
Bws<Ohm::kMaxCodecNameBytes> iCodecName;
TUint64 iSamplesTotal;
TUint64 iSampleStart;
TUint iLatency;
SocketUdp iSocket;
OhmMsgFactory iFactory;
FifoLite<OhmMsgAudio*, kMaxHistoryFrames> iFifoHistory;
};
class OhmSender
{
static const TUint kMaxMetadataBytes = 1000;
static const TUint kMaxAudioFrameBytes = 16 * 1024;
static const TUint kThreadStackBytesAudio = 64 * 1024;
static const TUint kThreadStackBytesNetwork = 64 * 1024;
static const TUint kThreadPriorityAudio = kPriorityNormal;
static const TUint kThreadPriorityNetwork = kPriorityNormal;
static const TUint kTimerAliveJoinTimeoutMs = 10000;
static const TUint kTimerAliveAudioTimeoutMs = 3000;
static const TUint kTimerExpiryTimeoutMs = 10000;
static const TUint kMaxSlaveCount = 4;
static const TUint kMaxZoneFrameBytes = 1 * 1024;
static const TUint kTimerZoneUriDelayMs = 100;
static const TUint kTimerPresetInfoDelayMs = 100;
public:
static const TUint kMaxNameBytes = 64;
static const TUint kMaxTrackUriBytes = Ohm::kMaxTrackUriBytes;
static const TUint kMaxTrackMetadataBytes = Ohm::kMaxTrackMetadataBytes;
static const TUint kMaxTrackMetatextBytes = Ohm::kMaxTrackMetatextBytes;
public:
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);
~OhmSender();
const Brx& SenderUri() const;
const Brx& SenderMetadata() const; // might change after SetName() and SetMulticast()
void SetName(const Brx& aValue);
void SetChannel(TUint aValue);
void SetInterface(TIpAddress aValue);
void SetTtl(TUint aValue);
void SetLatency(TUint aValue);
void SetMulticast(TBool aValue);
void SetEnabled(TBool aValue);
void SetTrack(const Brx& aUri, const Brx& aMetadata, TUint64 aSamplesTotal, TUint64 aSampleStart);
void SetMetatext(const Brx& aValue);
void SetPreset(TUint aValue);
private:
void RunMulticast();
void RunUnicast();
void RunZone();
void UpdateChannel();
void UpdateMetadata();
void UpdateUri();
void Start(TIpAddress aValue);
void Stop();
void StartZone(TIpAddress aValue);
void StopZone();
void EnabledChanged();
void ChannelChanged();
void TimerAliveJoinExpired();
void TimerAliveAudioExpired();
void TimerExpiryExpired();
void TimerZoneUriExpired();
void TimerPresetInfoExpired();
void Send();
void SendTrackInfo();
void SendTrack();
void SendMetatext();
void SendSlaveList();
void SendListen(const Endpoint& aEndpoint);
void SendLeave(const Endpoint& aEndpoint);
void SendZoneUri(TUint aCount);
void SendZoneUri();
void SendPresetInfo(TUint aCount);
void SendPresetInfo();
TUint FindSlave(const Endpoint& aEndpoint);
void RemoveSlave(TUint aIndex);
TBool CheckSlaveExpiry();
private:
Environment& iEnv;
Net::DvDevice& iDevice;
IOhmSenderDriver& iDriver;
Bws<kMaxNameBytes> iName;
TUint iChannel;
TIpAddress iOhmInterface;
TIpAddress iOhzInterface;
TUint iTtl;
TUint iLatency;
TBool iMulticast;
TBool iEnabled;
OhmSocket iSocketOhm;
OhzSocket iSocketOhz;
Srs<kMaxAudioFrameBytes> iRxBuffer;
Bws<kMaxAudioFrameBytes> iTxBuffer;
Srs<kMaxZoneFrameBytes> iRxZone;
Bws<kMaxZoneFrameBytes> iTxZone;
Mutex iMutexStartStop;
Mutex iMutexActive;
Mutex iMutexZone;
Semaphore iNetworkDeactivated;
Semaphore iZoneDeactivated;
ProviderSender* iProvider;
TBool iStarted;
TBool iZoneStarted;
TBool iActive;
TBool iAliveJoined;
TBool iAliveBlocked;
Endpoint iMulticastEndpoint;
Endpoint iTargetEndpoint;
TIpAddress iTargetInterface;
ThreadFunctor* iThreadMulticast;
ThreadFunctor* iThreadUnicast;
ThreadFunctor* iThreadZone;
Bws<Ohm::kMaxUriBytes> iUri;
Uri iSenderUri;
Bws<kMaxMetadataBytes> iSenderMetadata;
TUint iSlaveCount;
Endpoint iSlaveList[kMaxSlaveCount];
TUint iSlaveExpiry[kMaxSlaveCount];
Timer iTimerAliveJoin;
Timer iTimerAliveAudio;
Timer iTimerExpiry;
Timer iTimerZoneUri;
Timer iTimerPresetInfo;
Bws<Ohm::kMaxTrackUriBytes> iTrackUri;
Bws<Ohm::kMaxTrackMetadataBytes> iTrackMetadata;
Bws<Ohm::kMaxTrackMetatextBytes> iTrackMetatext;
TUint iSequenceTrack;
TUint iSequenceMetatext;
TBool iClientControllingTrackMetadata;
TUint iSendZoneUriCount;
TUint iSendPresetInfoCount;
TUint iPreset;
OhmSenderServer* iServer;
};
class IOhmSenderSessionData
{
public:
virtual const Brx& Image() const = 0;
virtual const Brx& MimeType() const = 0;
virtual ~IOhmSenderSessionData() {}
};
class OhmSenderSession : public SocketTcpSession
{
static const TUint kMaxRequestBytes = 4*1024;
static const TUint kMaxResponseBytes = 4*1024;
public:
OhmSenderSession(Environment& aEnv, const IOhmSenderSessionData& aData);
~OhmSenderSession();
private:
void Run();
void Error(const HttpStatus& aStatus);
void Get(TBool aWriteEntity);
private:
const IOhmSenderSessionData& iData;
Srx* iReadBuffer;
ReaderUntil* iReaderUntil;
ReaderHttpRequest* iReaderRequest;
Sws<kMaxResponseBytes>* iWriterBuffer;
WriterHttpResponse* iWriterResponse;
HttpHeaderHost iHeaderHost;
HttpHeaderExpect iHeaderExpect;
const HttpStatus* iErrorStatus;
TBool iResponseStarted;
TBool iResponseEnded;
Semaphore iSemaphore;
};
} // namespace Av
} // namespace OpenHome
#endif // HEADER_OHM_SENDER