Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions application/xiu/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,7 @@ impl Service {
let mut hls_remuxer = HlsRemuxer::new(
cient_event_consumer,
event_producer,
hls_cfg_value.need_record,
hls_cfg_value.path.clone(),
hls_cfg_value.fragment,
hls_cfg_value.aof_ratio,
self.cfg.hls.clone(),
);

tokio::spawn(async move {
Expand Down
2 changes: 2 additions & 0 deletions library/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl Config {
path: None,
fragment: None,
aof_ratio: None,
live_ts_count: None,
});
}

Expand Down Expand Up @@ -154,6 +155,7 @@ pub struct HlsConfig {
pub path: Option<String>,
pub fragment: Option<i64>,
pub aof_ratio: Option<i64>,
pub live_ts_count: Option<usize>,
}

pub enum LogLevel {
Expand Down
1 change: 1 addition & 0 deletions protocol/hls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ streamhub = { path = "../../library/streamhub/" }
xmpegts = { path = "../../library/container/mpegts/" }
xflv = { path = "../../library/container/flv/" }
commonlib = { path = "../../library/common/" }
config = { path = "../../library/config/" }

[dependencies.tokio]
version = "1.4.0"
Expand Down
25 changes: 15 additions & 10 deletions protocol/hls/src/flv2hls.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use {
super::{define::FlvDemuxerData, errors::MediaError, m3u8::M3u8},
bytes::BytesMut,
xflv::{
super::{define::FlvDemuxerData, errors::MediaError, m3u8::M3u8}, bytes::BytesMut, config::HlsConfig, xflv::{
define::{frame_type, FlvData},
demuxer::{FlvAudioTagDemuxer, FlvVideoTagDemuxer},
},
streamhub::{define::{StreamHubEventSender, StreamHubEvent}, stream::StreamIdentifier},
Comment on lines +2 to 6
Copy link

Copilot AI Sep 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The imports are formatted on a single line making them hard to read. Consider breaking this into multiple lines for better readability.

Suggested change
super::{define::FlvDemuxerData, errors::MediaError, m3u8::M3u8}, bytes::BytesMut, config::HlsConfig, xflv::{
define::{frame_type, FlvData},
demuxer::{FlvAudioTagDemuxer, FlvVideoTagDemuxer},
},
streamhub::{define::{StreamHubEventSender, StreamHubEvent}, stream::StreamIdentifier},
super::{define::FlvDemuxerData, errors::MediaError, m3u8::M3u8},
bytes::BytesMut,
config::HlsConfig,
xflv::{
define::{frame_type, FlvData},
demuxer::{FlvAudioTagDemuxer, FlvVideoTagDemuxer},
},
streamhub::{
define::{StreamHubEventSender, StreamHubEvent},
stream::StreamIdentifier,
},

Copilot uses AI. Check for mistakes.
xmpegts::{
define::{epsi_stream_type, MPEG_FLAG_IDR_FRAME},
ts::TsMuxer,
},
}
};

pub struct Flv2HlsRemuxer {
Expand Down Expand Up @@ -40,13 +38,10 @@ pub struct Flv2HlsRemuxer {

impl Flv2HlsRemuxer {
pub fn new(
duration: i64,
app_name: String,
stream_name: String,
need_record: bool,
hls_config: Option<HlsConfig>,
event_producer: Option<StreamHubEventSender>,
path: String,
aof_ratio: i64,
) -> Self {
let mut ts_muxer = TsMuxer::new();
let audio_pid = ts_muxer
Expand All @@ -55,7 +50,17 @@ impl Flv2HlsRemuxer {
let video_pid = ts_muxer
.add_stream(epsi_stream_type::PSI_STREAM_H264, BytesMut::new())
.unwrap();


let duration = hls_config
.as_ref()
.and_then(|config| config.fragment)
.unwrap_or(5);

let aof_ratio = hls_config
.as_ref()
.and_then(|config| config.aof_ratio)
.unwrap_or(5);

Self {
video_demuxer: FlvVideoTagDemuxer::new(),
audio_demuxer: FlvAudioTagDemuxer::new(),
Expand All @@ -74,7 +79,7 @@ impl Flv2HlsRemuxer {
video_pid,
audio_pid,

m3u8_handler: M3u8::new(duration, 6, app_name.clone(), stream_name.clone(), need_record, path),
m3u8_handler: M3u8::new(duration, app_name.clone(), stream_name.clone(), hls_config),
event_producer,
app_name,
stream_name,
Expand Down
8 changes: 3 additions & 5 deletions protocol/hls/src/flv_data_receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use {
errors::{HlsError, HlsErrorValue},
flv2hls::Flv2HlsRemuxer,
},
config::HlsConfig,
std::time::Duration,
streamhub::{
define::{
Expand Down Expand Up @@ -34,10 +35,7 @@ impl FlvDataReceiver {
app_name: String,
stream_name: String,
event_producer: StreamHubEventSender,
duration: i64,
need_record: bool,
path: String,
aof_ratio: i64,
hls_config: Option<HlsConfig>,
) -> Self {
let (_, data_consumer) = mpsc::unbounded_channel();
let subscriber_id = Uuid::new(RandomDigitCount::Four);
Expand All @@ -47,7 +45,7 @@ impl FlvDataReceiver {
stream_name: stream_name.clone(),
data_consumer,
event_producer: event_producer.clone(),
media_processor: Flv2HlsRemuxer::new(duration, app_name, stream_name, need_record, Some(event_producer), path, aof_ratio),
media_processor: Flv2HlsRemuxer::new(app_name, stream_name, hls_config, Some(event_producer)),
subscriber_id,
}
}
Expand Down
23 changes: 19 additions & 4 deletions protocol/hls/src/m3u8.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {
super::{errors::MediaError, ts::Ts},
config::HlsConfig,
bytes::BytesMut,
std::{collections::VecDeque, fs, fs::File, io::Write},
streamhub::define::Segment,
Expand Down Expand Up @@ -32,16 +33,30 @@ pub struct M3u8 {
impl M3u8 {
pub fn new(
duration: i64,
live_ts_count: usize,
app_name: String,
stream_name: String,
need_record: bool,
path: String,
hls_config: Option<HlsConfig>,
) -> Self {

let path = hls_config
.as_ref()
.and_then(|config| config.path.clone())
.unwrap_or("./".to_string());

let m3u8_folder = format!("{path}{app_name}/{stream_name}");
fs::create_dir_all(m3u8_folder.clone()).unwrap();

let live_m3u8_name = format!("{stream_name}.m3u8");

let need_record = hls_config
.as_ref()
.and_then(|config| Some(config.need_record))
Copy link

Copilot AI Sep 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .and_then(|config| Some(config.need_record)) pattern is unnecessarily complex. Since need_record is a boolean field, you can simplify this to .map(|config| config.need_record) or access it directly if the field exists.

Suggested change
.and_then(|config| Some(config.need_record))
.map(|config| config.need_record)

Copilot uses AI. Check for mistakes.
.unwrap_or(false);

let live_ts_count = hls_config
.as_ref()
.and_then(|config| config.live_ts_count)
.unwrap_or(6);

let vod_m3u8_name = if need_record {
format!("vod_{stream_name}.m3u8")
} else {
Expand Down
21 changes: 5 additions & 16 deletions protocol/hls/src/remuxer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {
super::{errors::HlsError, flv_data_receiver::FlvDataReceiver},
config::HlsConfig,
streamhub::{
define::{BroadcastEvent, BroadcastEventReceiver, StreamHubEventSender},
stream::StreamIdentifier,
Expand All @@ -9,28 +10,19 @@ use {
pub struct HlsRemuxer {
client_event_consumer: BroadcastEventReceiver,
event_producer: StreamHubEventSender,
need_record: bool,
path: String,
fragment: i64,
aof_ratio: i64,
hls_config: Option<HlsConfig>,
}

impl HlsRemuxer {
pub fn new(
consumer: BroadcastEventReceiver,
event_producer: StreamHubEventSender,
need_record: bool,
path: Option<String>,
fragment: Option<i64>,
aof_ratio: Option<i64>,
hls_config: Option<HlsConfig>
) -> Self {
Self {
client_event_consumer: consumer,
event_producer,
need_record,
path: path.unwrap_or("./".to_string()),
fragment: fragment.unwrap_or(2),
aof_ratio: aof_ratio.unwrap_or(1),
hls_config,
}
}

Expand All @@ -48,10 +40,7 @@ impl HlsRemuxer {
app_name,
stream_name,
self.event_producer.clone(),
self.fragment,
self.need_record,
self.path.clone(),
self.aof_ratio,
self.hls_config.clone(),
);

tokio::spawn(async move {
Expand Down
2 changes: 1 addition & 1 deletion protocol/hls/src/test_flv2hls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ mod tests {

let start = Instant::now();
let mut media_demuxer =
Flv2HlsRemuxer::new(5, String::from("live"), String::from("test"), false, None, String::from("./"), 1);
Flv2HlsRemuxer::new(String::from("live"), String::from("test"), None, None);

loop {
let data_ = demuxer.read_flv_tag();
Expand Down