streamtop 1.5.0

Terminal HLS, DASH, and IPTV stream monitor with wire probes, TR 101 290, SCTE-35, and Prometheus metrics
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! IPTV lineups and JSON/YAML channel catalogs.

use std::path::{Path, PathBuf};

use color_eyre::eyre::{eyre, Result, WrapErr};
use serde::Deserialize;
use url::Url;

use crate::engine::dash::looks_like_dash;
use crate::models::ChannelEntry;

/// Result of detecting and parsing an input URL/file body.
#[derive(Debug, Clone)]
pub enum ParsedInput {
    /// HLS master/media or DASH MPD - open diagnostics for this URL.
    SingleStream { origin: String, url: String },
    /// Channel lineup → Channel Picker.
    IptvChannels {
        origin: String,
        channels: Vec<ChannelEntry>,
    },
}

#[derive(Debug, Deserialize)]
struct CatalogItem {
    #[serde(default)]
    name: Option<String>,
    #[serde(default)]
    title: Option<String>,
    #[serde(default)]
    url: Option<String>,
    #[serde(default)]
    stream: Option<String>,
    #[serde(default)]
    uri: Option<String>,
    #[serde(default)]
    group: Option<String>,
    #[serde(default)]
    category: Option<String>,
    #[serde(default)]
    logo: Option<String>,
    #[serde(default)]
    tvg_id: Option<String>,
}

#[derive(Debug, Deserialize)]
struct CatalogRoot {
    #[serde(default)]
    channels: Vec<CatalogItem>,
}

pub fn looks_like_remote_url(input: &str) -> bool {
    let t = input.trim();
    t.starts_with("http://") || t.starts_with("https://")
}

pub fn path_to_file_url(path: &Path) -> Result<String> {
    let abs = if path.is_absolute() {
        path.to_path_buf()
    } else {
        std::env::current_dir().wrap_err("cwd")?.join(path)
    };
    Url::from_file_path(&abs)
        .map(|u| u.to_string())
        .map_err(|()| eyre!("cannot convert path to file URL: {}", abs.display()))
}

/// Classify input bytes (IPTV / HLS / DASH / catalog).
pub fn detect_and_parse(
    origin: &str,
    body: &[u8],
    content_type: Option<&str>,
) -> Result<ParsedInput> {
    classify_source(origin, body, content_type)
}

pub fn classify_source(
    origin: &str,
    body: &[u8],
    content_type: Option<&str>,
) -> Result<ParsedInput> {
    let text = String::from_utf8_lossy(body);
    let trimmed = text.trim_start_matches('\u{feff}').trim_start();
    let lower_origin = origin.to_ascii_lowercase();
    let path_hint = origin_path_hint(origin);

    if trimmed.starts_with('#') || trimmed.contains("#EXT") {
        return classify_ext_playlist(origin, trimmed);
    }

    if looks_like_dash(origin, body, content_type)
        || trimmed.contains("<MPD")
        || trimmed.contains("<mpd")
    {
        return Ok(ParsedInput::SingleStream {
            origin: origin.to_string(),
            url: origin.to_string(),
        });
    }

    if is_json_hint(&lower_origin, path_hint.as_deref(), trimmed) {
        let channels = parse_json_catalog(trimmed, origin)?;
        return Ok(ParsedInput::IptvChannels {
            origin: origin.to_string(),
            channels,
        });
    }

    if is_yaml_hint(&lower_origin, path_hint.as_deref(), trimmed) {
        let channels = parse_yaml_catalog(trimmed, origin)?;
        return Ok(ParsedInput::IptvChannels {
            origin: origin.to_string(),
            channels,
        });
    }

    if trimmed.starts_with('{') || trimmed.starts_with('[') {
        let channels = parse_json_catalog(trimmed, origin)?;
        return Ok(ParsedInput::IptvChannels {
            origin: origin.to_string(),
            channels,
        });
    }

    Ok(ParsedInput::SingleStream {
        origin: origin.to_string(),
        url: origin.to_string(),
    })
}

