Esempio n. 1
0
        private static MediaSourceInfo GetVersionInfo(Audio i, bool enablePathSubstituion)
        {
            var locationType = i.LocationType;

            var info = new MediaSourceInfo
            {
                Id           = i.Id.ToString("N"),
                Protocol     = locationType == LocationType.Remote ? MediaProtocol.Http : MediaProtocol.File,
                MediaStreams = MediaSourceManager.GetMediaStreams(i.Id).ToList(),
                Name         = i.Name,
                Path         = enablePathSubstituion ? GetMappedPath(i, i.Path, locationType) : i.Path,
                RunTimeTicks = i.RunTimeTicks,
                Container    = i.Container,
                Size         = i.Size
            };

            if (info.Protocol == MediaProtocol.File)
            {
                info.ETag = i.DateModified.Ticks.ToString(CultureInfo.InvariantCulture).GetMD5().ToString("N");
            }

            if (string.IsNullOrEmpty(info.Container))
            {
                if (!string.IsNullOrWhiteSpace(i.Path) && locationType != LocationType.Remote && locationType != LocationType.Virtual)
                {
                    info.Container = System.IO.Path.GetExtension(i.Path).TrimStart('.');
                }
            }

            info.Bitrate = i.TotalBitrate;
            info.InferTotalBitrate();

            return(info);
        }
Esempio n. 2
0
        public MediaSourceInfo ToMediaSource()
        {
            var id = Path.GetMD5().ToString("N");

            var source = new MediaSourceInfo
            {
                MediaStreams = GetMediaStreams(this).ToList(),

                Container           = Container,
                Protocol            = Protocol,
                Path                = Path,
                RequiredHttpHeaders = RequiredHttpHeaders,
                RunTimeTicks        = RunTimeTicks,
                Name                = id,
                Id = id,
                ReadAtNativeFramerate = ReadAtNativeFramerate,
                SupportsDirectStream  = Protocol == MediaProtocol.Http && !string.IsNullOrWhiteSpace(Container) && !string.Equals(Container, "hls", StringComparison.OrdinalIgnoreCase),
                SupportsDirectPlay    = SupportsDirectPlay,
                IsRemote = true
            };

            source.InferTotalBitrate();

            return(source);
        }
Esempio n. 3
0
        private MediaSourceInfo GetVersionInfo(Audio i, bool enablePathSubstituion)
        {
            var protocol = i.PathProtocol;

            var info = new MediaSourceInfo
            {
                Id           = i.Id.ToString("N"),
                Protocol     = protocol ?? MediaProtocol.File,
                MediaStreams = MediaSourceManager.GetMediaStreams(i.Id),
                Name         = i.Name,
                Path         = enablePathSubstituion ? GetMappedPath(i, i.Path, protocol) : i.Path,
                RunTimeTicks = i.RunTimeTicks,
                Container    = i.Container,
                Size         = i.Size
            };

            if (info.Protocol == MediaProtocol.File)
            {
                info.ETag = i.DateModified.Ticks.ToString(CultureInfo.InvariantCulture).GetMD5().ToString("N");
            }

            if (string.IsNullOrEmpty(info.Container))
            {
                if (i.IsFileProtocol)
                {
                    info.Container = System.IO.Path.GetExtension(i.Path).TrimStart('.');
                }
            }

            info.Bitrate = i.TotalBitrate;
            info.InferTotalBitrate();

            return(info);
        }
Esempio n. 4
0
        public MediaSourceInfo ToMediaSource()
        {
            Path = Channel.Path;

            if (!Path.EndsWith("m3u8", StringComparison.InvariantCultureIgnoreCase))
            {
                Path = ""; // no live stream
            }
            var id = Path.GetMD5().ToString("N");

            var source = new MediaSourceInfo
            {
                Container           = Container,
                Protocol            = Protocol,
                Path                = Path,
                RequiredHttpHeaders = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase),
                RunTimeTicks        = null,
                Name                = id,
                Id = id,
                SupportsDirectStream = true,
                SupportsDirectPlay   = SupportsDirectPlay,
                IsRemote             = true,
                IsInfiniteStream     = true
            };

            source.InferTotalBitrate();

            return(source);
        }
Esempio n. 5
0
        public MediaSourceInfo ToMediaSource(Guid itemId)
        {
            var id = string.IsNullOrWhiteSpace(Path) ?
                     itemId.ToString("N") :
                     Path.GetMD5().ToString("N");

            var source = new MediaSourceInfo
            {
                MediaStreams = GetMediaStreams(this).ToList(),

                Container           = Container,
                Protocol            = Protocol,
                Path                = Path,
                RequiredHttpHeaders = RequiredHttpHeaders,
                RunTimeTicks        = RunTimeTicks,
                Name                = id,
                Id = id,
                ReadAtNativeFramerate = ReadAtNativeFramerate,
                SupportsDirectStream  = false,
                SupportsDirectPlay    = SupportsDirectPlay,
                IsRemote = true
            };

            source.InferTotalBitrate();

            return(source);
        }
