-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsudpsend.c
185 lines (168 loc) · 5.22 KB
/
tsudpsend.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
/*
* Copyright (C) 2008-2013, Lorenzo Pallara [email protected]
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define MULTICAST
#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#define TS_PACKET_SIZE 188
long long int usecDiff(struct timespec* time_stop,
struct timespec* time_start) {
long long int temp = 0;
long long int utemp = 0;
if (time_stop && time_start) {
if (time_stop->tv_nsec >= time_start->tv_nsec) {
utemp = time_stop->tv_nsec - time_start->tv_nsec;
temp = time_stop->tv_sec - time_start->tv_sec;
} else {
utemp = time_stop->tv_nsec + 1000000000 - time_start->tv_nsec;
temp = time_stop->tv_sec - 1 - time_start->tv_sec;
}
if (temp >= 0 && utemp >= 0) {
temp = (temp * 1000000000) + utemp;
} else {
fprintf(stderr, "start time %ld.%ld is after stop time %ld.%ld\n",
time_start->tv_sec, time_start->tv_nsec, time_stop->tv_sec,
time_stop->tv_nsec);
temp = -1;
}
} else {
fprintf(stderr, "memory is garbaged?\n");
temp = -1;
}
return temp / 1000;
}
int main(int argc, char* argv[]) {
int sockfd;
int len;
int sent;
int ret;
int is_multicast;
int transport_fd;
unsigned char option_ttl;
char start_addr[4];
struct sockaddr_in addr;
unsigned long int packet_size;
char* tsfile;
unsigned char* send_buf;
unsigned int bitrate;
unsigned long long int packet_time;
unsigned long long int real_time;
struct timespec time_start;
struct timespec time_stop;
struct timespec nano_sleep_packet;
memset(&addr, 0, sizeof(addr));
memset(&time_start, 0, sizeof(time_start));
memset(&time_stop, 0, sizeof(time_stop));
memset(&nano_sleep_packet, 0, sizeof(nano_sleep_packet));
if (argc < 5) {
fprintf(stderr,
"Usage: %s file.ts ipaddr port bitrate [ts_packet_per_ip_packet] "
"[udp_packet_ttl]\n",
argv[0]);
fprintf(stderr, "ts_packet_per_ip_packet default is 7\n");
fprintf(stderr, "bit rate refers to transport stream bit rate\n");
fprintf(stderr, "zero bitrate is 100.000.000 bps\n");
return 0;
} else {
tsfile = argv[1];
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(argv[2]);
addr.sin_port = htons(atoi(argv[3]));
bitrate = atoi(argv[4]);
if (bitrate <= 0) {
bitrate = 100000000;
}
if (argc >= 6) {
packet_size = strtoul(argv[5], 0, 0) * TS_PACKET_SIZE;
} else {
packet_size = 7 * TS_PACKET_SIZE;
}
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("socket(): error ");
return 0;
}
if (argc >= 7) {
option_ttl = atoi(argv[6]);
is_multicast = 0;
memcpy(start_addr, argv[2], 3);
start_addr[3] = 0;
is_multicast = atoi(start_addr);
is_multicast = (is_multicast >= 224) || (is_multicast <= 239);
if (is_multicast) {
ret = setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &option_ttl,
sizeof(option_ttl));
} else {
ret = setsockopt(sockfd, IPPROTO_IP, IP_TTL, &option_ttl,
sizeof(option_ttl));
}
if (ret < 0) {
perror("ttl configuration fail");
}
}
transport_fd = open(tsfile, O_RDONLY);
if (transport_fd < 0) {
fprintf(stderr, "can't open file %s\n", tsfile);
close(sockfd);
return 0;
}
int completed = 0;
send_buf = malloc(packet_size);
packet_time = 0;
real_time = 0;
nano_sleep_packet.tv_nsec = 665778; /* 1 packet at 100mbps*/
clock_gettime(CLOCK_MONOTONIC, &time_start);
while (!completed) {
clock_gettime(CLOCK_MONOTONIC, &time_stop);
real_time = usecDiff(&time_stop, &time_start);
while (real_time * bitrate > packet_time * 1000000 &&
!completed) { /* theorical bits against sent bits */
len = read(transport_fd, send_buf, packet_size);
if (len < 0) {
fprintf(stderr, "ts file read error \n");
completed = 1;
} else if (len == 0) {
fprintf(stderr, "ts sent done\n");
completed = 1;
} else {
sent = sendto(sockfd, send_buf, len, 0, (struct sockaddr*)&addr,
sizeof(struct sockaddr_in));
if (sent <= 0) {
perror("send(): error ");
completed = 1;
} else {
packet_time += packet_size * 8;
}
}
}
nanosleep(&nano_sleep_packet, 0);
}
close(transport_fd);
close(sockfd);
free(send_buf);
return 0;
}