public static WebMediaInfo GetMediaInfo(MediaSource source) { if (source.MediaType == WebStreamMediaType.TV) { // cache tv files for 10 seconds if (TvCache.ContainsKey(source.Id) && TvCache[source.Id].Item1.AddSeconds(10).CompareTo(DateTime.Now) > 0) { // cache is valid, use it return(TvCache[source.Id].Item2); } // get media info and save it to the cache TsBuffer buf = new TsBuffer(source.Id); WebMediaInfo info = GetMediaInfo(buf.GetCurrentFilePath(), true); TvCache[source.Id] = new Tuple <DateTime, WebMediaInfo>(DateTime.Now, info); return(info); } else if (!source.Exists) { throw new FileNotFoundException(); } else if (source.SupportsDirectAccess) { using (var impersonator = source.GetImpersonator()) { return(GetMediaInfo(source.GetPath(), false)); } } else { // not (yet?) supported throw new NotSupportedException(); } }
public static WebMediaInfo GetMediaInfo(MediaSource source) { // we can't use our persistent cache for TV unfortunately, but we do cache them in memory for 60 seconds if (source.MediaType == WebMediaType.TV) { if (tvCache.ContainsKey(source.Id) && DateTime.Now - tvCache[source.Id].Item1 > TimeSpan.FromSeconds(60)) { return(tvCache[source.Id].Item2); } // save it to our memory cache for a while TsBuffer buf = new TsBuffer(source.Id); string path = buf.GetCurrentFilePath(); Log.Debug("Using path {0} from TS buffer {1} as source for {2}", path, source.Id, source.GetDebugName()); WebMediaInfo info = DoLoadMediaInfo(buf.GetCurrentFilePath(), true); tvCache[source.Id] = new Tuple <DateTime, WebMediaInfo>(DateTime.Now, info); return(info); } // load this item from persistent disk cache, if possible if (persistentCache.HasForSource(source)) { return(persistentCache.GetForSource(source)); } // some checks that only matter when we are actually going to load it from disk if (!source.Exists) { Log.Warn("Trying to load mediainfo for {0}, which doesn't seem to exist", source.GetDebugName()); return(null); } else if (!source.SupportsDirectAccess) { // not (yet?) supported Log.Warn("Loading mediainfo for non-direct access source {0} isn't supported yet", source.GetDebugName()); return(null); } // actually load it WebMediaInfo outInfo; using (var impersonator = source.GetImpersonator()) { outInfo = DoLoadMediaInfo(source.GetPath(), false); } if (outInfo != null) { persistentCache.Save(source, outInfo); } return(outInfo); }
public static WebMediaInfo GetMediaInfo(MediaSource source) { // Timeshiftings are a special case, as they can't be cached and need an additional path resolving step if (source.MediaType == WebMediaType.TV) { if (tvCache.ContainsKey(source.Id) && DateTime.Now - tvCache[source.Id].Item1 > TimeSpan.FromSeconds(60)) { return(tvCache[source.Id].Item2); } TsBuffer tsBuffer = new TsBuffer(source.Id); Log.Debug("Using path {0} from TS buffer {1} as source for {2}", tsBuffer.GetCurrentFilePath(), source.Id, source.GetDebugName()); WebMediaInfo info = LoadMediaInfo(tsBuffer.GetCurrentFilePath()); tvCache[source.Id] = new Tuple <DateTime, WebMediaInfo>(DateTime.Now, info); return(info); } using (var context = source.CreateNetworkContext()) { // verify the file actually exists and is accessible over the local file system if (!source.Exists) { Log.Warn("Trying to load MediaInfo for {0}, which does not exist or is inaccessible", source.GetDebugName()); return(null); } else if (!source.SupportsDirectAccess) { Log.Warn("Loading MediaInfo for non-direct access source {0} isn't supported yet", source.GetDebugName()); return(null); } // if we got the file in the cache, return it if we have it and the file hasn't been changed var fileInfo = source.GetFileInfo(); if (source.MediaType != WebMediaType.TV && persistentCache.HasForSource(source)) { var cachedItem = persistentCache.GetForSource(source); if (cachedItem.Size == fileInfo.Size && cachedItem.CachedDate >= fileInfo.LastModifiedTime) { return(cachedItem.Info); } } var info = LoadMediaInfo(context.RewritePath(source.GetPath())); if (info != null) { persistentCache.Save(source, new CachedInfoWrapper(info, fileInfo)); } return(info); } }
public bool Setup() { try { if (source.IndexOf(".ts.tsbuffer") != -1) { Log.Info("Using TsBuffer to read input"); DataOutputStream = new TsBuffer(this.source); } else { Log.Info("Using FileStream to read input"); DataOutputStream = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); } } catch (Exception e) { Log.Error("Failed to setup InputProcessingUnit", e); return(false); } return(true); }
public static WebMediaInfo GetMediaInfo(TsBuffer buffer) { string path = buffer.GetCurrentFilePath(); return(GetMediaInfo(path, true)); }
protected void Page_Load(object sender, EventArgs e) { string filename = ""; List <EncoderConfig> cfgs = Utils.LoadConfig(); EncoderConfig cfg = cfgs[Int32.Parse(Request.QueryString["idProfile"])]; ServiceInterface server = new ServiceInterface(); if (Request.QueryString["idChannel"] != null) { if (server.GetChannel(Int32.Parse(Request.QueryString["idChannel"])).isRadio) { bufferSize = 2560; } WebTvResult res = server.StartTimeShifting(Int32.Parse(Request.QueryString["idChannel"])); if (res.result != 0) { Utils.Log("Streamer.aspx: ERROR: StartTimeShifting failed with error: " + res.result.ToString()); Response.Output.WriteLine("ERROR: StartTimeShifting failed with error: " + res.result.ToString()); Response.End(); return; } usedCard = res.user.idCard; usedChannel = res.user.idChannel; tvServerUsername = res.user.name; if (cfg.inputMethod == TransportMethod.Filename) { filename = res.rtspURL; } else { filename = res.timeshiftFile; } } else if (Request.QueryString["idRecording"] != null) { WebRecording rec = server.GetRecording(Int32.Parse(Request.QueryString["idRecording"])); filename = rec.fileName; } else if (Request.QueryString["idMovie"] != null) { WebMovie movie = server.GetMovie(Int32.Parse(Request.QueryString["idMovie"])); filename = movie.file; } else if (Request.QueryString["idMusicTrack"] != null) { WebMusicTrack track = server.GetMusicTrack(Int32.Parse(Request.QueryString["idMusicTrack"])); filename = track.file; } else if (Request.QueryString["idTvSeries"] != null) { WebSeries series = server.GetTvSeries(Request.QueryString["idTvSeries"]); filename = series.filename; } else if (Request.QueryString["idMovingPicture"] != null) { WebMovingPicture pic = server.GetMovingPicture(Int32.Parse(Request.QueryString["idMovingPicture"])); filename = pic.filename; } if (!File.Exists(filename) && !filename.StartsWith("rtsp://")) { Utils.Log("Streamer.aspx: Requested file " + filename + " does not exist."); return; } Response.Clear(); Response.Buffer = false; Response.BufferOutput = false; Response.AppendHeader("Connection", "Keep-Alive"); Response.ContentType = "video/x-msvideo"; Response.StatusCode = 200; Stream mediaStream = null; EncoderWrapper encoder = null; Stream outStream = null; if (cfg.inputMethod != TransportMethod.Filename) { if (Request.QueryString["idChannel"] != null) { mediaStream = new TsBuffer(filename); } else { mediaStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); } encoder = new EncoderWrapper(mediaStream, cfg); } else { encoder = new EncoderWrapper(filename, cfg); } if (cfg.useTranscoding) { outStream = encoder; } else { outStream = mediaStream; } byte[] buffer = new byte[bufferSize]; int read; try { while ((read = outStream.Read(buffer, 0, buffer.Length)) > 0) { try { Response.OutputStream.Write(buffer, 0, read); } catch (Exception) { break; // stream is closed } if (Request.QueryString["idChannel"] != null) { server.SendHeartBeat(usedChannel, usedCard, tvServerUsername); } } } catch (Exception ex) { Utils.Log("Streamer.aspx: Exception raised=" + ex.Message + Environment.NewLine + ex.StackTrace); } if (mediaStream != null) { mediaStream.Close(); } if (Request.QueryString["idChannel"] != null) { server.StopTimeShifting(usedChannel, usedCard, tvServerUsername); } encoder.StopProcess(); Response.End(); }