-
Notifications
You must be signed in to change notification settings - Fork 2
/
MpRtp.c
76 lines (67 loc) · 1.98 KB
/
MpRtp.c
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
#include "MpRtp.h"
#include <limits.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MIN_RTP_SIZE 12
#define MPRTP_HEADER_SIZE 24
void initContext(MPRTPHeaderContext* pContext)
{
pContext->SchedulerType = ROUND_ROBIN;
pContext->LastFlow = 0;
pContext->Flows[0].FlowId = 0;
pContext->Flows[1].FlowId = 1;
pContext->Flows[0].FSSN = rand() % USHRT_MAX;
pContext->Flows[1].FSSN = rand() % USHRT_MAX;
}
void schedulePacket(MPRTPHeaderContext* pContext, int* iFlowId, int* iFSSN)
{
switch (pContext->SchedulerType)
{
case ROUND_ROBIN:
default:
{
*iFlowId = (pContext->LastFlow == 0) ? 1 : 0;
*iFSSN = pContext->Flows[*iFlowId].FSSN++;
pContext->LastFlow = *iFlowId;
}
}
}
int insertMPRTPHeader(MPRTPHeaderContext* pContext,
const unsigned char* pRtpPacket, unsigned uiRtpSize, unsigned char* pMpRtpPacket, unsigned uiMpRtpSize)
{
int iFlowId = 0;
int iFSSN = 0;
// check pContext
if (!pContext) return INVALID_CONTEXT;
if (!pRtpPacket || uiRtpSize < MIN_RTP_SIZE) return INVALID_INPUT;
if (!pMpRtpPacket || (uiMpRtpSize < MPRTP_HEADER_SIZE)) return INSUFFICIENT_MEMORY;
memcpy(pMpRtpPacket, pRtpPacket, MIN_RTP_SIZE);
memcpy(pMpRtpPacket + MPRTP_HEADER_SIZE, pRtpPacket + MIN_RTP_SIZE, uiRtpSize - MIN_RTP_SIZE);
// set extension header bit
pMpRtpPacket[0] = pMpRtpPacket[0] | 0x10;
// rfc5285 header extension
pMpRtpPacket[12] = 0xbe;
pMpRtpPacket[13] = 0xde;
pMpRtpPacket[14] = 0;
pMpRtpPacket[15] = 0x02;
// MPRTP 9.1.1
// ID + LEN
pMpRtpPacket[16] = 0x04;
// MPID: subflow RTP header + LEN
pMpRtpPacket[17] = 0x04;
schedulePacket(pContext, &iFlowId, &iFSSN);
fprintf(stderr, "Scheduling packet Flow: %d FSSN: %d\r\n", iFlowId, iFSSN);
// subflow header
// subflow id
pMpRtpPacket[18] = 0x00;
pMpRtpPacket[19] = iFlowId & 0xFF;
// FSSN
pMpRtpPacket[20] = (iFSSN >> 8) & 0xFF;
pMpRtpPacket[21] = iFSSN & 0xFF;
// padding
pMpRtpPacket[22] = 0;
pMpRtpPacket[23] = 0;
return 0;
}