Repository: streamlink/streamlink

Issue:
Failed to read HDS stream http://www.rte.ie/manifests/newsnow.f4m
### Checklist

- [x] This is a bug report.
- [ ] This is a feature request.
- [ ] This is a plugin (improvement) request.
- [ ] I have read the contribution guidelines.

### Description

Streamlink cannot read the HDS stream http://www.rte.ie/manifests/newsnow.f4m.

### Reproduction steps / Explicit stream URLs to test

```
$ streamlink "hds://http://www.rte.ie/manifests/newsnow.f4m"
[cli][info] Found matching plugin stream for URL hds://http://www.rte.ie/manifests/newsnow.f4m
Available streams: 256k (worst), 512k, 1024k, 2048k (best)

$ streamlink "hds://http://www.rte.ie/manifests/newsnow.f4m" best
[cli][info] Found matching plugin stream for URL hds://http://www.rte.ie/manifests/newsnow.f4m
[cli][info] Available streams: 256k (worst), 512k, 1024k, 2048k (best)
[cli][info] Opening stream: 2048k (hds)
[stream.hds][error] Failed to open fragment 1-2: Unable to open URL: http://cdn.rasset.ie/hds-live/streams/events/_definst_/newsnow/newsnow-576Seg1-Frag2 (404 Client Error: Not found. for url: http://cdn.rasset.ie/hds-live/streams/events/_definst_/newsnow/newsnow-576Seg1-Frag2)
[stream.hds][error] Failed to open fragment 1-2: Unable to open URL: http://cdn.rasset.ie/hds-live/streams/events/_definst_/newsnow/newsnow-576Seg1-Frag2 (404 Client Error: Not found. for url: http://cdn.rasset.ie/hds-live/streams/events/_definst_/newsnow/newsnow-576Seg1-Frag2)
[stream.hds][error] Failed to open fragment 1-2: Unable to open URL: http://cdn.rasset.ie/hds-live/streams/events/_definst_/newsnow/newsnow-576Seg1-Frag2 (404 Client Error: Not found. for url: http://cdn.rasset.ie/hds-live/streams/events/_definst_/newsnow/newsnow-576Seg1-Frag2)
[cli][error] Try 1/1: Could not open stream <HDSStream('http://cdn.rasset.ie/hds-live/_definst_/newsnow/', '../../streams/events/_definst_/newsnow/newsnow-576', 'http://cdn.rasset.ie/hds-live/streams/events/_definst_/newsnow/newsnow-576.bootstrap', metadata=<ScriptData name=onMetaData value=ScriptDataECMAArray([('duration', 8.014), ('width', 1024.0), ('height', 576.0), ('videocodecid', 'H264'), ('audiocodecid', 'mp4a'), ('avcprofile', 77.0), ('avclevel', 31.0), ('aacaot', 0.0), ('videoframerate', nan), ('audiosamplerate', 48000.0), ('audiochannels', 2.0), ('trackinfo', [ScriptDataObject([('length', 0.0), ('timescale', 1000.0), ('language', 'eng')]), ScriptDataObject([('length', 0.0), ('timescale', 1000.0), ('language', 'eng')]), ScriptDataObject([('length', 0.0), ('timescale', 1000.0), ('language', 'eng')])])])>, timeout=60)> (Failed to read data from stream: Read timeout)
error: Could not open stream <HDSStream('http://cdn.rasset.ie/hds-live/_definst_/newsnow/', '../../streams/events/_definst_/newsnow/newsnow-576', 'http://cdn.rasset.ie/hds-live/streams/events/_definst_/newsnow/newsnow-576.bootstrap', metadata=<ScriptData name=onMetaData value=ScriptDataECMAArray([('duration', 8.014), ('width', 1024.0), ('height', 576.0), ('videocodecid', 'H264'), ('audiocodecid', 'mp4a'), ('avcprofile', 77.0), ('avclevel', 31.0), ('aacaot', 0.0), ('videoframerate', nan), ('audiosamplerate', 48000.0), ('audiochannels', 2.0), ('trackinfo', [ScriptDataObject([('length', 0.0), ('timescale', 1000.0), ('language', 'eng')]), ScriptDataObject([('length', 0.0), ('timescale', 1000.0), ('language', 'eng')]), ScriptDataObject([('length', 0.0), ('timescale', 1000.0), ('language', 'eng')])])])>, timeout=60)>, tried 1 times, exiting
[cli][info] Closing currently open stream...
```