Esempio n. 6
0
        private void AddMediaInfo(MediaSourceInfo mediaSource, bool isAudio)
        {
            mediaSource.DefaultSubtitleStreamIndex = null;

            // Null this out so that it will be treated like a live stream
            if (mediaSource.IsInfiniteStream)
            {
                mediaSource.RunTimeTicks = null;
            }

            var audioStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaBrowser.Model.Entities.MediaStreamType.Audio);

            if (audioStream == null || audioStream.Index == -1)
            {
                mediaSource.DefaultAudioStreamIndex = null;
            }
            else
            {
                mediaSource.DefaultAudioStreamIndex = audioStream.Index;
            }

            var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaBrowser.Model.Entities.MediaStreamType.Video);

            if (videoStream != null)
            {
                if (!videoStream.BitRate.HasValue)
                {
                    var width = videoStream.Width ?? 1920;

                    if (width >= 3000)
                    {
                        videoStream.BitRate = 30000000;
                    }

                    else if (width >= 1900)
                    {
                        videoStream.BitRate = 20000000;
                    }

                    else if (width >= 1200)
                    {
                        videoStream.BitRate = 8000000;
                    }

                    else if (width >= 700)
                    {
                        videoStream.BitRate = 2000000;
                    }
                }
            }

            // Try to estimate this
            mediaSource.InferTotalBitrate();
        }
        protected override Task <List <MediaSourceInfo> > GetChannelStreamMediaSources(TunerHostInfo tuner, BaseItem dbChannnel, ChannelInfo tunerChannel, CancellationToken cancellationToken)
        {
            var config  = GetProviderOptions <DVBViewerOptions>(tuner);
            var baseUrl = tuner.Url;

            var dvbChannelId = GetTunerChannelIdFromEmbyChannelId(tuner, tunerChannel.Id);

            var url = String.Format("{0}/upnp/channelstream/{1}.ts", baseUrl.TrimEnd('/'), dvbChannelId);

            // need to change the port
            if (Uri.TryCreate(url, UriKind.Absolute, out Uri uri))
            {
                var builder = new UriBuilder(uri);
                builder.Port = config.StreamingPort;

                url = builder.Uri.ToString();
            }

            var mediaSource = new MediaSourceInfo
            {
                // Make sure that it is predictable and returns the same result each time
                Path     = url,
                Protocol = MediaProtocol.Http,

                RequiresOpening = false,
                RequiresClosing = false,

                Container = "ts",
                Id        = "native_" + dvbChannelId,

                // this needs review but I'm not sure these values matter at this earlier stage
                SupportsDirectPlay   = false,
                SupportsDirectStream = true,
                SupportsTranscoding  = true,

                IsInfiniteStream = true
            };

            mediaSource.InferTotalBitrate();

            return(Task.FromResult(new List <MediaSourceInfo> {
                mediaSource
            }));
        }
