public async Task <bool> EndTuningAsync(string clientId) { try { if (!_clientChannels.TryRemove(clientId, out ChannelInfo channel)) { return(false); } if (channel != null && _clientChannels.All(c => c.Value?.ChannelId != channel.ChannelId)) { ITimeshiftControlEx timeshiftControl = ServiceRegistration.Get <ITvProvider>() as ITimeshiftControlEx; if (!(await timeshiftControl.StopTimeshiftAsync(TV_USER_NAME, channel.SlotIndex).ConfigureAwait(false))) { _logger.Error("SlimTvHandler: Couldn't stop timeshifting for channel {0}", channel.ChannelId); return(false); } } return(true); } catch (Exception ex) { _logger.Error("SlimTvHandler: Error ending tuning for client {0}", ex, clientId); } return(false); }
private UPnPError OnDeInit(DvAction action, IList <object> inParams, out IList <object> outParams, CallContext context) { outParams = new List <object>(); ITimeshiftControlEx timeshiftControl = ServiceRegistration.Get <ITvProvider>() as ITimeshiftControlEx; if (timeshiftControl == null) { return(new UPnPError(500, "ITimeshiftControl service not available")); } // We use the client's RemoteAdress as unique "user name", so we do not need to pass this argument from clients via UPnP. timeshiftControl.StopTimeshift(BuildUserName(context), 0); timeshiftControl.StopTimeshift(BuildUserName(context), 1); outParams = new List <object> { true }; return(null); }
private UPnPError OnStopTimeshift(DvAction action, IList <object> inParams, out IList <object> outParams, CallContext context) { outParams = new List <object>(); ITimeshiftControlEx timeshiftControl = ServiceRegistration.Get <ITvProvider>() as ITimeshiftControlEx; if (timeshiftControl == null) { return(new UPnPError(500, "ITimeshiftControl service not available")); } int slotIndex = (int)inParams[0]; // We use the client's RemoteAdress as unique "user name", so we do not need to pass this argument from clients via UPnP. bool result = timeshiftControl.StopTimeshiftAsync(BuildUserName(context), slotIndex).Result; outParams = new List <object> { result }; return(null); }
public async Task <(bool Success, MediaItem LiveMediaItem)> StartTuningAsync(string clientId, int channelId) { try { var client = _clientChannels.FirstOrDefault(c => c.Value.ChannelId == channelId); if (client.Value?.Channel?.MediaItem != null) { //Check if already streaming if (client.Key == clientId) { return(true, client.Value.Channel.MediaItem); } //Use same stream url as other channel if (!_clientChannels.TryAdd(clientId, client.Value)) { return(false, null); } else { return(true, client.Value.Channel.MediaItem); } } if (ServiceRegistration.IsRegistered <ITvProvider>()) { IChannelAndGroupInfoAsync channelAndGroupInfo = ServiceRegistration.Get <ITvProvider>() as IChannelAndGroupInfoAsync; var channelResult = await channelAndGroupInfo.GetChannelAsync(channelId).ConfigureAwait(false); if (!channelResult.Success) { _logger.Error("SlimTvHandler: Couldn't find channel {0}", channelId); return(false, null); } var slotIndex = GetFreeSlot(); if (slotIndex == null) { _logger.Error("SlimTvHandler: Couldn't find free slot for channel {0}", channelId); return(false, null); } ITimeshiftControlEx timeshiftControl = ServiceRegistration.Get <ITvProvider>() as ITimeshiftControlEx; var mediaItem = (await timeshiftControl.StartTimeshiftAsync(TV_USER_NAME, slotIndex.Value, channelResult.Result).ConfigureAwait(false)); if (!mediaItem.Success) { _logger.Error("SlimTvHandler: Couldn't start timeshifting for channel {0}", channelId); return(false, null); } try { //Initiate channel cache ChannelInfo newChannel = new ChannelInfo { Channel = new TranscodeChannel(), SlotIndex = slotIndex.Value, ChannelId = channelId, }; if (!_clientChannels.TryAdd(clientId, newChannel)) { await timeshiftControl.StopTimeshiftAsync(TV_USER_NAME, slotIndex.Value); return(false, null); } newChannel.Channel.SetChannel(mediaItem.Result); } catch { _clientChannels.TryRemove(clientId, out ChannelInfo c); await timeshiftControl.StopTimeshiftAsync(TV_USER_NAME, slotIndex.Value); throw; } return(true, mediaItem.Result); } return(false, null); } catch (Exception ex) { _logger.Error("SlimTvHandler: Error starting tuning of channel {0}", ex, channelId); return(false, null); } }