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
|
/*
* Video On Demand Samples
*
* Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You may also obtain this software under a propriety license from Microchip.
* Please contact Microchip for further information.
*
*/
/*----------------------------------------------------------*/
/*! \file
* \brief This file contains the CMultiplexer class.
*/
/*----------------------------------------------------------*/
#ifndef _MULTIPLEXER_H_
#define _MULTIPLEXER_H_
//#define TS_STUFFING
//#define UDP_STREAM_TARGET_IP "127.0.0.1"
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "Console.h"
#include "ThreadReadHdd.h"
#include "ThreadWriteNetwork.h"
#ifdef UDP_STREAM_TARGET_IP
#include "udp-stream.h"
static bool udpStreamCreated = false;
#endif
typedef enum WriteResult_tag
{
WriteResult_Success,
WriteResult_Failed,
WriteResult_WouldBlock
} WriteResult_t;
/*----------------------------------------------------------*/
/*! \brief Class multiplexing multiple transport streams into
* a single driver output.
*/
/*----------------------------------------------------------*/
class CMultiplexer : public CStreamList
{
int m_hDriver;
char m_szDriver[64];
uint64_t m_nBytesStuffed;
uint32_t m_nStuffingCount;
uint32_t m_nWriteLen;
uint8_t *m_pBuf;
uint32_t m_nBufPos;
uint8_t m_scratchBuffer[CTsPacket::TS_PACKET_LEN];
uint8_t m_scratchBufPos;
bool m_patSent;
CThreadReadHdd *readThread;
CThreadWriteNetwork *writeThread;
#ifdef UDP_STREAM_TARGET_IP
bool useUdp;
#endif
static const char *GetErrnoString(int err)
{
switch( err )
{
case 0:
return "Nothing stored in errno";
case 1:
return "Operation not permitted";
case 2:
return "No such file or directory";
case 3:
return "No such process";
case 4:
return "Interrupted system call";
case 5:
return "I/O error";
case 6:
return "No such device or address";
case 7:
return "Argument list too long";
case 8:
return "Exec format error";
case 9:
return "Bad file number";
case 10:
return "No child processes";
case 11:
return "Try again";
case 12:
return "Out of memory";
case 13:
return "Permission denied";
case 14:
return "Bad address";
case 15:
return "Block device required";
case 16:
return "Device or resource busy";
case 17:
return "File exists";
case 18:
return "Cross-device link";
case 19:
return "No such device";
case 20:
return "Not a directory";
case 21:
return "Is a directory";
case 22:
return "Invalid argument";
case 23:
return "File table overflow";
case 24:
return "Too many open files";
case 25:
return "Not a typewriter";
case 26:
return "Text file busy";
case 27:
return "File too large";
case 28:
return "No space left on device";
case 29:
return "Illegal seek";
case 30:
return "Read-only file system";
case 31:
return "Too many links";
case 32:
return "Broken pipe";
case 33:
return "Math argument out of domain of func";
case 34:
return "Math result not representable";
default:
break;
}
return "Unknown";
}
public:
/*----------------------------------------------------------*/
/*! \brief Constructs CMultiplexer instance
*
* \param szDriver - The character device name of isochronous
* MOST device channel. (e.g. "/root/myFifo")
* minimal bandwidth.
*/
/*----------------------------------------------------------*/
CMultiplexer( const char *szDriver, uint32_t nWriteLen ) :
m_nStuffingCount( 0 ), m_nWriteLen( nWriteLen ), m_nBufPos( 0 ),
m_scratchBufPos( 0 ), m_patSent( false )
{
#ifdef UDP_STREAM_TARGET_IP
useUdp = !udpStreamCreated;
if (!udpStreamCreated)
{
udpStreamCreated = true;
UdpStream_Init( UDP_STREAM_TARGET_IP, 1234 );
}
#endif
m_pBuf = (uint8_t *)malloc( nWriteLen );
if ( NULL == m_pBuf )
{
ConsolePrintf( PRIO_ERROR, RED"! memory error in CMultiplexer"\
" constructor"RESETCOLOR"\n");
throw( -40 );
}
strncpy(m_szDriver, szDriver, sizeof(m_szDriver));
m_hDriver = open( szDriver, O_WRONLY );
if( -1 == m_hDriver )
{
int err = errno;
ConsolePrintf( PRIO_ERROR, RED"! failed to open driver: '%s', "\
"ERRNO: %d ('%s')"RESETCOLOR"\n",
szDriver, err, GetErrnoString( err ));
throw( -30 );
return;
}
readThread = new CThreadReadHdd();
readThread->AddStreamList( this );
writeThread = new CThreadWriteNetwork();
writeThread->AddMultiplexer( this );
}
/*----------------------------------------------------------*/
/*! \brief Destructs CMultiplexer instance
*/
/*----------------------------------------------------------*/
~CMultiplexer()
{
readThread->RemoveStreamList( this );
delete readThread;
writeThread->RemoveMultiplexer( this );
if( -1 != m_hDriver )
close( m_hDriver );
}
/*----------------------------------------------------------*/
/*! \brief Returns the name of the character device.
* \return The name of the device (e.g. "/dev/mdev0").
*/
/*----------------------------------------------------------*/
const char *GetDriverName( void )
{
return m_szDriver;
}
/*----------------------------------------------------------*/
/*! \brief Starts to add/change a sub-stream in the
* multiplex.
* \param MacAddr - The MAC address of the destination device.
* \param MacAddr - The full path to transport stream file.
*/
/*----------------------------------------------------------*/
void PlayStream( CMacAddr *MacAddr, CSource *pSource )
{
CStream *pStream = GetStream( MacAddr ); // is there already a stream assigned to this MAC?
if( NULL == pStream )
{
pStream = GetStream( NULL ); // find a stream object having an empty MAC
if( NULL == pStream )
{ // all streams in use?
ConsolePrintf( PRIO_ERROR, RED"too many streams open"RESETCOLOR );
return;
}
pStream->SetMacAddr( MacAddr ); // set MAC address of the stream
}
pStream->SetSource( pSource ); // select file to be streamed
pStream->SetPause( false ); // switch off pause
}
/*----------------------------------------------------------*/
/*! \brief Changes play position of a stream
* \param MacAddr - The MAC address of the destination device.
* \param nPos - The new time position of the stream.
*/
/*----------------------------------------------------------*/
void SetPos( CMacAddr *MacAddr, uint16_t nPos )
{
CStream *pStream = GetStream( MacAddr );
if( pStream )
pStream->SetPos( nPos );
}
/*----------------------------------------------------------*/
/*! \brief Pauses a stream
* \param MacAddr - The MAC address of the destination device.
* \param bPause - true, if the stream shall be paused. false,
* if the stream shall be playing.
*/
/*----------------------------------------------------------*/
void SetPause( CMacAddr *MacAddr, bool bPause )
{
CStream *pStream = GetStream( MacAddr );
if( pStream )
pStream->SetPause( bPause );
}
/*----------------------------------------------------------*/
/*! \brief Pauses a stream
* \param MacAddr - The MAC address of the destination device.
* \param bRepetition - true, if the stream shall be
* repeated infinitely, false, if the stream shall be
* stopped after reaching the end.
*/
/*----------------------------------------------------------*/
void SetRepetition( CMacAddr *MacAddr, bool bRepetition )
{
CStream *pStream = GetStream( MacAddr );
if( pStream )
pStream->SetRepetition( bRepetition );
}
/*----------------------------------------------------------*/
/*! \brief Stops a stream and releases resources
* \param MacAddr - The MAC address of the destination device.
*/
/*----------------------------------------------------------*/
void StopStream( CMacAddr *MacAddr )
{
CStream *pStream = GetStream( MacAddr );
if( pStream )
pStream->SetSource( NULL );
}
/*----------------------------------------------------------*/
/*! \brief Structure holding statistics values.
* \note Pass this structure to the GetStatistics method.
*/
/*----------------------------------------------------------*/
typedef struct
{
uint64_t bytesSent; /*! The amount of bytes sent with video and audio
* data are written into the given buffer. */
uint64_t bytesStuffed; /*! The amount of bytes sent with stuffing payload. */
uint64_t bytesRead; /*! The amount of bytes read with video and audio
* data are written into the given buffer. */
uint64_t errPcr; /*! The amount of PCR errors are written into
* the given buffer. */
uint64_t errBufUnderflow; /*! Number of buffer underflows
* occurred since last call. Values higher than zero
* are indicating that the thread reading stream data
* from files, has not enough performance */
uint64_t errBufOverflow; /*! Number of buffer overflows
* occurred since last call. Values higher than zero
* are indicating that the sending thread does not
* get away data fast enough. Reason could be that
* the MOST channel is too small, that the CPU is
* too small, ... */
uint64_t errTs; /*! The amount of TS errors are written into
* the given buffer. */
} MultiplexStats_t;
/*----------------------------------------------------------*/
/*! \brief Retrieves the statistics for the stream
* \param MacAddr - The MAC address of the destination device.
* \param pStats - Pointer to the MultiplexStats_t structure.
* This will be filled with data by this method.
*/
/*----------------------------------------------------------*/
bool GetStatistics( CMacAddr *MacAddr, MultiplexStats_t *pStats )
{
CStream *pStream = GetStream( MacAddr );
if( NULL == pStats )
return false;
memset( ( void * )pStats, 0, sizeof( MultiplexStats_t ) );
if( NULL != pStream )
{
pStats->bytesSent = pStream->GetBytesSent();
pStats->bytesRead = pStream->GetBytesRead();
pStats->errPcr = pStream->GetErrPcr();
pStats->errBufUnderflow = pStream->GetErrBufUnderflow();
pStats->errBufOverflow = pStream->GetErrBufOverflow();
pStats->errTs = pStream->GetErrTs();
}
pStats->bytesStuffed = m_nBytesStuffed;
m_nBytesStuffed = 0;
return true;
}
/*----------------------------------------------------------*/
/*! \brief sends some data if possible
* \return enumeration, holding the result of this operation
*/
/*----------------------------------------------------------*/
WriteResult_t WriteToDriver()
{
uint8_t *pBuf;
if (0 != m_scratchBufPos)
{
uint32_t tail = CTsPacket::TS_PACKET_LEN - m_scratchBufPos;
memcpy(&m_pBuf[m_nBufPos], &m_scratchBuffer[m_scratchBufPos], tail);
m_nBufPos += tail;
}
m_scratchBufPos = 0;
for( ; m_nBufPos < m_nWriteLen; m_nBufPos += CTsPacket::TS_PACKET_LEN )
{
if ( m_nBufPos + CTsPacket::TS_PACKET_LEN <= m_nWriteLen )
{
pBuf = m_pBuf + m_nBufPos; //The buffer has enough space to hold a complete transport stream packet
}
else
{
pBuf = m_scratchBuffer; //The buffer is not full, but there is a little space left, so use scratch buffer
m_scratchBufPos = m_nWriteLen - m_nBufPos;
}
if ( !m_patSent && GetPatPacket( pBuf ) )
{
m_patSent = true;
}
else if( !GetTsPacket( pBuf ) )
{
#ifdef TS_STUFFING
CTsPacket::CreateStuffing( pBuf, m_nStuffingCount++ );
m_nBytesStuffed += CTsPacket::TS_PACKET_LEN;
#else
return WriteResult_WouldBlock;
#endif
}
if (0 != m_scratchBufPos)
{
memcpy(m_pBuf + m_nBufPos, m_scratchBuffer, m_scratchBufPos);
break;
}
}
#ifdef UDP_STREAM_TARGET_IP
if (useUdp)
{
uint32_t udpSent = 0;
const uint32_t maxUdpSend = 7 * CTsPacket::TS_PACKET_LEN;
do
{
uint32_t bytesToSend = m_nWriteLen - udpSent;
if (bytesToSend > maxUdpSend)
bytesToSend = maxUdpSend;
UdpStream_Send( &m_pBuf[udpSent], bytesToSend );
udpSent += bytesToSend;
}
while (udpSent < m_nWriteLen);
}
#endif
m_nBufPos = 0;
m_patSent = false;
return ( m_nWriteLen == ( uint32_t )write( m_hDriver, m_pBuf, m_nWriteLen ) ) ? WriteResult_Success : WriteResult_Failed;
}
};
#endif
|