-
Notifications
You must be signed in to change notification settings - Fork 21
/
eventBridge.ts
188 lines (175 loc) · 5.08 KB
/
eventBridge.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
/*eslint import/no-unresolved: 0 */
import {
ChimeSDKMediaPipelinesClient,
CreateMediaConcatenationPipelineCommand,
CreateMediaConcatenationPipelineCommandInput,
GetMediaPipelineCommand,
GetMediaPipelineCommandOutput,
} from '@aws-sdk/client-chime-sdk-media-pipelines';
import {
ChimeSDKMeetingsClient,
ListAttendeesCommand,
DeleteMeetingCommand,
DeleteAttendeeCommand,
} from '@aws-sdk/client-chime-sdk-meetings';
import { Handler } from 'aws-cdk-lib/aws-lambda';
const chimeSdkMediaPipelinesClient = new ChimeSDKMediaPipelinesClient({
region: 'us-east-1',
});
const chimeSdkMeetingsClient = new ChimeSDKMeetingsClient({
region: 'us-east-1',
});
var concatBucketArn = process.env.CONCAT_BUCKET_ARN;
interface Detail {
version: '0';
eventType: string;
timestamp: number;
meetingId: string;
attendeeId: string;
externalUserId: string;
externalMeetingId: string;
mediaPipelineId: string;
mediaRegion: string;
}
interface EventBridge {
'version': '0';
'id': string;
'detail-type': string;
'source': 'aws.chime';
'account': string;
'time': string;
'region': string;
'resources': [];
'detail': Detail;
}
export const lambdaHandler: Handler = async (
event: EventBridge,
): Promise<null> => {
console.info(event);
switch (event['detail-type']) {
case 'Chime Meeting State Change':
if (
event.detail.eventType == 'chime:AttendeeLeft' ||
event.detail.eventType == 'chime:AttendeeDropped'
) {
await deleteAttendee(event.detail.meetingId, event.detail.attendeeId);
}
if (event.detail.eventType == 'chime:AttendeeDeleted') {
const meetingAttendees = await listAttendees(event.detail.meetingId);
if (meetingAttendees && meetingAttendees.Attendees) {
if (
meetingAttendees.Attendees.length == 1 &&
meetingAttendees.Attendees[0].ExternalUserId?.slice(0, 17) ==
'aws:MediaPipeline'
) {
await deleteMeeting(event.detail.meetingId);
}
}
}
break;
case 'Chime Media Pipeline State Change':
if (event.detail.eventType == 'chime:MediaPipelineInProgress') {
const mediaCapturePipeline = await getMediaPipeline(
event.detail.mediaPipelineId,
);
if (
mediaCapturePipeline &&
mediaCapturePipeline.MediaPipeline &&
mediaCapturePipeline.MediaPipeline.MediaCapturePipeline &&
mediaCapturePipeline.MediaPipeline.MediaCapturePipeline
.MediaPipelineArn
) {
await startConcat(
mediaCapturePipeline.MediaPipeline.MediaCapturePipeline
.MediaPipelineArn,
);
}
}
break;
}
return null;
};
async function getMediaPipeline(mediaPipelineId: string) {
try {
const getMediaPipelineResponse: GetMediaPipelineCommandOutput =
await chimeSdkMediaPipelinesClient.send(
new GetMediaPipelineCommand({ MediaPipelineId: mediaPipelineId }),
);
return getMediaPipelineResponse;
} catch (error) {
console.log(error);
return false;
}
}
async function listAttendees(meetingId: string) {
try {
const listAttendeesResponse = await chimeSdkMeetingsClient.send(
new ListAttendeesCommand({ MeetingId: meetingId }),
);
return listAttendeesResponse;
} catch (error) {
console.log(error);
return false;
}
}
async function deleteAttendee(meetingId: string, attendeeId: string) {
try {
await chimeSdkMeetingsClient.send(
new DeleteAttendeeCommand({
MeetingId: meetingId,
AttendeeId: attendeeId,
}),
);
} catch (error) {
console.log(error);
}
}
async function deleteMeeting(meetingId: string) {
try {
await chimeSdkMeetingsClient.send(
new DeleteMeetingCommand({ MeetingId: meetingId }),
);
} catch (error) {
console.log(error);
}
}
async function startConcat(mediaCapturePipelineArn: string) {
const createConcatPipelineParams: CreateMediaConcatenationPipelineCommandInput =
{
Sinks: [
{
S3BucketSinkConfiguration: { Destination: concatBucketArn },
Type: 'S3Bucket',
},
],
Sources: [
{
MediaCapturePipelineSourceConfiguration: {
ChimeSdkMeetingConfiguration: {
ArtifactsConfiguration: {
Audio: { State: 'Enabled' },
CompositedVideo: { State: 'Enabled' },
Content: { State: 'Disabled' },
DataChannel: { State: 'Enabled' },
MeetingEvents: { State: 'Enabled' },
TranscriptionMessages: { State: 'Enabled' },
Video: { State: 'Disabled' },
},
},
MediaPipelineArn: mediaCapturePipelineArn,
},
Type: 'MediaCapturePipeline',
},
],
};
console.log(JSON.stringify(createConcatPipelineParams));
try {
await chimeSdkMediaPipelinesClient.send(
new CreateMediaConcatenationPipelineCommand(createConcatPipelineParams),
);
return true;
} catch (error) {
console.log(error);
return false;
}
}