Created
November 7, 2015 00:11
-
-
Save astein/9fc984bee6e35bb765c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.truex.placements.extensions | |
{ | |
import com.adobe.serialization.json.JSONEncoder; | |
import com.truex.placements.vpaid.VPAIDLoader; | |
import flash.display.Sprite; | |
import flash.events.Event; | |
import flash.external.ExternalInterface; | |
import flash.system.Security; | |
import flash.utils.setTimeout; | |
import tv.freewheel.ad.behavior.IConstants; | |
import tv.freewheel.ad.behavior.ICreativeRendition; | |
import tv.freewheel.ad.behavior.ICreativeRenditionAsset; | |
import tv.freewheel.ad.behavior.IRenderer; | |
import tv.freewheel.ad.behavior.IRendererContext; | |
import tv.freewheel.ad.behavior.IRendererController; | |
public class TrueXAdRenderer extends Sprite implements IRenderer | |
{ | |
private var _fwController:IRendererController; | |
private var _fwContext:IRendererContext; | |
private var _fwEventBus:Object; | |
private var _fwContainer:Sprite; | |
private var _constants:IConstants; | |
private var _width:Number; | |
private var _height:Number; | |
private var _configURL:String; | |
private var _placementHash:String; | |
private var _vpaid:VPAIDLoader; | |
private var _credited:Boolean; | |
private var _paused:Boolean; | |
private var _ended:Boolean; | |
public function TrueXAdRenderer() | |
{ | |
Security.allowDomain('*'); | |
} | |
public function getInfo():Object | |
{ | |
return {moduleType: "renderer"}; | |
} | |
public function getNewInstance(rendererInterfaceVersion:int):IRenderer | |
{ | |
return this; | |
} | |
public function init(context:IRendererContext, controller:IRendererController):void | |
{ | |
_fwContext = context; | |
_fwController = controller; | |
_fwContainer = _fwContext.getSlotBaseDisplay(); | |
_constants = _fwContext.getConstants(); | |
_width = _fwContext.getSlotBaseDisplaySize().width; | |
_height = _fwContext.getSlotBaseDisplaySize().height; | |
var rendition:ICreativeRendition = context.getPrimaryCreativeRendition(); | |
var asset:ICreativeRenditionAsset = rendition.getPrimaryCreativeRenditionAsset(); | |
_configURL = asset.getURL(); | |
if (_configURL) { | |
_configURL = _configURL.replace('http://', '').replace('https://', ''); | |
_placementHash = _configURL.substring(_configURL.indexOf('/')+1, _configURL.indexOf('/vast/config')); | |
} else { | |
_placementHash = _fwContext.getParameter('placementHash') || '4b735a619a7935c3f94216189533a232ffb2b80c'; | |
_configURL = "qa-get.truex.com/" + _placementHash + "/vast/config"; | |
} | |
_vpaid = new VPAIDLoader(); | |
addChild(_vpaid); | |
_vpaid.addEventListener('AdLoaded', onVPAIDLoaded); | |
_vpaid.addEventListener('AdStopped', onVPAIDStopped); | |
_vpaid.addEventListener('AdError', onVPAIDError); | |
_vpaid.addEventListener('superTagInteract', onInteract); | |
_vpaid.addEventListener('superTagCredit', onCredit); | |
_vpaid.addEventListener('superTagCreditStream', onCreditStream); | |
_fwController.setCapability( _constants.RENDERER_EVENT_IMPRESSION, true); | |
_fwController.setCapability( _constants.RENDERER_EVENT_FAIL, true); | |
_fwController.handleStateTransition(_constants.RENDERER_STATE_INITIALIZE_COMPLETE); | |
} | |
public function preload():void | |
{ | |
_fwController.addAllowedDomain('*.truex.com'); | |
_fwController.addAllowedDomain('truex.com'); | |
var envVars:Object = null; | |
var slot:Object = _fwContext.getSlot(); | |
if (slot && slot.hasOwnProperty('cuePointSequence')) { | |
_configURL += (_configURL.indexOf('?') == -1 ? '?' : '&') + 'dimension_2=' + slot.cuePointSequence; | |
} | |
_vpaid.initAd(_width, _height, 'normal', 268, (new JSONEncoder({"user_id":'',"placement_hash":_placementHash,"vast_config_url":_configURL})).getString(), null); | |
} | |
protected function onVPAIDLoaded(e:Event):void | |
{ | |
_vpaid.removeEventListener('AdLoaded', onVPAIDLoaded); | |
_fwController.handleStateTransition(_constants.RENDERER_STATE_PRELOAD_COMPLETE); | |
} | |
protected function onVPAIDError(e:Event):void | |
{ | |
_vpaid.removeEventListener('AdError', onVPAIDError); | |
_fwController.handleStateTransition(_constants.RENDERER_STATE_FAIL, {fallback:true}); | |
} | |
protected function onVPAIDStopped(e:Event):void | |
{ | |
_vpaid.removeEventListener('AdStopped', onVPAIDStopped); | |
_ended = true; | |
if (_credited) { | |
_fwController.dispatchCustomEvent('truex_credit', _fwContext.getSlot()); | |
//_fwContext.getSlot().stop(); | |
_fwController.handleStateTransition(_constants.RENDERER_STATE_STOP_COMPLETE, {fallback:true}); | |
} else { | |
if (_paused) { | |
setTimeout(function():void { | |
_fwController.handleStateTransition(_constants.RENDERER_STATE_FAIL, {fallback:true}); | |
}, 200); | |
} else { | |
_fwController.handleStateTransition(_constants.RENDERER_STATE_FAIL, {fallback:true}); | |
} | |
} | |
} | |
protected function onCredit(e:Event):void | |
{ | |
_credited = true; | |
_fwController.processEvent(_constants.RENDERER_EVENT_IMPRESSION); | |
} | |
protected function onCreditStream(e:Event):void | |
{ | |
_vpaid.removeEventListener('superTagCreditStream', onCreditStream); | |
_fwController.dispatchCustomEvent('truex_stream_credit', {}); | |
} | |
protected function onInteract(e:Event):void | |
{ | |
_vpaid.removeEventListener('superTagInteract', onInteract); | |
} | |
protected function suppressNextAdStart(e:Event):void | |
{ | |
_fwEventBus.removeEventListener('adStart', suppressNextAdStart); | |
e.stopImmediatePropagation(); | |
} | |
public function start():void | |
{ | |
_fwContainer.addChild(this); | |
_vpaid.startAd(); | |
_fwController.handleStateTransition(_constants.RENDERER_STATE_PLAYING); | |
} | |
public function stop(immediate:Boolean=false):void | |
{ | |
_vpaid.stopAd(); | |
} | |
public function resize():void | |
{ | |
_width = _fwContext.getSlotBaseDisplaySize().width; | |
_height = _fwContext.getSlotBaseDisplaySize().height; | |
_vpaid.resizeAd(_width, _height, 'normal'); | |
} | |
public function getPlayheadTime():Number | |
{ | |
if (_vpaid.adDuration > 0 && _vpaid.adRemainingTime > 0) | |
return (_vpaid.adDuration - _vpaid.adRemainingTime); | |
return -1; | |
} | |
public function getTotalBytes():int | |
{ | |
return -1; | |
} | |
public function getBytesLoaded():int | |
{ | |
return -1; | |
} | |
public function getDuration():Number | |
{ | |
return -1; | |
} | |
public function setAdVolume(volume:uint):void | |
{ | |
_vpaid.adVolume = volume < 1 ? volume : volume / 100; | |
} | |
public function pause():void | |
{ | |
if (_ended || _paused) return; | |
_paused = true; | |
_vpaid.pauseAd(); | |
} | |
public function resume():void | |
{ | |
if (_ended || !_paused) return; | |
_paused = false; | |
_vpaid.resumeAd(); | |
} | |
public function get container():Sprite { return _fwContainer; } | |
public function dispose():void | |
{ | |
if (_fwContainer.contains(this)) | |
_fwContainer.removeChild(this); | |
if (_fwEventBus) | |
_fwEventBus.removeEventListener('adStart', suppressNextAdStart); | |
if (_vpaid) { | |
_vpaid.removeEventListener('AdLoaded', onVPAIDLoaded); | |
_vpaid.removeEventListener('AdStopped', onVPAIDStopped); | |
_vpaid.removeEventListener('AdError', onVPAIDError); | |
_vpaid.removeEventListener('superTagInteract', onInteract); | |
_vpaid.removeEventListener('superTagCredit', onCredit); | |
_vpaid.removeEventListener('superTagCreditStream', onCreditStream); | |
} | |
} | |
} | |
} | |
import flash.events.Event; | |
internal class CustomEvent extends Event | |
{ | |
public var info:Object; | |
public function CustomEvent(type:String, typeStr:String) | |
{ | |
super(type, true); | |
info = {type:typeStr, title:''}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment