From 76b3a61eb75a2577acde7dd74d9d12648c3937a0 Mon Sep 17 00:00:00 2001 From: Scott Lamb Date: Fri, 20 Aug 2021 10:10:34 -0700 Subject: [PATCH] mp4 example: fix panic on empty video ``` thread 'main' panicked at 'range start index 1 out of range for slice of length 0', examples/client/mp4.rs:171:44 stack backtrace: 0: rust_begin_unwind at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panicking.rs:515:5 1: core::panicking::panic_fmt at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/panicking.rs:92:14 2: core::slice::index::slice_start_index_len_fail at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/slice/index.rs:34:5 3: client::mp4::TrakTracker::write_common_stbl_parts 4: client::mp4::Mp4Writer::write_audio_trak 5: client::mp4::run::{{closure}} 6: as core::future::future::Future>::poll 7: tokio::park::thread::CachedParkThread::block_on 8: tokio::runtime::thread_pool::ThreadPool::block_on 9: tokio::runtime::Runtime::block_on 10: client::main note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. ``` --- examples/client/mp4.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/client/mp4.rs b/examples/client/mp4.rs index 799318d..5e3dee4 100644 --- a/examples/client/mp4.rs +++ b/examples/client/mp4.rs @@ -168,14 +168,14 @@ impl TrakTracker { buf.put_u32(u32::try_from(self.chunks.len())?); let mut prev_sample_number = 1; let mut chunk_number = 1; - for &(sample_number, _pos) in &self.chunks[1..] { - buf.put_u32(chunk_number); - buf.put_u32(sample_number - prev_sample_number); - buf.put_u32(1); // sample_description_index - prev_sample_number = sample_number; - chunk_number += 1; - } if !self.chunks.is_empty() { + for &(sample_number, _pos) in &self.chunks[1..] { + buf.put_u32(chunk_number); + buf.put_u32(sample_number - prev_sample_number); + buf.put_u32(1); // sample_description_index + prev_sample_number = sample_number; + chunk_number += 1; + } buf.put_u32(chunk_number); buf.put_u32(self.samples + 1 - prev_sample_number); buf.put_u32(1); // sample_description_index