|
| 1 | +import { CfnRecordingConfiguration } from 'aws-cdk-lib/aws-ivs'; |
| 2 | +import { IBucket } from 'aws-cdk-lib/aws-s3'; |
| 3 | +import { Duration, Fn, IResource, Resource, Stack, Token } from 'aws-cdk-lib/core'; |
| 4 | +import { Construct } from 'constructs'; |
| 5 | +import { RenditionConfiguration } from './rendition-configuration'; |
| 6 | +import { ThumbnailConfiguration } from './thumbnail-configuration'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Properties of the IVS Recording configuration |
| 10 | + */ |
| 11 | +export interface RecordingConfigurationProps { |
| 12 | + /** |
| 13 | + * S3 bucket where recorded videos will be stored. |
| 14 | + */ |
| 15 | + readonly bucket: IBucket; |
| 16 | + |
| 17 | + /** |
| 18 | + * The name of the Recording configuration. |
| 19 | + * The value does not need to be unique. |
| 20 | + * |
| 21 | + * @default - auto generate |
| 22 | + */ |
| 23 | + readonly recordingConfigurationName?: string; |
| 24 | + |
| 25 | + /** |
| 26 | + * If a broadcast disconnects and then reconnects within the specified interval, |
| 27 | + * the multiple streams will be considered a single broadcast and merged together. |
| 28 | + * |
| 29 | + * `recordingReconnectWindow` must be between 0 and 300 seconds |
| 30 | + * |
| 31 | + * @default - 0 seconds (means disabled) |
| 32 | + */ |
| 33 | + readonly recordingReconnectWindow?: Duration; |
| 34 | + |
| 35 | + /** |
| 36 | + * A rendition configuration describes which renditions should be recorded for a stream. |
| 37 | + * |
| 38 | + * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ivs-recordingconfiguration-renditionconfiguration.html |
| 39 | + * |
| 40 | + * @default - no rendition configuration |
| 41 | + */ |
| 42 | + readonly renditionConfiguration?: RenditionConfiguration; |
| 43 | + |
| 44 | + /** |
| 45 | + * A thumbnail configuration enables/disables the recording of thumbnails for a live session and controls the interval at which thumbnails are generated for the live session. |
| 46 | + * |
| 47 | + * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ivs-recordingconfiguration-thumbnailconfiguration.html |
| 48 | + * |
| 49 | + * @default - no thumbnail configuration |
| 50 | + */ |
| 51 | + readonly thumbnailConfiguration?:ThumbnailConfiguration; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Represents the IVS Recording configuration. |
| 56 | + */ |
| 57 | +export interface IRecordingConfiguration extends IResource { |
| 58 | + /** |
| 59 | + * The ID of the Recording configuration. |
| 60 | + * @attribute |
| 61 | + */ |
| 62 | + readonly recordingConfigurationId: string; |
| 63 | + |
| 64 | + /** |
| 65 | + * The ARN of the Recording configuration. |
| 66 | + * @attribute |
| 67 | + */ |
| 68 | + readonly recordingConfigurationArn: string; |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * The IVS Recording configuration |
| 73 | + * |
| 74 | + * @resource AWS::IVS::RecordingConfiguration |
| 75 | + */ |
| 76 | +export class RecordingConfiguration extends Resource implements IRecordingConfiguration { |
| 77 | + /** |
| 78 | + * Imports an IVS Recording Configuration from attributes. |
| 79 | + */ |
| 80 | + public static fromRecordingConfigurationId(scope: Construct, id: string, |
| 81 | + recordingConfigurationId: string): IRecordingConfiguration { |
| 82 | + |
| 83 | + class Import extends Resource implements IRecordingConfiguration { |
| 84 | + public readonly recordingConfigurationId = recordingConfigurationId; |
| 85 | + public readonly recordingConfigurationArn = Stack.of(this).formatArn({ |
| 86 | + resource: 'recording-configuration', |
| 87 | + service: 'ivs', |
| 88 | + resourceName: recordingConfigurationId, |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + return new Import(scope, id); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Imports an IVS Recording Configuration from its ARN |
| 97 | + */ |
| 98 | + public static fromArn(scope: Construct, id: string, recordingConfigurationArn: string): IRecordingConfiguration { |
| 99 | + const resourceParts = Fn.split('/', recordingConfigurationArn); |
| 100 | + |
| 101 | + if (!resourceParts || resourceParts.length < 2) { |
| 102 | + throw new Error(`Unexpected ARN format: ${recordingConfigurationArn}`); |
| 103 | + } |
| 104 | + |
| 105 | + const recordingConfigurationId = Fn.select(1, resourceParts); |
| 106 | + |
| 107 | + class Import extends Resource implements IRecordingConfiguration { |
| 108 | + public readonly recordingConfigurationId = recordingConfigurationId; |
| 109 | + public readonly recordingConfigurationArn = recordingConfigurationArn; |
| 110 | + } |
| 111 | + |
| 112 | + return new Import(scope, id); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * The ID of the Recording configuration. |
| 117 | + * @attribute |
| 118 | + */ |
| 119 | + readonly recordingConfigurationId: string; |
| 120 | + |
| 121 | + /** |
| 122 | + * The ARN of the Recording configuration. |
| 123 | + * @attribute |
| 124 | + */ |
| 125 | + readonly recordingConfigurationArn: string; |
| 126 | + |
| 127 | + private readonly props: RecordingConfigurationProps; |
| 128 | + |
| 129 | + public constructor(scope: Construct, id: string, props: RecordingConfigurationProps) { |
| 130 | + super(scope, id, { |
| 131 | + physicalName: props.recordingConfigurationName, |
| 132 | + }); |
| 133 | + |
| 134 | + this.props = props; |
| 135 | + |
| 136 | + this.validateRecordingConfigurationName(); |
| 137 | + this.validateRecordingReconnectWindowSeconds(); |
| 138 | + |
| 139 | + const resource = new CfnRecordingConfiguration(this, 'Resource', { |
| 140 | + destinationConfiguration: { |
| 141 | + s3: { |
| 142 | + bucketName: this.props.bucket.bucketName, |
| 143 | + }, |
| 144 | + }, |
| 145 | + name: this.props.recordingConfigurationName, |
| 146 | + recordingReconnectWindowSeconds: this.props.recordingReconnectWindow?.toSeconds(), |
| 147 | + renditionConfiguration: this._renderRenditionConfiguration(), |
| 148 | + thumbnailConfiguration: this._renderThumbnailConfiguration(), |
| 149 | + }); |
| 150 | + |
| 151 | + this.recordingConfigurationId = resource.ref; |
| 152 | + this.recordingConfigurationArn = resource.attrArn; |
| 153 | + } |
| 154 | + |
| 155 | + private _renderRenditionConfiguration(): CfnRecordingConfiguration.RenditionConfigurationProperty | undefined { |
| 156 | + if (!this.props.renditionConfiguration) { |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + return { |
| 161 | + renditions: this.props.renditionConfiguration.renditions, |
| 162 | + renditionSelection: this.props.renditionConfiguration.renditionSelection, |
| 163 | + }; |
| 164 | + }; |
| 165 | + |
| 166 | + private _renderThumbnailConfiguration(): CfnRecordingConfiguration.ThumbnailConfigurationProperty | undefined { |
| 167 | + if (!this.props.thumbnailConfiguration) { |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + return { |
| 172 | + recordingMode: this.props.thumbnailConfiguration.recordingMode, |
| 173 | + resolution: this.props.thumbnailConfiguration.resolution, |
| 174 | + storage: this.props.thumbnailConfiguration.storage, |
| 175 | + targetIntervalSeconds: this.props.thumbnailConfiguration.targetInterval?.toSeconds(), |
| 176 | + }; |
| 177 | + }; |
| 178 | + |
| 179 | + private validateRecordingConfigurationName(): undefined { |
| 180 | + const recordingConfigurationName = this.props.recordingConfigurationName; |
| 181 | + |
| 182 | + if (recordingConfigurationName == undefined || Token.isUnresolved(recordingConfigurationName)) { |
| 183 | + return; |
| 184 | + } |
| 185 | + |
| 186 | + if (!/^[a-zA-Z0-9-_]*$/.test(recordingConfigurationName)) { |
| 187 | + throw new Error(`\`recordingConfigurationName\` must consist only of alphanumeric characters, hyphens or underbars, got: ${recordingConfigurationName}.`); |
| 188 | + } |
| 189 | + |
| 190 | + if (recordingConfigurationName.length > 128) { |
| 191 | + throw new Error(`\`recordingConfigurationName\` must be less than or equal to 128 characters, got: ${recordingConfigurationName.length} characters.`); |
| 192 | + } |
| 193 | + }; |
| 194 | + |
| 195 | + private validateRecordingReconnectWindowSeconds(): undefined { |
| 196 | + const recordingReconnectWindow = this.props.recordingReconnectWindow; |
| 197 | + |
| 198 | + if (recordingReconnectWindow === undefined || Token.isUnresolved(recordingReconnectWindow)) { |
| 199 | + return; |
| 200 | + } |
| 201 | + |
| 202 | + if (0 < recordingReconnectWindow.toMilliseconds() && recordingReconnectWindow.toMilliseconds() < Duration.seconds(1).toMilliseconds()) { |
| 203 | + throw new Error(`\`recordingReconnectWindow\` must be between 0 and 300 seconds, got ${recordingReconnectWindow.toMilliseconds()} milliseconds.`); |
| 204 | + } |
| 205 | + |
| 206 | + if (recordingReconnectWindow.toSeconds() > 300) { |
| 207 | + throw new Error(`\`recordingReconnectWindow\` must be between 0 and 300 seconds, got ${recordingReconnectWindow.toSeconds()} seconds.`); |
| 208 | + } |
| 209 | + }; |
| 210 | +} |
0 commit comments