private StreamingDetails GetStream(CancellationToken cancellationToken, WebMediaType webMediaType, string itemId, TimeSpan startPosition)
        {
            // var profile = GetTranscoderProfile(cancellationToken, "Direct");

            var profile = GetTranscoderProfile(cancellationToken, Configuration.StreamingProfileName);
            if (profile == null)
            {
                throw new Exception(String.Format("Cannot find a profile with the name {0}", Configuration.StreamingProfileName));
            }

            var identifier = HttpUtility.UrlEncode(String.Format("{0}-{1}-{2:yyyyMMddHHmmss}", webMediaType, itemId, DateTime.UtcNow));
            var isStreamInitialised = GetFromService<WebBoolResult>(cancellationToken,
                    "InitStream?type={0}&provider={1}&itemId={2}&identifier={3}&idleTimeout={4}&clientDescription={5}",
                    webMediaType,
                    STREAM_TV_RECORDING_PROVIDER, // Provider - use 0 for recordings and tv
                    itemId, // itemId
                    identifier, // identifier
                    STREAM_TIMEOUT_DIRECT,
                    identifier).Result; //Idletimoue

            if (!isStreamInitialised)
            {
                throw new Exception(String.Format("Could not initialise the stream. Identifier={0}", identifier));
            }

            // Returns the url for streaming
            var url = GetFromService<WebStringResult>(cancellationToken,"StartStream?identifier={0}&profileName={1}&startPosition={2}",
                    identifier,
                    profile.Name, // Provider
                    (Int32)startPosition.TotalSeconds).Result;

            var isAuthorised = true;
            foreach (var ipAddress in _networkManager.GetLocalIpAddresses())
            {
                isAuthorised = isAuthorised && GetFromService<WebBoolResult>(
                    cancellationToken, "AuthorizeRemoteHostForStreaming?host={0}", ipAddress).Result;
            }

            if (!isAuthorised)
            {
                throw new Exception(String.Format("Could not authorise the stream. Identifier={0}", identifier));
            }

            var streamingDetails = new StreamingDetails()
            {
                StreamIdentifier = identifier,
                SourceInfo = new MediaSourceInfo()
                {
                    Path = url,
                    Protocol = MediaProtocol.Http,
                    Id = itemId,
                }
            };

            var mediaInfoId = webMediaType == WebMediaType.Recording ? itemId : identifier;
            var mediaInfo = GetMediaInfoFromStream(cancellationToken, webMediaType, mediaInfoId);
            if (mediaInfo != null)
            {
                streamingDetails.SourceInfo.Container = mediaInfo.Container;
                streamingDetails.SourceInfo.RunTimeTicks = TimeSpan.FromSeconds(mediaInfo.Duration).Ticks;

                //streamingDetails.SourceInfo.AudioChannels = mediaInfo.AudioStreams.Count;
                //var defaultAudioStream = mediaInfo.AudioStreams.FirstOrDefault();
                //if (defaultAudioStream != null)
                //{
                //    streamingDetails.SourceInfo.AudioCodec = defaultAudioStream.Codec;
                //}

                //var defaultVideoStream = mediaInfo.VideoStreams.FirstOrDefault();
                //if (defaultVideoStream != null)
                //{
                //    streamingDetails.SourceInfo.VideoCodec = defaultVideoStream.Codec;
                //    streamingDetails.SourceInfo.Height = defaultVideoStream.Height;
                //    streamingDetails.SourceInfo.Width = defaultVideoStream.Width;
                //}
            }

            return streamingDetails;
        }
        private StreamingDetails GetStream(CancellationToken cancellationToken, WebMediaType webMediaType, string itemId, TimeSpan startPosition)
        {
            Plugin.Logger.Info("Streaming setting RequiresAuthentication: {0}", Configuration.RequiresAuthentication);
            Plugin.Logger.Info("Streaming setting StreamingProfileName: {0}", Configuration.StreamingProfileName);
            Plugin.Logger.Info("Streaming setting StreamDelay: {0}", Configuration.StreamDelay);

            var configuration = Plugin.Instance.Configuration;
            var profile = GetTranscoderProfile(cancellationToken, Configuration.StreamingProfileName);
            var identifier = HttpUtility.UrlEncode(String.Format("{0}-{1}-{2:yyyyMMddHHmmss}", webMediaType, itemId, DateTime.UtcNow));

            var url = GetUrl(_streamingEndpoint, "DoStream?type={0}&provider={1}&itemId={2}&clientDescription={3}&profileName={4}&startPosition={5}&idleTimeout={6}&identifier={7}",
                        webMediaType,
                        STREAM_TV_RECORDING_PROVIDER,
                        itemId,
                        identifier,
                        profile.Name,
                        (Int32)startPosition.TotalSeconds,
                        STREAM_TIMEOUT_DIRECT,
                        identifier);

            var streamingDetails = new StreamingDetails()
            {
                StreamIdentifier = identifier,
                SourceInfo = new MediaSourceInfo()
                {
                    Path = url,
                    Protocol = MediaProtocol.Http,
                    Id = identifier, //itemId,
                }
            };

            if (configuration.RequiresAuthentication)
            {
                string authInfo = String.Format("{0}:{1}", configuration.UserName, configuration.Password);
                authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));

                streamingDetails.SourceInfo.SupportsDirectPlay = false;
                streamingDetails.SourceInfo.RequiredHttpHeaders = new Dictionary<string, string> { { "Authentication", "Basic " + authInfo } };
            }

            System.Threading.Thread.Sleep(Plugin.Instance.Configuration.StreamDelay.Value);

            return streamingDetails;
        }
        public Task<MediaSourceInfo> GetRecordingStream(string recordingId, string streamId, CancellationToken cancellationToken)
        {
            // Cancel the existing stream if present
            if (_currentStreamDetails != null)
            {
                Plugin.StreamingProxy.CancelStream(cancellationToken, _currentStreamDetails.StreamIdentifier);
            }

            // Start a new one and store it away
            _currentStreamDetails = Plugin.StreamingProxy.GetRecordingStream(cancellationToken, recordingId, TimeSpan.Zero);
            return Task.FromResult(_currentStreamDetails.SourceInfo);

        }
        public Task<MediaSourceInfo> GetChannelStream(string channelId, string streamId, CancellationToken cancellationToken)
        {
            // Cancel the existing stream if present
            if (_currentStreamDetails != null)
            {
                Plugin.StreamingProxy.CancelStream(cancellationToken, _currentStreamDetails.StreamIdentifier);
            }

            // Start a new one and store it away
            _currentStreamDetails = Plugin.StreamingProxy.GetLiveTvStream(cancellationToken, channelId);
            return Task.FromResult(_currentStreamDetails.SourceInfo);

        }
 public Task<MediaSourceInfo> GetRecordingStream(string recordingId, string streamId, CancellationToken cancellationToken)
 {
     _currentStreamDetails = Plugin.StreamingProxy.GetRecordingStream(cancellationToken, recordingId, TimeSpan.Zero);
     return Task.FromResult(_currentStreamDetails.SourceInfo);
 }
 public Task<MediaSourceInfo> GetChannelStream(string channelId, string streamId, CancellationToken cancellationToken)
 {
     _currentStreamDetails = Plugin.StreamingProxy.GetLiveTvStream(cancellationToken, channelId);
     return Task.FromResult(_currentStreamDetails.SourceInfo);
 }