-
Notifications
You must be signed in to change notification settings - Fork 0
/
playbackReporting.php
213 lines (177 loc) · 6.65 KB
/
playbackReporting.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
require_once 'auth.php';
require_once 'data.php';
abstract class PlayState
{
const PLAYING = 'Playing';
const STOPPED = 'Stopped';
const PAUSED = 'Paused';
}
class PlayingMedia
{
public $itemId;
public $Duration;
public $PlayState;
public $PositionInSeconds;
public $StartedTime;
public $LastPositionUpdate;
public $skipSeconds;
public $trimSeconds;
public function __construct($itemId, $duration, $skipSeconds, $trimSeconds)
{
$this->itemId = $itemId;
$this->Duration = $duration;
$this->skipSeconds = $skipSeconds;
$this->trimSeconds = $trimSeconds;
}
}
class PositionAndPlayed
{
public $ItemId;
public $PositionInSeconds;
public $Played;
}
class PlaybackReporting
{
private const PROGRESSUPDATEFREQUENCY = 60;
private const PLAYSTATEDIR = 'playstate/';
private const JSONEXT = '.json';
private const MINRESUME = 0.05;
private const MAXRESUME = 0.90;
private $playing;
private $sessionId;
public function __construct($sessionId, $itemId, $duration, $skipSeconds = 0, $trimSeconds = 0)
{
$this->sessionId = $sessionId;
$this->playing = new PlayingMedia($itemId, $duration, $skipSeconds, $trimSeconds);
}
private function getPlaybackPayload($eventName = null)
{
$playback = array(
'CanSeek' => true,
'ItemId' => $this->playing->itemId,
'PlayMethod' => 'DirectPlay'
);
if ($eventName) {
$playback['EventName'] = $eventName;
}
if ($this->playing->PositionInSeconds) {
//ticks = ten millionth of a second
$ticks = $this->playing->PositionInSeconds * 10000 * 1000;
$playback['PositionTicks'] = $ticks;
}
return $playback;
}
private function updatePlaystate($PositionInSeconds, $playState = PlayState::PLAYING)
{
$this->playing->PositionInSeconds = $PositionInSeconds;
$this->playing->PlayState = $playState;
$this->playing->LastPositionUpdate = time();
//save state to file
file_put_contents(self::PLAYSTATEDIR . $this->sessionId . self::JSONEXT, json_encode($this->playing));
}
private function loadPlaystate()
{
$this->playing = json_decode(file_get_contents(self::PLAYSTATEDIR . $this->sessionId . self::JSONEXT));
}
public function deletePlaystate()
{
$filepath = self::PLAYSTATEDIR . $this->sessionId . self::JSONEXT;
if (file_exists($filepath)) {
unlink($filepath);
}
}
private function calculateCurrentPosition()
{
$this->playing->PositionInSeconds = time() - $this->playing->StartedTime;
$this->playing->LastPositionUpdate = time();
return $this->playing->PositionInSeconds;
}
private function Progress($PositionInSeconds, $eventName = 'TimeUpdate')
{
self::apiJSON(
'/Sessions/Playing/Progress',
self::getPlaybackPayload($eventName),
$PositionInSeconds
);
}
private function handleProgess()
{
//this is the periodic update so JF won't time out - run once a PROGRESSUPDATEFREQUENCY seconds
ignore_user_abort(true);
set_time_limit(0);
do {
sleep(self::PROGRESSUPDATEFREQUENCY);
//reload PlayState from disk
self::loadPlaystate();
if ($this->playing->PlayState == PlayState::PLAYING) {
if (self::calculateCurrentPosition() >= $this->playing->Duration) {
//video not stopped, but after the end of the video, send stop message to JF
self::Stop();
} else {
//send keep alive progress message
self::Progress(self::calculateCurrentPosition());
}
}
} while ($this->playing->PlayState == PlayState::PLAYING);
}
public function Start($PositionInSeconds = 0)
{
$this->playing->PositionInSeconds = $PositionInSeconds;
$this->playing->PlayState = PlayState::PLAYING;
$this->playing->StartedTime = time() - $PositionInSeconds;
self::apiJSON(
'/Sessions/Playing',
self::getPlaybackPayload(),
$this->playing->PositionInSeconds
);
//keep sending progress updates until duration or stop from another thread
self::handleProgess();
}
public function Stop(): PositionAndPlayed
{
//get updated position from session
self::loadPlaystate();
$auth = new Authentication();
$auth->verifySession(true);
if ($this->playing->PlayState == PlayState::PLAYING) {
$this->playing->PositionInSeconds = self::calculateCurrentPosition();
//add trim seconds to current position
$trimmedPosition = $this->playing->PositionInSeconds + $this->playing->trimSeconds;
//if result is >90% resume
if ($trimmedPosition / $this->playing->Duration >= PlaybackReporting::MAXRESUME) {
//set current position to be trimmed position
$this->playing->PositionInSeconds = $trimmedPosition;
}
if (($this->playing->PositionInSeconds - $this->playing->skipSeconds) <= ($this->playing->Duration * PlaybackReporting::MINRESUME)) {
//in first 5% of video (excluding skipSeconds), don't save resume position
//report position with skipSeconds removed
$this->playing->PositionInSeconds -= $this->playing->skipSeconds;
}
self::apiJSON(
'/Sessions/Playing/Stopped',
self::getPlaybackPayload(),
$this->playing->PositionInSeconds,
PlayState::STOPPED
);
}
$retval = new PositionAndPlayed();
$retval->ItemId = $this->playing->itemId;
//look up played state for this item
$playedItem = getItem($this->playing->itemId);
$retval->Played = $playedItem->UserData->Played;
//report skipSeconds to NMT if at beginning of video
//+1 because position will be set to 1 if in first 5%
if ($this->playing->PositionInSeconds <= $this->playing->skipSeconds + 1) {
$retval->PositionInSeconds = $this->playing->skipSeconds;
} else {
$retval->PositionInSeconds = $this->playing->PositionInSeconds;
}
return $retval;
}
private function apiJSON($apiendpoint, $payload, $PositionInSeconds, $playState = PlayState::PLAYING)
{
apiCallPost($apiendpoint, $payload);
self::updatePlaystate($PositionInSeconds, $playState);
}
}