public bool TryGetSegmentByNumber(int SegNumber, ref Segment seg) { lock (syncLock) { bool stopWaiting = false; if (!DoesFileExistForSegmentNumber(SegNumber)) { segmentsWaiting.Add(SegNumber); do { Monitor.Wait(syncLock); stopWaiting = (!segmentsWaiting.Contains(SegNumber)); } while ( (!DoesFileExistForSegmentNumber(SegNumber)) && (! stopWaiting) ); } if (stopWaiting) return false; // It's arrived! Remove segments waiting flag segmentsWaiting.Remove(SegNumber); seg = _GetSegment(SegNumber); return true; } }
public void StoreSegment(Segment s) { lock (syncLock) { _StoreSegment(s); Monitor.PulseAll(syncLock); } }
void beginNextSegment() { incomingSegment = new Segment(); incomingSegment.Number = AwaitingSegmentNumber; incomingSegmentDataBuffer = new byte[MaxIncomingDataSize]; MemoryStream ms = new MemoryStream(incomingSegmentDataBuffer); bw = new BinaryWriter(ms); }
void _StoreSegment(Segment s) { try { string FN = FileNameForSegmentNumber(s.Number); FileStream fs = File.Create(FN, 1000); BinaryWriter bw = new BinaryWriter(fs); bw.Write(s.Data); bw.Flush(); bw.Close(); } catch { } // e.g. directory structure now erased, cannot store }
bool TryGetSegment(int index, int segmentNumber, ref Segment retrievedSegment, ref SegmentAvailabilities segAvailability) { if (store.HasSegment(index, segmentNumber)) { SendDebugMessage("Broker] Segment " + index+"-"+segmentNumber.ToString() + " is available in store - retrieving"); bool foo = store.TryGetSegmentByNumber(index, segmentNumber, ref retrievedSegment); // shouldn't block, as it's in the store segAvailability = SegmentAvailabilities.IsAvailable; return true; } // Is there a runner if ( Runner == null) { SendDebugMessage("Broker] require seek (runner stopped)"); segAvailability = SegmentAvailabilities.RequiresSeek; // require, in fact! return false; } // Store does not have segment. Is it coming soon? int difference2 = (index - Runner.AwaitingFileIndex); int difference = (segmentNumber - Runner.AwaitingSegmentNumber); if (difference2 < 0 || (difference2 ==0 && difference < 0)) // requested segment is in past { SendDebugMessage("Broker] Seg " + index + "-" + segmentNumber.ToString() + " is in the past - require seek."); segAvailability = SegmentAvailabilities.RequiresSeek; return false; } // if (difference >= NUMBER_OF_SEGMENTS_CONSIDERED_COMING_SOON) if (Request.NewLiveTV && (difference2 >= NUMBER_OF_SEGMENTS_CONSIDERED_COMING_SOON_NEWLIVETV)) { SendDebugMessage("Broker] Seg " + index + "-" + segmentNumber.ToString() + " is a huge " + difference2 + " indexfiles away from arrival - require seek."); segAvailability = SegmentAvailabilities.RequiresSeek; return false; } else if ((!Request.LiveTV && !Request.NewLiveTV && (difference >= NUMBER_OF_SEGMENTS_CONSIDERED_COMING_SOON)) || (Request.LiveTV && Request.NewLiveTV && (difference >= NUMBER_OF_SEGMENTS_CONSIDERED_COMING_SOON_LIVETV))) { SendDebugMessage("Broker] Seg " + index + "-" + segmentNumber.ToString() + " is a huge " + difference + " segs away from arrival - require seek."); segAvailability = SegmentAvailabilities.RequiresSeek; return false; } // WAIT FOR A SEGMENT ************************************************************** SendDebugMessage("Broker] Seg " + index + "-" + segmentNumber.ToString() + " is only " + difference2 + " indexfiles away from arrival - requesting from store, which will block..."); bool didGet = (store.TryGetSegmentByNumber(index, segmentNumber, ref retrievedSegment)); segAvailability = didGet ? SegmentAvailabilities.IsAvailable : SegmentAvailabilities.Cancelled; //segAvailability = segmentNumber == 99999 ? SegmentAvailabilities.IsAvailable : segAvailability; //NewLiveTV hack, s.t. non available waitsscreen gets displayed //return (didGet || segmentNumber==99999);//99999 is newlivtv hack return didGet; }
Segment _GetSegment(int segNumber) { Segment s = new Segment(); s.Number = segNumber; string FN = FileNameForSegmentNumber(s.Number); s.Data = FileToByteArray(FN); return s; }
void _StoreSegment(Segment s) { try { string FN = FileNameForSegmentNumber(s.Number, true);//FileIndex not interesting since only used for newlivetv, and that is generated by the new segmenter in livetvparts.cs FileStream fs = File.Create(FN, 1000); BinaryWriter bw = new BinaryWriter(fs); bw.Write(s.Data); bw.Flush(); bw.Close(); } catch { } // e.g. directory structure now erased, cannot store }
Segment _GetSegment(int index, int segNumber, bool doTranscode) { Segment s = new Segment(); s.Number = segNumber; s.FileIndex = index; string FN = FileNameForSegmentNumber(s.Number, doTranscode); s.Data = FileToByteArray(FN); if (s.Data == null) // retry cuz could be NewLiveTV { string FN2 = FileNameForNewLiveTVSegmentNumber(s.Number); s.Data = FileToByteArray(FN2); } return s; }
public bool TryGetSegmentByNumber(int index, int SegNumber, ref Segment seg, bool doTranscode) { if (doTranscode) { // TODO this has been added to make the latest ffmpeg work: Mayeb this should be rermoved if (newffmpeg && !NewLiveTV && SegNumber != 99999) { while (!DoesFileExistForSegmentNumber(0, SegNumber)) { } } lock (syncLock) { bool stopWaiting = false; if (!DoesFileExistForSegmentNumber(index, SegNumber)) { segmentsWaiting.Add("" + SegNumber); do { Monitor.Wait(syncLock); stopWaiting = (!segmentsWaiting.Contains("" + SegNumber)); } while ( (!DoesFileExistForSegmentNumber(index, SegNumber)) && (!stopWaiting) ); } if (stopWaiting) { if (SegNumber == 99999) // the ffmpeg runner is started now (for NewLiveTV and latest newffmpeg) { string workingFolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "RemotePotato"); workingFolderPath = Path.Combine(workingFolderPath + "\\static\\mediastreams\\", ID.ToString()); if (!Directory.Exists(workingFolderPath)) Directory.CreateDirectory(workingFolderPath); File.Delete(workingFolderPath + "\\seg-99999.ts"); File.Delete(workingFolderPath + "\\segment-99999.ts"); string toolkitfolder = Functions2.ToolkitFolder; File.Copy(toolkitfolder + "\\pleasewait.ts", workingFolderPath + "\\seg-99999.ts"); if (newffmpeg && !NewLiveTV) { File.Copy(toolkitfolder + "\\pleasewait.ts", workingFolderPath + "\\segment-99999.ts"); while (!DoesFileExistForSegmentNumber(0, 99999)) { } } } return false; } // It's arrived! Remove segments waiting flag segmentsWaiting.Remove("" + SegNumber); } } seg = _GetSegment(index, SegNumber, doTranscode); return true; }
private void SendSegmentData(string txtAction, ref BrowserSender browserSender, int ID, bool sendToBrowser, bool doTranscode) { bool LatestFFmpeg = false; if (txtAction.StartsWith("seg!")) //first wait screen segment for NewLiveTV { txtAction = "seg-99999.ts"; } else if (txtAction.StartsWith("seg$")) //first wait screen segment for latest FFMPEG { LatestFFmpeg = true; txtAction = "seg-99999.ts"; } bool IsNewLiveTV = txtAction.StartsWith("liveseg"); txtAction = txtAction.Replace(".ts", ""); // remove extension // Get segment number string strSegNumber; List<string> parts = txtAction.Split('-').ToList(); if (parts.Count > 1) strSegNumber = parts[1]; else { if (!sendToBrowser) return; browserSender.Send404Page(); return; } int iSegNumber = 0; if (!int.TryParse(strSegNumber, out iSegNumber)) { if (!sendToBrowser) return; browserSender.Send404Page(); return; } int iIndexNumber = 0; //if (IsNewLiveTV) //{ // iIndexNumber = SegmentTabel.getIndex(""+ID, iSegNumber); // iSegNumber = SegmentTabel.getSegment(iIndexNumber, iSegNumber); //} byte[] TSdata = new byte[] { }; string txtError = ""; if (LatestFFmpeg && strSegNumber.Equals("99999")) { StreamingManager.Default.SegmentFromStreamer(iIndexNumber, ID, iSegNumber, ref TSdata, ref txtError, doTranscode); //start up the runners Functions.WriteLineToLogFileIfSetting(Settings.Default.DebugStreaming, "Sending please wait screen"); Segment s = new Segment(); string FN = Functions.ToolkitFolder + "\\pleasewait.ts"; if (!sendToBrowser) return; s.Data = FileToByteArray(FN); browserSender.SendDataToBrowser("video/mp2t", s.Data); } else { if (IsNewLiveTV && strSegNumber.Equals("99999")) { Functions.WriteLineToLogFileIfSetting(Settings.Default.DebugStreaming, "Could not get streaming segment number " + strSegNumber + "but live tv, so continuing"); if (!sendToBrowser) return; // browserSender.Send404Page(); Functions.WriteLineToLogFileIfSetting(Settings.Default.DebugStreaming, "Sending please wait screen"); Segment s = new Segment(); string FN = Functions.ToolkitFolder + "\\pleasewait.ts"; s.Data = FileToByteArray(FN); browserSender.SendDataToBrowser("video/mp2t", s.Data); } else if (StreamingManager.Default.SegmentFromStreamer(iIndexNumber, ID, iSegNumber, ref TSdata, ref txtError, doTranscode)) { { if (!sendToBrowser) return; browserSender.SendDataToBrowser("video/mp2t", TSdata); } return; } else { Functions.WriteLineToLogFileIfSetting(Settings.Default.DebugStreaming, "Could not get streaming segment number " + strSegNumber + ":" + txtError); if (!sendToBrowser) return; browserSender.Send404Page(); } } }
bool TryGetSegment(int segmentNumber, ref Segment retrievedSegment, ref SegmentAvailabilities segAvailability) { if (store.HasSegment(segmentNumber)) { SendDebugMessage("Broker] Segment " + segmentNumber.ToString() + " is available in store - retrieving"); bool foo = store.TryGetSegmentByNumber(segmentNumber, ref retrievedSegment); // shouldn't block, as it's in the store segAvailability = SegmentAvailabilities.IsAvailable; return true; } // Is there a runner if ( Runner == null) { SendDebugMessage("Broker] require seek (runner stopped)"); segAvailability = SegmentAvailabilities.RequiresSeek; // require, in fact! return false; } // Store does not have segment. Is it coming soon? int difference = (segmentNumber - Runner.AwaitingSegmentNumber); if (difference < 0) // requested segment is in past { SendDebugMessage("Broker] Seg " + segmentNumber.ToString() + " is in the past - require seek."); segAvailability = SegmentAvailabilities.RequiresSeek; return false; } // if (difference >= NUMBER_OF_SEGMENTS_CONSIDERED_COMING_SOON) if ((!Request.LiveTV && (difference >= NUMBER_OF_SEGMENTS_CONSIDERED_COMING_SOON)) || (Request.LiveTV && (difference >= NUMBER_OF_SEGMENTS_CONSIDERED_COMING_SOON_LIVETV))) { SendDebugMessage("Broker] Seg " + segmentNumber.ToString() + " is a huge " + difference + " segs away from arrival - require seek."); segAvailability = SegmentAvailabilities.RequiresSeek; return false; } // WAIT FOR A SEGMENT ************************************************************** SendDebugMessage("Broker] Seg " + segmentNumber.ToString() + " is only " + difference + " away from arrival - requesting from store, which will block..."); bool didGet = (store.TryGetSegmentByNumber(segmentNumber, ref retrievedSegment)); segAvailability = didGet ? SegmentAvailabilities.IsAvailable : SegmentAvailabilities.Cancelled; return didGet; }