fn classify_ext_playlist(origin: &str, text: &str) -> Result<ParsedInput> {
    if is_iptv_channel_list(text) {
        let channels = parse_m3u_channels(text, origin);
        if channels.is_empty() {
            return Err(eyre!("M3U lineup contains no playable channel URLs"));
        }
        return Ok(ParsedInput::IptvChannels {
            origin: origin.to_string(),
            channels,
        });
    }

    if text.contains("#EXT-X-STREAM-INF") {
        return Ok(ParsedInput::SingleStream {
            origin: origin.to_string(),
            url: origin.to_string(),
        });
    }

    if is_hls_media_playlist(text) {
        return Ok(ParsedInput::SingleStream {
            origin: origin.to_string(),
            url: origin.to_string(),
        });
    }

    if text.contains("#EXTINF") {
        let channels = parse_m3u_channels(text, origin);
        if !channels.is_empty() {
            return Ok(ParsedInput::IptvChannels {
                origin: origin.to_string(),
                channels,
            });
        }
    }

    Ok(ParsedInput::SingleStream {
        origin: origin.to_string(),
        url: origin.to_string(),
    })
}

/// True when body looks like an IPTV channel list (not HLS media).
pub fn is_iptv_channel_list(text: &str) -> bool {
    if !text.contains("#EXTINF") {
        return false;
    }
    if text.contains("#EXT-X-STREAM-INF") {
        return false;
    }
    if text.contains("#EXT-X-TARGETDURATION") {
        return false;
    }
    if text.contains("#EXT-X-MEDIA-SEQUENCE")
        || text.contains("#EXT-X-MAP:")
        || text.contains("#EXT-X-PART:")
        || text.contains("#EXT-X-SERVER-CONTROL")
        || text.contains("#EXT-X-PLAYLIST-TYPE")
    {
        return false;
    }
    let _has_iptv_attrs = text.contains("tvg-name")
        || text.contains("tvg-id")
        || text.contains("group-title")
        || text.contains("tvg-logo");
    true
}

fn is_hls_media_playlist(text: &str) -> bool {
    text.contains("#EXT-X-TARGETDURATION")
        || text.contains("#EXT-X-MEDIA-SEQUENCE")
        || text.contains("#EXT-X-MAP:")
        || text.contains("#EXT-X-PART:")
        || text.contains("#EXT-X-SERVER-CONTROL")
        || text.contains("#EXT-X-PLAYLIST-TYPE")
}

fn origin_path_hint(origin: &str) -> Option<String> {
    if let Ok(u) = Url::parse(origin) {
        return u
            .path_segments()
            .and_then(|mut s| s.next_back())
            .map(str::to_ascii_lowercase);
    }
    Path::new(origin)
        .file_name()
        .and_then(|s| s.to_str())
        .map(str::to_ascii_lowercase)
}

fn is_json_hint(origin: &str, file: Option<&str>, body: &str) -> bool {
    origin.contains(".json")
        || file.is_some_and(|f| {
            std::path::Path::new(f)
                .extension()
                .is_some_and(|ext| ext.eq_ignore_ascii_case("json"))
        })
        || ((body.starts_with('{') || body.starts_with('[')) && !body.contains("#EXT"))
}

fn is_yaml_hint(origin: &str, file: Option<&str>, body: &str) -> bool {
    origin.contains(".yaml")
        || origin.contains(".yml")
        || file.is_some_and(|f| {
            std::path::Path::new(f).extension().is_some_and(|ext| {
                ext.eq_ignore_ascii_case("yaml") || ext.eq_ignore_ascii_case("yml")
            })
        })
        || body.starts_with("---")
}

