oximedia-net 0.1.7

Network streaming for OxiMedia
Documentation
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
//! HLS streaming client with adaptive bitrate support.
//!
//! This module provides the [`HlsClient`] type which manages HLS playback,
//! including segment fetching, buffering, and adaptive bitrate switching.

#![allow(dead_code)]
#![allow(clippy::doc_markdown)]
#![allow(clippy::similar_names)]
#![allow(clippy::unreadable_literal)]
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::cast_precision_loss)]
#![allow(clippy::cast_lossless)]
#![allow(clippy::cast_sign_loss)]
#![allow(clippy::match_same_arms)]
#![allow(clippy::many_single_char_names)]
#![allow(clippy::unnecessary_wraps)]
#![allow(clippy::range_plus_one)]
#![allow(clippy::needless_pass_by_value)]
#![allow(clippy::manual_div_ceil)]
#![allow(clippy::comparison_chain)]
#![allow(clippy::unused_self)]
#![allow(clippy::trivially_copy_pass_by_ref)]
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::struct_excessive_bools)]
#![allow(clippy::needless_range_loop)]
#![allow(clippy::redundant_closure_for_method_calls)]
#![allow(clippy::must_use_candidate)]
#![allow(clippy::should_implement_trait)]
#![allow(clippy::items_after_statements)]
#![allow(clippy::if_not_else)]
#![allow(clippy::format_push_string)]
#![allow(clippy::single_match_else)]
#![allow(clippy::redundant_slicing)]
#![allow(clippy::uninlined_format_args)]
#![allow(clippy::map_unwrap_or)]
#![allow(clippy::derivable_impls)]
#![allow(clippy::assigning_clones)]
#![allow(clippy::if_same_then_else)]
#![allow(clippy::format_collect)]
#![allow(clippy::useless_conversion)]
#![allow(clippy::unused_async)]
#![allow(clippy::identity_op)]

use crate::error::{NetError, NetResult};
use crate::hls::{
    AbrController, ByteRange, MasterPlaylist, MediaPlaylist, QualityLevel, Segment, SegmentCache,
    SegmentFetcher, ThroughputBasedAbr,
};
use bytes::Bytes;
use reqwest::Client;
use std::collections::VecDeque;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::{Mutex, RwLock};

/// Configuration for the HLS client.
#[derive(Debug, Clone)]
pub struct HlsClientConfig {
    /// Initial buffer size in seconds.
    pub initial_buffer: Duration,
    /// Target buffer size in seconds.
    pub target_buffer: Duration,
    /// Maximum buffer size in seconds.
    pub max_buffer: Duration,
    /// Minimum buffer before rebuffering.
    pub min_buffer: Duration,
    /// Maximum number of segments to prefetch.
    pub max_prefetch: usize,
    /// Interval for refreshing live playlists.
    pub playlist_refresh_interval: Duration,
    /// Enable segment caching.
    pub enable_cache: bool,
    /// Cache size in bytes.
    pub cache_size: usize,
    /// Maximum number of cached segments.
    pub max_cached_segments: usize,
    /// Maximum number of concurrent segment downloads.
    pub max_concurrent_downloads: usize,
    /// Enable adaptive bitrate switching.
    pub enable_abr: bool,
    /// Request timeout.
    pub request_timeout: Duration,
    /// Maximum retries for failed requests.
    pub max_retries: u32,
}

impl Default for HlsClientConfig {
    fn default() -> Self {
        Self {
            initial_buffer: Duration::from_secs(5),
            target_buffer: Duration::from_secs(15),
            max_buffer: Duration::from_secs(30),
            min_buffer: Duration::from_secs(3),
            max_prefetch: 10,
            playlist_refresh_interval: Duration::from_secs(5),
            enable_cache: true,
            cache_size: 100 * 1024 * 1024, // 100MB
            max_cached_segments: 50,
            max_concurrent_downloads: 3,
            enable_abr: true,
            request_timeout: Duration::from_secs(30),
            max_retries: 3,
        }
    }
}

/// State of the HLS client.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClientState {
    /// Client is idle, not yet started.
    Idle,
    /// Loading initial playlist and segments.
    Loading,
    /// Playing back segments.
    Playing,
    /// Paused playback.
    Paused,
    /// Buffering more segments.
    Buffering,
    /// Switching to a different variant.
    Switching,
    /// Stream has ended.
    Ended,
    /// Error occurred.
    Error,
}

