private void AddAudioResource(XmlElement container, Audio audio, string deviceId, Filter filter) { var res = container.OwnerDocument.CreateElement(string.Empty, "res", NS_DIDL); var sources = _dtoService.GetMediaSources(audio); var streamInfo = new StreamBuilder().BuildAudioItem(new AudioOptions { ItemId = audio.Id.ToString("N"), MediaSources = sources, Profile = _profile, DeviceId = deviceId }); var url = streamInfo.ToDlnaUrl(_serverAddress); res.InnerText = url; var mediaSource = sources.First(i => string.Equals(i.Id, streamInfo.MediaSourceId)); if (mediaSource.RunTimeTicks.HasValue) { res.SetAttribute("duration", TimeSpan.FromTicks(mediaSource.RunTimeTicks.Value).ToString("c", _usCulture)); } if (filter.Contains("res@size")) { if (streamInfo.IsDirectStream || streamInfo.EstimateContentLength) { var size = streamInfo.TargetSize; if (size.HasValue) { res.SetAttribute("size", size.Value.ToString(_usCulture)); } } } var targetAudioBitrate = streamInfo.TargetAudioBitrate; var targetSampleRate = streamInfo.TargetAudioSampleRate; var targetChannels = streamInfo.TargetAudioChannels; if (targetChannels.HasValue) { res.SetAttribute("nrAudioChannels", targetChannels.Value.ToString(_usCulture)); } if (targetSampleRate.HasValue) { res.SetAttribute("sampleFrequency", targetSampleRate.Value.ToString(_usCulture)); } if (targetAudioBitrate.HasValue) { res.SetAttribute("bitrate", targetAudioBitrate.Value.ToString(_usCulture)); } var formatProfile = new MediaFormatProfileResolver().ResolveAudioFormat(streamInfo.Container, targetAudioBitrate, targetSampleRate, targetChannels); var filename = url.Substring(0, url.IndexOf('?')); var orgOpValue = DlnaMaps.GetOrgOpValue(mediaSource.RunTimeTicks.HasValue, streamInfo.IsDirectStream, streamInfo.TranscodeSeekInfo); var orgCi = streamInfo.IsDirectStream ? ";DLNA.ORG_CI=0" : ";DLNA.ORG_CI=1"; res.SetAttribute("protocolInfo", String.Format( "http-get:*:{0}:DLNA.ORG_PN={1};DLNA.ORG_OP={2};DLNA.ORG_CI={3};DLNA.ORG_FLAGS={4}", MimeTypes.GetMimeType(filename), formatProfile, orgOpValue, orgCi, DlnaMaps.DefaultStreaming )); container.AppendChild(res); }
/// <summary> /// Adds fields used by both items and folders /// </summary> /// <param name="item">The item.</param> /// <param name="element">The element.</param> /// <param name="filter">The filter.</param> private void AddCommonFields(BaseItem item, XmlElement element, Filter filter) { if (filter.Contains("dc:date")) { if (item.PremiereDate.HasValue) { AddValue(element, "dc", "date", item.PremiereDate.Value.ToString("o"), NS_DC); } } foreach (var genre in item.Genres) { AddValue(element, "upnp", "genre", genre, NS_UPNP); } foreach (var studio in item.Studios) { AddValue(element, "upnp", "publisher", studio, NS_UPNP); } if (filter.Contains("dc:title")) { AddValue(element, "dc", "title", item.Name, NS_DC); } if (filter.Contains("dc:description")) { if (!string.IsNullOrWhiteSpace(item.Overview)) { AddValue(element, "dc", "description", item.Overview, NS_DC); } } if (filter.Contains("upnp:longDescription")) { if (!string.IsNullOrWhiteSpace(item.Overview)) { AddValue(element, "upnp", "longDescription", item.Overview, NS_UPNP); } } if (!string.IsNullOrEmpty(item.OfficialRating)) { if (filter.Contains("dc:rating")) { AddValue(element, "dc", "rating", item.OfficialRating, NS_DC); } if (filter.Contains("upnp:rating")) { AddValue(element, "upnp", "rating", item.OfficialRating, NS_UPNP); } } AddPeople(item, element); }
private void AddVideoResource(XmlElement container, Video video, string deviceId, Filter filter) { var res = container.OwnerDocument.CreateElement(string.Empty, "res", NS_DIDL); var sources = _dtoService.GetMediaSources(video); int? maxBitrateSetting = null; var streamInfo = new StreamBuilder().BuildVideoItem(new VideoOptions { ItemId = video.Id.ToString("N"), MediaSources = sources, Profile = _profile, DeviceId = deviceId, MaxBitrate = maxBitrateSetting }); var url = streamInfo.ToDlnaUrl(_serverAddress); res.InnerText = url; var mediaSource = sources.First(i => string.Equals(i.Id, streamInfo.MediaSourceId)); if (mediaSource.RunTimeTicks.HasValue) { res.SetAttribute("duration", TimeSpan.FromTicks(mediaSource.RunTimeTicks.Value).ToString("c", _usCulture)); } if (filter.Contains("res@size")) { if (streamInfo.IsDirectStream || streamInfo.EstimateContentLength) { var size = streamInfo.TargetSize; if (size.HasValue) { res.SetAttribute("size", size.Value.ToString(_usCulture)); } } } var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video && !string.Equals(i.Codec, "mjpeg", StringComparison.OrdinalIgnoreCase)); var targetAudioBitrate = streamInfo.TargetAudioBitrate; var targetSampleRate = streamInfo.TargetAudioSampleRate; var targetChannels = streamInfo.TargetAudioChannels; var targetWidth = streamInfo.MaxWidth ?? (videoStream == null ? null : videoStream.Width); var targetHeight = streamInfo.MaxHeight ?? (videoStream == null ? null : videoStream.Height); var targetVideoCodec = streamInfo.IsDirectStream ? (videoStream == null ? null : videoStream.Codec) : streamInfo.VideoCodec; var targetAudioCodec = streamInfo.TargetAudioCodec; var targetBitrate = maxBitrateSetting ?? mediaSource.Bitrate; if (targetChannels.HasValue) { res.SetAttribute("nrAudioChannels", targetChannels.Value.ToString(_usCulture)); } if (filter.Contains("res@resolution")) { if (targetWidth.HasValue && targetHeight.HasValue) { res.SetAttribute("resolution", string.Format("{0}x{1}", targetWidth.Value, targetHeight.Value)); } } if (targetSampleRate.HasValue) { res.SetAttribute("sampleFrequency", targetSampleRate.Value.ToString(_usCulture)); } if (targetAudioBitrate.HasValue) { res.SetAttribute("bitrate", targetAudioBitrate.Value.ToString(_usCulture)); } var formatProfile = new MediaFormatProfileResolver().ResolveVideoFormat(streamInfo.Container, targetVideoCodec, targetAudioCodec, targetWidth, targetHeight, targetBitrate, TransportStreamTimestamp.NONE); var filename = url.Substring(0, url.IndexOf('?')); var orgOpValue = DlnaMaps.GetOrgOpValue(mediaSource.RunTimeTicks.HasValue, streamInfo.IsDirectStream, streamInfo.TranscodeSeekInfo); var orgCi = streamInfo.IsDirectStream ? ";DLNA.ORG_CI=0" : ";DLNA.ORG_CI=1"; res.SetAttribute("protocolInfo", String.Format( "http-get:*:{0}:DLNA.ORG_PN={1};DLNA.ORG_OP={2};DLNA.ORG_CI={3};DLNA.ORG_FLAGS={4}", MimeTypes.GetMimeType(filename), formatProfile, orgOpValue, orgCi, DlnaMaps.DefaultStreaming )); container.AppendChild(res); }