/// Parse IPTV M3U into channel entries.
pub fn parse_m3u_channels(text: &str, base: &str) -> Vec<ChannelEntry> {
    let base_url = Url::parse(base).ok();
    let mut channels = Vec::new();
    #[allow(clippy::type_complexity)]
    let mut pending: Option<(String, Option<String>, Option<String>, Option<String>)> = None;

    for raw in text.lines() {
        let line = raw.trim();
        if line.is_empty() || line.starts_with("#EXTM3U") {
            continue;
        }
        if line.starts_with("#EXTINF") {
            pending = Some(parse_extinf(line));
            continue;
        }
        if line.starts_with('#') {
            continue;
        }
        let url = resolve_channel_url(line, base_url.as_ref());
        if url.is_empty() {
            continue;
        }
        let (name, group, logo, tvg_id) = pending
            .take()
            .unwrap_or_else(|| (format!("Channel {}", channels.len() + 1), None, None, None));
        channels.push(ChannelEntry {
            name,
            url,
            group,
            logo,
            tvg_id,
        });
    }

    channels
}

fn parse_extinf(line: &str) -> (String, Option<String>, Option<String>, Option<String>) {
    let rest = line.strip_prefix("#EXTINF:").unwrap_or(line);
    let (attrs_part, display) = match rest.rsplit_once(',') {
        Some((a, d)) => (a, d.trim()),
        None => (rest, ""),
    };

    let tvg_name = attr(attrs_part, "tvg-name");
    let group = attr(attrs_part, "group-title");
    let logo = attr(attrs_part, "tvg-logo");
    let tvg_id = attr(attrs_part, "tvg-id");

    let name = tvg_name
        .filter(|s| !s.is_empty())
        .or_else(|| {
            if display.is_empty() {
                None
            } else {
                Some(display.to_string())
            }
        })
        .unwrap_or_else(|| "Untitled".into());

    (name, group, logo, tvg_id)
}

fn attr(hay: &str, key: &str) -> Option<String> {
    let needle = format!("{key}=");
    let idx = hay.find(&needle)?;
    let after = &hay[idx + needle.len()..];
    if let Some(rest) = after.strip_prefix('"') {
        let end = rest.find('"')?;
        return Some(rest[..end].to_string());
    }
    if let Some(rest) = after.strip_prefix('\'') {
        let end = rest.find('\'')?;
        return Some(rest[..end].to_string());
    }
    let end = after
        .find(|c: char| c.is_whitespace() || c == ',')
        .unwrap_or(after.len());
    Some(after[..end].to_string())
}

fn parse_json_catalog(text: &str, base: &str) -> Result<Vec<ChannelEntry>> {
    if let Ok(items) = serde_json::from_str::<Vec<CatalogItem>>(text) {
        return items_to_channels(items, base);
    }
    if let Ok(root) = serde_json::from_str::<CatalogRoot>(text) {
        if !root.channels.is_empty() {
            return items_to_channels(root.channels, base);
        }
    }
    if let Ok(item) = serde_json::from_str::<CatalogItem>(text) {
        return items_to_channels(vec![item], base);
    }
    Err(eyre!("JSON channel catalog parse error"))
}

fn parse_yaml_catalog(text: &str, base: &str) -> Result<Vec<ChannelEntry>> {
    if let Ok(items) = serde_yaml::from_str::<Vec<CatalogItem>>(text) {
        return items_to_channels(items, base);
    }
    if let Ok(root) = serde_yaml::from_str::<CatalogRoot>(text) {
        if !root.channels.is_empty() {
            return items_to_channels(root.channels, base);
        }
    }
    Err(eyre!("YAML channel catalog parse error"))
}

fn items_to_channels(items: Vec<CatalogItem>, base: &str) -> Result<Vec<ChannelEntry>> {
    let base_url = Url::parse(base).ok();
    let mut channels = Vec::new();
    for item in items {
        let url_raw = item
            .url
            .or(item.stream)
            .or(item.uri)
            .ok_or_else(|| eyre!("catalog item missing url"))?;
        let url = resolve_channel_url(&url_raw, base_url.as_ref());
        let name = item
            .name
            .or(item.title)
            .unwrap_or_else(|| format!("Channel {}", channels.len() + 1));
        channels.push(ChannelEntry {
            name,
            url,
            group: item.group.or(item.category),
            logo: item.logo,
            tvg_id: item.tvg_id,
        });
    }
    if channels.is_empty() {
        return Err(eyre!("channel catalog is empty"));
    }
    Ok(channels)
}

