Skip to content

Commit

Permalink
Merge pull request #7 from zype/akamai-analytics
Browse files Browse the repository at this point in the history
Akamai analytics
  • Loading branch information
basit-n authored Sep 22, 2017
2 parents 8807a74 + 4dd40ba commit 6ee6245
Show file tree
Hide file tree
Showing 22 changed files with 4,034 additions and 44 deletions.
17 changes: 8 additions & 9 deletions components/screens/DetailsScreen/DetailsScreen.brs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Function Init()
m.description = m.top.findNode("Description")
m.background = m.top.findNode("Background")

m.canWatchVideo = false
m.top.canWatchVideo = false
m.buttons.setFocus(true)

' Set theme
Expand Down Expand Up @@ -89,7 +89,7 @@ Sub OnFocusedChildChange()
m.overlay.uri = m.global.theme.overlay_uri
m.overlay.visible = true

if m.canWatchVideo <> invalid and m.canWatchVideo = true
if m.top.canWatchVideo <> invalid and m.top.canWatchVideo = true
AddButtons()
m.buttons.setFocus(true)
else
Expand All @@ -102,7 +102,7 @@ End Sub
' set proper focus on buttons and stops video if return from Playback to details
Sub onVideoVisibleChange()
if m.top.videoPlayer.visible = false and m.top.visible = true
if m.canWatchVideo <> invalid AND m.canWatchVideo = true
if m.top.canWatchVideo <> invalid AND m.top.canWatchVideo = true
AddButtons()
m.buttons.setFocus(true)
m.top.videoPlayer.control = "stop"
Expand Down Expand Up @@ -175,9 +175,8 @@ Function PrepareVideoPlayer()
print "nextVideoObject: "; nextVideoObject
print "New: "; m.top.content

if(m.canWatchVideo)
if(m.top.canWatchVideo)
m.top.videoPlayer.visible = true
m.top.triggerPlay = true
else
m.top.videoPlayer.visible = false
m.top.videoPlayer.setFocus(false)
Expand Down Expand Up @@ -212,17 +211,17 @@ Sub OnContentChange()
' idParts = m.top.content.id.tokenize(":")

if(m.top.content.subscriptionRequired = false OR m.global.auth.isLoggedIn = true OR m.top.NoAuthenticationEnabled = true)
m.canWatchVideo = true
m.top.canWatchVideo = true
else
m.canWatchVideo = false
m.top.canWatchVideo = false
end if

' If all else is good and device is linked but there's no subscription found on the server then show native subscription buttons.
if(m.top.isDeviceLinked = true AND m.global.usvod.UniversalSubscriptionsCount = 0 AND m.top.content.subscriptionRequired = true AND m.top.BothActive = true AND m.top.JustBoughtNativeSubscription = false AND m.global.nsvod.isLoggedInViaNativeSVOD = false)
m.canWatchVideo = false
m.top.canWatchVideo = false
end if

if(m.canWatchVideo)
if(m.top.canWatchVideo)
AddButtons()
m.top.SubscriptionButtonsShown = false
else
Expand Down
1 change: 1 addition & 0 deletions components/screens/DetailsScreen/DetailsScreen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@

<!-- True value will immediately play the video loaded into the screen -->
<field id="triggerPlay" type="bool" />
<field id="canWatchVideo" type="bool" />

<!-- Gets populated by app configuration returned from zype API -->
<field id="autoplay" type="bool" />
Expand Down
4 changes: 2 additions & 2 deletions components/screens/HomeScene/HomeScene.brs
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,9 @@ Function OnKeyEvent(key, press) as Boolean
End Function

Function isSpecialScreen()
if (m.detailsScreen.visible = true) OR (m.contentStack.count() > 0 and m.gridScreen.visible = true) OR (m.deviceLinking.visible = true)
if (m.detailsScreen.visible = true) OR (m.contentStack.count() > 0 and m.gridScreen.visible = true) OR (m.deviceLinking.visible = true)
return true
else
return false
end if
End Function
End Function
114 changes: 81 additions & 33 deletions source/main.brs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Sub SetHomeScene(contentID = invalid, mediaType = invalid)
screen.SetMessagePort(m.port)
screen.Show()

m.AKaMAAnalyticsPlugin = AkaMA_plugin()
m.akamai_service = AkamaiService()

m.LoadingScreen = m.scene.findNode("LoadingScreen")

m.loadingIndicator = m.scene.findNode("loadingIndicator")
Expand Down Expand Up @@ -260,14 +263,31 @@ Sub SetHomeScene(contentID = invalid, mediaType = invalid)
index = msg.getData()