/// A buffered segment ready for playback.
#[derive(Debug, Clone)]
pub struct BufferedSegment {
    /// Segment metadata.
    pub segment: Segment,
    /// Segment data.
    pub data: Bytes,
    /// Sequence number.
    pub sequence: u64,
    /// Quality level index.
    pub quality_level: usize,
    /// When the segment was buffered.
    pub buffered_at: Instant,
}

/// Statistics about the HLS client.
#[derive(Debug, Clone, Default)]
pub struct ClientStats {
    /// Total bytes downloaded.
    pub bytes_downloaded: u64,
    /// Total segments downloaded.
    pub segments_downloaded: u64,
    /// Total segments dropped (due to quality switch).
    pub segments_dropped: u64,
    /// Number of quality switches.
    pub quality_switches: u64,
    /// Number of rebuffering events.
    pub rebuffer_events: u64,
    /// Total rebuffering time.
    pub rebuffer_time: Duration,
    /// Estimated throughput in bits per second.
    pub estimated_throughput: f64,
    /// Current buffer level.
    pub buffer_level: Duration,
}

/// HLS streaming client with adaptive bitrate support.
pub struct HlsClient {
    /// Client configuration.
    config: HlsClientConfig,
    /// HTTP client.
    http_client: Client,
    /// Current state.
    state: Arc<RwLock<ClientState>>,
    /// Master playlist (if available).
    master_playlist: Arc<RwLock<Option<MasterPlaylist>>>,
    /// Current media playlist.
    media_playlist: Arc<RwLock<Option<MediaPlaylist>>>,
    /// Available quality levels.
    quality_levels: Arc<RwLock<Vec<QualityLevel>>>,
    /// Current quality level index.
    current_quality: Arc<RwLock<usize>>,
    /// Segment buffer queue.
    buffer_queue: Arc<Mutex<VecDeque<BufferedSegment>>>,
    /// Segment fetcher.
    fetcher: Arc<Mutex<SegmentFetcher>>,
    /// Segment cache.
    cache: Option<Arc<SegmentCache>>,
    /// ABR controller.
    abr_controller: Arc<Mutex<Box<dyn AbrController>>>,
    /// Client statistics.
    stats: Arc<RwLock<ClientStats>>,
    /// Next segment sequence number to fetch.
    next_sequence: Arc<Mutex<u64>>,
    /// Last playlist refresh time.
    last_playlist_refresh: Arc<Mutex<Option<Instant>>>,
    /// Playback position (in seconds).
    playback_position: Arc<RwLock<f64>>,
    /// Base URL for resolving relative URLs.
    base_url: Arc<RwLock<Option<String>>>,
}

impl HlsClient {
    /// Creates a new HLS client with default configuration.
    #[must_use]
    pub fn new() -> Self {
        Self::with_config(HlsClientConfig::default())
    }

    /// Creates a new HLS client with the given configuration.
    #[must_use]
    pub fn with_config(config: HlsClientConfig) -> Self {
        let http_client = Client::builder()
            .timeout(config.request_timeout)
            .build()
            .unwrap_or_else(|_| Client::new());

        let cache = if config.enable_cache {
            Some(Arc::new(SegmentCache::new(
                config.cache_size,
                config.max_cached_segments,
            )))
        } else {
            None
        };

        let abr_controller: Box<dyn AbrController> = Box::new(ThroughputBasedAbr::new());

        Self {
            config,
            http_client: http_client.clone(),
            state: Arc::new(RwLock::new(ClientState::Idle)),
            master_playlist: Arc::new(RwLock::new(None)),
            media_playlist: Arc::new(RwLock::new(None)),
            quality_levels: Arc::new(RwLock::new(Vec::new())),
            current_quality: Arc::new(RwLock::new(0)),
            buffer_queue: Arc::new(Mutex::new(VecDeque::new())),
            fetcher: Arc::new(Mutex::new(SegmentFetcher::with_client(http_client))),
            cache,
            abr_controller: Arc::new(Mutex::new(abr_controller)),
            stats: Arc::new(RwLock::new(ClientStats::default())),
            next_sequence: Arc::new(Mutex::new(0)),
            last_playlist_refresh: Arc::new(Mutex::new(None)),
            playback_position: Arc::new(RwLock::new(0.0)),
            base_url: Arc::new(RwLock::new(None)),
        }
    }

