|
1 | 1 | use std::io::{BufRead, BufReader}; |
2 | 2 | use std::net::{TcpListener, TcpStream}; |
3 | 3 | use std::num::ParseIntError; |
4 | | -use std::process; |
5 | 4 | use std::sync::atomic::{AtomicUsize, Ordering}; |
| 5 | +use std::thread::sleep; |
| 6 | +use std::time::{Duration, SystemTime}; |
6 | 7 |
|
7 | 8 | use stoppable_thread::StoppableHandle; |
| 9 | + |
| 10 | +use cli::cli_util::error_with_ack; |
8 | 11 | use ffmpeg::report_files::capture_group; |
9 | 12 |
|
10 | | -static LOCALHOST: &str = "127.0.0.1"; |
| 13 | +static LOCALHOST: &str = "localhost"; |
11 | 14 | static PORT: &str = "1234"; |
12 | 15 |
|
13 | 16 | pub fn start_listening_to_ffmpeg_stats(verbose: bool, frame: &'static AtomicUsize, previous_frame: &'static AtomicUsize) -> StoppableHandle<()> { |
14 | 17 | let stat_listener = TcpListener::bind(format!("{}:{}", LOCALHOST, PORT)).unwrap(); |
| 18 | + // important so that this thread doesn't just hang here |
| 19 | + stat_listener.set_nonblocking(true).expect("Unable to set non-blocking for tcp listener, listener might block..."); |
15 | 20 |
|
16 | 21 | let tcp_reading_thread; |
17 | | - match stat_listener.accept() { |
18 | | - Ok(client) => { |
19 | | - if verbose { |
20 | | - println!("Connected to ffmpeg's -progress output via TCP..."); |
21 | | - } |
22 | 22 |
|
23 | | - tcp_reading_thread = spawn_tcp_reading_thread(client.0, frame, previous_frame); |
| 23 | + let listen_start_time = SystemTime::now(); |
| 24 | + let allowed_elapsed_time = Duration::from_secs(10); |
| 25 | + |
| 26 | + loop { |
| 27 | + if listen_start_time.elapsed().unwrap() > allowed_elapsed_time { |
| 28 | + println!("Unable to connect to ffmpeg output for {} seconds, either ffmpeg didn't start correctly or the tcp connection: {}:{} could not be created...", allowed_elapsed_time.as_secs(), LOCALHOST, PORT); |
| 29 | + error_with_ack(true); |
24 | 30 | } |
25 | | - // probably log this error eventually |
26 | | - Err(_e) => { |
27 | | - println!("Not able to connect to client for reading stats, cannot proceed"); |
28 | | - process::exit(1); |
| 31 | + |
| 32 | + // will try to connect for 10 seconds |
| 33 | + match stat_listener.accept() { |
| 34 | + Ok(client) => { |
| 35 | + if verbose { |
| 36 | + println!("Connected to ffmpeg's -progress output via TCP..."); |
| 37 | + } |
| 38 | + |
| 39 | + // making received client non-blocking, otherwise it dies pretty quick |
| 40 | + client.0.set_nonblocking(false).unwrap(); |
| 41 | + tcp_reading_thread = spawn_tcp_reading_thread(client.0, frame, previous_frame); |
| 42 | + break; |
| 43 | + } |
| 44 | + // probably log this error eventually |
| 45 | + Err(_e) => { |
| 46 | + if verbose { |
| 47 | + println!("Not able to connect to ffmpeg stat output, will try again..."); |
| 48 | + sleep(Duration::from_secs(1)); |
| 49 | + } |
| 50 | + } |
29 | 51 | } |
30 | 52 | } |
31 | 53 |
|
32 | | - // eventually we'll want to add code where we kill the listener here |
33 | | - |
34 | 54 | return tcp_reading_thread; |
35 | 55 | } |
36 | 56 |
|
|
0 commit comments