handleButtonEvents(index, lclscreen)
else if msg.getField() = "state"
state = msg.getData()
m.akamai_service.handleVideoEvents(state, m.AKaMAAnalyticsPlugin.pluginInstance, m.AKaMAAnalyticsPlugin.sessionTimer, m.AKaMAAnalyticsPlugin.lastHeadPosition)

' autoplay
next_video = m.detailsScreen.videosTree[m.detailsScreen.PlaylistRowIndex][m.detailsScreen.CurrentVideoIndex]
if state = "finished" and m.detailsScreen.autoplay = true and m.detailsScreen.canWatchVideo = true and next_video <> invalid
m.detailsScreen.triggerPlay = true
end if

else if msg.getField() = "position"
' print m.videoPlayer.position
' print GetLimitStreamObject().limit
m.AKaMAAnalyticsPlugin.lastHeadPosition = m.videoPlayer.position
print m.videoPlayer.position
if(m.videoPlayer.position >= 30 and m.videoPlayer.content.onAir = false)
AddVideoIdForResumeToReg(m.detailsScreen.content.id,m.videoPlayer.position.ToStr())
AddVideoIdTimeSaveForResumeToReg(m.detailsScreen.content.id,startDate.asSeconds().ToStr())
end if

' If midroll ads exist, watch for midroll ads
if m.midroll_ads <> invalid and m.midroll_ads.count() > 0
handleMidrollAd()
end if ' end of midroll ad if statement

' if(m.on_air)
' if GetLimitStreamObject() <> invalid
' GetLimitStreamObject().played = GetLimitStreamObject().played + 1
Expand Down Expand Up @@ -494,6 +514,25 @@ end sub
sub playVideo(screen as Object, auth As Object, adsEnabled = false)
playerInfo = GetPlayerInfo(screen.content.id, auth)

if(screen.content.onAir <> true AND playerInfo.analytics.beacon <> invalid AND playerInfo.analytics.beacon <> "")
print "PlayerInfo.analytics: "; playerInfo.analytics

if auth.access_token <> invalid then token_info = RetrieveTokenStatus({ access_token: auth.access_token }) else token_info = invalid
if token_info <> invalid then consumer_id = token_info.resource_owner_id else consumer_id = ""

cd = {
siteId: playerInfo.analytics.siteid,
videoId: playerInfo.analytics.videoid,
title: screen.content.title,
deviceType: playerInfo.analytics.device,
playerId: playerInfo.analytics.playerId,
contentLength: screen.content.length,
consumerId: consumer_id
}
print "Custom Dimensions: "; cd
m.AKaMAAnalyticsPlugin.pluginMain({configXML: playerInfo.analytics.beacon, customDimensions:cd})
end if

screen.content.stream = playerInfo.stream
screen.content.streamFormat = playerInfo.streamFormat
screen.content.url = playerInfo.url
Expand All @@ -508,11 +547,20 @@ sub playVideo(screen as Object, auth As Object, adsEnabled = false)
PrepareVideoPlayerWithSubtitles(screen, playerInfo.subtitles.count() > 0, playerInfo)
playContent = true

m.VideoPlayer = screen.VideoPlayer
m.VideoPlayer.observeField("position", m.port)

if(screen.content.onAir <> true)
m.VideoPlayer.observeField("state", m.port)
end if

m.videoPlayer.content = screen.content

if(adsEnabled)
no_ads = (m.global.swaf and m.global.is_subscribed)
ads = video_service.PrepareAds(playerInfo, no_ads)

if screen.content.onAir = true then ads.midroll = []
if screen.content.onAir = true then m.midroll_ads = [] else m.midroll_ads = ads.midroll

m.loadingIndicator.control = "stop"

Expand Down Expand Up @@ -541,16 +589,13 @@ sub playVideo(screen as Object, auth As Object, adsEnabled = false)
EndLoader()
end if

m.currentVideoInfo = playerInfo.video

m.videoPlayer.setFocus(true)
m.videoPlayer.control = "play"

sleep(500)
' If midroll ads exist, watch for midroll ads
if adsEnabled AND ads.midroll.count() > 0
AttachMidrollAds(ads.midroll, playerInfo)
end if ' end of midroll ad if statement
else
CloseVideoPlayer()
m.currentVideoInfo = invalid
end if ' end of if playContent
end if
end sub
Expand All @@ -575,39 +620,40 @@ sub PrepareVideoPlayerWithSubtitles(screen, subtitleEnabled, playerInfo)
m.VideoPlayer.seek = m.VideoPlayer.seek
end sub

sub AttachMidrollAds(midroll_ads, playerInfo)
while midroll_ads.count() > 0
currPos = m.videoPlayer.position
sub handleMidrollAd()
currPos = m.videoPlayer.position