    /// Sets a custom ABR controller.
    pub async fn set_abr_controller(&self, controller: Box<dyn AbrController>) {
        let mut abr = self.abr_controller.lock().await;
        *abr = controller;
    }

    /// Returns the current client state.
    pub async fn state(&self) -> ClientState {
        *self.state.read().await
    }

    /// Returns the current statistics.
    pub async fn stats(&self) -> ClientStats {
        self.stats.read().await.clone()
    }

    /// Loads a master playlist from a URL.
    ///
    /// # Errors
    ///
    /// Returns an error if the playlist cannot be fetched or parsed.
    pub async fn load_master_playlist(&self, url: &str) -> NetResult<()> {
        self.set_state(ClientState::Loading).await;

        // Fetch the playlist
        let response =
            self.http_client.get(url).send().await.map_err(|e| {
                NetError::connection(format!("Failed to fetch master playlist: {e}"))
            })?;

        let status = response.status();
        if !status.is_success() {
            return Err(NetError::http(
                status.as_u16(),
                format!("Failed to fetch master playlist: {url}"),
            ));
        }

        let text = response
            .text()
            .await
            .map_err(|e| NetError::connection(format!("Failed to read playlist: {e}")))?;

        // Parse the playlist
        let mut master = MasterPlaylist::parse(&text)?;

        // Set base URL
        master.base_uri = Some(url.to_string());
        let mut base_url = self.base_url.write().await;
        *base_url = Some(url.to_string());
        drop(base_url);

        // Extract quality levels from variants
        let quality_levels: Vec<QualityLevel> = master
            .variants
            .iter()
            .enumerate()
            .map(|(idx, variant)| {
                let mut level = QualityLevel::new(idx, variant.stream_inf.bandwidth);
                if let Some((w, h)) = variant.stream_inf.resolution {
                    level = level.with_resolution(w, h);
                }
                if let Some(ref codecs) = variant.stream_inf.codecs {
                    level = level.with_codecs(codecs.clone());
                }
                level
            })
            .collect();

        // Store the master playlist and quality levels
        let mut playlist = self.master_playlist.write().await;
        *playlist = Some(master);
        drop(playlist);

        let mut levels = self.quality_levels.write().await;
        *levels = quality_levels;
        drop(levels);

        // Select initial quality level (lowest to start)
        let mut current = self.current_quality.write().await;
        *current = 0;
        drop(current);

        Ok(())
    }

    /// Loads a media playlist from a URL (for direct media playlist playback).
    ///
    /// # Errors
    ///
    /// Returns an error if the playlist cannot be fetched or parsed.
    pub async fn load_media_playlist(&self, url: &str) -> NetResult<()> {
        self.set_state(ClientState::Loading).await;

        let playlist = self.fetch_media_playlist(url).await?;

        // Set base URL
        let mut base_url = self.base_url.write().await;
        *base_url = Some(url.to_string());
        drop(base_url);

        // Store the media playlist
        let mut media = self.media_playlist.write().await;
        *media = Some(playlist);
        drop(media);

        // Initialize sequence number
        let media_ref = self.media_playlist.read().await;
        if let Some(ref playlist) = *media_ref {
            let mut seq = self.next_sequence.lock().await;
            *seq = playlist.media_sequence;
        }

        Ok(())
    }

    /// Starts playback.
    ///
    /// # Errors
    ///
    /// Returns an error if playback cannot be started.
    pub async fn start(&self) -> NetResult<()> {
        let current_state = self.state().await;
        if current_state != ClientState::Idle && current_state != ClientState::Loading {
            return Err(NetError::invalid_state(format!(
                "Cannot start from state: {current_state:?}"
            )));
        }

        // Load the initial media playlist if we have a master playlist
        let master = self.master_playlist.read().await;
        if let Some(ref master_pl) = *master {
            let current_quality = *self.current_quality.read().await;
            if let Some(variant) = master_pl.variants.get(current_quality) {
                let base = master_pl
                    .base_uri
                    .as_ref()
                    .ok_or_else(|| NetError::invalid_state("Master playlist has no base URI"))?;
                let url = self.resolve_variant_url(base, &variant.uri);
                drop(master);
                self.fetch_and_update_media_playlist(&url).await?;
            }
        }

        self.set_state(ClientState::Playing).await;

        // Start the download loop
        self.start_download_loop().await;

        Ok(())
    }

