Skip to content

Commit 9ca93b3

Browse files
authored
Merge pull request #177 from nikitos/hls_params3
Use config struct to pass hls params
2 parents 97c7ba7 + f441670 commit 9ca93b3

File tree

9 files changed

+48
-40
lines changed

9 files changed

+48
-40
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

application/xiu/src/service.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,7 @@ impl Service {
333333
let mut hls_remuxer = HlsRemuxer::new(
334334
cient_event_consumer,
335335
event_producer,
336-
hls_cfg_value.need_record,
337-
hls_cfg_value.path.clone(),
338-
hls_cfg_value.fragment,
339-
hls_cfg_value.aof_ratio,
336+
self.cfg.hls.clone(),
340337
);
341338

342339
tokio::spawn(async move {

library/config/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ impl Config {
7878
path: None,
7979
fragment: None,
8080
aof_ratio: None,
81+
live_ts_count: None,
8182
});
8283
}
8384

@@ -154,6 +155,7 @@ pub struct HlsConfig {
154155
pub path: Option<String>,
155156
pub fragment: Option<i64>,
156157
pub aof_ratio: Option<i64>,
158+
pub live_ts_count: Option<usize>,
157159
}
158160

159161
pub enum LogLevel {

protocol/hls/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ streamhub = { path = "../../library/streamhub/" }
2222
xmpegts = { path = "../../library/container/mpegts/" }
2323
xflv = { path = "../../library/container/flv/" }
2424
commonlib = { path = "../../library/common/" }
25+
config = { path = "../../library/config/" }
2526

2627
[dependencies.tokio]
2728
version = "1.4.0"

protocol/hls/src/flv2hls.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
use {
2-
super::{define::FlvDemuxerData, errors::MediaError, m3u8::M3u8},
3-
bytes::BytesMut,
4-
xflv::{
2+
super::{define::FlvDemuxerData, errors::MediaError, m3u8::M3u8}, bytes::BytesMut, config::HlsConfig, xflv::{
53
define::{frame_type, FlvData},
64
demuxer::{FlvAudioTagDemuxer, FlvVideoTagDemuxer},
75
},
86
streamhub::{define::{StreamHubEventSender, StreamHubEvent}, stream::StreamIdentifier},
97
xmpegts::{
108
define::{epsi_stream_type, MPEG_FLAG_IDR_FRAME},
119
ts::TsMuxer,
12-
},
10+
}
1311
};
1412

1513
pub struct Flv2HlsRemuxer {
@@ -40,13 +38,10 @@ pub struct Flv2HlsRemuxer {
4038

4139
impl Flv2HlsRemuxer {
4240
pub fn new(
43-
duration: i64,
4441
app_name: String,
4542
stream_name: String,
46-
need_record: bool,
43+
hls_config: Option<HlsConfig>,
4744
event_producer: Option<StreamHubEventSender>,
48-
path: String,
49-
aof_ratio: i64,
5045
) -> Self {
5146
let mut ts_muxer = TsMuxer::new();
5247
let audio_pid = ts_muxer
@@ -55,7 +50,17 @@ impl Flv2HlsRemuxer {
5550
let video_pid = ts_muxer
5651
.add_stream(epsi_stream_type::PSI_STREAM_H264, BytesMut::new())
5752
.unwrap();
58-
53+
54+
let duration = hls_config
55+
.as_ref()
56+
.and_then(|config| config.fragment)
57+
.unwrap_or(5);
58+
59+
let aof_ratio = hls_config
60+
.as_ref()
61+
.and_then(|config| config.aof_ratio)
62+
.unwrap_or(5);
63+
5964
Self {
6065
video_demuxer: FlvVideoTagDemuxer::new(),
6166
audio_demuxer: FlvAudioTagDemuxer::new(),
@@ -74,7 +79,7 @@ impl Flv2HlsRemuxer {
7479
video_pid,
7580
audio_pid,
7681

77-
m3u8_handler: M3u8::new(duration, 6, app_name.clone(), stream_name.clone(), need_record, path),
82+
m3u8_handler: M3u8::new(duration, app_name.clone(), stream_name.clone(), hls_config),
7883
event_producer,
7984
app_name,
8085
stream_name,

protocol/hls/src/flv_data_receiver.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use {
55
errors::{HlsError, HlsErrorValue},
66
flv2hls::Flv2HlsRemuxer,
77
},
8+
config::HlsConfig,
89
std::time::Duration,
910
streamhub::{
1011
define::{
@@ -34,10 +35,7 @@ impl FlvDataReceiver {
3435
app_name: String,
3536
stream_name: String,
3637
event_producer: StreamHubEventSender,
37-
duration: i64,
38-
need_record: bool,
39-
path: String,
40-
aof_ratio: i64,
38+
hls_config: Option<HlsConfig>,
4139
) -> Self {
4240
let (_, data_consumer) = mpsc::unbounded_channel();
4341
let subscriber_id = Uuid::new(RandomDigitCount::Four);
@@ -47,7 +45,7 @@ impl FlvDataReceiver {
4745
stream_name: stream_name.clone(),
4846
data_consumer,
4947
event_producer: event_producer.clone(),
50-
media_processor: Flv2HlsRemuxer::new(duration, app_name, stream_name, need_record, Some(event_producer), path, aof_ratio),
48+
media_processor: Flv2HlsRemuxer::new(app_name, stream_name, hls_config, Some(event_producer)),
5149
subscriber_id,
5250
}
5351
}

protocol/hls/src/m3u8.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use {
22
super::{errors::MediaError, ts::Ts},
3+
config::HlsConfig,
34
bytes::BytesMut,
45
std::{collections::VecDeque, fs, fs::File, io::Write},
56
streamhub::define::Segment,
@@ -32,16 +33,30 @@ pub struct M3u8 {
3233
impl M3u8 {
3334
pub fn new(
3435
duration: i64,
35-
live_ts_count: usize,
3636
app_name: String,
3737
stream_name: String,
38-
need_record: bool,
39-
path: String,
38+
hls_config: Option<HlsConfig>,
4039
) -> Self {
40+
41+
let path = hls_config
42+
.as_ref()
43+
.and_then(|config| config.path.clone())
44+
.unwrap_or("./".to_string());
45+
4146
let m3u8_folder = format!("{path}{app_name}/{stream_name}");
4247
fs::create_dir_all(m3u8_folder.clone()).unwrap();
43-
4448
let live_m3u8_name = format!("{stream_name}.m3u8");
49+
50+
let need_record = hls_config
51+
.as_ref()
52+
.and_then(|config| Some(config.need_record))
53+
.unwrap_or(false);
54+
55+
let live_ts_count = hls_config
56+
.as_ref()
57+
.and_then(|config| config.live_ts_count)
58+
.unwrap_or(6);
59+
4560
let vod_m3u8_name = if need_record {
4661
format!("vod_{stream_name}.m3u8")
4762
} else {

protocol/hls/src/remuxer.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use {
22
super::{errors::HlsError, flv_data_receiver::FlvDataReceiver},
3+
config::HlsConfig,
34
streamhub::{
45
define::{BroadcastEvent, BroadcastEventReceiver, StreamHubEventSender},
56
stream::StreamIdentifier,
@@ -9,28 +10,19 @@ use {
910
pub struct HlsRemuxer {
1011
client_event_consumer: BroadcastEventReceiver,
1112
event_producer: StreamHubEventSender,
12-
need_record: bool,
13-
path: String,
14-
fragment: i64,
15-
aof_ratio: i64,
13+
hls_config: Option<HlsConfig>,
1614
}
1715

1816
impl HlsRemuxer {
1917
pub fn new(
2018
consumer: BroadcastEventReceiver,
2119
event_producer: StreamHubEventSender,
22-
need_record: bool,
23-
path: Option<String>,
24-
fragment: Option<i64>,
25-
aof_ratio: Option<i64>,
20+
hls_config: Option<HlsConfig>
2621
) -> Self {
2722
Self {
2823
client_event_consumer: consumer,
2924
event_producer,
30-
need_record,
31-
path: path.unwrap_or("./".to_string()),
32-
fragment: fragment.unwrap_or(2),
33-
aof_ratio: aof_ratio.unwrap_or(1),
25+
hls_config,
3426
}
3527
}
3628

@@ -48,10 +40,7 @@ impl HlsRemuxer {
4840
app_name,
4941
stream_name,
5042
self.event_producer.clone(),
51-
self.fragment,
52-
self.need_record,
53-
self.path.clone(),
54-
self.aof_ratio,
43+
self.hls_config.clone(),
5544
);
5645

5746
tokio::spawn(async move {

protocol/hls/src/test_flv2hls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ mod tests {
6565

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

7070
loop {
7171
let data_ = demuxer.read_flv_tag();

0 commit comments

Comments
 (0)