Esempio n. 8
0
        protected override async Task <List <MediaSourceInfo> > GetChannelStreamMediaSources(TunerHostInfo info, string channelId, CancellationToken cancellationToken)
        {
            var urlHash = info.Url.GetMD5().ToString("N");
            var prefix  = ChannelIdPrefix + urlHash;

            if (!channelId.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }

            var channels = await GetChannels(info, true, cancellationToken).ConfigureAwait(false);

            var m3uchannels = channels.Cast <M3UChannel>();
            var channel     = m3uchannels.FirstOrDefault(c => string.Equals(c.Id, channelId, StringComparison.OrdinalIgnoreCase));

            if (channel != null)
            {
                var           path     = channel.Path;
                MediaProtocol protocol = MediaProtocol.File;
                if (path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                {
                    protocol = MediaProtocol.Http;
                }
                else if (path.StartsWith("rtmp", StringComparison.OrdinalIgnoreCase))
                {
                    protocol = MediaProtocol.Rtmp;
                }
                else if (path.StartsWith("rtsp", StringComparison.OrdinalIgnoreCase))
                {
                    protocol = MediaProtocol.Rtsp;
                }
                else if (path.StartsWith("udp", StringComparison.OrdinalIgnoreCase))
                {
                    protocol = MediaProtocol.Udp;
                }
                else if (path.StartsWith("rtp", StringComparison.OrdinalIgnoreCase))
                {
                    protocol = MediaProtocol.Rtmp;
                }

                var mediaSource = new MediaSourceInfo
                {
                    Path         = channel.Path,
                    Protocol     = protocol,
                    MediaStreams = new List <MediaStream>
                    {
                        new MediaStream
                        {
                            Type = MediaStreamType.Video,
                            // Set the index to -1 because we don't know the exact index of the video stream within the container
                            Index        = -1,
                            IsInterlaced = true
                        },
                        new MediaStream
                        {
                            Type = MediaStreamType.Audio,
                            // Set the index to -1 because we don't know the exact index of the audio stream within the container
                            Index = -1
                        }
                    },
                    RequiresOpening = true,
                    RequiresClosing = true,
                    RequiresLooping = true,

                    ReadAtNativeFramerate = false,

                    Id = channel.Path.GetMD5().ToString("N"),
                    IsInfiniteStream = true,
                    IsRemote         = true
                };

                mediaSource.InferTotalBitrate();

                return(new List <MediaSourceInfo> {
                    mediaSource
                });
            }
            return(new List <MediaSourceInfo>());
        }
Esempio n. 9
0
        private MediaSourceInfo GetMediaSource(TunerHostInfo info, string channelId, ChannelInfo channelInfo, string profile)
        {
            int?   width        = null;
            int?   height       = null;
            bool   isInterlaced = true;
            string videoCodec   = null;

            int?videoBitrate = null;

            var isHd = channelInfo.IsHD ?? true;

            if (string.Equals(profile, "mobile", StringComparison.OrdinalIgnoreCase))
            {
                width        = 1280;
                height       = 720;
                isInterlaced = false;
                videoCodec   = "h264";
                videoBitrate = 2000000;
            }
            else if (string.Equals(profile, "heavy", StringComparison.OrdinalIgnoreCase))
            {
                width        = 1920;
                height       = 1080;
                isInterlaced = false;
                videoCodec   = "h264";
                videoBitrate = 15000000;
            }
            else if (string.Equals(profile, "internet720", StringComparison.OrdinalIgnoreCase))
            {
                width        = 1280;
                height       = 720;
                isInterlaced = false;
                videoCodec   = "h264";
                videoBitrate = 8000000;
            }
            else if (string.Equals(profile, "internet540", StringComparison.OrdinalIgnoreCase))
            {
                width        = 960;
                height       = 540;
                isInterlaced = false;
                videoCodec   = "h264";
                videoBitrate = 2500000;
            }
            else if (string.Equals(profile, "internet480", StringComparison.OrdinalIgnoreCase))
            {
                width        = 848;
                height       = 480;
                isInterlaced = false;
                videoCodec   = "h264";
                videoBitrate = 2000000;
            }
            else if (string.Equals(profile, "internet360", StringComparison.OrdinalIgnoreCase))
            {
                width        = 640;
                height       = 360;
                isInterlaced = false;
                videoCodec   = "h264";
                videoBitrate = 1500000;
            }
            else if (string.Equals(profile, "internet240", StringComparison.OrdinalIgnoreCase))
            {
                width        = 432;
                height       = 240;
                isInterlaced = false;
                videoCodec   = "h264";
                videoBitrate = 1000000;
            }
            else
            {
                // This is for android tv's 1200 condition. Remove once not needed anymore so that we can avoid possible side effects of dummying up this data
                if (isHd)
                {
                    width  = 1920;
                    height = 1080;
                }
            }

            if (string.IsNullOrWhiteSpace(videoCodec))
            {
                videoCodec = channelInfo.VideoCodec;
            }
            string audioCodec = channelInfo.AudioCodec;

            if (!videoBitrate.HasValue)
            {
                videoBitrate = isHd ? 15000000 : 2000000;
            }
            int?audioBitrate = isHd ? 448000 : 192000;

            // normalize
            if (string.Equals(videoCodec, "mpeg2", StringComparison.OrdinalIgnoreCase))
            {
                videoCodec = "mpeg2video";
            }

            string nal = null;

            if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
            {
                nal = "0";
            }

            var url = GetApiUrl(info);

            var id = profile;

            if (string.IsNullOrWhiteSpace(id))
            {
                id = "native";
            }
            id += "_" + channelId.GetMD5().ToString("N") + "_" + url.GetMD5().ToString("N");

            var mediaSource = new MediaSourceInfo
            {
                Path         = url,
                Protocol     = MediaProtocol.Udp,
                MediaStreams = new List <MediaStream>
                {
                    new MediaStream
                    {
                        Type = MediaStreamType.Video,
                        // Set the index to -1 because we don't know the exact index of the video stream within the container
                        Index         = -1,
                        IsInterlaced  = isInterlaced,
                        Codec         = videoCodec,
                        Width         = width,
                        Height        = height,
                        BitRate       = videoBitrate,
                        NalLengthSize = nal
                    },
                    new MediaStream
                    {
                        Type = MediaStreamType.Audio,
                        // Set the index to -1 because we don't know the exact index of the audio stream within the container
                        Index   = -1,
                        Codec   = audioCodec,
                        BitRate = audioBitrate
                    }
                },
                RequiresOpening = true,
                RequiresClosing = true,
                BufferMs        = 0,
                Container       = "ts",
                Id = id,
                SupportsDirectPlay   = false,
                SupportsDirectStream = true,
                SupportsTranscoding  = true,
                IsInfiniteStream     = true,
                IgnoreDts            = true,
                //IgnoreIndex = true,
                //ReadAtNativeFramerate = true
            };

            mediaSource.InferTotalBitrate();

            return(mediaSource);
        }
Esempio n. 10
0
        public async Task AddMediaInfoWithProbe(MediaSourceInfo mediaSource, bool isAudio, CancellationToken cancellationToken)
        {
            var originalRuntime = mediaSource.RunTimeTicks;

            var now = DateTime.UtcNow;

            var info = await _mediaEncoder.GetMediaInfo(new MediaInfoRequest
            {
                InputPath         = mediaSource.Path,
                Protocol          = mediaSource.Protocol,
                MediaType         = isAudio ? DlnaProfileType.Audio : DlnaProfileType.Video,
                ExtractChapters   = false,
                AnalyzeDurationMs = AnalyzeDurationMs
            }, cancellationToken).ConfigureAwait(false);

            _logger.Info("Live tv media info probe took {0} seconds", (DateTime.UtcNow - now).TotalSeconds.ToString(CultureInfo.InvariantCulture));

            mediaSource.Bitrate       = info.Bitrate;
            mediaSource.Container     = info.Container;
            mediaSource.Formats       = info.Formats;
            mediaSource.MediaStreams  = info.MediaStreams;
            mediaSource.RunTimeTicks  = info.RunTimeTicks;
            mediaSource.Size          = info.Size;
            mediaSource.Timestamp     = info.Timestamp;
            mediaSource.Video3DFormat = info.Video3DFormat;
            mediaSource.VideoType     = info.VideoType;

            mediaSource.DefaultSubtitleStreamIndex = null;

            // Null this out so that it will be treated like a live stream
            if (!originalRuntime.HasValue)
            {
                mediaSource.RunTimeTicks = null;
            }

            var audioStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaBrowser.Model.Entities.MediaStreamType.Audio);

            if (audioStream == null || audioStream.Index == -1)
            {
                mediaSource.DefaultAudioStreamIndex = null;
            }
            else
            {
                mediaSource.DefaultAudioStreamIndex = audioStream.Index;
            }

            var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaBrowser.Model.Entities.MediaStreamType.Video);

            if (videoStream != null)
            {
                if (!videoStream.BitRate.HasValue)
                {
                    var width = videoStream.Width ?? 1920;

                    if (width >= 3000)
                    {
                        videoStream.BitRate = 30000000;
                    }

                    else if (width >= 1900)
                    {
                        videoStream.BitRate = 20000000;
                    }

                    else if (width >= 1200)
                    {
                        videoStream.BitRate = 8000000;
                    }

                    else if (width >= 700)
                    {
                        videoStream.BitRate = 2000000;
                    }
                }

                // This is coming up false and preventing stream copy
                videoStream.IsAVC = null;
            }

            // Try to estimate this
            mediaSource.InferTotalBitrate(true);

            mediaSource.AnalyzeDurationMs = AnalyzeDurationMs;
        }
