forked from opentok/OpenTok-PHP-SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenTokArchive.php
173 lines (135 loc) · 4.73 KB
/
OpenTokArchive.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
<?php
require_once 'API_Config.php';
class OpenTokArchive {
private $archiveId;
private $archiveTitle;
private $server_url;
//Array of resources listed in this Manifest
private $resources = array();
//Array of the timeline from the Manifest file
private $timeline = array();
public function __construct($archiveId, $archiveTitle, $resources, $timeline, $server_url) {
$this->archiveId = $archiveId;
$this->archiveTitle = $archiveTitle;
$this->resources = $resources;
$this->timeline = $timeline;
$this->server_url = $server_url;
}
/*************/
////Getters///
/*************/
public function getId() {
return ((string) $this->archiveId);
}
public function getTitle() {
return $this->archiveTitle;
}
public function getResources() {
return $this->resources;
}
public function getTimeline() {
return $this->timeline;
}
/*************/
////Public FNs/
/*************/
public function downloadArchiveURL($videoId, $token) {
$url = $this->server_url . '/archive/url/' .$this->archiveId.'/'.$videoId;
$authString = "X-TB-TOKEN-AUTH: ".$token;
$res=$this->doApiCall($url,$authString);
return $res;
}
/**
* This function deletes an archive. It was added by Tobias Nyholm
*/
public function deleteArchive($token) {
$url = $this->server_url . '/archive/delete/' .$this->archiveId.'/';
$authString = "X-TB-TOKEN-AUTH: ".$token;
$this->doApiCall($url,$authString);
}
/**
* This function does the actuall call and return the result
*/
private function doApiCall($url, $authString){
if(function_exists("curl_init")) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array($authString));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
if(curl_errno($ch)) {
throw new RequestException('Request error: ' . curl_error($ch));
}
curl_close($ch);
}
else {
if (function_exists("file_get_contents")) {
$context_source = array ('http' => array ( 'method' => 'GET', 'header'=> Array($authString) ) );
$context = stream_context_create($context_source);
$res = @file_get_contents( $url ,false, $context);
}
else{
throw new RequestException("Your PHP installion neither supports the file_get_contents method nor cURL. Please enable one of these functions so that you can make API calls.");
}
}
return $res;
}
/*************/
////Parser/////
/*************/
public static function parseManifest($manifest, $server_url) {
$archiveId = $manifest['archiveid'];
$title = $manifest['title'];
$resources = array();
$timeline = array();
foreach($manifest->resources->video as $videoResourceItem) {
$resources[] = OpenTokArchiveVideoResource::parseXML($videoResourceItem);
}
foreach($manifest->timeline->event as $timelineItem) {
$timeline[] = OpenTokArchiveTimelineEvent::parseXML($timelineItem);
}
return new OpenTokArchive($archiveId, $title, $resources, $timeline, $server_url);
}
}
class OpenTokArchiveVideoResource {
private $id;
private $type = 'video';
private $length;
public function __construct($id, $length) {
$this->id = $id;
$this->length = $length;
}
public function getId() {
return $this->id;
}
public function getLength() {
return $this->length;
}
public static function parseXML($videoResourceItem) {
return new OpenTokArchiveVideoResource($videoResourceItem['id'], $videoResourceItem['length']);
}
}
class OpenTokArchiveTimelineEvent {
private $eventType;
private $resourceId;
private $offset;
public function __construct($eventType, $resourceId, $offset) {
$this->eventType = $eventType;
$this->resourceId = $resourceId;
$this->offset = $offset;
}
public function getEventType() {
return $this->eventType;
}
public function getResourceId() {
return $this->resourceId;
}
public function getOffset() {
return $this->offset;
}
public static function parseXML($timelineItem) {
return new OpenTokArchiveTimelineEvent($timelineItem['type'], $timelineItem['id'], $timelineItem['offset']);
}
}