| 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 |
#ifdef HAVE_CONFIG_H |
|---|
| 28 |
# include "config.h" |
|---|
| 29 |
#endif |
|---|
| 30 |
|
|---|
| 31 |
#include <vlc_common.h> |
|---|
| 32 |
#include <vlc_plugin.h> |
|---|
| 33 |
#include <vlc_interface.h> |
|---|
| 34 |
#include <vlc_input.h> |
|---|
| 35 |
#include <vlc_es_out.h> |
|---|
| 36 |
|
|---|
| 37 |
#ifdef HAVE_UNISTD_H |
|---|
| 38 |
# include <unistd.h> |
|---|
| 39 |
#endif |
|---|
| 40 |
#ifdef HAVE_SYS_TIME_H |
|---|
| 41 |
# include <sys/time.h> |
|---|
| 42 |
#endif |
|---|
| 43 |
#ifdef HAVE_SYS_TYPES_H |
|---|
| 44 |
# include <sys/types.h> |
|---|
| 45 |
#endif |
|---|
| 46 |
#ifdef HAVE_POLL |
|---|
| 47 |
# include <poll.h> |
|---|
| 48 |
#endif |
|---|
| 49 |
|
|---|
| 50 |
#include <vlc_network.h> |
|---|
| 51 |
|
|---|
| 52 |
#define NETSYNC_PORT 9875 |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
#include "../../src/input/input_internal.h" |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
static int Activate( vlc_object_t * ); |
|---|
| 61 |
static void Close ( vlc_object_t * ); |
|---|
| 62 |
|
|---|
| 63 |
static mtime_t GetClockRef( intf_thread_t *, mtime_t ); |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
#define NETSYNC_TEXT N_( "Act as master" ) |
|---|
| 67 |
#define NETSYNC_LONGTEXT N_( "Should " \ |
|---|
| 68 |
"act as the master client for the network synchronisation?" ) |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
#define MIP_TEXT N_( "Master client ip address" ) |
|---|
| 72 |
#define MIP_LONGTEXT N_( "IP address of " \ |
|---|
| 73 |
"the master client used for the network synchronisation." ) |
|---|
| 74 |
|
|---|
| 75 |
vlc_module_begin(); |
|---|
| 76 |
set_shortname( N_("Network Sync")); |
|---|
| 77 |
set_description( N_("Network synchronisation") ); |
|---|
| 78 |
set_category( CAT_ADVANCED ); |
|---|
| 79 |
set_subcategory( SUBCAT_ADVANCED_MISC ); |
|---|
| 80 |
|
|---|
| 81 |
add_bool( "netsync-master", 0, NULL, |
|---|
| 82 |
NETSYNC_TEXT, NETSYNC_LONGTEXT, true ); |
|---|
| 83 |
add_string( "netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT, |
|---|
| 84 |
true ); |
|---|
| 85 |
|
|---|
| 86 |
set_capability( "interface", 0 ); |
|---|
| 87 |
set_callbacks( Activate, Close ); |
|---|
| 88 |
vlc_module_end(); |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
static void Run( intf_thread_t *p_intf ); |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
static int Activate( vlc_object_t *p_this ) |
|---|
| 99 |
{ |
|---|
| 100 |
intf_thread_t *p_intf = (intf_thread_t*)p_this; |
|---|
| 101 |
int fd; |
|---|
| 102 |
|
|---|
| 103 |
if( config_GetInt( p_intf, "netsync-master" ) <= 0 ) |
|---|
| 104 |
{ |
|---|
| 105 |
char *psz_master = config_GetPsz( p_intf, "netsync-master-ip" ); |
|---|
| 106 |
if( psz_master == NULL ) |
|---|
| 107 |
{ |
|---|
| 108 |
msg_Err( p_intf, "master address not specified" ); |
|---|
| 109 |
return VLC_EGENERIC; |
|---|
| 110 |
} |
|---|
| 111 |
fd = net_ConnectUDP( VLC_OBJECT(p_intf), psz_master, NETSYNC_PORT, -1 ); |
|---|
| 112 |
free( psz_master ); |
|---|
| 113 |
} |
|---|
| 114 |
else |
|---|
| 115 |
fd = net_ListenUDP1( VLC_OBJECT(p_intf), NULL, NETSYNC_PORT ); |
|---|
| 116 |
|
|---|
| 117 |
if( fd == -1 ) |
|---|
| 118 |
{ |
|---|
| 119 |
msg_Err( p_intf, "Netsync socket failure" ); |
|---|
| 120 |
return VLC_EGENERIC; |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
p_intf->p_sys = (void *)(intptr_t)fd; |
|---|
| 124 |
p_intf->pf_run = Run; |
|---|
| 125 |
return VLC_SUCCESS; |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
void Close( vlc_object_t *p_this ) |
|---|
| 132 |
{ |
|---|
| 133 |
intf_thread_t *p_intf = (intf_thread_t*)p_this; |
|---|
| 134 |
|
|---|
| 135 |
net_Close( (intptr_t)p_intf->p_sys ); |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
static void Run( intf_thread_t *p_intf ) |
|---|
| 142 |
{ |
|---|
| 143 |
#define MAX_MSG_LENGTH (2 * sizeof(int64_t)) |
|---|
| 144 |
|
|---|
| 145 |
input_thread_t *p_input = NULL; |
|---|
| 146 |
char p_data[MAX_MSG_LENGTH]; |
|---|
| 147 |
int i_socket; |
|---|
| 148 |
int canc = vlc_savecancel(); |
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_INPUT ); |
|---|
| 152 |
|
|---|
| 153 |
while( vlc_object_alive( p_intf ) ) |
|---|
| 154 |
{ |
|---|
| 155 |
|
|---|
| 156 |
if( p_input == NULL ) |
|---|
| 157 |
p_input = |
|---|
| 158 |
(input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT, |
|---|
| 159 |
FIND_ANYWHERE ); |
|---|
| 160 |
else if( p_input->b_dead ) |
|---|
| 161 |
{ |
|---|
| 162 |
vlc_object_release( p_input ); |
|---|
| 163 |
p_input = NULL; |
|---|
| 164 |
} |
|---|
| 165 |
|
|---|
| 166 |
if( p_input == NULL ) |
|---|
| 167 |
{ |
|---|
| 168 |
|
|---|
| 169 |
msleep( INTF_IDLE_SLEEP ); |
|---|
| 170 |
continue; |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
struct pollfd ufd = { .fd = i_socket, .events = POLLIN, }; |
|---|
| 180 |
|
|---|
| 181 |
if( b_master ) |
|---|
| 182 |
{ |
|---|
| 183 |
struct sockaddr_storage from; |
|---|
| 184 |
mtime_t i_date, i_clockref, i_master_clockref; |
|---|
| 185 |
int i_struct_size, i_read, i_ret; |
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
i_ret = poll( &ufd, 1, 500 ); |
|---|
| 189 |
if( i_ret == 0 ) continue; |
|---|
| 190 |
if( i_ret < 0 ) |
|---|
| 191 |
{ |
|---|
| 192 |
|
|---|
| 193 |
msleep( INTF_IDLE_SLEEP ); |
|---|
| 194 |
continue; |
|---|
| 195 |
} |
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
i_struct_size = sizeof( from ); |
|---|
| 199 |
i_read = recvfrom( i_socket, p_data, MAX_MSG_LENGTH, 0, |
|---|
| 200 |
(struct sockaddr*)&from, |
|---|
| 201 |
(unsigned int *)&i_struct_size ); |
|---|
| 202 |
|
|---|
| 203 |
i_clockref = ntoh64(*(int64_t *)p_data); |
|---|
| 204 |
|
|---|
| 205 |
i_date = mdate(); |
|---|
| 206 |
*(int64_t *)p_data = hton64( i_date ); |
|---|
| 207 |
|
|---|
| 208 |
i_master_clockref = GetClockRef( p_intf, i_clockref ); |
|---|
| 209 |
*(((int64_t *)p_data)+1) = hton64( i_master_clockref ); |
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
sendto( i_socket, p_data, 2 * sizeof(int64_t), 0, |
|---|
| 213 |
(struct sockaddr *)&from, i_struct_size ); |
|---|
| 214 |
|
|---|
| 215 |
#if 0 |
|---|
| 216 |
msg_Dbg( p_intf, "Master clockref: %"PRId64" -> %"PRId64", from %s " |
|---|
| 217 |
"(date: %"PRId64")", i_clockref, i_master_clockref, |
|---|
| 218 |
from.ss_family == AF_INET |
|---|
| 219 |
? inet_ntoa(((struct sockaddr_in *)&from)->sin_addr) |
|---|
| 220 |
: "non-IPv4", i_date ); |
|---|
| 221 |
#endif |
|---|
| 222 |
} |
|---|
| 223 |
else |
|---|
| 224 |
{ |
|---|
| 225 |
mtime_t i_send_date, i_receive_date, i_master_date, i_diff_date; |
|---|
| 226 |
mtime_t i_master_clockref, i_client_clockref, i_drift; |
|---|
| 227 |
mtime_t i_clockref = 0; |
|---|
| 228 |
int i_sent, i_read, i_ret; |
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
*(int64_t *)p_data = hton64( i_clockref ); |
|---|
| 232 |
i_send_date = mdate(); |
|---|
| 233 |
|
|---|
| 234 |
i_sent = send( i_socket, p_data, sizeof(int64_t), 0 ); |
|---|
| 235 |
if( i_sent <= 0 ) |
|---|
| 236 |
{ |
|---|
| 237 |
|
|---|
| 238 |
msleep( INTF_IDLE_SLEEP ); |
|---|
| 239 |
continue; |
|---|
| 240 |
} |
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
i_ret = poll( &ufd, 1, 500 ); |
|---|
| 244 |
if( i_ret == 0 ) continue; |
|---|
| 245 |
if( i_ret < 0 ) |
|---|
| 246 |
{ |
|---|
| 247 |
|
|---|
| 248 |
msleep( INTF_IDLE_SLEEP ); |
|---|
| 249 |
continue; |
|---|
| 250 |
} |
|---|
| 251 |
|
|---|
| 252 |
i_receive_date = mdate(); |
|---|
| 253 |
|
|---|
| 254 |
i_read = recv( i_socket, p_data, MAX_MSG_LENGTH, 0 ); |
|---|
| 255 |
if( i_read <= 0 ) |
|---|
| 256 |
{ |
|---|
| 257 |
|
|---|
| 258 |
msleep( INTF_IDLE_SLEEP ); |
|---|
| 259 |
continue; |
|---|
| 260 |
} |
|---|
| 261 |
|
|---|
| 262 |
i_master_date = ntoh64(*(int64_t *)p_data); |
|---|
| 263 |
i_master_clockref = ntoh64(*(((int64_t *)p_data)+1)); |
|---|
| 264 |
|
|---|
| 265 |
i_diff_date = i_receive_date - |
|---|
| 266 |
((i_receive_date - i_send_date) / 2 + i_master_date); |
|---|
| 267 |
|
|---|
| 268 |
i_client_clockref = i_drift = 0; |
|---|
| 269 |
if( p_input && i_master_clockref ) |
|---|
| 270 |
{ |
|---|
| 271 |
i_client_clockref = GetClockRef( p_intf, i_clockref ); |
|---|
| 272 |
i_drift = i_client_clockref - i_master_clockref - i_diff_date; |
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
if( i_client_clockref ) |
|---|
| 276 |
p_input->i_pts_delay -= i_drift; |
|---|
| 277 |
} |
|---|
| 278 |
|
|---|
| 279 |
#if 0 |
|---|
| 280 |
msg_Dbg( p_intf, "Slave clockref: %"PRId64" -> %"PRId64" -> %"PRId64", " |
|---|
| 281 |
"clock diff: %"PRId64" drift: %"PRId64, |
|---|
| 282 |
i_clockref, i_master_clockref, |
|---|
| 283 |
i_client_clockref, i_diff_date, i_drift ); |
|---|
| 284 |
#endif |
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
msleep( INTF_IDLE_SLEEP ); |
|---|
| 288 |
} |
|---|
| 289 |
} |
|---|
| 290 |
|
|---|
| 291 |
if( p_input ) vlc_object_release( p_input ); |
|---|
| 292 |
vlc_restorecancel( canc ); |
|---|
| 293 |
} |
|---|
| 294 |
|
|---|
| 295 |
static mtime_t GetClockRef( intf_thread_t *p_intf, mtime_t i_pts ) |
|---|
| 296 |
{ |
|---|
| 297 |
input_thread_t *p_input = p_intf->p_sys->p_input; |
|---|
| 298 |
mtime_t i_ts; |
|---|
| 299 |
|
|---|
| 300 |
if( !p_input || !p_input->p->p_es_out ) return 0; |
|---|
| 301 |
|
|---|
| 302 |
if( es_out_Control( p_input->p->p_es_out, ES_OUT_GET_TS, i_pts, &i_ts ) == |
|---|
| 303 |
VLC_SUCCESS ) |
|---|
| 304 |
{ |
|---|
| 305 |
return i_ts; |
|---|
| 306 |
} |
|---|
| 307 |
|
|---|
| 308 |
return 0; |
|---|
| 309 |
} |
|---|