Esempio n. 11
0
File: Video.cs Progetto: vvuk/Emby
        private static MediaSourceInfo GetVersionInfo(bool enablePathSubstitution, Video i, MediaSourceType type)
        {
            var mediaStreams = MediaSourceManager.GetMediaStreams(i.Id)
                               .ToList();

            var locationType = i.LocationType;

            var info = new MediaSourceInfo
            {
                Id                      = i.Id.ToString("N"),
                IsoType                 = i.IsoType,
                Protocol                = locationType == LocationType.Remote ? MediaProtocol.Http : MediaProtocol.File,
                MediaStreams            = mediaStreams,
                Name                    = GetMediaSourceName(i, mediaStreams),
                Path                    = enablePathSubstitution ? GetMappedPath(i, i.Path, locationType) : i.Path,
                RunTimeTicks            = i.RunTimeTicks,
                Video3DFormat           = i.Video3DFormat,
                VideoType               = i.VideoType,
                Container               = i.Container,
                Size                    = i.Size,
                Timestamp               = i.Timestamp,
                Type                    = type,
                PlayableStreamFileNames = i.PlayableStreamFileNames.ToList(),
                SupportsDirectStream    = i.VideoType == VideoType.VideoFile
            };

            if (info.Protocol == MediaProtocol.File)
            {
                info.ETag = i.DateModified.Ticks.ToString(CultureInfo.InvariantCulture).GetMD5().ToString("N");
            }

            if (i.IsShortcut)
            {
                info.Path = i.ShortcutPath;

                if (info.Path.StartsWith("Http", StringComparison.OrdinalIgnoreCase))
                {
                    info.Protocol = MediaProtocol.Http;
                }
                else if (info.Path.StartsWith("Rtmp", StringComparison.OrdinalIgnoreCase))
                {
                    info.Protocol = MediaProtocol.Rtmp;
                }
                else if (info.Path.StartsWith("Rtsp", StringComparison.OrdinalIgnoreCase))
                {
                    info.Protocol = MediaProtocol.Rtsp;
                }
                else
                {
                    info.Protocol = MediaProtocol.File;
                }
            }

            if (string.IsNullOrEmpty(info.Container))
            {
                if (i.VideoType == VideoType.VideoFile || i.VideoType == VideoType.Iso)
                {
                    if (!string.IsNullOrWhiteSpace(i.Path) && locationType != LocationType.Remote && locationType != LocationType.Virtual)
                    {
                        info.Container = System.IO.Path.GetExtension(i.Path).TrimStart('.');
                    }
                }
            }

            info.Bitrate = i.TotalBitrate;
            info.InferTotalBitrate();

            return(info);
        }
