-
Notifications
You must be signed in to change notification settings - Fork 17
/
ctn91xx_rtp.c
289 lines (221 loc) · 6.59 KB
/
ctn91xx_rtp.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
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
#include "ctn91xx.h"
#include "ctn91xx_mpeg.h"
#include "ctn91xx_rtp.h"
#include "ctn91xx_net.h"
void ctn91xx_rtp_setup(ctn91xx_dev_t* dev, uint8_t* buf, uint32_t len)
{
vbuffer_t* vbuffer = NULL;
rtp_state_t* rtp = NULL;
uint32_t tuner_index;
if( len < SIZEOF_CONTROL_MSG_RTP_SETUP ) {
WARNING("len %d too small", len);
return;
}
tuner_index = ntohl( *((uint32_t*)&buf[0] ) );
if( tuner_index >= NUM_MPEG_STREAMS ) {
WARNING("tuner index %08x was too large", tuner_index);
return;
}
#if HAS_MPEG_DMA
mpeg_vbuffer_from_tuner_index(tuner_index, dev, &vbuffer);
#endif
if( !vbuffer ) {
return;
}
rtp = &vbuffer->rtp_state;
buf += 4;
memcpy( rtp->remote_hw_addr, buf, 6 );
buf += 6;
memcpy( rtp->local_hw_addr, buf, 6 );
buf += 6;
rtp->remote_ip_addr = *((uint32_t*)&buf[0]);
buf += 4;
rtp->local_ip_addr = *((uint32_t*)&buf[0]);
buf += 4;
rtp->remote_port = *((uint16_t*)&buf[0]);
buf += 2;
rtp->local_port = *((uint16_t*)&buf[0]);
buf += 2;
if( !rtp->got_setup ) {
rtp_header_t* hdr = (rtp_header_t*)rtp->buffer;
rtp->buffer_used = RTP_HDR_SIZE;
hdr->version = 2;
hdr->padding = 0;
hdr->extension = 0;
hdr->csrc_len = 0;
hdr->marker = 0;
hdr->payload = MP2T_PAYLOAD_TYPE;
rtp->got_setup = 1;
rtp->running = 1;
dev->mpeg_user_cnt[tuner_index]++;
vbuffer->read_idx = vbuffer->notify_idx;
}
}
void ctn91xx_rtp_destroy(ctn91xx_dev_t* dev, uint8_t* buf, uint32_t len)
{
vbuffer_t* vbuffer = NULL;
rtp_state_t* rtp = NULL;
uint32_t tuner_index;
if( len < SIZEOF_CONTROL_MSG_RTP_DESTROY ) {
WARNING("len %d too small", len);
return;
}
tuner_index = ntohl( *((uint32_t*)&buf[0] ) );
if( tuner_index >= NUM_MPEG_STREAMS ) {
WARNING("tuner index %d was too large", tuner_index);
return;
}
#if HAS_MPEG_DMA
mpeg_vbuffer_from_tuner_index(tuner_index, dev, &vbuffer);
#endif
if( !vbuffer ) {
return;
}
rtp = &vbuffer->rtp_state;
memset( rtp->remote_hw_addr, 0, 6 );
memset( rtp->local_hw_addr, 0, 6 );
rtp->remote_ip_addr = 0;
rtp->local_ip_addr = 0;
rtp->remote_port = 0;
rtp->local_port = 0;
rtp->got_setup = 0;
rtp->running = 0;
if( dev->mpeg_user_cnt[tuner_index] > 0 ) {
dev->mpeg_user_cnt[tuner_index]--;
}
}
uint16_t ip_hdr_chksum( uint8_t* ip, int len )
{
uint16_t* buf = (uint16_t*)ip;
int sum = 0;
while( len > 1 ) {
sum += *buf;
buf++;
if( sum & 0x80000000 ) {
/* if high order bit set, fold */
sum = (sum & 0xFFFF) + (sum >> 16);
}
len -= 2;
}
if( len ) {
/* take care of left over byte */
sum += (unsigned short) *(unsigned char *)buf;
}
while( sum >> 16 ) {
sum = (sum & 0xFFFF) + (sum >> 16);
}
return (uint16_t)~sum;
}
#define ETH_PACKET_SIZE(rtp) (rtp->buffer_used + 14 /* eth */ + 20 /* ip */ + 8 /* udp */)
#define IP_PACKET_SIZE(rtp) (rtp->buffer_used + 20 /* ip */ + 8 /* udp */)
#define UDP_PACKET_SIZE(rtp) (rtp->buffer_used + 8 /* udp */)
#define RTP_PACKET_SIZE(rtp) (rtp->buffer_used)
static void rtp_send_packet( vbuffer_t* vbuffer )
{
ctn91xx_dev_t* dev = vbuffer->dev;
rtp_state_t* rtp = &vbuffer->rtp_state;
struct sk_buff* skb;
rtp_header_t* hdr = (rtp_header_t*)rtp->buffer;
uint8_t* buf = NULL;
uint16_t rx_len = ETH_PACKET_SIZE(rtp);
uint16_t ip_chk_sum = 0;
hdr->seq_no = htons( rtp->seq_no++ );
hdr->timestamp = MP2T_TIMESTAMP_CLOCK;//TODO get actual timstamp
hdr->ssrc = htonl( rtp->ssrc );
skb = dev_alloc_skb( rx_len );
if( !skb ) {
if( printk_ratelimit() ) {
WARNING("memory squeeze, dropping packet");
}
return;
}
buf = skb->data;
memcpy( buf, rtp->remote_hw_addr, 6 );
memcpy( buf + 6, rtp->local_hw_addr, 6 );
//ether type = IP
buf[12] = 0x08;
buf[13] = 0x00;
buf += 14;
//ipv4 header
buf[0] = 0x45;
buf[1] = 0x00;
//ip length
*((uint16_t*)&buf[2]) = htons( IP_PACKET_SIZE( rtp ) );
//identification
*((uint16_t*)&buf[4]) = htons( rtp->ip_seq_no++ );
/* flags, fragment offset */
buf[6] = 0;
buf[7] = 0;
/* ttl */
buf[8] = 10;
/* protocol = UDP */
buf[9] = 0x11;
/* checksum */
*((uint16_t*)&buf[10]) = 0x0000;
/* addrs */
*((uint32_t*)&buf[12]) = rtp->local_ip_addr;
*((uint32_t*)&buf[16]) = rtp->remote_ip_addr;
/* go back and calc checksum */
ip_chk_sum = ip_hdr_chksum( buf, 20 );
*((uint16_t*)&buf[10]) = ip_chk_sum;
buf += 20;
//udp header
*((uint16_t*)&buf[0]) = rtp->local_port;
*((uint16_t*)&buf[2]) = rtp->remote_port;
*((uint16_t*)&buf[4]) = htons( UDP_PACKET_SIZE( rtp ) );
//checksum
*((uint16_t*)&buf[6]) = 0x0000;
buf += 8;
memcpy( buf, rtp->buffer, RTP_PACKET_SIZE( rtp ) );
rtp->buffer_used = RTP_HDR_SIZE;
ctn91xx_net_rx_skb( dev, skb, rx_len );
}
static void rtp_sink_read_data( vbuffer_t* vbuffer )
{
rtp_state_t* rtp = &vbuffer->rtp_state;
uint32_t bytes_left = RTP_SINK_BUFFER_SIZE - rtp->buffer_used;
uint32_t num_ts_pkts = bytes_left / MPEG_TS_PKT_SIZE;
uint8_t* unused_buffer_start = &rtp->buffer[rtp->buffer_used];
if( num_ts_pkts > 0 ) {
#if HAS_MPEG_DMA
uint32_t read_size = ctn91xx_mpeg_kernel_read(
vbuffer, unused_buffer_start, MPEG_TS_PKT_SIZE*num_ts_pkts );
rtp->buffer_used += read_size;
#endif
}
}
static void rtp_data_ready( vbuffer_t* vbuffer )
{
rtp_state_t* rtp = &vbuffer->rtp_state;
uint32_t bytes_left = RTP_SINK_BUFFER_SIZE - rtp->buffer_used;
if( bytes_left >= MPEG_TS_PKT_SIZE ) {
rtp_sink_read_data( vbuffer );
}
bytes_left = RTP_SINK_BUFFER_SIZE - rtp->buffer_used;
if( bytes_left < MPEG_TS_PKT_SIZE ) {
rtp_send_packet( vbuffer );
}
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
void ctn91xx_rtp_reader(void* work)
#else
void ctn91xx_rtp_reader(struct work_struct* work)
#endif
{
rtp_state_t* rtp = container_of(work, rtp_state_t, reader);
vbuffer_t* vbuffer = container_of(rtp, vbuffer_t, rtp_state);
spin_lock_w_flags(&vbuffer->lock);
if( !rtp->got_setup || !rtp->running ) {
goto end;
}
#if HAS_MPEG_DMA
while( mpeg_data_ready( vbuffer ) ) {
rtp_data_ready( vbuffer );
if( !rtp->running ) {
break;
}
}
#endif
end:
spin_unlock_w_flags(&vbuffer->lock);
}