    /// Pauses playback.
    pub async fn pause(&self) {
        self.set_state(ClientState::Paused).await;
    }

    /// Resumes playback.
    pub async fn resume(&self) {
        self.set_state(ClientState::Playing).await;
    }

    /// Stops playback and clears buffers.
    pub async fn stop(&self) {
        self.set_state(ClientState::Idle).await;

        // Clear buffers
        let mut queue = self.buffer_queue.lock().await;
        queue.clear();

        // Clear cache
        if let Some(ref cache) = self.cache {
            cache.clear().await;
        }

        // Reset statistics
        let mut stats = self.stats.write().await;
        *stats = ClientStats::default();

        // Reset ABR controller
        let mut abr = self.abr_controller.lock().await;
        abr.reset();
    }

    /// Gets the next buffered segment for playback.
    pub async fn get_next_segment(&self) -> Option<BufferedSegment> {
        let mut queue = self.buffer_queue.lock().await;
        let segment = queue.pop_front();

        if let Some(ref seg) = segment {
            // Update playback position
            let mut pos = self.playback_position.write().await;
            *pos += seg.segment.duration.as_secs_f64();

            // Update buffer level
            let buffer_duration: Duration = queue.iter().map(|s| s.segment.duration).sum();
            let mut stats = self.stats.write().await;
            stats.buffer_level = buffer_duration;

            // Report buffer level to ABR controller
            let mut abr = self.abr_controller.lock().await;
            abr.report_buffer(buffer_duration);
        }

        // Check if we need to rebuffer
        let current_state = self.state().await;
        if current_state == ClientState::Playing {
            let buffer_level = self.get_buffer_level().await;
            if buffer_level < self.config.min_buffer {
                self.set_state(ClientState::Buffering).await;

                let mut stats = self.stats.write().await;
                stats.rebuffer_events += 1;
            }
        }

        segment
    }

    /// Returns the current buffer level in seconds.
    pub async fn get_buffer_level(&self) -> Duration {
        let queue = self.buffer_queue.lock().await;
        queue.iter().map(|s| s.segment.duration).sum()
    }

    /// Returns the number of buffered segments.
    pub async fn buffered_segment_count(&self) -> usize {
        let queue = self.buffer_queue.lock().await;
        queue.len()
    }

    /// Manually switches to a different quality level.
    ///
    /// # Errors
    ///
    /// Returns an error if the quality level is invalid.
    pub async fn switch_quality(&self, level: usize) -> NetResult<()> {
        let levels = self.quality_levels.read().await;
        if level >= levels.len() {
            return Err(NetError::invalid_state(format!(
                "Invalid quality level: {level}"
            )));
        }
        drop(levels);

        let mut current = self.current_quality.write().await;
        if *current == level {
            return Ok(()); // Already at this level
        }

        *current = level;
        drop(current);

        // Update stats
        let mut stats = self.stats.write().await;
        stats.quality_switches += 1;

        self.set_state(ClientState::Switching).await;

        Ok(())
    }

    /// Returns the available quality levels.
    pub async fn available_quality_levels(&self) -> Vec<QualityLevel> {
        self.quality_levels.read().await.clone()
    }

    /// Returns the current quality level index.
    pub async fn current_quality_level(&self) -> usize {
        *self.current_quality.read().await
    }

    // Private helper methods

    async fn set_state(&self, state: ClientState) {
        let mut current_state = self.state.write().await;
        *current_state = state;
    }

    async fn fetch_media_playlist(&self, url: &str) -> NetResult<MediaPlaylist> {
        let response =
            self.http_client.get(url).send().await.map_err(|e| {
                NetError::connection(format!("Failed to fetch media playlist: {e}"))
            })?;

        let status = response.status();
        if !status.is_success() {
            return Err(NetError::http(
                status.as_u16(),
                format!("Failed to fetch media playlist: {url}"),
            ));
        }

        let text = response
            .text()
            .await
            .map_err(|e| NetError::connection(format!("Failed to read playlist: {e}")))?;

        let mut playlist = MediaPlaylist::parse(&text)?;
        playlist.base_uri = Some(url.to_string());

        Ok(playlist)
    }