Esempio n. 12
0
        protected virtual MediaSourceInfo CreateMediaSourceInfo(TunerHostInfo info, ChannelInfo channel)
        {
            var path = channel.Path;

            var supportsDirectPlay   = !info.EnableStreamLooping && info.TunerCount == 0;
            var supportsDirectStream = !info.EnableStreamLooping;

            var protocol = _mediaSourceManager.GetPathProtocol(path);

            var isRemote = true;

            if (Uri.TryCreate(path, UriKind.Absolute, out var uri))
            {
                isRemote = !_networkManager.IsInLocalNetwork(uri.Host);
            }

            var httpHeaders = new Dictionary <string, string>();

            if (protocol == MediaProtocol.Http)
            {
                // Use user-defined user-agent. If there isn't one, make it look like a browser.
                httpHeaders[HeaderNames.UserAgent] = string.IsNullOrWhiteSpace(info.UserAgent) ?
                                                     "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.85 Safari/537.36" :
                                                     info.UserAgent;
            }

            var mediaSource = new MediaSourceInfo
            {
                Path         = path,
                Protocol     = protocol,
                MediaStreams = new List <MediaStream>
                {
                    new MediaStream
                    {
                        Type = MediaStreamType.Video,
                        // Set the index to -1 because we don't know the exact index of the video stream within the container
                        Index        = -1,
                        IsInterlaced = true
                    },
                    new MediaStream
                    {
                        Type = MediaStreamType.Audio,
                        // Set the index to -1 because we don't know the exact index of the audio stream within the container
                        Index = -1
                    }
                },
                RequiresOpening = true,
                RequiresClosing = true,
                RequiresLooping = info.EnableStreamLooping,

                ReadAtNativeFramerate = false,

                Id = channel.Path.GetMD5().ToString("N", CultureInfo.InvariantCulture),
                IsInfiniteStream = true,
                IsRemote         = isRemote,

                IgnoreDts            = true,
                SupportsDirectPlay   = supportsDirectPlay,
                SupportsDirectStream = supportsDirectStream,

                RequiredHttpHeaders = httpHeaders
            };

            mediaSource.InferTotalBitrate();

            return(mediaSource);
        }
