PreloadOperation LoadAdUnit(ActiveAdUnit adUnit, CancellationToken cancellationToken)
        {
            var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
            Task <ActiveAdUnit> task;

            if (loadOperation != null)
            {
                if (loadOperation.AdSource != adUnit.AdSource)  // cancel active task and wait for it to finish
                {
                    task = TaskHelpers.Create <Task <ActiveAdUnit> >(async() =>
                    {
                        try
                        {
                            await loadOperation.CancelAsync();
                        }
                        finally
                        {
                            loadOperation = null;
                        }
                        cancellationToken.ThrowIfCancellationRequested();
                        return(await GetInitializationTask(adUnit, cts.Token));
                    });
                }
                else
                {
                    task = loadOperation.Task;
                }
            }
            else
            {
                task = GetInitializationTask(adUnit, cts.Token);
            }

            return(new PreloadOperation(task, adUnit.AdSource, cts));
        }
 /// <summary>
 /// Preloads an ad that will be played later.
 /// </summary>
 /// <param name="adDocument">The ad document to execute.</param>
 /// <param name="adSource">The original ad source that the ad document came from.</param>
 /// <param name="cancellationToken">A cancellation token to cancel the ad.</param>
 /// <returns></returns>
 async Task PreloadAdAsync(AdDocumentPayload adDocument, IAdSource adSource, CancellationToken cancellationToken)
 {
     foreach (var adPod in adDocument.AdPods)
     {
         try
         {
             // model expects ads to be in the correct order.
             foreach (var ad in adPod.Ads)
             {
                 // group the creatives by sequence number. Always put the group without sequence number at the back of the piority list in compliance with VAST spec.
                 foreach (var creativeSet in ad.Creatives.GroupBy(c => c.Sequence).OrderBy(cs => cs.Key.GetValueOrDefault(int.MaxValue)))
                 {
                     var newAdUnit = CreateAdUnit(creativeSet, ad, adSource);
                     if (newAdUnit != null) // a violation of the VAST spec but we will just ignore
                     {
                         loadOperation = LoadAdUnit(newAdUnit, cancellationToken);
                         try
                         {
                             await loadOperation.Task;
                             cancellationToken.ThrowIfCancellationRequested();
                         }
                         catch
                         {
                             loadOperation = null;
                             throw;
                         }
                         return;
                     }
                 }
             }
         }
         catch (LoadException) { /* ignore, move to the next adpod */ }
     }
 }
        internal async Task <bool> CancelActiveAd(bool force)
#endif
        {
            // if there is a linear ad playing, return false.
            if (activeAd != null && !force && activeAd.Player.AdLinear)
            {
                return(false);
            }
            if (activeOperation != null)
            {
                var cancelingOperation = activeOperation;
                try
                {
                    // there are active ads still. Force them to close.
                    await cancelingOperation.CancelAsync();
                }
                catch { /* ignore */ }
            }
            if (loadOperation != null && force)
            {
                try
                {
                    // there are preloads happening, force them to stop.
                    await loadOperation.CancelAsync();
                }
                catch { /* ignore */ }
                finally
                {
                    loadOperation = null;
                }
            }
            return(true);
        }
예제 #4
0
        /// <summary>
        /// サウンド準備
        /// </summary>
        public IPreloadOperation Prepare(string cueSheetname)
        {
            Recorder.Catch("Cri Sound", cueSheetname);
            var process   = PrepareImpl(cueSheetname);
            var operation = new PreloadOperation(process);

            return(AddQueue(operation));
        }
예제 #5
0
 async Task SetOrReplaceActivePreloadOperation(PreloadOperation value)
 {
     if (activePreloadOperation != null)
     {
         await activePreloadOperation.CancelAsync();
     }
     activePreloadOperation = value;
 }
예제 #6
0
        public IPreloadOperation Prepare(string path)
        {
            Recorder.Catch("Cri Movie", path);
            var operation = new PreloadOperation(SkipLoadProcess.Create(path));

            operation.OnStart = _ => OnStartAny?.Invoke();
            operation.OnStop  = _ => OnStopAny?.Invoke();
            return(m_routine.Register(operation));
        }
