-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtp.c
190 lines (156 loc) · 5.6 KB
/
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
/*
* Apple RTP protocol handler. This file is part of Shairport.
* Copyright (c) James Laird 2013
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <pthread.h>
#include <signal.h>
#include <unistd.h>
#include <memory.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "common.h"
#include "player.h"
// only one RTP session can be active at a time.
static int running = 0;
static int please_shutdown;
static SOCKADDR rtp_client;
static int sock;
static pthread_t rtp_thread;
static void *rtp_receiver(void *arg) {
// we inherit the signal mask (SIGUSR1)
uint8_t packet[2048], *pktp;
ssize_t nread;
while (1) {
if (please_shutdown)
break;
nread = recv(sock, packet, sizeof(packet), 0);
if (nread < 0)
break;
ssize_t plen = nread;
uint8_t type = packet[1] & ~0x80;
if (type == 0x60 || type == 0x56) { // audio data / resend
pktp = packet;
if (type==0x56) {
pktp += 4;
plen -= 4;
}
seq_t seqno = ntohs(*(unsigned short *)(pktp+2));
pktp += 12;
plen -= 12;
// check if packet contains enough content to be reasonable
if (plen >= 16) {
player_put_packet(seqno, pktp, plen);
continue;
}
if (type == 0x56 && seqno == 0) {
debug(2, "resend-related request packet received, ignoring.\n");
continue;
}
debug(1, "Unknown RTP packet of type 0x%02X length %d seqno %d\n", type, nread, seqno);
}
warn("Unknown RTP packet of type 0x%02X length %d", type, nread);
}
debug(1, "RTP thread interrupted. terminating.\n");
close(sock);
return NULL;
}
static int bind_port(SOCKADDR *remote) {
struct addrinfo hints, *info;
memset(&hints, 0, sizeof(hints));
hints.ai_family = remote->SAFAMILY;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
int ret = getaddrinfo(NULL, "0", &hints, &info);
if (ret < 0)
die("failed to get usable addrinfo?! %s", gai_strerror(ret));
sock = socket(remote->SAFAMILY, SOCK_DGRAM, IPPROTO_UDP);
ret = bind(sock, info->ai_addr, info->ai_addrlen);
freeaddrinfo(info);
if (ret < 0)
die("could not bind a UDP port!");
int sport;
SOCKADDR local;
socklen_t local_len = sizeof(local);
getsockname(sock, (struct sockaddr*)&local, &local_len);
#ifdef AF_INET6
if (local.SAFAMILY == AF_INET6) {
struct sockaddr_in6 *sa6 = (struct sockaddr_in6*)&local;
sport = htons(sa6->sin6_port);
} else
#endif
{
struct sockaddr_in *sa = (struct sockaddr_in*)&local;
sport = htons(sa->sin_port);
}
return sport;
}
int rtp_setup(SOCKADDR *remote, int cport, int tport) {
if (running)
die("rtp_setup called with active stream!");
debug(1, "rtp_setup: cport=%d tport=%d\n", cport, tport);
// we do our own timing and ignore the timing port.
// an audio perfectionist may wish to learn the protocol.
memcpy(&rtp_client, remote, sizeof(rtp_client));
#ifdef AF_INET6
if (rtp_client.SAFAMILY == AF_INET6) {
struct sockaddr_in6 *sa6 = (struct sockaddr_in6*)&rtp_client;
sa6->sin6_port = htons(cport);
} else
#endif
{
struct sockaddr_in *sa = (struct sockaddr_in*)&rtp_client;
sa->sin_port = htons(cport);
}
int sport = bind_port(remote);
debug(1, "rtp listening on port %d\n", sport);
please_shutdown = 0;
pthread_create(&rtp_thread, NULL, &rtp_receiver, NULL);
running = 1;
return sport;
}
void rtp_shutdown(void) {
if (!running)
die("rtp_shutdown called without active stream!");
debug(2, "shutting down RTP thread\n");
please_shutdown = 1;
pthread_kill(rtp_thread, SIGUSR1);
void *retval;
pthread_join(rtp_thread, &retval);
running = 0;
}
void rtp_request_resend(seq_t first, seq_t last) {
if (!running)
die("rtp_request_resend called without active stream!");
warn("requesting resend on %d packets (%04X:%04X)",
seq_diff(first,last) + 1, first, last);
char req[8]; // *not* a standard RTCP NACK
req[0] = 0x80;
req[1] = 0x55|0x80; // Apple 'resend'
*(unsigned short *)(req+2) = htons(1); // our seqnum
*(unsigned short *)(req+4) = htons(first); // missed seqnum
*(unsigned short *)(req+6) = htons(last-first+1); // count
sendto(sock, req, sizeof(req), 0, (struct sockaddr*)&rtp_client, sizeof(rtp_client));
}