Пример #1
0
        public async Task <bool> StartTimeshiftAsync(int slotIndex, IChannel channel)
        {
            if (TimeshiftControl == null || channel == null)
            {
                return(false);
            }

            ServiceRegistration.Get <ILogger>().Debug("SlimTvHandler: StartTimeshift slot {0} for channel '{1}'", slotIndex, channel.Name);

            int newSlotIndex = GetMatchingSlotIndex(slotIndex);

            if (_slotContexes[newSlotIndex].Channel != null && _slotContexes[newSlotIndex].Channel.MediaType != channel.MediaType)
            {
                // Switching between TV and radio - must close old player and start brand new one
                IPlayerContext playerContext = existingPlayerContext(newSlotIndex);
                if (playerContext == null)
                {
                    return(false);
                }
                playerContext.Close();
                await StopTimeshiftAsync(newSlotIndex);
            }
            var result = await TimeshiftControl.StartTimeshiftAsync(newSlotIndex, channel);

            IList <MultipleMediaItemAspect> pras;
            MediaItem timeshiftMediaItem = result.Result;

            if (result.Success && timeshiftMediaItem != null && MediaItemAspect.TryGetAspects(timeshiftMediaItem.Aspects, ProviderResourceAspect.Metadata, out pras))
            {
                string newAccessorPath = (string)pras[0].GetAttributeValue(ProviderResourceAspect.ATTR_RESOURCE_ACCESSOR_PATH);

                // if slot was empty, start a new player
                if (_slotContexes[newSlotIndex].AccessorPath == null)
                {
                    AddOrUpdateTimeshiftContext(timeshiftMediaItem as LiveTvMediaItem, channel);
                    PlayerContextConcurrencyMode playMode = GetMatchingPlayMode();
                    await PlayItemsModel.PlayOrEnqueueItem(timeshiftMediaItem, true, playMode);
                }
                else
                {
                    try
                    {
                        _slotContexes[newSlotIndex].CardChanging = true;
                        UpdateExistingMediaItem(timeshiftMediaItem, newSlotIndex);
                    }
                    finally
                    {
                        _slotContexes[newSlotIndex].CardChanging = false;
                    }
                }
                _slotContexes[newSlotIndex].AccessorPath = newAccessorPath;
                _slotContexes[newSlotIndex].Channel      = channel;
            }
            return(result.Success);
        }
Пример #2
0
        internal static async Task <MediaItem> StartTimeshiftAsync(IOwinContext context, int id, string userName)
        {
            var channel = await GetChannelAsync(id);

            if (channel == null)
            {
                return(null);
            }

            var client = CLIENT_CHANNELS.FirstOrDefault(c => c.Value.ChannelId == id);

            if (client.Value?.MediaItem != null)
            {
                //Check if already streaming
                if (client.Key == userName)
                {
                    return(client.Value.MediaItem);
                }

                //Use same stream url as other channel
                if (!CLIENT_CHANNELS.TryAdd(userName, client.Value))
                {
                    return(null);
                }
                else
                {
                    return(client.Value.MediaItem);
                }
            }

            var freeSlot = GetFreeSlot();

            if (freeSlot == null)
            {
                return(null);
            }

            var item = await TimeshiftControl.StartTimeshiftAsync(userName, freeSlot.Value, channel);

            if (!item.Success)
            {
                return(null);
            }

            //Initiate channel cache
            ChannelInfo newChannel = new ChannelInfo
            {
                SlotIndex = freeSlot.Value,
                ChannelId = id,
                MediaItem = item.Result,
                UserName  = userName
            };

            if (!CLIENT_CHANNELS.TryAdd(userName, newChannel))
            {
                await TimeshiftControl.StopTimeshiftAsync(userName, freeSlot.Value);

                return(null);
            }

            return(item.Result);
        }