Esempio n. 13
0
        private async Task <MediaSourceInfo> GetMediaSource(TunerHostInfo info, string channelId, string profile)
        {
            int?   width        = null;
            int?   height       = null;
            bool   isInterlaced = true;
            string videoCodec   = null;
            string audioCodec   = "ac3";

            int?videoBitrate = null;
            int?audioBitrate = null;

            if (string.Equals(profile, "mobile", StringComparison.OrdinalIgnoreCase))
            {
                width        = 1280;
                height       = 720;
                isInterlaced = false;
                videoCodec   = "h264";
                videoBitrate = 2000000;
            }
            else if (string.Equals(profile, "heavy", StringComparison.OrdinalIgnoreCase))
            {
                width        = 1920;
                height       = 1080;
                isInterlaced = false;
                videoCodec   = "h264";
                videoBitrate = 15000000;
            }
            else if (string.Equals(profile, "internet540", StringComparison.OrdinalIgnoreCase))
            {
                width        = 960;
                height       = 546;
                isInterlaced = false;
                videoCodec   = "h264";
                videoBitrate = 2500000;
            }
            else if (string.Equals(profile, "internet480", StringComparison.OrdinalIgnoreCase))
            {
                width        = 848;
                height       = 480;
                isInterlaced = false;
                videoCodec   = "h264";
                videoBitrate = 2000000;
            }
            else if (string.Equals(profile, "internet360", StringComparison.OrdinalIgnoreCase))
            {
                width        = 640;
                height       = 360;
                isInterlaced = false;
                videoCodec   = "h264";
                videoBitrate = 1500000;
            }
            else if (string.Equals(profile, "internet240", StringComparison.OrdinalIgnoreCase))
            {
                width        = 432;
                height       = 240;
                isInterlaced = false;
                videoCodec   = "h264";
                videoBitrate = 1000000;
            }

            var channels = await GetChannels(info, true, CancellationToken.None).ConfigureAwait(false);

            var channel = channels.FirstOrDefault(i => string.Equals(i.Number, channelId, StringComparison.OrdinalIgnoreCase));

            if (channel != null)
            {
                if (string.IsNullOrWhiteSpace(videoCodec))
                {
                    videoCodec = channel.VideoCodec;
                }
                audioCodec = channel.AudioCodec;

                if (!videoBitrate.HasValue)
                {
                    videoBitrate = (channel.IsHD ?? true) ? 15000000 : 2000000;
                }
                audioBitrate = (channel.IsHD ?? true) ? 448000 : 192000;
            }

            // normalize
            if (string.Equals(videoCodec, "mpeg2", StringComparison.OrdinalIgnoreCase))
            {
                videoCodec = "mpeg2video";
            }

            string nal = null;

            if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
            {
                nal = "0";
            }

            var url = GetApiUrl(info, true) + "/auto/v" + channelId;

            // If raw was used, the tuner doesn't support params
            if (!string.IsNullOrWhiteSpace(profile) &&
                !string.Equals(profile, "native", StringComparison.OrdinalIgnoreCase))
            {
                url += "?transcode=" + profile;
            }

            var id = profile;

            if (string.IsNullOrWhiteSpace(id))
            {
                id = "native";
            }
            id += "_" + url.GetMD5().ToString("N");

            var mediaSource = new MediaSourceInfo
            {
                Path         = url,
                Protocol     = MediaProtocol.Http,
                MediaStreams = new List <MediaStream>
                {
                    new MediaStream
                    {
                        Type = MediaStreamType.Video,
                        // Set the index to -1 because we don't know the exact index of the video stream within the container
                        Index         = -1,
                        IsInterlaced  = isInterlaced,
                        Codec         = videoCodec,
                        Width         = width,
                        Height        = height,
                        BitRate       = videoBitrate,
                        NalLengthSize = nal
                    },
                    new MediaStream
                    {
                        Type = MediaStreamType.Audio,
                        // Set the index to -1 because we don't know the exact index of the audio stream within the container
                        Index   = -1,
                        Codec   = audioCodec,
                        BitRate = audioBitrate
                    }
                },
                RequiresOpening = true,
                RequiresClosing = false,
                BufferMs        = 0,
                Container       = "ts",
                Id = id,
                SupportsDirectPlay   = false,
                SupportsDirectStream = true,
                SupportsTranscoding  = true,
                IsInfiniteStream     = true
            };

            mediaSource.InferTotalBitrate();

            return(mediaSource);
        }
