-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathexercise-live-streams.php
135 lines (117 loc) · 6.29 KB
/
exercise-live-streams.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
<?php
require_once(__DIR__ . '/../assert.php');
require_once 'vendor/autoload.php';
// Exercises all live stream operations.
// Authentication Setup
$config = MuxPhp\Configuration::getDefaultConfiguration()
->setUsername(getenv('MUX_TOKEN_ID'))
->setPassword(getenv('MUX_TOKEN_SECRET'));
// API Client Initialization
$liveApi = new MuxPhp\Api\LiveStreamsApi(
new GuzzleHttp\Client(),
$config
);
$playbackIdApi = new MuxPhp\Api\PlaybackIDApi(
new GuzzleHttp\Client(),
$config
);
// ========== create-live-stream ==========
$createAssetRequest = new MuxPhp\Models\CreateAssetRequest(["playback_policy" => [MuxPhp\Models\PlaybackPolicy::_PUBLIC]]);
$createLiveStreamRequest = new MuxPhp\Models\CreateLiveStreamRequest(["playback_policy" => [MuxPhp\Models\PlaybackPolicy::_PUBLIC], "new_asset_settings" => $createAssetRequest, "reduced_latency" => true]);
$stream = $liveApi->createLiveStream($createLiveStreamRequest);
assert($stream->getData()->getId() !== null);
print("create-live-stream OK ✅\n");
// ========== list-live-streams ==========
$streams = $liveApi->listLiveStreams();
assert($streams->getData()[0]->getId() !== null);
assert($streams->getData()[0]->getId() === $stream->getData()->getId());
print("list-live-streams OK ✅\n");
// ========== get-live-stream ==========
$getStream = $liveApi->getLiveStream($stream->getData()->getId());
assert($getStream->getData()->getId() !== null);
assert($getStream->getData()->getId() === $stream->getData()->getId());
print("get-live-stream OK ✅\n");
// ========== get-asset-or-livestream-id ==========
$pbId = $stream->getData()->getPlaybackIds()[0]->getId();
$pbPlaybackAssetGet = $playbackIdApi->getAssetOrLivestreamId($pbId);
assert($pbPlaybackAssetGet->getData()->getObject()->getId() === $stream->getData()->getId());
assert($pbPlaybackAssetGet->getData()->getObject()->getType() === "live_stream");
print("get-asset-or-livestream-id OK ✅\n");
// ========== create-live-stream-simulcast-target ==========
$createTarget = new MuxPhp\Models\CreateSimulcastTargetRequest(["passthrough" => "That's not on fire", "url" => "rtmp://glitch.tv", "stream_key" => "somethingverylongnoonewilleverremember"]);
$target = $liveApi->createLiveStreamSimulcastTarget($stream->getData()->getId(), $createTarget);
assert($target !== null);
assert($target->getData() !== null);
assert($target->getData()->getId() !== null);
print("create-live-stream-simulcast-target OK ✅\n");
// ========== get-live-stream-simulcast-target ==========
$getTarget = $liveApi->getLiveStreamSimulcastTarget($stream->getData()->getId(), $target->getData()->getId());
assert($getTarget !== null);
assert($getTarget->getData() !== null);
assert($getTarget->getData()->getId() !== null);
assert($getTarget->getData()->getId() === $target->getData()->getId());
print("get-live-stream-simulcast-target OK ✅\n");
// ========== delete-live-stream-simulcast-target ==========
$liveApi->deleteLiveStreamSimulcastTarget($stream->getData()->getId(), $target->getData()->getId());
$getStreamNoTarget = $liveApi->getLiveStream($stream->getData()->getId());
assert($getStreamNoTarget->getData()->getSimulcastTargets() === null);
print("delete-live-stream-simulcast-target OK ✅\n");
// ========== create-live-stream-playback-id ==========
$createPlaybackIdRequest = new MuxPhp\Models\CreatePlaybackIDRequest(["policy" => MuxPhp\Models\PlaybackPolicy::SIGNED]);
$publicAndPrivateStream = $liveApi->createLiveStreamPlaybackId($stream->getData()->getId(), $createPlaybackIdRequest);
assert($publicAndPrivateStream->getData()->getId() !== null);
assert($publicAndPrivateStream->getData()->getPolicy() === "signed");
print("create-live-stream-playback-id OK ✅\n");
// ========== delete-live-stream-playback-id ==========
$liveApi->deleteLiveStreamPlaybackId($stream->getData()->getId(), $publicAndPrivateStream->getData()->getId());
$publicStream = $liveApi->getLiveStream($stream->getData()->getId());
assert($publicStream->getData()->getId() !== null);
assert(sizeof($publicStream->getData()->getPlaybackIds()) === 1);
assert($publicStream->getData()->getPlaybackIds()[0]->getPolicy() === "public");
print("delete-live-stream-playback-id OK ✅\n");
// ========== reset-stream-key ==========
$newStreamKey = $liveApi->resetStreamKey($stream->getData()->getId());
assert($newStreamKey->getData()->getId() !== null);
assert($newStreamKey->getData()->getId() === $stream->getData()->getId());
assert($newStreamKey->getData()->getStreamKey() !== $stream->getData()->getStreamKey());
print("reset-stream-key OK ✅\n");
// ========== signal-live-stream-complete ==========
try {
$liveApi->signalLiveStreamComplete($stream->getData()->getId());
}
catch (Exception $e) {
print("Should not have errored when signalling live stream complete ❌ ");
exit(1);
}
print("signal-live-stream-complete OK ✅\n");
// ========== disable-live-stream ==========
try {
$liveApi->disableLiveStream($stream->getData()->getId());
}
catch (Exception $e) {
print("Should not have errored when disabling live stream ❌ ");
exit(1);
}
$disabledStream = $liveApi->getLiveStream($stream->getData()->getId());
assert($disabledStream->getData()->getStatus() === 'disabled');
print("disable-live-stream OK ✅\n");
// ========== enable-live-stream ==========
try {
$liveApi->enableLiveStream($stream->getData()->getId());
}
catch (Exception $e) {
print("Should not have errored when enabling live stream ❌ ");
exit(1);
}
$enabledStream = $liveApi->getLiveStream($stream->getData()->getId());
assert($enabledStream->getData()->getStatus() === 'idle');
print("enable-live-stream OK ✅\n");
// ========== delete-live-stream ==========
$liveApi->deleteLiveStream($stream->getData()->getId());
try {
$result = $liveApi->getLiveStream($stream->getData()->getId());
print("Should have 404'd when getting deleted live stream ❌ \n");
exit(1);
} catch (Exception $e) {
print("delete-live-stream OK ✅\n");
}