public string IndexFileForStreamer(int StreamerID) { MediaStreamer ms = GetStreamerByID(StreamerID); TimeSpan mediaDuration = FileBrowseExporter.DurationOfMediaFile_OSSpecific(ms.Request.InputFile); int msSegmentDuration = ms.Request.ActualSegmentDuration; StringBuilder sbIndexFile = new StringBuilder(1000); sbIndexFile.AppendLine("#EXTM3U"); sbIndexFile.AppendLine("#EXT-X-TARGETDURATION:" + msSegmentDuration.ToString()); // maximum duration of any one file, in seconds sbIndexFile.AppendLine("#EXT-X-ALLOW-CACHE:YES"); // allow client to cache files double dNumberSegments = mediaDuration.TotalSeconds / Convert.ToDouble(msSegmentDuration); int WholeNumberSegments = Convert.ToInt32(Math.Floor(dNumberSegments)); int i; for (i = 0; i < WholeNumberSegments; i++) { sbIndexFile.AppendLine("#EXTINF:" + msSegmentDuration.ToString() + ","); string strSegID = "seg-" + i.ToString() + ".ts"; sbIndexFile.AppendLine(strSegID); } // Duration of final segment? double dFinalSegTime = mediaDuration.TotalSeconds % Convert.ToDouble(msSegmentDuration); int iFinalSegTime = Convert.ToInt32(dFinalSegTime); sbIndexFile.AppendLine("#EXTINF:" + iFinalSegTime.ToString() + ","); string strFinalSegID = "seg-" + i.ToString() + ".ts"; sbIndexFile.AppendLine(strFinalSegID); sbIndexFile.AppendLine("#EXT-X-ENDLIST"); return(sbIndexFile.ToString()); }
public string IndexFileForStreamer(int StreamerID, bool background) { MediaStreamer ms = GetStreamerByID(StreamerID); ms.Request.InputFile = HttpUtility.HtmlDecode(ms.Request.InputFile); TimeSpan mediaDuration = (!(ms.Request.LiveTV || ms.Request.NewLiveTV) ? FileBrowseExporter.DurationOfMediaFile_OSSpecific(ms.Request.InputFile) : new TimeSpan(0, ms.Request.DurationLiveTVBlocks, 0)); int msSegmentDuration = ms.Request.ActualSegmentDuration; StringBuilder sbIndexFile = new StringBuilder(1000); string WorkingDirectory = "c:\\"; if (background) { // StopStreamer(StreamerID, 99); } if (ms.Request.NewLiveTV) { if (LiveTVParts.usingVLCWithsegmenter || LiveTVParts.UseVLCHLSsegmenter || LiveTVParts.dontUsePipe) { // do it the old style } else { //if (Settings.Default.DebugStreaming) // Functions.WriteLineToLogFile2("StreamingManager: client asks to generate new m3u8, resistance is futile"); string rpPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "RemotePotato"); WorkingDirectory = Path.Combine(rpPath, "static\\mediastreams\\" + StreamerID.ToString()); if (!Directory.Exists(WorkingDirectory)) { Directory.CreateDirectory(WorkingDirectory); } if (!File.Exists(WorkingDirectory + "\\livetvtemp0.m3u8")) { return(""); } var fs = File.Open(WorkingDirectory + "\\livetvtemp0.m3u8", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); var sr2 = new StreamReader(fs); string m3u8File = sr2.ReadToEnd(); return(m3u8File); } } sbIndexFile.AppendLine("#EXTM3U"); if (ms.Request.UseNewerFFMPEG) { sbIndexFile.AppendLine("#EXT-X-TARGETDURATION:" + (msSegmentDuration + 1)); // maximum duration of any one file, in seconds } else { sbIndexFile.AppendLine("#EXT-X-TARGETDURATION:" + msSegmentDuration.ToString()); // maximum duration of any one file, in seconds } sbIndexFile.AppendLine("#EXT-X-ALLOW-CACHE:YES"); // allow client to cache files double dNumberSegments; if (ms.Request.LiveTV)// || ms.Request.NewLiveTV) { int q = ms.Request.InitialWaitTimeBeforeLiveTVStarts; int r = ms.Request.SegmentIncreasingStepsLiveTV; double StartAtSegmentWhereCalculatedSegmentDurationIsMax = (msSegmentDuration - q) / r; double StartAtSecondsWhereCalculatedSegmentDurationIsMax = (Math.Min(q + (int)StartAtSegmentWhereCalculatedSegmentDurationIsMax * r, msSegmentDuration) - q) * (Math.Min(q + (int)StartAtSegmentWhereCalculatedSegmentDurationIsMax * r, msSegmentDuration) + q - r) / (2 * r); dNumberSegments = (mediaDuration.TotalSeconds - StartAtSecondsWhereCalculatedSegmentDurationIsMax) / msSegmentDuration + StartAtSegmentWhereCalculatedSegmentDurationIsMax; } else //never change a winning team: { dNumberSegments = mediaDuration.TotalSeconds / Convert.ToDouble(msSegmentDuration); } int WholeNumberSegments = Convert.ToInt32(Math.Floor(dNumberSegments)); int i; // int OldSegmentDuration = 2; int SegmentDuration; string strSegID = ""; int from = (ms.Request.NewLiveTV?1:0); for (i = from; i < WholeNumberSegments; i++) // TODO: for newlivetv: have to cheang wholenumbersegments to differrent nr (higher) cuz now also segmnent<4sec { if (ms.Request.LiveTV) // || ms.Request.NewLiveTV) { SegmentDuration = Math.Min(msSegmentDuration, ms.Request.InitialWaitTimeBeforeLiveTVStarts + i * ms.Request.SegmentIncreasingStepsLiveTV); //make segments q second bigger untill 60 seconds reached // start with 4 seconds first then gradually increase up to 1 minute of segmentlength sbIndexFile.AppendLine("#EXTINF:" + SegmentDuration.ToString() + ","); // sbIndexFile.AppendLine("#EXTINF:4,"); } else { if (ms.Request.UseNewerFFMPEG) { sbIndexFile.AppendLine("#EXTINF:" + msSegmentDuration.ToString() + ".0,"); } else { sbIndexFile.AppendLine("#EXTINF:" + msSegmentDuration.ToString() + ","); } } if (!ms.Request.NewLiveTV) { if (background) { strSegID = "segbackground-" + i.ToString() + ".ts"; } else { strSegID = "seg-" + i.ToString() + ".ts"; } } else { strSegID = "liveseg-" + i + ".ts"; } sbIndexFile.AppendLine(strSegID); } // Duration of final segment? TODO for NEWLIVETV double dFinalSegTime; if (ms.Request.LiveTV) //|| ms.Request.NewLiveTV) { dFinalSegTime = (dNumberSegments - WholeNumberSegments) * msSegmentDuration; // TODO: should also take inot account special case where totalduration smalller than when mssegmentduration segments appear } else { dFinalSegTime = mediaDuration.TotalSeconds % Convert.ToDouble(msSegmentDuration); } int iFinalSegTime = Convert.ToInt32(dFinalSegTime); if (iFinalSegTime > 0) // adding this prevents stream from freezing at end { sbIndexFile.AppendLine("#EXTINF:" + iFinalSegTime.ToString() + ","); string strFinalSegID = ""; if (!ms.Request.NewLiveTV) { if (background) { strFinalSegID = "segbackground-" + i.ToString() + ".ts"; } else { strFinalSegID = "seg-" + i.ToString() + ".ts"; } } else { strFinalSegID = "liveseg-" + i + ".ts"; } sbIndexFile.AppendLine(strFinalSegID); } sbIndexFile.AppendLine("#EXT-X-ENDLIST"); TextWriter tw = new StreamWriter(Functions.AppDataFolder + "\\static\\mediastreams\\" + StreamerID + "\\index.m3u8"); tw.Write(sbIndexFile.ToString()); tw.Close(); return(sbIndexFile.ToString()); }