Exemplo n.º 1
0
        private async Task <StreamManifest> GetManifestAsync(StreamContext streamContext)
        {
            // To make sure there are no duplicates streams, group them by tag
            var streams = new Dictionary <int, IStreamInfo>();

            foreach (var streamInfo in streamContext.StreamInfoProviders)
            {
                var tag = streamInfo.GetTag();
                var url = streamInfo.GetUrl();

                // Signature
                var signature          = streamInfo.TryGetSignature();
                var signatureParameter = streamInfo.TryGetSignatureParameter() ?? "signature";

                if (!string.IsNullOrWhiteSpace(signature))
                {
                    signature = streamContext.CipherOperations.Decipher(signature);
                    url       = Url.SetQueryParameter(url, signatureParameter, signature);
                }

                // Content length
                var contentLength = streamInfo.TryGetContentLength() ??
                                    await _httpClient.TryGetContentLengthAsync(url, false) ??
                                    0;

                if (contentLength <= 0)
                {
                    continue; // broken stream URL?
                }
                // Common
                var container = Container.Parse(streamInfo.GetContainer());
                var fileSize  = new FileSize(contentLength);
                var bitrate   = new Bitrate(streamInfo.GetBitrate());

                var audioCodec = streamInfo.TryGetAudioCodec();
                var videoCodec = streamInfo.TryGetVideoCodec();

                // Muxed or Video-only
                if (!string.IsNullOrWhiteSpace(videoCodec))
                {
                    var framerate = new Framerate(streamInfo.TryGetFramerate() ?? 24);

                    var videoQualityLabel = streamInfo.TryGetVideoQualityLabel() ??
                                            Heuristics.GetVideoQualityLabel(tag, framerate.FramesPerSecond);

                    var videoQuality = Heuristics.GetVideoQuality(videoQualityLabel);

                    var videoWidth      = streamInfo.TryGetVideoWidth();
                    var videoHeight     = streamInfo.TryGetVideoHeight();
                    var videoResolution = videoWidth != null && videoHeight != null
                        ? new VideoResolution(videoWidth.Value, videoHeight.Value)
                        : Heuristics.GetVideoResolution(videoQuality);

                    // Muxed
                    if (!string.IsNullOrWhiteSpace(audioCodec))
                    {
                        streams[tag] = new MuxedStreamInfo(
                            tag,
                            url,
                            container,
                            fileSize,
                            bitrate,
                            audioCodec,
                            videoCodec,
                            videoQualityLabel,
                            videoQuality,
                            videoResolution,
                            framerate
                            );
                    }
                    // Video-only
                    else
                    {
                        streams[tag] = new VideoOnlyStreamInfo(
                            tag,
                            url,
                            container,
                            fileSize,
                            bitrate,
                            videoCodec,
                            videoQualityLabel,
                            videoQuality,
                            videoResolution,
                            framerate
                            );
                    }
                }
                // Audio-only
                else if (!string.IsNullOrWhiteSpace(audioCodec))
                {
                    streams[tag] = new AudioOnlyStreamInfo(
                        tag,
                        url,
                        container,
                        fileSize,
                        bitrate,
                        audioCodec
                        );
                }
                else
                {
#if DEBUG
                    throw FatalFailureException.Generic("Stream info doesn't contain audio/video codec information.");
#endif
                }
            }

            return(new StreamManifest(streams.Values.ToArray()));
        }