Esempio n. 14
0
        protected virtual MediaSourceInfo CreateMediaSourceInfo(TunerHostInfo info, ChannelInfo channel)
        {
            var           path     = channel.Path;
            MediaProtocol protocol = MediaProtocol.File;

            if (path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
            {
                protocol = MediaProtocol.Http;
            }
            else if (path.StartsWith("rtmp", StringComparison.OrdinalIgnoreCase))
            {
                protocol = MediaProtocol.Rtmp;
            }
            else if (path.StartsWith("rtsp", StringComparison.OrdinalIgnoreCase))
            {
                protocol = MediaProtocol.Rtsp;
            }
            else if (path.StartsWith("udp", StringComparison.OrdinalIgnoreCase))
            {
                protocol = MediaProtocol.Udp;
            }
            else if (path.StartsWith("rtp", StringComparison.OrdinalIgnoreCase))
            {
                protocol = MediaProtocol.Rtmp;
            }

            Uri uri;
            var isRemote = true;

            if (Uri.TryCreate(path, UriKind.Absolute, out uri))
            {
                isRemote = !_networkManager.IsInLocalNetwork(uri.Host);
            }

            var mediaSource = new MediaSourceInfo
            {
                Path         = path,
                Protocol     = protocol,
                MediaStreams = new List <MediaStream>
                {
                    new MediaStream
                    {
                        Type = MediaStreamType.Video,
                        // Set the index to -1 because we don't know the exact index of the video stream within the container
                        Index        = -1,
                        IsInterlaced = true
                    },
                    new MediaStream
                    {
                        Type = MediaStreamType.Audio,
                        // Set the index to -1 because we don't know the exact index of the audio stream within the container
                        Index = -1
                    }
                },
                RequiresOpening = true,
                RequiresClosing = true,
                RequiresLooping = info.EnableStreamLooping,

                ReadAtNativeFramerate = false,

                Id = channel.Path.GetMD5().ToString("N"),
                IsInfiniteStream = true,
                IsRemote         = isRemote,

                IgnoreDts = true
            };

            mediaSource.InferTotalBitrate();

            return(mediaSource);
        }
Esempio n. 15
0
        private MediaSourceInfo GetVersionInfo(bool enablePathSubstitution, Video media, MediaSourceType type)
        {
            if (media == null)
            {
                throw new ArgumentNullException("media");
            }

            var locationType = media.LocationType;

            var info = new MediaSourceInfo
            {
                Id                   = media.Id.ToString("N"),
                IsoType              = media.IsoType,
                Protocol             = locationType == LocationType.Remote ? MediaProtocol.Http : MediaProtocol.File,
                MediaStreams         = MediaSourceManager.GetMediaStreams(media.Id),
                Name                 = GetMediaSourceName(media),
                Path                 = enablePathSubstitution ? GetMappedPath(media, media.Path, locationType) : media.Path,
                RunTimeTicks         = media.RunTimeTicks,
                Video3DFormat        = media.Video3DFormat,
                VideoType            = media.VideoType,
                Container            = media.Container,
                Size                 = media.Size,
                Timestamp            = media.Timestamp,
                Type                 = type,
                SupportsDirectStream = media.VideoType == VideoType.VideoFile,
                IsRemote             = media.IsShortcut
            };

            if (info.Protocol == MediaProtocol.File)
            {
                info.ETag = media.DateModified.Ticks.ToString(CultureInfo.InvariantCulture).GetMD5().ToString("N");
            }

            if (media.IsShortcut)
            {
                info.Path = media.ShortcutPath;

                if (!string.IsNullOrWhiteSpace(info.Path))
                {
                    if (info.Path.StartsWith("Http", StringComparison.OrdinalIgnoreCase))
                    {
                        info.Protocol             = MediaProtocol.Http;
                        info.SupportsDirectStream = false;
                    }
                    else if (info.Path.StartsWith("Rtmp", StringComparison.OrdinalIgnoreCase))
                    {
                        info.Protocol             = MediaProtocol.Rtmp;
                        info.SupportsDirectStream = false;
                    }
                    else if (info.Path.StartsWith("Rtsp", StringComparison.OrdinalIgnoreCase))
                    {
                        info.Protocol             = MediaProtocol.Rtsp;
                        info.SupportsDirectStream = false;
                    }
                    else
                    {
                        info.Protocol = MediaProtocol.File;
                    }
                }
            }

            if (string.IsNullOrEmpty(info.Container))
            {
                if (media.VideoType == VideoType.VideoFile || media.VideoType == VideoType.Iso)
                {
                    if (!string.IsNullOrWhiteSpace(media.Path) && locationType != LocationType.Remote && locationType != LocationType.Virtual)
                    {
                        info.Container = System.IO.Path.GetExtension(media.Path).TrimStart('.');
                    }
                }
            }

            info.Bitrate = media.TotalBitrate;
            info.InferTotalBitrate();

            return(info);
        }
Esempio n. 16
0
        public async Task AddMediaInfoWithProbe(MediaSourceInfo mediaSource, bool isAudio, string cacheKey, bool addProbeDelay, bool isLiveStream, CancellationToken cancellationToken)
        {
            var originalRuntime = mediaSource.RunTimeTicks;

            var now = DateTime.UtcNow;

            MediaInfo mediaInfo     = null;
            var       cacheFilePath = string.IsNullOrEmpty(cacheKey) ? null : Path.Combine(_appPaths.CachePath, "mediainfo", cacheKey.GetMD5().ToString("N") + ".json");

            if (!string.IsNullOrEmpty(cacheKey))
            {
                try
                {
                    mediaInfo = _jsonSerializer.DeserializeFromFile <MediaInfo>(cacheFilePath);

                    //_logger.Debug("Found cached media info");
                }
                catch (Exception ex)
                {
                }
            }

            if (mediaInfo == null)
            {
                if (addProbeDelay)
                {
                    var delayMs = mediaSource.AnalyzeDurationMs ?? 0;
                    delayMs = Math.Max(3000, delayMs);
                    await Task.Delay(delayMs, cancellationToken).ConfigureAwait(false);
                }

                if (isLiveStream)
                {
                    mediaSource.AnalyzeDurationMs = 3000;
                }

                mediaInfo = await _mediaEncoder().GetMediaInfo(new MediaInfoRequest
                {
                    MediaSource     = mediaSource,
                    MediaType       = isAudio ? DlnaProfileType.Audio : DlnaProfileType.Video,
                    ExtractChapters = false
                }, cancellationToken).ConfigureAwait(false);

                if (cacheFilePath != null)
                {
                    _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(cacheFilePath));
                    _jsonSerializer.SerializeToFile(mediaInfo, cacheFilePath);

                    //_logger.Debug("Saved media info to {0}", cacheFilePath);
                }
            }

            var mediaStreams = mediaInfo.MediaStreams;

            if (isLiveStream && !string.IsNullOrEmpty(cacheKey))
            {
                var newList = new List <MediaStream>();
                newList.AddRange(mediaStreams.Where(i => i.Type == MediaStreamType.Video).Take(1));
                newList.AddRange(mediaStreams.Where(i => i.Type == MediaStreamType.Audio).Take(1));

                foreach (var stream in newList)
                {
                    stream.Index    = -1;
                    stream.Language = null;
                }

                mediaStreams = newList;
            }

            _logger.Info("Live tv media info probe took {0} seconds", (DateTime.UtcNow - now).TotalSeconds.ToString(CultureInfo.InvariantCulture));

            mediaSource.Bitrate       = mediaInfo.Bitrate;
            mediaSource.Container     = mediaInfo.Container;
            mediaSource.Formats       = mediaInfo.Formats;
            mediaSource.MediaStreams  = mediaStreams;
            mediaSource.RunTimeTicks  = mediaInfo.RunTimeTicks;
            mediaSource.Size          = mediaInfo.Size;
            mediaSource.Timestamp     = mediaInfo.Timestamp;
            mediaSource.Video3DFormat = mediaInfo.Video3DFormat;
            mediaSource.VideoType     = mediaInfo.VideoType;

            mediaSource.DefaultSubtitleStreamIndex = null;

            if (isLiveStream)
            {
                // Null this out so that it will be treated like a live stream
                if (!originalRuntime.HasValue)
                {
                    mediaSource.RunTimeTicks = null;
                }
            }

            var audioStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio);

            if (audioStream == null || audioStream.Index == -1)
            {
                mediaSource.DefaultAudioStreamIndex = null;
            }
            else
            {
                mediaSource.DefaultAudioStreamIndex = audioStream.Index;
            }

            var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);

            if (videoStream != null)
            {
                if (!videoStream.BitRate.HasValue)
                {
                    var width = videoStream.Width ?? 1920;

                    if (width >= 3000)
                    {
                        videoStream.BitRate = 30000000;
                    }

                    else if (width >= 1900)
                    {
                        videoStream.BitRate = 20000000;
                    }

                    else if (width >= 1200)
                    {
                        videoStream.BitRate = 8000000;
                    }

                    else if (width >= 700)
                    {
                        videoStream.BitRate = 2000000;
                    }
                }

                // This is coming up false and preventing stream copy
                videoStream.IsAVC = null;
            }

            if (isLiveStream)
            {
                mediaSource.AnalyzeDurationMs = 3000;
            }

            // Try to estimate this
            mediaSource.InferTotalBitrate(true);
        }
