Mercurial > mplayer.hg
annotate udp_sync.c @ 31992:4229b325910e
Fix indentation.
author | reimar |
---|---|
date | Wed, 08 Sep 2010 17:52:32 +0000 |
parents | 52a0d770adee |
children | 2b209fa9c48d |
rev | line source |
---|---|
31982
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
1 /* |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
2 * Network playback synchronization |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
3 * Copyright (C) 2009 Google Inc. |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
4 * |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
5 * This file is part of MPlayer. |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
6 * |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
7 * MPlayer is free software; you can redistribute it and/or modify |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
8 * it under the terms of the GNU General Public License as published by |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
9 * the Free Software Foundation; either version 2 of the License, or |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
10 * (at your option) any later version. |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
11 * |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
12 * MPlayer is distributed in the hope that it will be useful, |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
15 * GNU General Public License for more details. |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
16 * |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
17 * You should have received a copy of the GNU General Public License along |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
18 * with MPlayer; if not, write to the Free Software Foundation, Inc., |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
20 */ |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
21 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
22 #include "config.h" |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
23 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
24 #if !HAVE_WINSOCK2_H |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
25 #include <errno.h> |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
26 #include <sys/types.h> |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
27 #include <sys/socket.h> |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
28 #include <netinet/in.h> |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
29 #include <stdlib.h> |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
30 #include <sys/ioctl.h> |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
31 #include <fcntl.h> |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
32 #include <string.h> |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
33 #include <strings.h> |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
34 #include <netdb.h> |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
35 #include <signal.h> |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
36 #else |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
37 #include <winsock2.h> |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
38 #include <ws2tcpip.h> |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
39 #endif /* HAVE_WINSOCK2_H */ |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
40 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
41 #include "mplayer.h" |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
42 #include "mp_core.h" |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
43 #include "mp_msg.h" |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
44 #include "help_mp.h" |
31985 | 45 #include "udp_sync.h" |
31982
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
46 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
47 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
48 // config options for UDP sync |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
49 int udp_master = 0; |
31985 | 50 int udp_slave = 0; |
51 int udp_port = 23867; | |
31982
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
52 const char *udp_ip = "127.0.0.1"; // where the master sends datagrams |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
53 // (can be a broadcast address) |
31985 | 54 float udp_seek_threshold = 1.0; // how far off before we seek |
31982
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
55 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
56 // remember where the master is in the file |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
57 static float udp_master_position = -1.0; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
58 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
59 // how far off is still considered equal |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
60 #define UDP_TIMING_TOLERANCE 0.02 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
61 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
62 // gets a datagram from the master with or without blocking. updates |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
63 // master_position if successful. if the master has exited, returns 1. |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
64 // otherwise, returns 0. |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
65 int get_udp(int blocking, float *master_position) |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
66 { |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
67 long sock_flags; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
68 struct sockaddr_in cliaddr; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
69 char mesg[100]; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
70 socklen_t len; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
71 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
72 int chars_received; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
73 int n; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
74 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
75 static int done_init_yet = 0; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
76 static int sockfd; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
77 if (!done_init_yet) { |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
78 struct timeval tv; |
31990
4111548d28f9
Use initializer instead of memset, the memset in addition never
reimar
parents:
31985
diff
changeset
|
79 struct sockaddr_in servaddr = { 0 }; |
31982
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
80 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
81 done_init_yet = 1; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
82 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
83 sockfd = socket(AF_INET, SOCK_DGRAM, 0); |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
84 |
31985 | 85 servaddr.sin_family = AF_INET; |
31982
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
86 servaddr.sin_addr.s_addr = htonl(INADDR_ANY); |
31985 | 87 servaddr.sin_port = htons(udp_port); |
31982
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
88 bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
89 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
90 tv.tv_sec = 30; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
91 setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
92 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
93 } |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
94 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
95 #if HAVE_WINSOCK2_H |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
96 sock_flags = blocking; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
97 ioctlsocket(sockfd, FIONBIO, &sock_flags); |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
98 #else |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
99 sock_flags = fcntl(sockfd, F_GETFL, 0); |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
100 sock_flags = blocking ? sock_flags & ~O_NONBLOCK : sock_flags | O_NONBLOCK; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
101 fcntl(sockfd, F_SETFL, sock_flags); |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
102 #endif /* HAVE_WINSOCK2_H */ |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
103 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
104 len = sizeof(cliaddr); |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
105 |
31985 | 106 chars_received = recvfrom(sockfd, mesg, sizeof(mesg)-1, 0, |
107 (struct sockaddr *)&cliaddr, &len); | |
31982
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
108 |
31991 | 109 if (chars_received == -1) |
31985 | 110 return 0; |
31982
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
111 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
112 #if HAVE_WINSOCK2_H |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
113 sock_flags = 0; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
114 ioctlsocket(sockfd, FIONBIO, &sock_flags); |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
115 #else |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
116 fcntl(sockfd, F_SETFL, sock_flags | O_NONBLOCK); |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
117 #endif |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
118 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
119 // flush out any further messages so we don't get behind |
31985 | 120 while (-1 != (n = recvfrom(sockfd, mesg, sizeof(mesg)-1, 0, |
121 (struct sockaddr *)&cliaddr, &len))) { | |
31982
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
122 chars_received = n; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
123 mesg[chars_received] = 0; |
31991 | 124 if (strcmp(mesg, "bye") == 0) |
31985 | 125 return 1; |
31982
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
126 } |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
127 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
128 if (chars_received > -1) { |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
129 mesg[chars_received] = 0; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
130 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
131 if (strcmp(mesg, "bye") == 0) { |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
132 return 1; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
133 } else { |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
134 sscanf(mesg, "%f", master_position); |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
135 return 0; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
136 } |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
137 } else { |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
138 // UDP wait error, probably a timeout. Safe to ignore. |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
139 } |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
140 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
141 return 0; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
142 } |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
143 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
144 void send_udp(const char *send_to_ip, int port, char *mesg) |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
145 { |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
146 static int done_init_yet = 0; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
147 static int sockfd; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
148 static struct sockaddr_in socketinfo; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
149 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
150 int one = 1; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
151 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
152 if (!done_init_yet) { |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
153 int ip_valid = 0; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
154 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
155 done_init_yet = 1; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
156 |
31985 | 157 sockfd = socket(AF_INET, SOCK_DGRAM, 0); |
31982
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
158 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
159 // Enable broadcast |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
160 setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)); |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
161 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
162 #if HAVE_WINSOCK2_H |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
163 ip_valid = (inet_addr(send_to_ip) != INADDR_NONE); |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
164 #else |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
165 ip_valid = inet_aton(send_to_ip, &socketinfo.sin_addr); |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
166 #endif |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
167 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
168 if (!ip_valid) { |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
169 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_InvalidIP); |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
170 exit_player(EXIT_ERROR); |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
171 } |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
172 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
173 socketinfo.sin_family = AF_INET; |
31985 | 174 socketinfo.sin_port = htons(port); |
31982
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
175 } |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
176 |
31985 | 177 sendto(sockfd, mesg, strlen(mesg), 0, (struct sockaddr *) &socketinfo, |
178 sizeof(socketinfo)); | |
31982
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
179 } |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
180 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
181 // this function makes sure we stay as close as possible to the master's |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
182 // position. returns 1 if the master tells us to exit, 0 otherwise. |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
183 int udp_slave_sync(MPContext *mpctx) |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
184 { |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
185 // grab any waiting datagrams without blocking |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
186 int master_exited = get_udp(0, &udp_master_position); |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
187 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
188 while (!master_exited) { |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
189 float my_position = mpctx->sh_video->pts; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
190 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
191 // if we're way off, seek to catch up |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
192 if (FFABS(my_position - udp_master_position) > udp_seek_threshold) { |
31985 | 193 abs_seek_pos = SEEK_ABSOLUTE; |
31982
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
194 rel_seek_secs = udp_master_position; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
195 break; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
196 } |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
197 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
198 // normally we expect that the master will have just played the |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
199 // frame we're ready to play. break out and play it, and we'll be |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
200 // right in sync. |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
201 // or, the master might be up to a few seconds ahead of us, in |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
202 // which case we also want to play the current frame immediately, |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
203 // without waiting. |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
204 // UDP_TIMING_TOLERANCE is a small value that lets us consider |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
205 // the master equal to us even if it's very slightly ahead. |
31991 | 206 if (udp_master_position + UDP_TIMING_TOLERANCE > my_position) |
31992 | 207 break; |
31982
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
208 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
209 // the remaining case is that we're slightly ahead of the master. |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
210 // usually, it just means we called get_udp() before the datagram |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
211 // arrived. call get_udp again, but this time block until we receive |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
212 // a datagram. |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
213 master_exited = get_udp(1, &udp_master_position); |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
214 } |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
215 |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
216 return master_exited; |
184969a3a437
Add synchronization of multiple MPlayer instances over UDP.
reimar
parents:
diff
changeset
|
217 } |