public void ShouldDownloadManifestStream() { DownloaderManager downloaderManager = new DownloaderManager(); var stream = downloaderManager.DownloadManifest(this.manifestUri, true); Assert.IsNotNull(stream); }
public string GetSubClipManifest(Uri manifestUri, double markIn, double markOut) { string manifest = null; DownloaderManager downloaderManager = new DownloaderManager(); Stream manifestStream = downloaderManager.DownloadManifest(manifestUri, true, null); if (manifestStream != null) { SmoothStreamingManifestParser parser = new SmoothStreamingManifestParser(manifestStream); SmoothStreamingManifestWriter writer = new SmoothStreamingManifestWriter(); CompositeManifestInfo compositeManifestInfo = new CompositeManifestInfo(parser.ManifestInfo.MajorVersion, parser.ManifestInfo.MinorVersion); compositeManifestInfo.AddClip(manifestUri, (ulong)markIn, (ulong)markOut, parser.ManifestInfo); manifest = writer.GenerateCompositeManifest(compositeManifestInfo, false); } return manifest; }
public CreateTrainingSetResponse CreateTrainingSet(CreateTrainingSetRequest request) { var response = new CreateTrainingSetResponse(); try { var downloadManager = new DownloaderManager(); var compositeManifestInfo = new CompositeManifestInfo(2, 0); var videoRepository = serviceLocator.GetInstance <IVideoRepository>(); var catalogRepository = serviceLocator.GetInstance <ICatalogRepository>(); var templateRepository = serviceLocator.GetInstance <ITrainingSetTemplateRepository>(); var totalDuration = TimeSpan.Zero; foreach (var videoPart in request.TrainingSet.VideoParts) { var video = videoRepository.Get(videoPart.VideoId); var manifestStream = downloadManager.DownloadManifest(video.StreamUri, true, null); if (manifestStream != null) { var parser = new SmoothStreamingManifestParser(manifestStream); double startPosition = videoPart.From.TotalSeconds; double endPosition = videoPart.To.TotalSeconds; compositeManifestInfo.AddClip(video.StreamUri, (ulong)(startPosition * Timescale), (ulong)(endPosition * Timescale), parser.ManifestInfo); totalDuration = totalDuration.Add(videoPart.To.Subtract(videoPart.From)); } } var writer = new SmoothStreamingManifestWriter(); var manifest = writer.GenerateCompositeManifest(compositeManifestInfo, false, false); string csmPath = HttpContext.Current.Server.MapPath("csm"); if (!Directory.Exists(csmPath)) { Directory.CreateDirectory(csmPath); } var uniqueName = Guid.NewGuid().ToString(); string tmpFilePath = Path.Combine(csmPath, string.Format("{0}.tmpcsm", uniqueName)); string finalFilePath = Path.Combine(csmPath, string.Format("{0}.csm", uniqueName)); File.WriteAllText(tmpFilePath, manifest, Encoding.UTF8); if (File.Exists(tmpFilePath)) { if (File.Exists(finalFilePath)) { File.Delete(finalFilePath); } File.Move(tmpFilePath, finalFilePath); } var userService = serviceLocator.GetInstance <IApplicationUserService>(); var user = userService.RetrieveApplicationUser(new ApplicationUserFindCriteria() { Username = request.User }); var template = templateRepository.Get(request.TrainingSet.TrainingSetTemplateId); //var _telemetry = new List<Telemetry>(); var timer = TimeSpan.Zero; var recordingInterval = TimeSpan.FromSeconds(2); var telemetryFile = new StringBuilder().AppendLine("\"Minutes\",\"Torq(N-m)\",\"Km/h\",\"Watts\",\"Km\",\"Cadence\",\"Hrate\",\"ID\",\"Altitude(m)\""); foreach (var interval in template.Intervals) { var numberOfElements = interval.Duration.TotalSeconds / recordingInterval.TotalSeconds; for (int i = 0; i < numberOfElements; i++) { var telemetry = new Telemetry(); telemetry.PercentageThreshold = Convert.ToDouble(interval.Effort) / 100; telemetry.Watts = ((telemetry.PercentageThreshold) * user.FTP.GetValueOrDefault()); telemetry.TimePosition = timer; timer = timer.Add(recordingInterval); telemetryFile.AppendLine(telemetry.ToDelimitedString(',')); //telemetry.PercentageThreshold = Convert.ToDouble(interval.Effort); //telemetry.TimePosition = timer; //telemetryFile.AppendLine(telemetry.ToDelimitedString(',')); } } string telemetryPath = HttpContext.Current.Server.MapPath("telemetry"); if (!Directory.Exists(telemetryPath)) { Directory.CreateDirectory(telemetryPath); } string filePath = Path.Combine(telemetryPath, string.Format("{0}.csv", uniqueName)); File.WriteAllText(filePath, telemetryFile.ToString()); //var context = System.ServiceModel.OperationContext.Current; //RemoteEndpointMessageProperty property = (RemoteEndpointMessageProperty)context.IncomingMessageProperties[RemoteEndpointMessageProperty.Name]; string host = "www.indoorworx.com";// property.Address; //var host = "localhost"; //now create the video var workout = new Video(); workout.StreamUri = new Uri(string.Format("http://{0}/IndoorWorx/csm/{1}.csm", host, uniqueName), UriKind.Absolute); workout.Title = request.TrainingSet.Title; workout.Description = template.Description; workout.Duration = totalDuration; workout.TelemetryInfo.TelemetryUri = new Uri(string.Format("http://{0}/IndoorWorx/telemetry/{1}.csv", host, uniqueName), UriKind.Absolute); foreach (var text in template.VideoText) { workout.VideoText.Add(text.Clone()); } workout.Intervals = template.Intervals.Select(x => new VideoInterval(x.Duration, x.Effort, x.Sequence)).ToList(); var savedWorkout = videoRepository.Save(workout); userService.AddVideoToLibrary(new AddVideoRequest() { User = request.User, VideoId = savedWorkout.Id }); response.Status = CreateTrainingSetStatus.Success; response.TrainingSet = savedWorkout; } catch (Exception ex) { response.Status = CreateTrainingSetStatus.Error; response.Message = ex.StackTrace; //response.Message = ex.Message; } return(response); }
private IEnumerable <string> GetManifests(string pbpDataStreamName, string adsDataStreamName, bool compressManifest, string gapUriString, string gapCmsId, string gapAzureId, int sequenceNumber) { List <string> manifests = new List <string>(); this.gapUri = new Uri(gapUriString); DownloaderManager manager = new DownloaderManager(); string manifest = string.Empty; if (this.project.Sequences != null && this.project.Sequences.Count >= 1) { Stream gapStream = manager.DownloadManifest(this.gapUri, true); byte[] gapResult = null; using (BinaryReader reader = new BinaryReader(gapStream)) { gapResult = reader.ReadBytes((int)manager.LastResponseLength); } MemoryStream gapMemoryStream = null; try { gapMemoryStream = new MemoryStream(gapResult); Sequence sequence = this.project.Sequences[0]; if (this.project.Sequences.Count > sequenceNumber) { sequence = this.project.Sequences[sequenceNumber]; } foreach ( var track in sequence.Tracks.Where( t => (t.TrackType.Equals("visual", StringComparison.InvariantCultureIgnoreCase) || t.TrackType.Equals("audio", StringComparison.InvariantCultureIgnoreCase)) && t.Shots.Count > 0)) { CompositeManifestInfo compositeManifestInfo = new CompositeManifestInfo(2, 0); compositeManifestInfo.PlayByPlayDataStreamName = pbpDataStreamName; compositeManifestInfo.AdsDataStreamName = adsDataStreamName; compositeManifestInfo.RubberBandingDataStreamName = "RubberBanding"; compositeManifestInfo.TransitionDataStreamName = "Transition"; IDictionary <Shot, double> gapDurations = CalculateGapsDuration(track); foreach (Shot shot in track.Shots) { Resource resource = shot.Source.Resources.SingleOrDefault(x => !string.IsNullOrEmpty(x.Ref)); Uri assetUri; if (resource != null && Uri.TryCreate(resource.Ref, UriKind.Absolute, out assetUri)) { Stream manifestStream = manager.DownloadManifest(assetUri, true); MemoryStream stream = new MemoryStream(); if (manifestStream != null) { byte[] buffer = ReadFully(manifestStream); if (buffer != null) { var binaryWriter = new BinaryWriter(stream); binaryWriter.Write(buffer); } stream.Seek(0, SeekOrigin.Begin); } this.AddPreviousGap(gapDurations[shot], gapMemoryStream, gapCmsId, gapAzureId, compositeManifestInfo); AddClipToCompositeManifestInfo(shot, stream, compositeManifestInfo); this.AddRubberBandingPoints(shot, compositeManifestInfo); this.AddTransitions(shot, compositeManifestInfo); stream.Close(); if (track.TrackType.Equals("visual", StringComparison.InvariantCultureIgnoreCase)) { compositeManifestInfo.OverlayDataStreamName = "Overlay"; var overlaysTrack = sequence.Tracks.First(t => t.TrackType.Equals("Overlay")); foreach (Shot s in overlaysTrack.Shots) { this.AddOverlay(s, compositeManifestInfo); } if (sequence.AdOpportunities != null) { foreach ( RCE.Services.Contracts.AdOpportunity adOpportunity in sequence.AdOpportunities) { compositeManifestInfo.AddAdOpportunity( adOpportunity.ID, adOpportunity.TemplateType, adOpportunity.Time); } } if (sequence.MarkerCollection != null) { foreach (Marker marker in sequence.MarkerCollection) { compositeManifestInfo.AddPlayByPlay(marker.ID, marker.Text, marker.Time); } } } } } SmoothStreamingManifestWriter writer = new SmoothStreamingManifestWriter(); manifest = writer.GenerateCompositeManifest(compositeManifestInfo, false, compressManifest); manifests.Add(manifest); } } finally { if (gapMemoryStream != null) { gapMemoryStream.Close(); } } } return(manifests); }
public Metadata GetMetadata(object target) { SmoothStreamingMetadata metadata = new SmoothStreamingMetadata(); Uri smoothStreamingManifestUri = target as Uri; if (smoothStreamingManifestUri == null) { return(null); } Stream manifestStream; try { DownloaderManager manager = new DownloaderManager(); manifestStream = manager.DownloadManifest(smoothStreamingManifestUri, false); } catch (Exception) { return(null); } if (manifestStream != null) { SmoothStreamingManifestParser parser = new SmoothStreamingManifestParser(manifestStream); IEnumerable <StreamInfo> audioStreams = parser.ManifestInfo.Streams.Where(x => x.StreamType.Equals("audio", StringComparison.OrdinalIgnoreCase)); StreamInfo videoStreamInfo = parser.ManifestInfo.Streams.SingleOrDefault(x => x.StreamType.Equals("video", StringComparison.OrdinalIgnoreCase)); if (videoStreamInfo != null) { QualityLevel firstQualityLevel = videoStreamInfo.QualityLevels.FirstOrDefault(); string bitrate; string cameraAngle; if (firstQualityLevel != null && firstQualityLevel.Attributes.TryGetValue("Bitrate", out bitrate) && firstQualityLevel.CustomAttributes.TryGetValue("cameraAngle", out cameraAngle)) { List <string> cameraAngles = new List <string>(); foreach (QualityLevel qualityLevel in videoStreamInfo.QualityLevels) { if (qualityLevel.Attributes["Bitrate"] == bitrate && qualityLevel.CustomAttributes.TryGetValue("cameraAngle", out cameraAngle)) { cameraAngles.Add(cameraAngle); } } // order by string comparison cameraAngles.Sort(); if (cameraAngles.Count() > 0) { metadata.AddMetadataField(new MetadataField("VideoStreams", cameraAngles)); } } } metadata.AddMetadataField(new MetadataField("Duration", parser.ManifestInfo.ManifestDuration)); metadata.AddMetadataField(new MetadataField("AudioStreams", audioStreams)); } return(metadata); }
public string GetManifest(string projectXml, string pbpDataStreamName, string adsDataStreamName) { CompositeManifestInfo compositeManifestInfo = new CompositeManifestInfo(2, 0); compositeManifestInfo.PlayByPlayDataStreamName = pbpDataStreamName; compositeManifestInfo.AdsDataStreamName = adsDataStreamName; DownloaderManager manager = new DownloaderManager(); const ulong Timescale = 10000000; Project project; try { project = Deserialize<Project>(projectXml); } catch { return Resources.Resources.InvalidRCEProjectXml; } if (project.Timeline != null) { Track track = project.Timeline.SingleOrDefault(x => x.TrackType.ToUpperInvariant() == "VISUAL"); if (track != null && track.Shots != null) { foreach (Shot shot in track.Shots) { if (shot.Source != null && shot.Source is VideoItem && shot.Source.Resources.Count > 0 && shot.SourceAnchor != null) { Resource resource = shot.Source.Resources.SingleOrDefault(x => !String.IsNullOrEmpty(x.Ref)); Uri assetUri; if (resource != null && Uri.TryCreate(resource.Ref, UriKind.Absolute, out assetUri)) { Stream manifestStream = manager.DownloadManifest(assetUri, true, null); if (manifestStream != null) { double startPosition = (shot.Source is SmoothStreamingVideoItem) ? ((SmoothStreamingVideoItem)shot.Source).StartPosition : 0; SmoothStreamingManifestParser parser = new SmoothStreamingManifestParser(manifestStream); ulong clipBegin = (ulong)((shot.SourceAnchor.MarkIn.GetValueOrDefault() * Timescale) + (startPosition * Timescale)); ulong clipEnd = (ulong)((shot.SourceAnchor.MarkOut.GetValueOrDefault() * Timescale) + (startPosition * Timescale)); compositeManifestInfo.AddClip(assetUri, clipBegin, clipEnd, parser.ManifestInfo); } } } //if (shot.Source != null && shot.Source is ImageItem && shot.Source.Resources.Count > 0 && shot.SourceAnchor != null) //{ // Resource resource = shot.Source.Resources.SingleOrDefault(x => !String.IsNullOrEmpty(x.Ref)); // Uri assetUri; // if (resource != null && Uri.TryCreate(resource.Ref, UriKind.Absolute, out assetUri)) // { // Stream imageStream = manager.DownloadManifest(assetUri, true, null); // if (imageStream != null) // { // double startPosition = shot.SourceAnchor.MarkIn.GetValueOrDefault(); // //double startPosition = (shot.Source is SmoothStreamingVideoItem) ? ((SmoothStreamingVideoItem)shot.Source).StartPosition : 0; // SmoothStreamingManifestParser parser = new SmoothStreamingManifestParser(imageStream); // ulong clipBegin = (ulong)((shot.SourceAnchor.MarkIn.GetValueOrDefault() * Timescale) + (startPosition * Timescale)); // ulong clipEnd = (ulong)((shot.SourceAnchor.MarkOut.GetValueOrDefault() * Timescale) + (startPosition * Timescale)); // compositeManifestInfo.AddClip(assetUri, clipBegin, clipEnd, parser.ManifestInfo); // } // } } } } }
public void CreateCompositeStream(Contracts.Project project) { CompositeManifestInfo compositeManifestInfo = new CompositeManifestInfo(2, 1); compositeManifestInfo.PlayByPlayDataStreamName = "PBP"; compositeManifestInfo.AdsDataStreamName = "ADS"; DownloaderManager manager = new DownloaderManager(); const ulong Timescale = 10000000; if (project.Timeline != null) { Track track = project.Timeline.SingleOrDefault(x => x.TrackType.ToUpperInvariant() == "VISUAL"); if (track != null && track.Shots != null) { foreach (Shot shot in track.Shots) { if (shot.Source != null && shot.Source is VideoItem && shot.Source.Resources.Count > 0 && shot.SourceAnchor != null) { Resource resource = shot.Source.Resources.SingleOrDefault(x => !String.IsNullOrEmpty(x.Ref)); Uri assetUri; if (resource != null && Uri.TryCreate(resource.Ref, UriKind.Absolute, out assetUri)) { Stream manifestStream = manager.DownloadManifest(assetUri, true, null); if (manifestStream != null) { double startPosition = (shot.Source is SmoothStreamingVideoItem) ? ((SmoothStreamingVideoItem)shot.Source).StartPosition : 0; SmoothStreamingManifestParser parser = new SmoothStreamingManifestParser(manifestStream); ulong clipBegin = (ulong)((shot.SourceAnchor.MarkIn.GetValueOrDefault() * Timescale) + (startPosition * Timescale)); ulong clipEnd = (ulong)((shot.SourceAnchor.MarkOut.GetValueOrDefault() * Timescale) + (startPosition * Timescale)); compositeManifestInfo.AddClip(assetUri, clipBegin, clipEnd, parser.ManifestInfo); } } } } } } if (project.Titles != null) { foreach (var title in project.Titles) { //compositeManifestInfo } } if (project.AdOpportunities != null) { foreach (RCE.Services.Contracts.AdOpportunity adOpportunity in project.AdOpportunities) { compositeManifestInfo.AddAdOpportunity(adOpportunity.ID, adOpportunity.TemplateType, adOpportunity.Time); } } if (project.Markers != null) { foreach (Marker marker in project.Markers) { compositeManifestInfo.AddPlayByPlay(marker.ID, marker.Text, marker.Time); } } SmoothStreamingManifestWriter writer = new SmoothStreamingManifestWriter(); string manifest = writer.GenerateCompositeManifest(compositeManifestInfo, false); string csmPath = HttpContext.Current.Server.MapPath("csm"); if (!Directory.Exists(csmPath)) { Directory.CreateDirectory(csmPath); } string datetime = DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture); string tmpFilePath = Path.Combine(csmPath, string.Format(CultureInfo.InvariantCulture, "{0}-{1}.tmpcsm", project.Title.ToString(), datetime)); string finalFilePath = Path.Combine(csmPath, string.Format(CultureInfo.InvariantCulture, "{0}-{1}.csm", project.Title.ToString(), datetime)); File.WriteAllText(tmpFilePath, manifest, Encoding.UTF8); if (File.Exists(tmpFilePath)) { if (File.Exists(finalFilePath)) { File.Delete(finalFilePath); } File.Move(tmpFilePath, finalFilePath); } }