Esempio n. 17
0
        private MediaSourceInfo GetLegacyMediaSource(TunerHostInfo info, string channelId, ChannelInfo channel)
        {
            int?   width        = null;
            int?   height       = null;
            bool   isInterlaced = true;
            string videoCodec   = null;
            string audioCodec   = null;

            int?videoBitrate = null;
            int?audioBitrate = null;

            if (channel != null)
            {
                if (string.IsNullOrWhiteSpace(videoCodec))
                {
                    videoCodec = channel.VideoCodec;
                }
                audioCodec = channel.AudioCodec;
            }

            // normalize
            if (string.Equals(videoCodec, "mpeg2", StringComparison.OrdinalIgnoreCase))
            {
                videoCodec = "mpeg2video";
            }

            string nal = null;

            var url = GetApiUrl(info, false);
            var id  = channelId;

            id += "_" + url.GetMD5().ToString("N");

            var mediaSource = new MediaSourceInfo
            {
                Path         = url,
                Protocol     = MediaProtocol.Udp,
                MediaStreams = new List <MediaStream>
                {
                    new MediaStream
                    {
                        Type = MediaStreamType.Video,
                        // Set the index to -1 because we don't know the exact index of the video stream within the container
                        Index         = -1,
                        IsInterlaced  = isInterlaced,
                        Codec         = videoCodec,
                        Width         = width,
                        Height        = height,
                        BitRate       = videoBitrate,
                        NalLengthSize = nal
                    },
                    new MediaStream
                    {
                        Type = MediaStreamType.Audio,
                        // Set the index to -1 because we don't know the exact index of the audio stream within the container
                        Index   = -1,
                        Codec   = audioCodec,
                        BitRate = audioBitrate
                    }
                },
                RequiresOpening = true,
                RequiresClosing = true,
                BufferMs        = 0,
                Container       = "ts",
                Id = id,
                SupportsDirectPlay   = false,
                SupportsDirectStream = true,
                SupportsTranscoding  = true,
                IsInfiniteStream     = true
            };

            mediaSource.InferTotalBitrate();

            return(mediaSource);
        }