public bool Stop(bool force) { if (IsStopped) { return(true); } if (!IsStarted && !force) { Start(); } foreach (int i in dataUnits.Keys.OrderBy(k => k)) { StreamLog.Info(identifier, "Stopping data unit {0}", i); dataUnits[i].Stop(); } foreach (int i in logUnits.Keys.OrderBy(k => k)) { logUnits[i].Stop(); } StreamLog.Debug(identifier, "Pipeline stopped"); IsStopped = true; return(true); }
public Stream RetrieveStream(string identifier) { if (!Streams.ContainsKey(identifier)) { Log.Warn("Client called RetrieveStream() for non-existing identifier", identifier); WCFUtil.SetResponseCode(HttpStatusCode.NotFound); return(Stream.Null); } lock (Streams[identifier]) { WCFUtil.SetContentType(Streams[identifier].Context.Profile.MIME); if (Streams[identifier].OutputStream == null) { StreamLog.Warn(identifier, "Encountered null stream in RetrieveStream"); WCFUtil.SetResponseCode(HttpStatusCode.NotFound); return(Stream.Null); } WCFUtil.SetContentType(Streams[identifier].Context.Profile.MIME); Streams[identifier].LastActivity = DateTime.Now; if (Streams[identifier].Transcoder is IRetrieveHookTranscoder) { (Streams[identifier].Transcoder as IRetrieveHookTranscoder).RetrieveStreamCalled(); } return(Streams[identifier].OutputStream); } }
public bool InitStream(string identifier, string clientDescription, MediaSource source, int timeout) { if (!source.Exists) { StreamLog.Warn(identifier, "Tried to start stream for non-existing file {0}", source.GetDebugName()); return(false); } ActiveStream stream = new ActiveStream(); stream.Identifier = identifier; stream.ClientDescription = clientDescription; stream.StartTime = DateTime.Now; stream.Timeout = timeout; stream.LastActivity = DateTime.Now; stream.UseActivityForTimeout = false; stream.Context = new StreamContext(); stream.Context.Identifier = identifier; stream.Context.Source = source; stream.Context.IsTv = source.MediaType == WebMediaType.TV; // Some clients such as WebMP proxy the streams before relying it to the client. We should give these clients the option to // forward the real IP address, so that we can show that one in the configurator too. However, to avoid abuse, we should show // the real IP of the client too, so make up some nice text string. string realIp = WCFUtil.GetHeaderValue("forwardedFor", "X-Forwarded-For"); stream.ClientIP = realIp == null?WCFUtil.GetClientIPAddress() : String.Format("{0} (via {1})", realIp, WCFUtil.GetClientIPAddress()); Streams[identifier] = stream; return(true); }
public bool Start() { if (!IsAssembled) { Assemble(); } IsStarted = true; foreach (int i in dataUnits.Keys.OrderBy(k => k)) { StreamLog.Info(identifier, "Starting data unit {0}", i); if (!dataUnits[i].Start()) { StreamLog.Error(identifier, "Starting data unit {0} failed", i); Stop(true); return(false); } } StreamLog.Info(identifier, "All data units started!"); foreach (int i in logUnits.Keys.OrderBy(k => k)) { logUnits[i].Start(); } return(true); }
public virtual Stream ProvideCustomActionFile(string action, string param) { switch (action) { case "segment": if (keepSegments > 0) { RemoveOldSegments(param); } WCFUtil.SetContentType("video/MP2T"); string segmentPath = Path.Combine(TemporaryDirectory, Path.GetFileName(param)); if (!File.Exists(segmentPath)) { StreamLog.Warn(Identifier, "HTTPLiveStreamer: Client requested non-existing segment file {0}", segmentPath); return(Stream.Null); } return(new FileStream(segmentPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete)); case "playlist": WCFUtil.SetContentType("application/vnd.apple.mpegurl"); string playlistPath = Path.Combine(TemporaryDirectory, "index.m3u8"); if (!File.Exists(playlistPath)) { StreamLog.Warn(Identifier, "HTTPLiveStreamer: Client requested index.m3u8 that doesn't exist"); return(Stream.Null); } // Having CRLF instead of LF in the playlist is allowed by the spec, but causes problems for VLC during playback, so strip them. return(StripCarriageReturn(playlistPath)); default: StreamLog.Warn(Identifier, "HTTPLiveStreamer: Request invalid action '{0}' with param '{1}'", action, param); return(Stream.Null); } }
private void RemoveOldSegments(string currentRequest) { int currentSegmentNumber; if (!Int32.TryParse(currentRequest.Replace(".ts", ""), out currentSegmentNumber) || currentSegmentNumber - keepSegments <= 0) { return; } string filename = (currentSegmentNumber - keepSegments).ToString().PadLeft(currentRequest.Length - 3, '0') + ".ts"; string removeFileName = Path.Combine(TemporaryDirectory, filename); if (File.Exists(removeFileName)) { StreamLog.Trace(Identifier, "HTTPLiveStreamer: Remove old segment {0} during request for {1}", filename, currentRequest); File.Delete(removeFileName); } }
private void TimeoutStreamsTick(object source, ElapsedEventArgs args) { try { var toDelete = Streams .Where(x => (x.Value.UseActivityForTimeout || (x.Value.OutputStream != null && x.Value.OutputStream.TimeSinceLastRead > (x.Value.Timeout * 1000))) && (x.Value.LastActivity.Add(TimeSpan.FromSeconds(x.Value.Timeout)) < DateTime.Now) ) .Select(x => x.Value.Identifier) .ToList(); if (toDelete.Count > 0) { foreach (string key in toDelete) { // The stream could've been terminated between the moment we decide it should be terminated and we get here. if (!Streams.ContainsKey(key)) { continue; } if (Streams[key].UseActivityForTimeout) { StreamLog.Info(key, "Stream had last service activity at {0}, so cancel it", Streams[key].LastActivity); } else { StreamLog.Info(key, "Stream had last read {0} milliseconds ago (read {1} bytes in total) and last service activity at {2}, so cancel it", Streams[key].OutputStream.TimeSinceLastRead, Streams[key].OutputStream.ReadBytes, Streams[key].LastActivity); } service.FinishStream(key); } } } catch (Exception ex) { Log.Warn("Error in TimeoutStreamsTick", ex); } }
public Streaming(StreamingService serviceInstance) { service = serviceInstance; sharing = new WatchSharing(); timeoutTimer = new Timer() { AutoReset = true, Interval = 1000, }; timeoutTimer.Elapsed += new ElapsedEventHandler(TimeoutStreamsTick); timeoutTimer.Start(); ServiceState.Stopping += delegate() { foreach (var identifier in Streams.Select(x => x.Value.Identifier).ToList()) { StreamLog.Warn(identifier, "Killing stream because of service stop"); KillStream(identifier); } }; }
public void EndStream(string identifier) { if (!Streams.ContainsKey(identifier) || Streams[identifier] == null || Streams[identifier].Context.Pipeline == null || !Streams[identifier].Context.Pipeline.IsStarted) { return; } try { lock (Streams[identifier]) { StreamLog.Debug(identifier, "Stopping stream"); sharing.EndStream(Streams[identifier].Context.Source); Streams[identifier].Context.Pipeline.Stop(); Streams[identifier].Context.Pipeline = null; } } catch (Exception ex) { StreamLog.Error(identifier, "Failed to stop stream", ex); } }
public void KillStream(string identifier) { EndStream(identifier); Streams.Remove(identifier); StreamLog.Debug(identifier, "Killed stream"); }
public string StartStream(string identifier, TranscoderProfile profile, long position = 0, int audioId = STREAM_DEFAULT, int subtitleId = STREAM_DEFAULT) { // there's a theoretical race condition here between the insert in InitStream() and this, but the client should really, really // always have a positive result from InitStream() before continuing, so their bad that the stream failed. if (!Streams.ContainsKey(identifier) || Streams[identifier] == null) { Log.Warn("Stream requested for invalid identifier {0}", identifier); return(null); } if (profile == null) { StreamLog.Warn(identifier, "Stream requested for non-existent profile"); return(null); } try { lock (Streams[identifier]) { StreamLog.Debug(identifier, "StartStream for file {0}", Streams[identifier].Context.Source.GetDebugName()); // initialize stream and context ActiveStream stream = Streams[identifier]; stream.Context.StartPosition = position; stream.Context.Profile = profile; stream.UseActivityForTimeout = profile.Transport == "httplive"; stream.Context.MediaInfo = MediaInfoHelper.LoadMediaInfoOrSurrogate(stream.Context.Source); stream.Context.OutputSize = CalculateSize(stream.Context); StreamLog.Debug(identifier, "Using {0} as output size for stream {1}", stream.Context.OutputSize, identifier); Reference <WebTranscodingInfo> infoRef = new Reference <WebTranscodingInfo>(() => stream.Context.TranscodingInfo, x => { stream.Context.TranscodingInfo = x; }); sharing.StartStream(stream.Context, infoRef); // get transcoder stream.Transcoder = (ITranscoder)Activator.CreateInstance(Type.GetType(profile.Transcoder)); stream.Transcoder.Identifier = identifier; stream.Transcoder.Context = stream.Context; // get audio and subtitle id if (stream.Context.MediaInfo.AudioStreams.Any(x => x.ID == audioId)) { stream.Context.AudioTrackId = stream.Context.MediaInfo.AudioStreams.First(x => x.ID == audioId).ID; } else if (audioId == STREAM_DEFAULT) { string preferredLanguage = Configuration.Streaming.DefaultAudioStream; if (stream.Context.MediaInfo.AudioStreams.Any(x => x.Language == preferredLanguage)) { stream.Context.AudioTrackId = stream.Context.MediaInfo.AudioStreams.First(x => x.Language == preferredLanguage).ID; } else if (preferredLanguage != "none" && stream.Context.MediaInfo.AudioStreams.Any()) { stream.Context.AudioTrackId = stream.Context.MediaInfo.AudioStreams.First().ID; } } if (stream.Context.MediaInfo.SubtitleStreams.Any(x => x.ID == subtitleId)) { stream.Context.SubtitleTrackId = stream.Context.MediaInfo.SubtitleStreams.First(x => x.ID == subtitleId).ID; } else if (subtitleId == STREAM_DEFAULT) { string preferredLanguage = Configuration.Streaming.DefaultSubtitleStream; if (stream.Context.MediaInfo.SubtitleStreams.Any(x => x.Language == preferredLanguage)) { stream.Context.SubtitleTrackId = stream.Context.MediaInfo.SubtitleStreams.First(x => x.Language == preferredLanguage).ID; } else if (preferredLanguage == "external" && stream.Context.MediaInfo.SubtitleStreams.Any(x => x.Filename != null)) { stream.Context.SubtitleTrackId = stream.Context.MediaInfo.SubtitleStreams.First(x => x.Filename != null).ID; } else if (preferredLanguage == "first" && stream.Context.MediaInfo.SubtitleStreams.Any()) { stream.Context.SubtitleTrackId = stream.Context.MediaInfo.SubtitleStreams.First().ID; } } StreamLog.Debug(identifier, "Final stream selection: audioId={0}, subtitleId={1}", stream.Context.AudioTrackId, stream.Context.SubtitleTrackId); // build the pipeline stream.Context.Pipeline = new Pipeline(identifier); stream.Context.TranscodingInfo = new WebTranscodingInfo(); stream.Transcoder.BuildPipeline(); // start the pipeline, but imm bool assembleResult = stream.Context.Pipeline.Assemble(); bool startResult = assembleResult ? stream.Context.Pipeline.Start() : true; if (!assembleResult || !startResult) { StreamLog.Warn(identifier, "Starting pipeline failed"); return(null); } // get the final stream and return it Stream finalStream = Streams[identifier].Context.Pipeline.GetFinalStream(); if (finalStream != null) { Streams[identifier].OutputStream = new ReadTrackingStreamWrapper(finalStream); } StreamLog.Info(identifier, "Started stream"); Streams[identifier].LastActivity = DateTime.Now; return(stream.Transcoder.GetStreamURL()); } } catch (Exception ex) { StreamLog.Error(identifier, "Failed to start stream", ex); return(null); } }
public bool Assemble() { IsAssembled = true; Dictionary <int, int> dataConnections = new Dictionary <int, int>(); Dictionary <int, int> logConnections = new Dictionary <int, int>(); int lastKey = -1; foreach (int i in dataUnits.Keys.OrderBy(k => k)) { if (dataUnits.ContainsKey(lastKey)) { dataConnections[lastKey] = i; dataUnits[i].IsInputStreamConnected = true; dataUnits[lastKey].IsDataStreamConnected = true; } lastKey = i; } foreach (int i in logUnits.Keys.OrderBy(k => k)) { int nr = dataUnits.Keys.Where(k => k < i).DefaultIfEmpty(-1).Max(); if (dataUnits.ContainsKey(nr)) { logConnections[i] = nr; dataUnits[nr].IsLogStreamConnected = true; } } // connect the output of the last one as that's usually captured by the calling app (because else the whole pipeline is just useless) dataUnits[dataUnits.Keys.Max()].IsDataStreamConnected = true; // dump out the pipeline for debugging StreamLog.Info(identifier, "Assembling following pipeline:"); foreach (int i in dataUnits.Keys.OrderBy(k => k)) { StreamLog.Info(identifier, " data {0}: {1} (input {2}, data {3}, log {4})", i, dataUnits[i].ToString(), dataUnits[i].IsInputStreamConnected, dataUnits[i].IsDataStreamConnected, dataUnits[i].IsLogStreamConnected); } foreach (KeyValuePair <int, int> conn in dataConnections) { StreamLog.Info(identifier, " dataconn {0} -> {1}", conn.Key, conn.Value); } foreach (int i in logUnits.Keys.OrderBy(k => k)) { StreamLog.Info(identifier, " log {0}: {1}", i, logUnits[i].ToString()); } foreach (KeyValuePair <int, int> conn in logConnections) { StreamLog.Info(identifier, " logconn {0} -> {1}", conn.Value, conn.Key); } foreach (int i in dataUnits.Keys.OrderBy(k => k)) { if (!dataUnits[i].Setup()) { // it failed, stop and break out StreamLog.Error(identifier, "Setting up data unit {0} failed", i); Stop(true); return(false); } else { StreamLog.Debug(identifier, "Setup data unit {0}", i); } if (dataConnections.ContainsKey(i)) { dataUnits[dataConnections[i]].InputStream = dataUnits[i].DataOutputStream; } } foreach (int i in logUnits.Keys.OrderBy(k => k)) { if (logConnections.ContainsKey(i)) { logUnits[i].InputStream = dataUnits[logConnections[i]].LogOutputStream; } logUnits[i].Setup(); StreamLog.Debug(identifier, "Setup log unit {0}", i); } StreamLog.Info(identifier, "Pipeline assembled"); return(true); }