-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPHPClient.php
More file actions
188 lines (159 loc) · 6.02 KB
/
PHPClient.php
File metadata and controls
188 lines (159 loc) · 6.02 KB
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
<?php
namespace Remotion\LambdaPhp;
use Aws\Lambda\LambdaClient;
use Exception;
use stdClass;
class PHPClient
{
protected $client;
protected $region;
protected $serveUrl;
protected $functionName;
public function __construct(string $region, string $serveUrl, string $functionName, ?callable $credential)
{
$this->client = new LambdaClient([
'version' => '2015-03-31',
'region' => $region,
'credentials' => $credential,
]);
$this->setRegion($region);
$this->setServeUrl($serveUrl);
$this->setFunctionName($functionName);
}
public function constructInternals(RenderParams $render)
{
if (empty($render->getComposition())) {
throw new ValidationException("'composition' is required.");
}
$input = $this->serializeInputProps(
$render->getData(),
$this->getRegion(),
"video-or-audio",
null
);
$render->internal_setSerializedInputProps($input);
$render->setServeUrl($this->getServeUrl());
$render->setRegion($this->getRegion());
return $render->serializeParams();
}
public function renderMediaOnLambda(RenderParams $render): RenderMediaOnLambdaResponse
{
$params = $this->constructInternals($render);
$result = $this->invokeLambdaFunction(json_encode($params));
return $this->handleLambdaResponseRender($result);
}
public function makeRenderProgressPayload(string $renderId, string $bucketName, string $logLevel = "info")
{
$params = array(
'renderId' => $renderId,
'bucketName' => $bucketName,
'type' => 'status',
"version" => Semantic::VERSION,
"s3OutputProvider" => null,
"logLevel" => $logLevel ,
);
$result = json_encode($params);
return $result;
}
public function getRenderProgress(string $renderId, string $bucketName, string $logLevel = "info"): GetRenderProgressResponse
{
$payload = $this->makeRenderProgressPayload($renderId, $bucketName, $logLevel);
$result = $this->invokeLambdaFunction($payload);
return $this->handleLambdaResponseProgress($result);
}
private function invokeLambdaFunction(string $payload)
{
$result = $this->client->invoke([
'InvocationType' => 'RequestResponse',
'FunctionName' => $this->getFunctionName(),
'Payload' => $payload,
]);
if ($result['StatusCode'] !== 200) {
throw new Exception("Failed to invoke Lambda function");
}
return $result['Payload']->getContents();
}
private function handleLambdaResponseRender(string $response): RenderMediaOnLambdaResponse
{
$response = json_decode($response, true);
if (isset($response->errorMessage)) {
throw new Exception($response->errorMessage);
}
$classResponse = new RenderMediaOnLambdaResponse();
$classResponse->type = $response['type'];
$classResponse->renderId = $response['renderId'];
$classResponse->bucketName = $response['bucketName'];
return $classResponse;
}
private function handleLambdaResponseProgress(string $response): GetRenderProgressResponse
{
$response = json_decode($response, true);
if (isset($response->errorMessage)) {
throw new Exception($response->errorMessage);
}
$classResponse = new GetRenderProgressResponse();
$classResponse->bucket = $response['bucket'];
$classResponse->chunks = $response['chunks'];
$classResponse->currentTime = $response['currentTime'];
$classResponse->done = $response['done'];
$classResponse->fatalErrorEncountered = $response['fatalErrorEncountered'];
$classResponse->lambdasInvoked = $response['lambdasInvoked'];
$classResponse->outBucket = $response['outBucket'];
$classResponse->outKey = $response['outKey'];
$classResponse->outputFile = $response['outputFile'];
$classResponse->overallProgress = $response['overallProgress'];
$classResponse->renderSize = $response['renderSize'];
$classResponse->timeToFinish = $response['timeToFinish'];
$classResponse->type = $response['type'];
return $classResponse;
}
private function serializeInputProps($inputProps, string $region, string $type, ?string $userSpecifiedBucketName): array
{
try {
$payload = json_encode($inputProps);
$MAX_INLINE_PAYLOAD_SIZE = $type === 'still' ? 5000000 : 200000;
if (strlen($payload) > $MAX_INLINE_PAYLOAD_SIZE) {
throw new Exception(
sprintf(
"Warning: inputProps are over %dKB (%dKB) in size.\n This is not currently supported.",
round($MAX_INLINE_PAYLOAD_SIZE / 1000),
ceil(strlen($payload) / 1024)
)
);
}
return [
'type' => 'payload',
'payload' => !is_null($payload) && !empty($payload) && $payload !== "null" ?
$payload : json_encode(new stdClass()),
];
} catch (Exception $e) {
throw new Exception(
'Error serializing inputProps. Check it has no circular references or reduce the size if the object is big.'
);
}
}
public function getRegion(): string
{
return $this->region;
}
public function setRegion(string $region): void
{
$this->region = $region;
}
public function getServeUrl(): string
{
return $this->serveUrl;
}
public function setServeUrl(string $serveUrl): void
{
$this->serveUrl = $serveUrl;
}
public function getFunctionName(): string
{
return $this->functionName;
}
public function setFunctionName(string $functionName): void
{
$this->functionName = $functionName;
}
}