timeDiff = Abs(midroll_ads[0].offset - currPos)
print "Next midroll ad: "; midroll_ads[0].offset
print "Time until next midroll ad: "; timeDiff
timeDiff = Abs(m.midroll_ads[0].offset - currPos)
print "Next midroll ad: "; m.midroll_ads[0].offset
print "Time until next midroll ad: "; timeDiff

' Within half second of next midroll ad timing
if timeDiff <= 0.500
m.videoPlayer.control = "stop"
' Within half second of next midroll ad timing
if timeDiff <= 0.500
m.videoPlayer.control = "stop"

m.raf_service.playAds(playerInfo.video, midroll_ads[0].url)
finished_ad = m.raf_service.playAds(m.currentVideoInfo, m.midroll_ads[0].url)

' Remove midroll ad from array
midroll_ads.shift()
if finished_ad = false then CloseVideoPlayer()

' Start playing video at back from currPos just before midroll ad started
m.videoPlayer.seek = currPos
m.videoPlayer.control = "play"
' Remove midroll ad from array
m.midroll_ads.shift()

' In case they fast forwarded or resumed watching, remove unnecessary midroll ads
' Keep removing the first midroll ad in array until no midroll ads before current position
else if midroll_ads.count() > 0 and currPos > midroll_ads[0].offset
while midroll_ads.count() > 0 and currPos > midroll_ads[0].offset
midroll_ads.shift()
end while
else if m.videoPlayer.visible = false
m.videoPlayer.control = "none"
exit while
end if
' Start playing video at back from currPos just before midroll ad started
m.videoPlayer.seek = currPos
m.akamai_service.setPlayStartedOnce(true)
m.videoPlayer.control = "play"

end while ' end of midroll ad loop
' In case they fast forwarded or resumed watching, remove unnecessary midroll ads
' Keep removing the first midroll ad in array until no midroll ads before current position
else if m.midroll_ads.count() > 0 and currPos > m.midroll_ads[0].offset
while m.midroll_ads.count() > 0 and currPos > m.midroll_ads[0].offset
m.midroll_ads.shift()
end while
else if m.videoPlayer.visible = false
m.videoPlayer.control = "none"
m.midroll_ads = invalid
m.currentVideoInfo = invalid
end if
end sub

sub CloseVideoPlayer()
Expand Down Expand Up @@ -1034,6 +1080,8 @@ function handleButtonEvents(index, screen)

m.VideoPlayer = m.detailsScreen.VideoPlayer
m.VideoPlayer.seek = resume_time

m.akamai_service.setPlayStartedOnce(true)
playRegularVideo(screen)
else if button_role = "favorite"
markFavoriteButton(screen)
Expand Down
59 changes: 59 additions & 0 deletions source/services/akamai/AkaMA_BeaconSystem.brs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'Beacon system will handle operations related to sending a beacons

'Function : AkaMA_isBeaconInOrder
'Params : None.
'Return : Returns BeaconOrders which defines valid order for the beacons
'Description : Use this as a reference for beacon order
function AkaMA_isBeaconInOrder()
return {
BeaconReported:{
iLineReported : &H0001
sLineReported : &H0002
pLineReported : &H0004
cLineReported : &H0008
eLineReported : &H0010
vLineReported : &H0020
}
}
end function


'Function : AkaMA_MABeacons
'Params : None.
'Return : Returns function sendBeacon to send beacon
'Description : Use this while sending beacons
function AkaMA_MABeacons()
return {
'Function : sendBeacon
'Params : iBeacon. string with complete URL which will be sent to
' back end
'Return : Returns an error code if failed else success
'Description : Send's I-Line and sets beacon status to iLineSent
' Which maintains the status of the beacon system
' this can be used to check if it is right time to send
' a particular beacon
sendBeacon : function(beacon as string) as integer
beaconRequest = AkaMA_NewHttp(beacon)
if (beaconRequest.Http.AsyncGetToString())
event = wait(0, beaconRequest.Http.GetPort())
if type(event) = "roUrlEvent"
str = event.GetString()
'print "Returned string = ";str
if event.getResponseCode() <> 200
print "Http Request failed"
return AkaMAErrors().ERROR_CODES.AKAM_beacon_request_failed
else if event.getResponseCode() = 200
print "Beacon sent successfully!!!"
endif
else if event = invalid
beaconRequest.Http.AsyncCancel()
' reset the connection on timeouts
else
print "roUrlTransfer::AsyncGetToString(): unknown event"
endif
endif
return AkaMAErrors().ERROR_CODES.AKAM_Success
end function

}
end function
Loading

0 comments on commit 6ee6245

Please sign in to comment.