    async fn fetch_and_update_media_playlist(&self, url: &str) -> NetResult<()> {
        let playlist = self.fetch_media_playlist(url).await?;

        let mut media = self.media_playlist.write().await;
        *media = Some(playlist);

        // Update last refresh time
        let mut last_refresh = self.last_playlist_refresh.lock().await;
        *last_refresh = Some(Instant::now());

        Ok(())
    }

    fn resolve_variant_url(&self, base: &str, uri: &str) -> String {
        if uri.starts_with("http://") || uri.starts_with("https://") {
            return uri.to_string();
        }

        if uri.starts_with('/') {
            // Absolute path
            if let Some(pos) = base.find("://") {
                if let Some(slash_pos) = base[pos + 3..].find('/') {
                    return format!("{}{uri}", &base[..pos + 3 + slash_pos]);
                }
            }
            format!("{base}{uri}")
        } else {
            // Relative path
            if let Some(last_slash) = base.rfind('/') {
                format!("{}/{uri}", &base[..last_slash])
            } else {
                format!("{base}/{uri}")
            }
        }
    }

    #[allow(clippy::too_many_lines)]
    async fn start_download_loop(&self) {
        let state = self.state.clone();
        let config = self.config.clone();
        let buffer_queue = self.buffer_queue.clone();
        let fetcher = self.fetcher.clone();
        let cache = self.cache.clone();
        let media_playlist = self.media_playlist.clone();
        let next_sequence = self.next_sequence.clone();
        let stats = self.stats.clone();
        let abr_controller = self.abr_controller.clone();
        let quality_levels = self.quality_levels.clone();
        let current_quality = self.current_quality.clone();
        let master_playlist = self.master_playlist.clone();
        let base_url_arc = self.base_url.clone();
        let last_refresh = self.last_playlist_refresh.clone();
        let http_client = self.http_client.clone();

        tokio::spawn(async move {
            loop {
                let current_state = *state.read().await;

                // Exit if stopped
                if current_state == ClientState::Idle || current_state == ClientState::Error {
                    break;
                }

                // Skip if paused
                if current_state == ClientState::Paused {
                    tokio::time::sleep(Duration::from_millis(100)).await;
                    continue;
                }

                // Check if we need to refresh the playlist (for live streams)
                let should_refresh = {
                    let last = last_refresh.lock().await;
                    match *last {
                        Some(t) => t.elapsed() >= config.playlist_refresh_interval,
                        None => false,
                    }
                };

                if should_refresh {
                    let media_guard = media_playlist.read().await;
                    if let Some(ref playlist) = *media_guard {
                        if playlist.is_live() {
                            // Refresh the playlist
                            let url = playlist.base_uri.clone();
                            drop(media_guard);

                            if let Some(url) = url {
                                let response = http_client.get(&url).send().await;
                                if let Ok(response) = response {
                                    if let Ok(text) = response.text().await {
                                        if let Ok(mut new_playlist) = MediaPlaylist::parse(&text) {
                                            new_playlist.base_uri = Some(url);
                                            let mut media = media_playlist.write().await;
                                            *media = Some(new_playlist);
                                        }
                                    }
                                }

                                let mut last = last_refresh.lock().await;
                                *last = Some(Instant::now());
                            }
                        }
                    }
                }

                // Check buffer level
                let buffer_level: Duration = {
                    let queue = buffer_queue.lock().await;
                    queue.iter().map(|s| s.segment.duration).sum()
                };

                // Check if we have enough buffer
                if buffer_level >= config.max_buffer {
                    tokio::time::sleep(Duration::from_millis(500)).await;
                    continue;
                }

                // Check if we're buffering and have reached target
                if current_state == ClientState::Buffering && buffer_level >= config.initial_buffer
                {
                    let mut s = state.write().await;
                    *s = ClientState::Playing;
                }

                // Perform ABR decision if enabled
                if config.enable_abr {
                    let levels = quality_levels.read().await;
                    let current = *current_quality.read().await;
                    let abr = abr_controller.lock().await;
                    let decision = abr.select_quality(&levels, current);
                    drop(abr);
                    drop(levels);

                    if let Some(target) = decision.target_level() {
                        if target != current {
                            // Switch quality level
                            let mut qual = current_quality.write().await;
                            *qual = target;
                            drop(qual);

                            // Update stats
                            let mut stats_guard = stats.write().await;
                            stats_guard.quality_switches += 1;

                            // Reload media playlist for new variant
                            let master_guard = master_playlist.read().await;
                            if let Some(ref master) = *master_guard {
                                if let Some(variant) = master.variants.get(target) {
                                    let base = master.base_uri.as_ref();
                                    if let Some(base) = base {
                                        let url = Self::resolve_url_static(base, &variant.uri);
                                        drop(master_guard);

                                        let response = http_client.get(&url).send().await;
                                        if let Ok(response) = response {
                                            if let Ok(text) = response.text().await {
                                                if let Ok(mut new_playlist) =
                                                    MediaPlaylist::parse(&text)
                                                {
                                                    new_playlist.base_uri = Some(url);
                                                    let mut media = media_playlist.write().await;
                                                    *media = Some(new_playlist);

                                                    // Reset sequence to start of new playlist
                                                    let seq = media
                                                        .as_ref()
                                                        .map(|p| p.media_sequence)
                                                        .unwrap_or(0);
                                                    let mut next_seq = next_sequence.lock().await;
                                                    *next_seq = seq;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // Fetch next segment
                let segment_info = {
                    let media_guard = media_playlist.read().await;
                    if let Some(ref playlist) = *media_guard {
                        let seq = *next_sequence.lock().await;
                        let idx = (seq - playlist.media_sequence) as usize;

                        if idx < playlist.segments.len() {
                            Some((
                                playlist.segments[idx].clone(),
                                seq,
                                playlist.base_uri.clone(),
                            ))
                        } else if playlist.ended {
                            // Stream has ended
                            None
                        } else {
                            // Live stream, wait for more segments
                            None
                        }
                    } else {
                        None
                    }
                };

                if let Some((segment, seq, base_uri)) = segment_info {
                    // Resolve segment URL
                    let segment_url = if let Some(ref base) = base_uri {
                        Self::resolve_url_static(base, &segment.uri)
                    } else if let Some(ref base) = *base_url_arc.read().await {
                        Self::resolve_url_static(base, &segment.uri)
                    } else {
                        segment.uri.clone()
                    };

                    // Check cache first
                    let cached_data = if let Some(ref cache) = cache {
                        cache.get(&segment_url).await
                    } else {
                        None
                    };

                    let segment_data = if let Some(data) = cached_data {
                        // Cache hit
                        data
                    } else {
                        // Cache miss - fetch segment
                        let byte_range = segment.byte_range.map(|(len, offset)| {
                            if let Some(off) = offset {
                                ByteRange::from_offset_length(off, len)
                            } else {
                                ByteRange::from_offset_length(0, len)
                            }
                        });

                        let mut fetcher_guard = fetcher.lock().await;
                        let fetch_result = fetcher_guard
                            .fetch_with_retry(&segment_url, byte_range)
                            .await;

                        match fetch_result {
                            Ok(result) => {
                                // Update ABR controller
                                let mut abr = abr_controller.lock().await;
                                abr.report_download(result.content_length, result.fetch_time);

                                // Update stats
                                let mut stats_guard = stats.write().await;
                                stats_guard.bytes_downloaded += result.content_length as u64;
                                stats_guard.segments_downloaded += 1;
                                stats_guard.estimated_throughput = abr.estimated_throughput();

                                // Cache the segment
                                if let Some(ref cache) = cache {
                                    cache.put(segment_url.clone(), result.data.clone()).await;
                                }

                                result.data
                            }
                            Err(e) => {
                                eprintln!("Failed to fetch segment {seq}: {e}");
                                tokio::time::sleep(Duration::from_secs(1)).await;
                                continue;
                            }
                        }
                    };

                    // Add to buffer queue
                    let buffered = BufferedSegment {
                        segment: segment.clone(),
                        data: segment_data,
                        sequence: seq,
                        quality_level: *current_quality.read().await,
                        buffered_at: Instant::now(),
                    };

                    let mut queue = buffer_queue.lock().await;
                    queue.push_back(buffered);

                    // Increment sequence
                    let mut next_seq = next_sequence.lock().await;
                    *next_seq += 1;
                } else {
                    // No more segments available
                    let media_guard = media_playlist.read().await;
                    if let Some(ref playlist) = *media_guard {
                        if playlist.ended {
                            // Stream has ended
                            let mut s = state.write().await;
                            *s = ClientState::Ended;
                            break;
                        }
                    }

                    // Wait before checking again
                    tokio::time::sleep(Duration::from_millis(500)).await;
                }

                // Small delay to prevent tight loop
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
        });
    }

    fn resolve_url_static(base: &str, uri: &str) -> String {
        if uri.starts_with("http://") || uri.starts_with("https://") {
            return uri.to_string();
        }

        if uri.starts_with('/') {
            // Absolute path
            if let Some(pos) = base.find("://") {
                if let Some(slash_pos) = base[pos + 3..].find('/') {
                    return format!("{}{uri}", &base[..pos + 3 + slash_pos]);
                }
            }
            format!("{base}{uri}")
        } else {
            // Relative path
            if let Some(last_slash) = base.rfind('/') {
                format!("{}/{uri}", &base[..last_slash])
            } else {
                format!("{base}/{uri}")
            }
        }
    }
}

impl Default for HlsClient {
    fn default() -> Self {
        Self::new()
    }
}

/// Builder for configuring an HLS client.
pub struct HlsClientBuilder {
    config: HlsClientConfig,
}

impl HlsClientBuilder {
    /// Creates a new builder with default configuration.
    #[must_use]
    pub fn new() -> Self {
        Self {
            config: HlsClientConfig::default(),
        }
    }

    /// Sets the initial buffer duration.
    #[must_use]
    pub const fn initial_buffer(mut self, duration: Duration) -> Self {
        self.config.initial_buffer = duration;
        self
    }

    /// Sets the target buffer duration.
    #[must_use]
    pub const fn target_buffer(mut self, duration: Duration) -> Self {
        self.config.target_buffer = duration;
        self
    }

    /// Sets the maximum buffer duration.
    #[must_use]
    pub const fn max_buffer(mut self, duration: Duration) -> Self {
        self.config.max_buffer = duration;
        self
    }

    /// Sets the minimum buffer duration before rebuffering.
    #[must_use]
    pub const fn min_buffer(mut self, duration: Duration) -> Self {
        self.config.min_buffer = duration;
        self
    }

    /// Sets the maximum number of segments to prefetch.
    #[must_use]
    pub const fn max_prefetch(mut self, count: usize) -> Self {
        self.config.max_prefetch = count;
        self
    }

    /// Sets the playlist refresh interval for live streams.
    #[must_use]
    pub const fn playlist_refresh_interval(mut self, interval: Duration) -> Self {
        self.config.playlist_refresh_interval = interval;
        self
    }

    /// Enables or disables segment caching.
    #[must_use]
    pub const fn enable_cache(mut self, enable: bool) -> Self {
        self.config.enable_cache = enable;
        self
    }

    /// Sets the cache size in bytes.
    #[must_use]
    pub const fn cache_size(mut self, size: usize) -> Self {
        self.config.cache_size = size;
        self
    }

    /// Sets the maximum number of concurrent downloads.
    #[must_use]
    pub const fn max_concurrent_downloads(mut self, count: usize) -> Self {
        self.config.max_concurrent_downloads = count;
        self
    }

    /// Enables or disables adaptive bitrate switching.
    #[must_use]
    pub const fn enable_abr(mut self, enable: bool) -> Self {
        self.config.enable_abr = enable;
        self
    }

    /// Sets the request timeout.
    #[must_use]
    pub const fn request_timeout(mut self, timeout: Duration) -> Self {
        self.config.request_timeout = timeout;
        self
    }

    /// Sets the maximum number of retries for failed requests.
    #[must_use]
    pub const fn max_retries(mut self, retries: u32) -> Self {
        self.config.max_retries = retries;
        self
    }

    /// Builds the HLS client.
    #[must_use]
    pub fn build(self) -> HlsClient {
        HlsClient::with_config(self.config)
    }
}

impl Default for HlsClientBuilder {
    fn default() -> Self {
        Self::new()
    }
}