private void ExecuteAd(IAsyncAdPayload payload, IAdSource source)
 {
     if (!ExecutePayload(payload, source))
     {
         ExecuteAdFailed(source);
     }
 }
        /// <summary>
        /// This is where it all begins. Calling this method will load the source if the plugin format matches and ads will starting playing in succession.
        /// </summary>
        /// <param name="source">The source for the ad (contains info about the source and targets)</param>
        /// <returns>Returns an object representing the exectuing ad. This object can then be used to deactivate the ad.</returns>
        public IAdPayload Handle(IAdSource source)
        {
            if (!IsEnabled)
            {
                return(null);
            }

            if (!CanHandleAd(source))
            {
                return(null);
            }

            //assume we need to pull down source docs, block the player from starting a new video until this is done. If a video is already playing, this will not interfere.
            IAsyncAdPayload result = CreatePayload(source);

            ProcessPayload(result, source);

            return(result);
        }
        void ProcessPayload(IAsyncAdPayload payload, IAdSource source)
        {
            switch (payload.State)
            {
            case StateEnum.Loading:
                EventHandler payload_StateChanged = null;
                payload_StateChanged = (object sender, EventArgs e) =>
                {
                    payload.StateChanged -= payload_StateChanged;
                    ProcessPayload(payload, source);
                };
                payload.StateChanged += payload_StateChanged;
                break;

            case StateEnum.Ready:
                ExecuteAd(payload, source);
                break;

            case StateEnum.Failed:
                ExecuteAdFailed(source);
                break;
            }
        }
        protected override bool ExecutePayload(IAsyncAdPayload Payload, IAdSource Source)
        {
            var result = Payload as VastAdUnit;
            var adPod = result.AdPod;

            bool needsRelease;
            Player.ActiveMediaPlugin.VisualElement.IfNotNull(v => v.Visibility = System.Windows.Visibility.Collapsed);
            if (((Microsoft.SilverlightMediaFramework.Core.SMFPlayer)adHost).IsPlayBlocked)
            {
                // if we're already blocked, stick with it.
                AdHost.AddPlayBlock(loadBlocker);
                needsRelease = true;
            }
            else
            {
                Player.ActiveMediaPlugin.Pause();
                needsRelease = false;
            }

            adPod.LoadAsync(success =>
            {
                Player.ActiveMediaPlugin.VisualElement.IfNotNull(v => v.Visibility = System.Windows.Visibility.Visible);
                if (success)
                {
                    // now that it is loaded, watch for each vastAd and CreativeSet to begin and complete running
                    foreach (var vastAd in adPod.Ads)
                    {
                        vastAd.RunCompleted += vastAd_RunCompleted;
                        foreach (var creativeSet in vastAd.CreativeSets)
                        {
                            creativeSet.RunStarting += creativeSet_RunStarting;
                            creativeSet.RunStarted += creativeSet_RunStarted;
                            creativeSet.RunCompleted += creativeSet_RunCompleted;
                        }
                    }

                    // pass on that we are now running this ad. Note: It still could fail to run.
                    result.OnStart();
                    // actually run the ad
                    adPod.RunCompleted += new Action<AdPod, bool>(adPod_RunCompleted);
                    adPod.ReleasePlayer += new Action<AdPod>(adPod_ReleasePlayer);
                    adPod.RunAsync();
                }
                else
                {
                    // clear out the current running AdSpot. This permits other ads to be handled.
                    activeCreativeSets.Clear();
                    activeAdPod = null;

                    // notify upstream
                    result.OnFail();
                    result.Deactivate();

                    base.ExecuteAdFailed(Source);

                    if (!needsRelease)
                    {
                        Player.ActiveMediaPlugin.Play();
                    }
                }

                if (needsRelease)
                {
                    adHost.ReleasePlayBlock(loadBlocker);
                }

            });
            return true;
        }
 protected abstract bool ExecutePayload(IAsyncAdPayload PackagePayload, IAdSource Source);
 private void ExecuteAd(IAsyncAdPayload payload, IAdSource source)
 {
     if (!ExecutePayload(payload, source))
     {
         ExecuteAdFailed(source);
     }
 }
 void ProcessPayload(IAsyncAdPayload payload, IAdSource source)
 {
     switch (payload.State)
     {
         case StateEnum.Loading:
             EventHandler payload_StateChanged = null;
             payload_StateChanged = (object sender, EventArgs e) =>
             {
                 payload.StateChanged -= payload_StateChanged;
                 ProcessPayload(payload, source);
             };
             payload.StateChanged += payload_StateChanged;
             break;
         case StateEnum.Ready:
             ExecuteAd(payload, source);
             break;
         case StateEnum.Failed:
             ExecuteAdFailed(source);
             break;
     }
 }
        protected override bool ExecutePayload(IAsyncAdPayload Payload, IAdSource Source)
        {
            var result = Payload as VastAdUnit;
            var adPod  = result.AdPod;

            bool needsRelease;

            Player.ActiveMediaPlugin.VisualElement.IfNotNull(v => v.Visibility = System.Windows.Visibility.Collapsed);
            if (((Microsoft.SilverlightMediaFramework.Core.SMFPlayer)adHost).IsPlayBlocked)
            {
                // if we're already blocked, stick with it.
                AdHost.AddPlayBlock(loadBlocker);
                needsRelease = true;
            }
            else
            {
                Player.ActiveMediaPlugin.Pause();
                needsRelease = false;
            }

            adPod.LoadAsync(success =>
            {
                Player.ActiveMediaPlugin.VisualElement.IfNotNull(v => v.Visibility = System.Windows.Visibility.Visible);
                if (success)
                {
                    // now that it is loaded, watch for each vastAd and CreativeSet to begin and complete running
                    foreach (var vastAd in adPod.Ads)
                    {
                        vastAd.RunCompleted += vastAd_RunCompleted;
                        foreach (var creativeSet in vastAd.CreativeSets)
                        {
                            creativeSet.RunStarting  += creativeSet_RunStarting;
                            creativeSet.RunStarted   += creativeSet_RunStarted;
                            creativeSet.RunCompleted += creativeSet_RunCompleted;
                        }
                    }

                    // pass on that we are now running this ad. Note: It still could fail to run.
                    result.OnStart();
                    // actually run the ad
                    adPod.RunCompleted  += new Action <AdPod, bool>(adPod_RunCompleted);
                    adPod.ReleasePlayer += new Action <AdPod>(adPod_ReleasePlayer);
                    adPod.RunAsync();
                }
                else
                {
                    // clear out the current running AdSpot. This permits other ads to be handled.
                    activeCreativeSets.Clear();
                    activeAdPod = null;

                    // notify upstream
                    result.OnFail();
                    result.Deactivate();

                    base.ExecuteAdFailed(Source);

                    if (!needsRelease)
                    {
                        Player.ActiveMediaPlugin.Play();
                    }
                }

                if (needsRelease)
                {
                    adHost.ReleasePlayBlock(loadBlocker);
                }
            });
            return(true);
        }
 protected abstract bool ExecutePayload(IAsyncAdPayload PackagePayload, IAdSource Source);