public Resolution CalculateSize(TranscoderProfile profile, MediaSource source, WebMediaInfo info = null) { try { if (!profile.HasVideoStream) { return(new Resolution(0, 0)); } if (info == null) { info = MediaInfoHelper.LoadMediaInfoOrSurrogate(source); } if (info.VideoStreams.Count > 0) { var res = Resolution.Calculate(info.VideoStreams.First().DisplayAspectRatio, profile.MaxOutputWidth, profile.MaxOutputHeight, 2); if (res.Width == 0 && res.Height == 0) { return(new Resolution(info.VideoStreams.First().Width, info.VideoStreams.First().Height)); } return(res); } } catch (Exception ex) { Log.Warn("Failed to calculate size of output stream", ex); } // default return(Resolution.Calculate(MediaInfoHelper.DEFAULT_ASPECT_RATIO, profile.MaxOutputWidth, profile.MaxOutputHeight, 2)); }
public static ITranscoder GetTranscoder(this TranscoderProfile profile) { // For now only support files in current assembly ITranscoder inst = (ITranscoder)Activator.CreateInstance(Type.GetType(profile.TranscoderImplementationClass)); return(inst); }
public Resolution CalculateSize(TranscoderProfile profile, MediaSource source, WebMediaInfo info = null) { try { if (!profile.HasVideoStream) return new Resolution(0, 0); if (info == null) { info = MediaInfoHelper.LoadMediaInfoOrSurrogate(source); } if (info.VideoStreams.Count > 0) { return Resolution.Calculate(info.VideoStreams.First().DisplayAspectRatio, profile.MaxOutputWidth, profile.MaxOutputHeight, 2); } } catch (Exception ex) { Log.Warn("Failed to calculate size of output stream", ex); } // default return Resolution.Calculate(16 / 9, profile.MaxOutputWidth, profile.MaxOutputHeight, 2); }
private List <TranscoderProfile> ParseAndMergeTranscoders(XElement oldFile) { // load all the transcoders from the old file var list = new List <TranscoderProfile>(); foreach (var node in oldFile.Elements("transcoders").Elements("transcoder")) { var profile = new TranscoderProfile() { Name = node.Element("name").Value, Description = node.Element("description").Value, Bandwidth = Int32.Parse(node.Element("bandwidth").Value), Targets = new List <string>() { node.Element("target").Value }, Transport = node.Element("transport").Value, MaxOutputHeight = node.Element("maxOutputHeight") != null?Int32.Parse(node.Element("maxOutputHeight").Value) : 0, MaxOutputWidth = node.Element("maxOutputWidth") != null?Int32.Parse(node.Element("maxOutputWidth").Value) : 0, MIME = node.Element("mime").Value, HasVideoStream = node.Element("videoStream").Value == "true", Transcoder = (string)node.Element("transcoderConfiguration").Attribute("implementation") }; foreach (var configNode in node.Elements("transcoderConfiguration").Descendants()) { profile.TranscoderParameters[configNode.Name.LocalName] = configNode.Value; } list.Add(profile); } // parse the new default file var defaultSerializer = new StreamingSerializer(); Streaming defaultModel; using (var file = File.OpenRead(DefaultPath)) { defaultModel = (Streaming)defaultSerializer.Deserialize(file); } // merge the lists var finalList = defaultModel.Transcoders; foreach (var oldProfile in list) { if (!finalList.Any(x => x.Name == oldProfile.Name)) { finalList.Add(oldProfile); } } return(finalList); }
public static WebTranscoderProfile ToWebTranscoderProfile(this TranscoderProfile profile) { // WCF sucks a bit with returning child classes return(new WebTranscoderProfile() { Bandwidth = profile.Bandwidth, Description = profile.Description, HasVideoStream = profile.HasVideoStream, MaxOutputHeight = profile.MaxOutputHeight, MaxOutputWidth = profile.MaxOutputWidth, MIME = profile.MIME, Name = profile.Name, Targets = profile.Targets, Transport = profile.Transport }); }
public Resolution CalculateSize(TranscoderProfile profile, MediaSource source, WebMediaInfo info = null) { if (!profile.HasVideoStream) { return(new Resolution(0, 0)); } decimal aspect = (decimal)16 / 9; // the default aspect ratio if (source.MediaType != WebStreamMediaType.TV && profile != null) { if (info == null) { info = MediaInfoWrapper.GetMediaInfo(source); } if (info.VideoStreams.Count > 0) { aspect = info.VideoStreams.First().DisplayAspectRatio; } } return(Resolution.Calculate(aspect, profile.MaxOutputWidth, profile.MaxOutputHeight, 2)); }
public string StartStream(string identifier, TranscoderProfile profile, int 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) { Log.Warn("Stream requested for non-existent profile"); return(null); } try { lock (Streams[identifier]) { Log.Trace("StartStream called with identifier {0}", identifier); // initialize stream and context ActiveStream stream = Streams[identifier]; stream.Context.StartPosition = position; stream.Context.Profile = profile; stream.Context.MediaInfo = MediaInfoHelper.LoadMediaInfoOrSurrogate(stream.Context.Source); stream.Context.OutputSize = CalculateSize(stream.Context); Reference <WebTranscodingInfo> infoRef = new Reference <WebTranscodingInfo>(() => stream.Context.TranscodingInfo, x => { stream.Context.TranscodingInfo = x; }); Log.Trace("Using {0} as output size for stream {1}", stream.Context.OutputSize, identifier); sharing.StartStream(stream.Context, infoRef); // get transcoder stream.Transcoder = profile.GetTranscoder(); stream.Transcoder.Identifier = identifier; // get audio and subtitle id if (stream.Context.MediaInfo.AudioStreams.Where(x => x.ID == audioId).Count() > 0) { stream.Context.AudioTrackId = stream.Context.MediaInfo.AudioStreams.Where(x => x.ID == audioId).First().ID; } else if (audioId == STREAM_DEFAULT) { string preferredLanguage = Configuration.Streaming.DefaultAudioStream; if (stream.Context.MediaInfo.AudioStreams.Count(x => x.Language == preferredLanguage) > 0) { stream.Context.AudioTrackId = stream.Context.MediaInfo.AudioStreams.First(x => x.Language == preferredLanguage).ID; } else if (preferredLanguage != "none" && stream.Context.MediaInfo.AudioStreams.Count() > 0) { stream.Context.AudioTrackId = stream.Context.MediaInfo.AudioStreams.First().ID; } } if (stream.Context.MediaInfo.SubtitleStreams.Where(x => x.ID == subtitleId).Count() > 0) { stream.Context.SubtitleTrackId = stream.Context.MediaInfo.SubtitleStreams.Where(x => x.ID == subtitleId).First().ID; } else if (subtitleId == STREAM_DEFAULT) { string preferredLanguage = Configuration.Streaming.DefaultSubtitleStream; if (stream.Context.MediaInfo.SubtitleStreams.Count(x => x.Language == preferredLanguage) > 0) { stream.Context.SubtitleTrackId = stream.Context.MediaInfo.SubtitleStreams.First(x => x.Language == preferredLanguage).ID; } else if (preferredLanguage == "external" && stream.Context.MediaInfo.SubtitleStreams.Count(x => x.Filename != null) > 0) { stream.Context.SubtitleTrackId = stream.Context.MediaInfo.SubtitleStreams.First(x => x.Filename != null).ID; } else if (preferredLanguage == "first" && stream.Context.MediaInfo.SubtitleStreams.Count() > 0) { stream.Context.SubtitleTrackId = stream.Context.MediaInfo.SubtitleStreams.First().ID; } } Log.Debug("Final stream selection: audioId={0}, subtitleId={1}", stream.Context.AudioTrackId, stream.Context.SubtitleTrackId); // build the pipeline stream.Context.Pipeline = new Pipeline(); stream.Context.TranscodingInfo = new WebTranscodingInfo(); stream.Transcoder.BuildPipeline(stream.Context); // start the processes and retrieve output stream stream.Context.Pipeline.Assemble(); stream.Context.Pipeline.Start(); Stream finalStream = Streams[identifier].Context.Pipeline.GetFinalStream(); if (finalStream != null) { Streams[identifier].OutputStream = new ReadTrackingStreamWrapper(finalStream); } Log.Info("Started stream with identifier " + identifier); return(stream.Transcoder.GetStreamURL()); } } catch (Exception ex) { Log.Error("Failed to start stream " + identifier, ex); return(null); } }
public Resolution CalculateSize(TranscoderProfile profile, decimal displayAspectRatio) { return(Resolution.Calculate(displayAspectRatio, profile.MaxOutputWidth, profile.MaxOutputHeight, 2)); }
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 string StartStream(string identifier, TranscoderProfile profile, int 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) { Log.Warn("Stream requested for non-existent profile"); return null; } try { lock (Streams[identifier]) { Log.Trace("StartStream called with identifier {0}", identifier); // initialize stream and context ActiveStream stream = Streams[identifier]; stream.Context.StartPosition = position; stream.Context.Profile = profile; stream.Context.MediaInfo = MediaInfoHelper.LoadMediaInfoOrSurrogate(stream.Context.Source); stream.Context.OutputSize = CalculateSize(stream.Context); Reference<WebTranscodingInfo> infoRef = new Reference<WebTranscodingInfo>(() => stream.Context.TranscodingInfo, x => { stream.Context.TranscodingInfo = x; }); Log.Trace("Using {0} as output size for stream {1}", stream.Context.OutputSize, identifier); sharing.StartStream(stream.Context, infoRef); // get transcoder stream.Transcoder = (ITranscoder)Activator.CreateInstance(Type.GetType(profile.TranscoderImplementationClass)); stream.Transcoder.Identifier = identifier; // get audio and subtitle id if (stream.Context.MediaInfo.AudioStreams.Where(x => x.ID == audioId).Count() > 0) { stream.Context.AudioTrackId = stream.Context.MediaInfo.AudioStreams.Where(x => x.ID == audioId).First().ID; } else if (audioId == STREAM_DEFAULT) { string preferredLanguage = Configuration.Streaming.DefaultAudioStream; if (stream.Context.MediaInfo.AudioStreams.Count(x => x.Language == preferredLanguage) > 0) { stream.Context.AudioTrackId = stream.Context.MediaInfo.AudioStreams.First(x => x.Language == preferredLanguage).ID; } else if (preferredLanguage != "none" && stream.Context.MediaInfo.AudioStreams.Count() > 0) { stream.Context.AudioTrackId = stream.Context.MediaInfo.AudioStreams.First().ID; } } if (stream.Context.MediaInfo.SubtitleStreams.Where(x => x.ID == subtitleId).Count() > 0) { stream.Context.SubtitleTrackId = stream.Context.MediaInfo.SubtitleStreams.Where(x => x.ID == subtitleId).First().ID; } else if (subtitleId == STREAM_DEFAULT) { string preferredLanguage = Configuration.Streaming.DefaultSubtitleStream; if (stream.Context.MediaInfo.SubtitleStreams.Count(x => x.Language == preferredLanguage) > 0) { stream.Context.SubtitleTrackId = stream.Context.MediaInfo.SubtitleStreams.First(x => x.Language == preferredLanguage).ID; } else if (preferredLanguage == "external" && stream.Context.MediaInfo.SubtitleStreams.Count(x => x.Filename != null) > 0) { stream.Context.SubtitleTrackId = stream.Context.MediaInfo.SubtitleStreams.First(x => x.Filename != null).ID; } else if (preferredLanguage == "first" && stream.Context.MediaInfo.SubtitleStreams.Count() > 0) { stream.Context.SubtitleTrackId = stream.Context.MediaInfo.SubtitleStreams.First().ID; } } Log.Debug("Final stream selection: audioId={0}, subtitleId={1}", stream.Context.AudioTrackId, stream.Context.SubtitleTrackId); // build the pipeline stream.Context.Pipeline = new Pipeline(); stream.Context.TranscodingInfo = new WebTranscodingInfo(); stream.Transcoder.BuildPipeline(stream.Context); // start the processes and retrieve output stream stream.Context.Pipeline.Assemble(); stream.Context.Pipeline.Start(); Stream finalStream = Streams[identifier].Context.Pipeline.GetFinalStream(); if (finalStream != null) { Streams[identifier].OutputStream = new ReadTrackingStreamWrapper(finalStream); } Log.Info("Started stream with identifier " + identifier); return stream.Transcoder.GetStreamURL(); } } catch (Exception ex) { Log.Error("Failed to start stream " + identifier, ex); return null; } }