public ActiveCreative GetCompanionCreative(VastAdPod AdPod, VASTADInLine InlineAd, Companion_type Companion)
 {
     ICreativeSource PayloadSource = new CompanionSource(InlineAd, Companion);
     IAdTarget target = VastAdHandler.FindTarget(AdPod.AdUnit.Source, PayloadSource);
     if (target == null)
     {
         return null;
     }
     else
     {
         return GetCompanionCreative(PayloadSource, target);
     }
 }
 public ActiveCreative GetLinearCreative(VASTADInLine inline, VASTADInLineCreative Creative, VASTADInLineCreativeLinear linear, IAdSource AdSource)
 {
     //get the target. In this case because it is a linear ad, it will aways be the player's main adcontainer
     IAdTarget target = VastAdHandler.FindTarget(AdSource, new LinearSource(inline, null, linear, new VASTADWrapper[] { }));
     if (target == null)
     {
         return null;
     }
     else
     {
         return GetLinearCreative(linear, target, inline);
     }
 }
 public ActiveCreative GetNonLinearCreative(VASTADInLine InlineAd, VASTADInLineCreativeNonLinearAds nonLinears, NonLinear_type nl, IAdSource AdSource)
 {
     var PayloadSource = new NonLinearSource(InlineAd, nonLinears, nl);
     //get the target. In this case because it is a nonlinear ad, it will aways be the player's main adcontainer
     IAdTarget target = VastAdHandler.FindTarget(AdSource, PayloadSource);
     if (target == null)
     {
         return null;
     }
     else
     {
         return GetNonLinearCreative(PayloadSource, target);
     }
 }
        public ActiveCreative GetLinearCreative(VASTADInLine inline, VASTADInLineCreative Creative, VASTADInLineCreativeLinear linear, IAdSource AdSource)
        {
            //get the target. In this case because it is a linear ad, it will aways be the player's main adcontainer
            IAdTarget target = VastAdHandler.FindTarget(AdSource, new LinearSource(inline, null, linear, new VASTADWrapper[] { }));

            if (target == null)
            {
                return(null);
            }
            else
            {
                return(GetLinearCreative(linear, target, inline));
            }
        }
        public ActiveCreative GetCompanionCreative(VastAdPod AdPod, VASTADInLine InlineAd, Companion_type Companion)
        {
            ICreativeSource PayloadSource = new CompanionSource(InlineAd, Companion);
            IAdTarget       target        = VastAdHandler.FindTarget(AdPod.AdUnit.Source, PayloadSource);

            if (target == null)
            {
                return(null);
            }
            else
            {
                return(GetCompanionCreative(PayloadSource, target));
            }
        }
        public ActiveCreative GetNonLinearCreative(VASTADInLine InlineAd, VASTADInLineCreativeNonLinearAds nonLinears, NonLinear_type nl, IAdSource AdSource)
        {
            var PayloadSource = new NonLinearSource(InlineAd, nonLinears, nl);
            //get the target. In this case because it is a nonlinear ad, it will aways be the player's main adcontainer
            IAdTarget target = VastAdHandler.FindTarget(AdSource, PayloadSource);

            if (target == null)
            {
                return(null);
            }
            else
            {
                return(GetNonLinearCreative(PayloadSource, target));
            }
        }
        private static VASTADWrapperCreative FindMatchingCreative(VASTADInLine ad, VASTADInLineCreative creative, VASTADWrapper wrapperAd)
        {
            Type type = creative.Item.GetType();
            var  appropriateCreatives = ad.Creatives.Where(c => c.Item.GetType() == type);
            Type matchingType;

            if (type == typeof(VASTADInLineCreativeLinear))
            {
                matchingType = typeof(VASTADWrapperCreativeLinear);
            }
            else if (type == typeof(VASTADInLineCreativeNonLinearAds))
            {
                matchingType = typeof(VASTADWrapperCreativeNonLinearAds);
            }
            else if (type == typeof(VASTADInLineCreativeCompanionAds))
            {
                matchingType = typeof(VASTADWrapperCreativeCompanionAds);
            }
            else
            {
                return(null);
            }

            var appropriateWrapperCreatives = wrapperAd.Creatives.Where(c => c.Item.GetType() == matchingType);

            int index = appropriateCreatives.ToList().IndexOf(creative);

            if (appropriateWrapperCreatives.Count() > index)
            {
                return(appropriateWrapperCreatives.ElementAt(index));
            }
            else
            {
                return(null);
            }
        }
        public ActiveCreative GetLinearCreative(VASTADInLineCreativeLinear linear, IAdTarget adTarget, VASTADInLine ad, params VASTADWrapper[] wrappers)
        {
            IVpaid adPlayer;
            ICreativeSource adSource;
            var vPaidFactories = VastAdHandler.GetVpaidFactories().ToList();
            do
            {
                // get a list of all eligible media files
                var mediaAdSources = linear.MediaFiles.ToDictionary(m => m, m => new LinearSource(ad, m, linear, wrappers));

                var rankedMedia =
                    (from mediaAdSource in mediaAdSources
                     let vPaidFactoryAndPriority = vPaidFactories.ToDictionary(f => f, f => f.CheckSupport(mediaAdSource.Value, adTarget))
                     where vPaidFactoryAndPriority.Values.Any(v => v > PriorityCriteriaEnum.NotSupported)
                     let BestVpaidFactoryAndPriority = vPaidFactoryAndPriority.OrderByDescending(kvp => kvp.Value).First()
                     let rank = BestVpaidFactoryAndPriority.Value
                     orderby rank descending
                     select new
                     {
                         MediaFile = mediaAdSource.Key,
                         AdSource = mediaAdSource.Value,
                         VpaidFactory = BestVpaidFactoryAndPriority.Key,
                         Rank = rank,
                     }).ToList();

                if (rankedMedia.Any())
                {
                    // get all media with the best rankings
                    var topRank = rankedMedia.First().Rank;
                    var bestMedia = rankedMedia.Where(m => m.Rank == topRank);

                    // favor adaptive media
                    var adaptiveMedia = bestMedia.Where(m => m.MediaFile.delivery == VASTADInLineCreativeLinearMediaFileDelivery.streaming);
                    if (adaptiveMedia.Any())
                        bestMedia = adaptiveMedia;

                    double targetBitrateKbps = (double)VastAdHandler.AdHost.PlaybackBitrate / 1024;
                    if (targetBitrateKbps == 0.0)
                    {
                        // progressive videos won't have a bitrate. Therefore, target based on the one in the middle
                        targetBitrateKbps = rankedMedia.Where(m => m.MediaFile.bitrate.ToInt64().HasValue).Average(m => m.MediaFile.bitrate.ToInt64().Value);
                    }

                    // get the media with the closest bitrate
                    var bitrateMedia = bestMedia
                        .Where(m => m.MediaFile.bitrate.ToInt64().HasValue)
                        .GroupBy(m => Math.Abs(m.MediaFile.bitrate.ToInt64().Value))
                        .OrderBy(m => m.Key <= MaxBitrateKbps ? 0 : m.Key - MaxBitrateKbps)
                        .ThenBy(m => Math.Abs(m.Key - targetBitrateKbps))
                        .FirstOrDefault();
                    if (bitrateMedia != null && bitrateMedia.Any())
                        bestMedia = bitrateMedia;

                    //// get the media with the closest bitrate
                    //var selectedMedia = bestMedia
                    //    .Where(m => !string.IsNullOrEmpty(m.MediaFile.bitrate))
                    //    .OrderBy(m => Math.Abs(long.Parse(m.MediaFile.bitrate) - VastAdHandler.AdHost.PlaybackBitrate))
                    //    .FirstOrDefault();

                    // get the media with the closest size
                    var sizedMedia =
                        from m in bestMedia
                        where m.MediaFile.height.ToInt64().HasValue && m.MediaFile.width.ToInt64().HasValue
                        let x = VastAdHandler.AdHost.VideoArea.ActualHeight - m.MediaFile.height.ToInt64().Value
                        let y = VastAdHandler.AdHost.VideoArea.ActualWidth - m.MediaFile.width.ToInt64().Value
                        let delta = x + y
                        orderby delta
                        select new { Media = m, DeltaX = x, DeltaY = y };

                    // try to get the one with the closest size but both dimensions are within the current size
                    var selectedMedia = sizedMedia.Where(sm => sm.DeltaX >= 0 && sm.DeltaY >= 0).Select(sm => sm.Media).FirstOrDefault();
                    if (selectedMedia == null) // couldn't find one, instead get one with the closest size where only one dimension is over the current size
                        selectedMedia = sizedMedia.Where(sm => sm.DeltaX >= 0 || sm.DeltaY >= 0).Select(sm => sm.Media).FirstOrDefault();
                    if (selectedMedia == null) // couldn't find one, instead get one with the closest size
                        selectedMedia = sizedMedia.Select(sm => sm.Media).LastOrDefault();

                    // see if there were any bitrates, if not grab which ever one was first in the VAST file
                    if (selectedMedia == null)
                        selectedMedia = bestMedia.First();

                    //finally, get the ad player
                    adSource = selectedMedia.AdSource;
                    adPlayer = selectedMedia.VpaidFactory.GetVpaidPlayer(adSource, adTarget);

                    if (adPlayer == null)
                    {
                        //Log.Output(OutputType.Error, "Error - cannot find player to support video ad content. ");
                        // this should never happen and is the result of a bad VPaid plugin.
                        throw new Exception("VpaidFactory agreed to accept AdSource and then returned null during call to GetVPaidPlugin.");
                    }
                    // handshake with the ad player to make sure the version of VAST is OK
                    if (adPlayer == null || !vastAdHandler.VpaidController.Handshake(adPlayer))
                    {
                        // the version is no good, remove the factory from the list and try again.
                        vPaidFactories.Remove(selectedMedia.VpaidFactory);
                        if (!vPaidFactories.Any())
                        {
                            return null;
                        }
                        adPlayer = null;
                    }
                }
                else
                {
                    return null;
                }
            } while (adPlayer == null);

            //put video in target
            if (!adTarget.AddChild(adPlayer))
            {
                return null;
            }

            return new ActiveCreative(adPlayer, adSource, adTarget);
        }
        private static VASTADWrapperCreative FindMatchingCreative(VASTADInLine ad, VASTADInLineCreative creative, VASTADWrapper wrapperAd)
        {
            Type type = creative.Item.GetType();
            var appropriateCreatives = ad.Creatives.Where(c => c.Item.GetType() == type);
            Type matchingType;
            if (type == typeof(VASTADInLineCreativeLinear))
                matchingType = typeof(VASTADWrapperCreativeLinear);
            else if (type == typeof(VASTADInLineCreativeNonLinearAds))
                matchingType = typeof(VASTADWrapperCreativeNonLinearAds);
            else if (type == typeof(VASTADInLineCreativeCompanionAds))
                matchingType = typeof(VASTADWrapperCreativeCompanionAds);
            else
                return null;

            var appropriateWrapperCreatives = wrapperAd.Creatives.Where(c => c.Item.GetType() == matchingType);

            int index = appropriateCreatives.ToList().IndexOf(creative);
            if (appropriateWrapperCreatives.Count() > index)
                return appropriateWrapperCreatives.ElementAt(index);
            else
                return null;
        }
        /// <summary>
        /// Merges the tracking info for the two ads
        /// </summary>
        private static void MergeWrappedAd(VASTADWrapper wrapperAd, VASTADInLine inlineAd)
        {
            // carry over impressions
            {
                var newItems = inlineAd.Impression.ToList();
                newItems.AddRange(wrapperAd.Impression.Select(i => new Impression_type() { Value = i }));
                inlineAd.Impression = newItems.ToArray();
            }
            // carry over tracking events
            foreach (var creative in inlineAd.Creatives)
            {
                var creativeItem = creative.Item;
                VASTADWrapperCreative creativeWrapper = FindMatchingCreative(inlineAd, creative, wrapperAd);
                if (creativeWrapper != null)
                {
                    if (creativeItem is VASTADInLineCreativeLinear)
                    {
                        var linearWrapper = (VASTADWrapperCreativeLinear)creativeWrapper.Item;
                        if (linearWrapper != null && linearWrapper.TrackingEvents != null)
                        {
                            var linear = (VASTADInLineCreativeLinear)creativeItem;
                            List<TrackingEvents_typeTracking> newItems;
                            if (linear.TrackingEvents == null)
                                newItems = new List<TrackingEvents_typeTracking>();
                            else
                                newItems = linear.TrackingEvents.ToList();
                            newItems.AddRange(linearWrapper.TrackingEvents);

                            linear.TrackingEvents = newItems.ToArray();
                        }
                    }
                    else if (creativeItem is VASTADInLineCreativeNonLinearAds)
                    {
                        var nonlinearWrapper = (VASTADWrapperCreativeNonLinearAds)creativeWrapper.Item;
                        if (nonlinearWrapper != null && nonlinearWrapper.TrackingEvents != null)
                        {
                            var nonLinear = (VASTADInLineCreativeNonLinearAds)creativeItem;
                            List<TrackingEvents_typeTracking> newItems;
                            if (nonLinear.TrackingEvents == null)
                                newItems = new List<TrackingEvents_typeTracking>();
                            else
                                newItems = nonLinear.TrackingEvents.ToList();
                            newItems.AddRange(nonlinearWrapper.TrackingEvents);

                            nonLinear.TrackingEvents = newItems.ToArray();
                        }
                    }
                    else if (creativeItem is VASTADInLineCreativeCompanionAds)
                    {
                        var companions = (VASTADInLineCreativeCompanionAds)creativeItem;
                        foreach (var companion in companions.Companion)
                        {
                            Companion_type companionWrapper = FindMatchingCompanion(companions, companion, (VASTADWrapperCreativeCompanionAds)creativeWrapper.Item);
                            if (companionWrapper != null && companionWrapper.TrackingEvents != null)
                            {
                                List<TrackingEvents_typeTracking> newItems;
                                if (companion.TrackingEvents == null)
                                    newItems = new List<TrackingEvents_typeTracking>();
                                else
                                    newItems = companion.TrackingEvents.ToList();
                                newItems.AddRange(companionWrapper.TrackingEvents);

                                companion.TrackingEvents = newItems.ToArray();
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Merges the tracking info for the two ads
        /// </summary>
        private static void MergeWrappedAd(VASTADWrapper wrapperAd, VASTADInLine inlineAd)
        {
            // carry over impressions
            {
                var newItems = inlineAd.Impression.ToList();
                newItems.AddRange(wrapperAd.Impression.Select(i => new Impression_type()
                {
                    Value = i
                }));
                inlineAd.Impression = newItems.ToArray();
            }
            // carry over tracking events
            foreach (var creative in inlineAd.Creatives)
            {
                var creativeItem = creative.Item;
                VASTADWrapperCreative creativeWrapper = FindMatchingCreative(inlineAd, creative, wrapperAd);
                if (creativeWrapper != null)
                {
                    if (creativeItem is VASTADInLineCreativeLinear)
                    {
                        var linearWrapper = (VASTADWrapperCreativeLinear)creativeWrapper.Item;
                        if (linearWrapper != null && linearWrapper.TrackingEvents != null)
                        {
                            var linear = (VASTADInLineCreativeLinear)creativeItem;
                            List <TrackingEvents_typeTracking> newItems;
                            if (linear.TrackingEvents == null)
                            {
                                newItems = new List <TrackingEvents_typeTracking>();
                            }
                            else
                            {
                                newItems = linear.TrackingEvents.ToList();
                            }
                            newItems.AddRange(linearWrapper.TrackingEvents);

                            linear.TrackingEvents = newItems.ToArray();
                        }
                    }
                    else if (creativeItem is VASTADInLineCreativeNonLinearAds)
                    {
                        var nonlinearWrapper = (VASTADWrapperCreativeNonLinearAds)creativeWrapper.Item;
                        if (nonlinearWrapper != null && nonlinearWrapper.TrackingEvents != null)
                        {
                            var nonLinear = (VASTADInLineCreativeNonLinearAds)creativeItem;
                            List <TrackingEvents_typeTracking> newItems;
                            if (nonLinear.TrackingEvents == null)
                            {
                                newItems = new List <TrackingEvents_typeTracking>();
                            }
                            else
                            {
                                newItems = nonLinear.TrackingEvents.ToList();
                            }
                            newItems.AddRange(nonlinearWrapper.TrackingEvents);

                            nonLinear.TrackingEvents = newItems.ToArray();
                        }
                    }
                    else if (creativeItem is VASTADInLineCreativeCompanionAds)
                    {
                        var companions = (VASTADInLineCreativeCompanionAds)creativeItem;
                        foreach (var companion in companions.Companion)
                        {
                            Companion_type companionWrapper = FindMatchingCompanion(companions, companion, (VASTADWrapperCreativeCompanionAds)creativeWrapper.Item);
                            if (companionWrapper != null && companionWrapper.TrackingEvents != null)
                            {
                                List <TrackingEvents_typeTracking> newItems;
                                if (companion.TrackingEvents == null)
                                {
                                    newItems = new List <TrackingEvents_typeTracking>();
                                }
                                else
                                {
                                    newItems = companion.TrackingEvents.ToList();
                                }
                                newItems.AddRange(companionWrapper.TrackingEvents);

                                companion.TrackingEvents = newItems.ToArray();
                            }
                        }
                    }
                }
            }
        }
        public ActiveCreative GetLinearCreative(VASTADInLineCreativeLinear linear, IAdTarget adTarget, VASTADInLine ad, params VASTADWrapper[] wrappers)
        {
            IVpaid          adPlayer;
            ICreativeSource adSource;
            var             vPaidFactories = VastAdHandler.GetVpaidFactories().ToList();

            do
            {
                // get a list of all eligible media files
                var mediaAdSources = linear.MediaFiles.ToDictionary(m => m, m => new LinearSource(ad, m, linear, wrappers));

                var rankedMedia =
                    (from mediaAdSource in mediaAdSources
                     let vPaidFactoryAndPriority = vPaidFactories.ToDictionary(f => f, f => f.CheckSupport(mediaAdSource.Value, adTarget))
                                                   where vPaidFactoryAndPriority.Values.Any(v => v > PriorityCriteriaEnum.NotSupported)
                                                   let BestVpaidFactoryAndPriority = vPaidFactoryAndPriority.OrderByDescending(kvp => kvp.Value).First()
                                                                                     let rank = BestVpaidFactoryAndPriority.Value
                                                                                                orderby rank descending
                                                                                                select new
                {
                    MediaFile = mediaAdSource.Key,
                    AdSource = mediaAdSource.Value,
                    VpaidFactory = BestVpaidFactoryAndPriority.Key,
                    Rank = rank,
                }).ToList();

                if (rankedMedia.Any())
                {
                    // get all media with the best rankings
                    var topRank   = rankedMedia.First().Rank;
                    var bestMedia = rankedMedia.Where(m => m.Rank == topRank);

                    // favor adaptive media
                    var adaptiveMedia = bestMedia.Where(m => m.MediaFile.delivery == VASTADInLineCreativeLinearMediaFileDelivery.streaming);
                    if (adaptiveMedia.Any())
                    {
                        bestMedia = adaptiveMedia;
                    }

                    double targetBitrateKbps = (double)VastAdHandler.AdHost.PlaybackBitrate / 1024;
                    if (targetBitrateKbps == 0.0)
                    {
                        // progressive videos won't have a bitrate. Therefore, target based on the one in the middle
                        targetBitrateKbps = rankedMedia.Where(m => m.MediaFile.bitrate.ToInt64().HasValue).Average(m => m.MediaFile.bitrate.ToInt64().Value);
                    }

                    // get the media with the closest bitrate
                    var bitrateMedia = bestMedia
                                       .Where(m => m.MediaFile.bitrate.ToInt64().HasValue)
                                       .GroupBy(m => Math.Abs(m.MediaFile.bitrate.ToInt64().Value))
                                       .OrderBy(m => m.Key <= MaxBitrateKbps ? 0 : m.Key - MaxBitrateKbps)
                                       .ThenBy(m => Math.Abs(m.Key - targetBitrateKbps))
                                       .FirstOrDefault();
                    if (bitrateMedia != null && bitrateMedia.Any())
                    {
                        bestMedia = bitrateMedia;
                    }

                    //// get the media with the closest bitrate
                    //var selectedMedia = bestMedia
                    //    .Where(m => !string.IsNullOrEmpty(m.MediaFile.bitrate))
                    //    .OrderBy(m => Math.Abs(long.Parse(m.MediaFile.bitrate) - VastAdHandler.AdHost.PlaybackBitrate))
                    //    .FirstOrDefault();

                    // get the media with the closest size
                    var sizedMedia =
                        from m in bestMedia
                        where m.MediaFile.height.ToInt64().HasValue&& m.MediaFile.width.ToInt64().HasValue
                        let x                                                     = VastAdHandler.AdHost.VideoArea.ActualHeight - m.MediaFile.height.ToInt64().Value
                                                    let y                         = VastAdHandler.AdHost.VideoArea.ActualWidth - m.MediaFile.width.ToInt64().Value
                                                                        let delta = x + y
                                                                                    orderby delta
                                                                                    select new { Media = m, DeltaX = x, DeltaY = y };

                    // try to get the one with the closest size but both dimensions are within the current size
                    var selectedMedia = sizedMedia.Where(sm => sm.DeltaX >= 0 && sm.DeltaY >= 0).Select(sm => sm.Media).FirstOrDefault();
                    if (selectedMedia == null) // couldn't find one, instead get one with the closest size where only one dimension is over the current size
                    {
                        selectedMedia = sizedMedia.Where(sm => sm.DeltaX >= 0 || sm.DeltaY >= 0).Select(sm => sm.Media).FirstOrDefault();
                    }
                    if (selectedMedia == null) // couldn't find one, instead get one with the closest size
                    {
                        selectedMedia = sizedMedia.Select(sm => sm.Media).LastOrDefault();
                    }

                    // see if there were any bitrates, if not grab which ever one was first in the VAST file
                    if (selectedMedia == null)
                    {
                        selectedMedia = bestMedia.First();
                    }

                    //finally, get the ad player
                    adSource = selectedMedia.AdSource;
                    adPlayer = selectedMedia.VpaidFactory.GetVpaidPlayer(adSource, adTarget);

                    if (adPlayer == null)
                    {
                        //Log.Output(OutputType.Error, "Error - cannot find player to support video ad content. ");
                        // this should never happen and is the result of a bad VPaid plugin.
                        throw new Exception("VpaidFactory agreed to accept AdSource and then returned null during call to GetVPaidPlugin.");
                    }
                    // handshake with the ad player to make sure the version of VAST is OK
                    if (adPlayer == null || !vastAdHandler.VpaidController.Handshake(adPlayer))
                    {
                        // the version is no good, remove the factory from the list and try again.
                        vPaidFactories.Remove(selectedMedia.VpaidFactory);
                        if (!vPaidFactories.Any())
                        {
                            return(null);
                        }
                        adPlayer = null;
                    }
                }
                else
                {
                    return(null);
                }
            } while (adPlayer == null);

            //put video in target
            if (!adTarget.AddChild(adPlayer))
            {
                return(null);
            }

            return(new ActiveCreative(adPlayer, adSource, adTarget));
        }