예제 #7
0
        public IPreloadOperation Prepare(string cueSheetName)
        {
            Recorder.Catch("Cri Sound", cueSheetName);
            var operation = new PreloadOperation(SkipLoadProcess.Create(cueSheetName));

            operation.OnStart = _ => OnStartAny?.Invoke();
            operation.OnStop  = _ => OnStopAny?.Invoke();
            return(m_routine.Register(operation));
        }
        PreloadOperation LoadAdUnit(ActiveAdUnit adUnit, CancellationToken cancellationToken)
        {
            var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
            Task<ActiveAdUnit> task;
            if (loadOperation != null)
            {
                if (loadOperation.AdSource != adUnit.AdSource)  // cancel active task and wait for it to finish
                {
                    task = TaskHelpers.Create<Task<ActiveAdUnit>>(async () =>
                    {
                        try
                        {
                            await loadOperation.CancelAsync();
                        }
                        finally
                        {
                            loadOperation = null;
                        }
                        cancellationToken.ThrowIfCancellationRequested();
                        return await GetInitializationTask(adUnit, cts.Token);
                    });
                }
                else
                {
                    task = loadOperation.Task;
                }
            }
            else
            {
                task = GetInitializationTask(adUnit, cts.Token);
            }

            return new PreloadOperation(task, adUnit.AdSource, cts);
        }
 /// <summary>
 /// Preloads an ad that will be played later.
 /// </summary>
 /// <param name="adDocument">The ad document to execute.</param>
 /// <param name="adSource">The original ad source that the ad document came from.</param>
 /// <param name="cancellationToken">A cancellation token to cancel the ad.</param>
 /// <returns></returns>
 async Task PreloadAdAsync(AdDocumentPayload adDocument, IAdSource adSource, CancellationToken cancellationToken)
 {
     foreach (var adPod in adDocument.AdPods)
     {
         try
         {
             // model expects ads to be in the correct order.
             foreach (var ad in adPod.Ads)
             {
                 // group the creatives by sequence number. Always put the group without sequence number at the back of the piority list in compliance with VAST spec.
                 foreach (var creativeSet in ad.Creatives.GroupBy(c => c.Sequence).OrderBy(cs => cs.Key.GetValueOrDefault(int.MaxValue)))
                 {
                     var newAdUnit = CreateAdUnit(creativeSet, ad, adSource);
                     if (newAdUnit != null) // a violation of the VAST spec but we will just ignore
                     {
                         loadOperation = LoadAdUnit(newAdUnit, cancellationToken);
                         try
                         {
                             await loadOperation.Task;
                             cancellationToken.ThrowIfCancellationRequested();
                         }
                         catch
                         {
                             loadOperation = null;
                             throw;
                         }
                         return;
                     }
                 }
             }
         }
         catch (LoadException) { /* ignore, move to the next adpod */ }
     }
 }
        async Task<ActiveAdUnit> LoadAdUnitAsync(ICollection<ICreative> creativeSet, Ad ad, IAdSource adSource, CancellationToken cancellationToken)
        {
            bool preloaded = false;
            ActiveAdUnit result = null;

            // check to see if there are any pre-loading ads.
            if (loadOperation != null)
            {
                try
                {
                    if (loadOperation.AdSource == adSource)
                    {
                        cancellationToken.Register(() =>
                        {
                            if (loadOperation != null)
                            {
                                // no need to wait
                                var dummyTask = loadOperation.CancelAsync();
                            }
                        });
                        result = await loadOperation.Task;
                        preloaded = true;
                    }
                    else
                    {
                        await loadOperation.CancelAsync();
                    }
                }
                catch { /* ignore, we'll try again regardless of reason, it was just a preload optimization anyway */ }
                finally
                {
                    loadOperation = null;
                }
                cancellationToken.ThrowIfCancellationRequested();
            }
            if (result == null)
            {
                result = CreateAdUnit(creativeSet, ad, adSource);
            }
            if (result == null) throw new LoadException(new Exception("No ad unit could be created"));

            if (!preloaded)
            {
                // Start initializing the ad. It is OK if there are other ads playing still.
                loadOperation = LoadAdUnit(result, cancellationToken);
                try
                {
                    await loadOperation.Task;
                }
                catch (OperationCanceledException) { throw; }
                catch (Exception ex) { throw new LoadException(ex); }
                finally
                {
                    loadOperation = null;
                }
                cancellationToken.ThrowIfCancellationRequested();
            }

            return result;
        }
        internal async Task<bool> CancelActiveAd(bool force)
#endif
        {
            // if there is a linear ad playing, return false.
            if (activeAd != null && !force && activeAd.Player.AdLinear) return false;
            if (activeOperation != null)
            {
                var cancelingOperation = activeOperation;
                try
                {
                    // there are active ads still. Force them to close.
                    await cancelingOperation.CancelAsync();
                }
                catch { /* ignore */ }
            }
            if (loadOperation != null && force)
            {
                try
                {
                    // there are preloads happening, force them to stop.
                    await loadOperation.CancelAsync();
                }
                catch { /* ignore */ }
                finally
                {
                    loadOperation = null;
                }
            }
            return true;
        }
        async Task <ActiveAdUnit> LoadAdUnitAsync(ICollection <ICreative> creativeSet, Ad ad, IAdSource adSource, CancellationToken cancellationToken)
        {
            bool         preloaded = false;
            ActiveAdUnit result    = null;

            // check to see if there are any pre-loading ads.
            if (loadOperation != null)
            {
                try
                {
                    if (loadOperation.AdSource == adSource)
                    {
                        cancellationToken.Register(() =>
                        {
                            if (loadOperation != null)
                            {
                                // no need to wait
                                var dummyTask = loadOperation.CancelAsync();
                            }
                        });
                        result    = await loadOperation.Task;
                        preloaded = true;
                    }
                    else
                    {
                        await loadOperation.CancelAsync();
                    }
                }
                catch { /* ignore, we'll try again regardless of reason, it was just a preload optimization anyway */ }
                finally
                {
                    loadOperation = null;
                }
                cancellationToken.ThrowIfCancellationRequested();
            }
            if (result == null)
            {
                result = CreateAdUnit(creativeSet, ad, adSource);
            }
            if (result == null)
            {
                throw new LoadException(new Exception("No ad unit could be created"));
            }

            if (!preloaded)
            {
                // Start initializing the ad. It is OK if there are other ads playing still.
                loadOperation = LoadAdUnit(result, cancellationToken);
                try
                {
                    await loadOperation.Task;
                }
                catch (OperationCanceledException) { throw; }
                catch (Exception ex) { throw new LoadException(ex); }
                finally
                {
                    loadOperation = null;
                }
                cancellationToken.ThrowIfCancellationRequested();
            }

            return(result);
        }