-
Notifications
You must be signed in to change notification settings - Fork 31
/
playback.ts
333 lines (299 loc) · 8.63 KB
/
playback.ts
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import { Request, Router } from "express";
import { db } from "../store";
import {
getHLSPlaybackUrl,
getWebRTCPlaybackUrl,
getRecordingFields,
getRecordingPlaybackUrl,
} from "./stream";
import {
getPlaybackUrl as assetPlaybackUrl,
getStaticPlaybackInfo,
getThumbsVTTUrl,
StaticPlaybackInfo,
} from "./asset";
import { CliArgs } from "../parse-cli";
import { DBSession } from "../store/session-table";
import { Asset, PlaybackInfo, Stream, User } from "../schema/types";
import { DBStream } from "../store/stream-table";
import { WithID } from "../store/types";
import { NotFoundError, UnprocessableEntityError } from "../store/errors";
import { isExperimentSubject } from "../store/experiment-table";
import logger from "../logger";
import { getRunningRecording } from "./session";
import { cache } from "../store/cache";
/**
* CROSS_USER_ASSETS_CUTOFF_DATE represents the cut-off date for cross-account
* asset playback. Assets created before this date can still be played by other
* accounts by dStorage ID. Assets created after this date will not have
* cross-account playback enabled, ensuring users are billed appropriately. This
* was made for backward compatibility during the Viewership V2 deploy.
*/
const CROSS_USER_ASSETS_CUTOFF_DATE = 1686009600000; // 2023-06-06T00:00:00.000Z
var embeddablePlayerOrigin = /^https:\/\/(.+\.)?lvpr.tv$/;
function newPlaybackInfo(
type: PlaybackInfo["type"],
hlsUrl: string,
webRtcUrl?: string | null,
playbackPolicy?: Asset["playbackPolicy"] | Stream["playbackPolicy"],
staticFilesPlaybackInfo?: StaticPlaybackInfo[],
thumbsVTT?: string,
live?: PlaybackInfo["meta"]["live"],
recordingUrl?: string,
withRecordings?: boolean,
thumbUrl?: string
): PlaybackInfo {
let playbackInfo: PlaybackInfo = {
type,
meta: {
live,
playbackPolicy,
source: [],
},
};
if (staticFilesPlaybackInfo && staticFilesPlaybackInfo.length > 0) {
for (let staticFile of staticFilesPlaybackInfo) {
playbackInfo.meta.source.push({
hrn: "MP4",
type: "html5/video/mp4",
url: staticFile.playbackUrl,
size: staticFile.size,
width: staticFile.width,
height: staticFile.height,
bitrate: staticFile.bitrate,
});
}
}
playbackInfo.meta.source.push({
hrn: "HLS (TS)",
type: "html5/application/vnd.apple.mpegurl",
url: hlsUrl,
});
if (webRtcUrl) {
playbackInfo.meta.source.push({
hrn: "WebRTC (H264)",
type: "html5/video/h264",
url: webRtcUrl,
});
}
if (withRecordings) {
playbackInfo.meta.dvrPlayback = [];
if (recordingUrl) {
playbackInfo.meta.dvrPlayback.push({
hrn: "HLS (TS)",
type: "html5/application/vnd.apple.mpegurl",
url: recordingUrl,
});
} else {
playbackInfo.meta.dvrPlayback.push({
error: "no running recordings available for this stream.",
});
}
}
if (thumbUrl) {
playbackInfo.meta.source.push({
hrn: "Thumbnail (JPEG)",
type: "image/jpeg",
url: thumbUrl,
});
}
if (thumbsVTT) {
playbackInfo.meta.source.push({
hrn: "Thumbnails",
type: "text/vtt",
url: thumbsVTT,
});
}
return playbackInfo;
}
const getAssetPlaybackInfo = async (
config: CliArgs,
ingest: string,
asset: WithID<Asset>
) => {
const os = await db.objectStore.get(asset.objectStoreId, { useCache: true });
if (!os || os.deleted || os.disabled) {
return null;
}
const playbackUrl = assetPlaybackUrl(ingest, asset, os);
if (!playbackUrl) {
return null;
}
return newPlaybackInfo(
"vod",
playbackUrl,
null,
asset.playbackPolicy || null,
getStaticPlaybackInfo(asset, os),
getThumbsVTTUrl(asset, os)
);
};
type PlaybackResource = {
stream?: DBStream;
session?: DBSession;
asset?: WithID<Asset>;
};
export async function getResourceByPlaybackId(
id: string,
user?: User,
cutoffDate?: number
): Promise<PlaybackResource> {
let stream = await db.stream.getByPlaybackId(id);
if (!stream) {
const streamById = await db.stream.get(id);
// only allow retrieving child streams by ID
if (streamById?.parentId) {
stream = streamById;
}
}
if (stream && !stream.deleted && !stream.suspended) {
return { stream };
}
const asset =
(await db.asset.getByPlaybackId(id)) ??
(await db.asset.getByIpfsCid(id, user, cutoffDate)) ??
(await db.asset.getBySourceURL("ipfs://" + id, user, cutoffDate)) ??
(await db.asset.getBySourceURL("ar://" + id, user, cutoffDate));
if (asset && !asset.deleted) {
return { asset };
}
const session = await db.session.get(id);
if (session && !session.deleted) {
return { session };
}
return {};
}
async function getAttestationPlaybackInfo(
config: CliArgs,
ingest: string,
id: string,
user?: User,
cutoffDate?: number
): Promise<PlaybackInfo> {
try {
if (!isExperimentSubject("attestation", user?.id)) {
return null;
}
const attestation = await db.attestation.getByIdOrCid(id);
const videoUrl = attestation?.message?.video;
// Currently we only support attestations for videos stored in IPFS
if (!videoUrl?.startsWith("ipfs://")) {
return null;
}
const videoCid = videoUrl.slice("ipfs://".length);
const asset = await db.asset.getByIpfsCid(videoCid, user, cutoffDate);
if (!asset) {
return null;
}
const assetPlaybackInfo = await getAssetPlaybackInfo(config, ingest, asset);
assetPlaybackInfo.meta.attestation = attestation;
return assetPlaybackInfo;
} catch (e) {
logger.warn("Error while resolving playback from video attestation", e);
return null;
}
}
async function getPlaybackInfo(
req: Request,
ingest: string,
id: string,
isCrossUserQuery: boolean,
origin: string,
withRecordings?: boolean
): Promise<PlaybackInfo> {
const cutoffDate = isCrossUserQuery ? null : CROSS_USER_ASSETS_CUTOFF_DATE;
const cacheKey = `playbackInfo-${id}-user-${req.user?.id}-cutoff-${cutoffDate}`;
let resource = cache.get<PlaybackResource>(cacheKey);
if (!resource) {
resource = await getResourceByPlaybackId(id, req.user, cutoffDate);
const ttl =
resource.asset && resource.asset.status.phase !== "ready" ? 5 : 120;
cache.set(cacheKey, resource, ttl);
}
let { stream, asset, session } = resource;
if (asset) {
if (asset.status.phase !== "ready" && !asset.sourcePlaybackReady) {
throw new UnprocessableEntityError("asset is not ready for playback");
}
if (asset.userId !== req.user?.id && cutoffDate) {
console.log(
`Returning cross-user asset for playback. ` +
`userId=${req.user?.id} userEmail=${req.user?.email} origin=${origin} ` +
`assetId=${asset.id} assetUserId=${asset.userId} playbackId=${asset.playbackId}`
);
}
return await getAssetPlaybackInfo(req.config, ingest, asset);
}
// Streams represent "transcoding sessions" when they are a child stream, in
// which case they are used to playback old recordings and not the livestream.
const isChildStream = stream && (stream.parentId || !stream.playbackId);
if (isChildStream) {
session = stream;
stream = null;
}
if (stream) {
let url: string;
let thumbUrl: string;
try {
({ url, thumbUrl } = await getRunningRecording(stream, req));
} catch (e) {
logger.error("Error while getting recording", e);
}
return newPlaybackInfo(
"live",
getHLSPlaybackUrl(ingest, stream),
getWebRTCPlaybackUrl(ingest, stream),
stream.playbackPolicy,
null,
undefined,
stream.isActive ? 1 : 0,
url,
withRecordings,
thumbUrl
);
}
if (session) {
const { recordingUrl } = await getRecordingFields(
req.config,
ingest,
session,
false
);
if (recordingUrl) {
return newPlaybackInfo("recording", recordingUrl);
}
}
return await getAttestationPlaybackInfo(
req.config,
ingest,
id,
req.user,
cutoffDate
);
}
const app = Router();
app.get("/:id", async (req, res) => {
const ingests = await req.getIngest();
if (!ingests.length) {
res.status(501);
return res.json({ errors: ["Ingest not configured"] });
}
const ingest = ingests[0].base;
let { id } = req.params;
const withRecordings = req.query.recordings === "true";
const origin = req.headers["origin"] ?? "";
const isEmbeddablePlayer = embeddablePlayerOrigin.test(origin);
const info = await getPlaybackInfo(
req,
ingest,
id,
isEmbeddablePlayer,
origin,
withRecordings
);
if (!info) {
throw new NotFoundError(`No playback URL found for ${id}`);
}
res.status(200).json(info);
});
export default app;