fn resolve_channel_url(href: &str, base: Option<&Url>) -> String {
    let href = href.trim();
    if let Ok(u) = Url::parse(href) {
        return u.to_string();
    }
    if let Some(base) = base {
        if let Ok(u) = base.join(href) {
            return u.to_string();
        }
    }
    href.to_string()
}

pub fn local_path_from_url(url: &str) -> Option<PathBuf> {
    let u = Url::parse(url).ok()?;
    if u.scheme() != "file" {
        return None;
    }
    u.to_file_path().ok()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_iptv_m3u() {
        let body = r#"#EXTM3U
#EXTINF:-1 tvg-name="beIN Sports 1" group-title="Spor" tvg-logo="http://logo/bein.png",beIN Sports 1
https://edge.example/bein.m3u8
#EXTINF:-1 group-title="Haber",TRT Haber
https://edge.example/trt.m3u8
"#;
        let src = detect_and_parse("file:///lineup.m3u", body.as_bytes(), None).unwrap();
        assert!(matches!(src, ParsedInput::IptvChannels { .. }));
        if let ParsedInput::IptvChannels { channels, .. } = src {
            assert_eq!(channels.len(), 2);
            assert_eq!(channels[0].name, "beIN Sports 1");
            assert_eq!(channels[0].group.as_deref(), Some("Spor"));
            assert_eq!(channels[1].name, "TRT Haber");
        }
    }

    #[test]
    fn iptv_without_targetduration_never_single_stream() {
        let body = b"#EXTM3U\n#EXTINF:-1,TRT 1\nhttps://tv-trt1.example/master.m3u8\n";
        assert!(is_iptv_channel_list(&String::from_utf8_lossy(body)));
        let src = detect_and_parse(
            "https://raw.githubusercontent.com/iptv-org/iptv/master/streams/tr.m3u",
            body,
            Some("application/vnd.apple.mpegurl"),
        )
        .unwrap();
        assert!(matches!(src, ParsedInput::IptvChannels { .. }));
    }

    #[test]
    fn iptv_with_tvg_attrs() {
        let body =
            b"#EXTM3U\n#EXTINF:-1 tvg-id=\"trt1.tr\" tvg-name=\"TRT 1\",TRT 1\nhttps://a/b.m3u8\n";
        assert!(is_iptv_channel_list(&String::from_utf8_lossy(body)));
        let src = detect_and_parse("https://x/tr.m3u", body, None).unwrap();
        assert!(matches!(src, ParsedInput::IptvChannels { .. }));
        if let ParsedInput::IptvChannels { channels, .. } = src {
            assert_eq!(channels.len(), 1);
        }
    }

    #[test]
    fn hls_master_is_single() {
        let body = b"#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=800000\nmedia.m3u8\n";
        let src = detect_and_parse("https://ex.com/master.m3u8", body, None).unwrap();
        assert!(matches!(src, ParsedInput::SingleStream { .. }));
    }

    #[test]
    fn hls_media_with_extinf_is_single_not_iptv() {
        let body =
            b"#EXTM3U\n#EXT-X-TARGETDURATION:6\n#EXT-X-MEDIA-SEQUENCE:10\n#EXTINF:6.0,\nseg.ts\n";
        assert!(!is_iptv_channel_list(&String::from_utf8_lossy(body)));
        let src = detect_and_parse("https://ex.com/media.m3u8", body, None).unwrap();
        assert!(matches!(src, ParsedInput::SingleStream { .. }));
    }

    #[test]
    fn json_catalog() {
        let body = r#"[{"name":"beIN Sports 1","url":"https://ex.com/a.m3u8","group":"Spor"}]"#;
        let src = detect_and_parse("channels.json", body.as_bytes(), None).unwrap();
        assert!(matches!(src, ParsedInput::IptvChannels { .. }));
        if let ParsedInput::IptvChannels { channels, .. } = src {
            assert_eq!(channels[0].name, "beIN Sports 1");
        }
    }
}