### Environment details

Operating system and version: Fedora 25
Streamlink version: https://github.com/streamlink/streamlink/commit/93b64363c33eca9a212203db070e63fd115555e0 (latest snapshot today)
Python version: 3.5.3

Tested with both pycryptodome and pycrypto.

### Comments, logs, screenshots, etc.

Thanks to `git bissect`, it looks like this bug happens since commit https://github.com/streamlink/streamlink/commit/fddc80310def855efbbd6b12406980b8533c6dad; for all previous commits, as welle as the 0.5.0 release, reading the stream works fine.



Discussion:
Should probably revert 80eeba095c5a0a27f4c371c22a292b1d2f7656b4. The issue appears to be more subtle that I first realised :) 
Meanwhile you can watch here : 

```
streamlink "hlsvariant://http://wmsrtsp1.rte.ie/live/android.sdp/playlist.m3u8" best
[cli][info] Found matching plugin stream for URL hlsvariant://http://wmsrtsp1.rt
e.ie/live/android.sdp/playlist.m3u8
[cli][info] Available streams: 576p (worst, best)
[cli][info] Opening stream: 576p (hls)
[cli][info] Starting player: "C:\Program Files\VideoLAN\VLC\vlc.exe"
[cli][info] Player closed
[cli][info] Stream ended
[cli][info] Closing currently open stream...

streamlink "hlsvariant://http://cdn.rasset.ie/hls-live/_definst_/newsnow.m3u8" best
[cli][info] Found matching plugin stream for URL hlsvariant://http://cdn.rasset.
ie/hls-live/_definst_/newsnow.m3u8
[cli][info] Available streams: 300k (worst), 600k, 1200k, 2400k (best)
[cli][info] Opening stream: 2400k (hls)
[cli][info] Starting player: "C:\Program Files\VideoLAN\VLC\vlc.exe"
[cli][info] Player closed
[cli][info] Stream ended
[cli][info] Closing currently open stream...
```
@karlo2105 how did you get those HLS streams?
Android address I found on Internet and the second one comes from webplayer. Maybe you can get it through User Agent from android or ipad or iphone.
@karlo2105 @back-to : good catch :)

Resolving patch:
diff --git a/src/streamlink/stream/hds.py b/src/streamlink/stream/hds.py
--- a/src/streamlink/stream/hds.py
+++ b/src/streamlink/stream/hds.py
@@ -224,7 +224,6 @@ def fragment_url(self, segment, fragment):
     def fragment_count(self):
         table = self.fragmentruntable.payload.fragment_run_entry_table
         first_fragment, end_fragment = None, None
-        max_end_fragment = 0
 
         for i, fragmentrun in enumerate(table):
             if fragmentrun.discontinuity_indicator is not None:
@@ -240,16 +239,11 @@ def fragment_count(self):
             fragment_duration = (fragmentrun.first_fragment_timestamp +
                                  fragmentrun.fragment_duration)
 
-            max_end_fragment = max(fragmentrun.first_fragment, max_end_fragment)
-
             if self.timestamp > fragment_duration:
                 offset = ((self.timestamp - fragment_duration) /
                           fragmentrun.fragment_duration)
                 end_fragment += int(offset)
 
-        # don't go past the last fragment
-        end_fragment = min(max_end_fragment, end_fragment)
-
         if first_fragment is None:
             first_fragment = 1
 


Test patch: