Package software.amazon.awscdk.services.mediapackagev2.alpha


@Stability(Experimental) package software.amazon.awscdk.services.mediapackagev2.alpha

AWS::MediaPackageV2 Construct Library

---

cdk-constructs: Experimental

The APIs of higher level constructs in this module are experimental and under active development. They are subject to non-backward compatible changes or removal in any future version. These are not subject to the Semantic Versioning model and breaking changes will be announced in the release notes. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.


AWS Elemental MediaPackage V2

MediaPackage delivers high-quality video without concern for capacity and makes it easier to implement popular DVR features such as start over, pause, and rewind. Your content will be protected with comprehensive support for DRM. The service seamlessly integrates with other AWS media services as a complete set of tools for cloud-based video processing and delivery.

This package contains constructs for working with AWS Elemental MediaPackage V2. Allowing you to define AWS Elemental MediaPackage V2 Channel Groups, Channels, Origin Endpoints, Channel Policies and Origin Endpoint Policies.

For further information on AWS Elemental MediaPackage V2, see the documentation.

The following example creates an AWS Elemental MediaPackage V2 Channel Group, Channel and Origin Endpoint:

 Stack stack;
 
 ChannelGroup group = ChannelGroup.Builder.create(stack, "MyChannelGroup")
         .channelGroupName("my-test-channel-group")
         .build();
 
 Channel channel = Channel.Builder.create(stack, "MyChannel")
         .channelGroup(group)
         .channelName("my-testchannel")
         .input(InputConfiguration.cmaf())
         .build();
 
 OriginEndpoint endpoint = OriginEndpoint.Builder.create(stack, "MyOriginEndpoint")
         .channel(channel)
         .originEndpointName("my-test-endpoint")
         .segment(Segment.cmaf())
         .manifests(List.of(Manifest.hls(HlsManifestConfiguration.builder()
                 .manifestName("index")
                 .build())))
         .build();
 

Using Factory Methods

 Stack stack;
 
 
 // Create a channel group
 ChannelGroup group = ChannelGroup.Builder.create(stack, "MyChannelGroup")
         .channelGroupName("my-channel-group")
         .build();
 
 // Add a channel using the factory method
 Channel channel = group.addChannel("MyChannel", ChannelOptions.builder()
         .channelName("my-channel")
         .input(InputConfiguration.cmaf())
         .build());
 
 // Add an origin endpoint using the factory method
 OriginEndpoint endpoint = channel.addOriginEndpoint("MyEndpoint", OriginEndpointOptions.builder()
         .originEndpointName("my-endpoint")
         .segment(Segment.cmaf())
         .manifests(List.of(Manifest.hls(HlsManifestConfiguration.builder().manifestName("index").build())))
         .build());
 

Channel Group

A channel group is the top-level resource that consists of channels and origin endpoints associated with it.

The following code creates a Channel Group:

 Stack stack;
 
 ChannelGroup group = ChannelGroup.Builder.create(stack, "MyChannelGroup")
         .channelGroupName("my-test-channel-group")
         .build();
 

The following code imports an existing channel group using the name attribute:

 Stack stack;
 
 IChannelGroup channelGroup = ChannelGroup.fromChannelGroupAttributes(stack, "ImportedChannelGroup", ChannelGroupAttributes.builder()
         .channelGroupName("MyChannelGroup")
         .build());
 

Channel

A channel is part of a channel group and represents the entry point for a content stream into MediaPackage.

Input Configuration

Channels support two input types: HLS and CMAF.

 Stack stack;
 ChannelGroup group;
 
 
 Channel hlsChannel = Channel.Builder.create(stack, "HlsChannel")
         .channelGroup(group)
         .input(InputConfiguration.hls())
         .build();
 
 Channel cmafChannel = Channel.Builder.create(stack, "CmafChannel")
         .channelGroup(group)
         .input(InputConfiguration.cmaf(CmafInputProps.builder()
                 .inputSwitchConfiguration(InputSwitchConfiguration.builder()
                         .mqcsInputSwitching(true)
                         .build())
                 .outputHeaders(List.of(HeadersCMSD.MQCS))
                 .build()))
         .build();
 
 Channel simpleCmafChannel = Channel.Builder.create(stack, "SimpleCmafChannel")
         .channelGroup(group)
         .input(InputConfiguration.cmaf(CmafInputProps.builder()
                 .outputHeaders(List.of(HeadersCMSD.MQCS))
                 .build()))
         .build();
 

Importing an Existing Channel

The following code imports an existing channel using the name attributes:

 Stack stack;
 
 IChannel channel = Channel.fromChannelAttributes(stack, "ImportedChannel", ChannelAttributes.builder()
         .channelName("MyChannel")
         .channelGroupName("MyChannelGroup")
         .build());
 

Channel Resource Policy

The following code creates a resource policy directly on the channel. This will automatically create a policy on the first call:

 Channel channel;
 
 channel.addToResourcePolicy(PolicyStatement.Builder.create()
         .sid("AllowMediaLiveRoleToAccessEmpChannel")
         .principals(List.of(new ArnPrincipal("arn:aws:iam::AccountID:role/MediaLiveAccessRole")))
         .effect(Effect.ALLOW)
         .actions(List.of("mediapackagev2:PutObject"))
         .resources(List.of(channel.getChannelArn()))
         .build());
 

Origin Endpoint

 Stack stack;
 Channel channel;
 
 OriginEndpoint.Builder.create(stack, "myendpoint")
         .channel(channel)
         .originEndpointName("my-test-endpoint")
         .segment(Segment.cmaf())
         .manifests(List.of(Manifest.hls(HlsManifestConfiguration.builder()
                 .manifestName("index")
                 .build())))
         .build();
 

The following code imports an existing origin endpoint using the name attributes:

 Stack stack;
 
 IOriginEndpoint originEndpoint = OriginEndpoint.fromOriginEndpointAttributes(stack, "ImportedOriginEndpoint", OriginEndpointAttributes.builder()
         .channelGroupName("MyChannelGroup")
         .channelName("MyChannel")
         .originEndpointName("MyExampleOriginEndpoint")
         .build());
 

The following code creates a resource policy on the origin endpoint. This will automatically create a policy on the first call:

 OriginEndpoint origin;
 
 
 origin.addToResourcePolicy(PolicyStatement.Builder.create()
         .sid("AllowRequestsFromCloudFront")
         .principals(List.of(new ServicePrincipal("cloudfront.amazonaws.com")))
         .effect(Effect.ALLOW)
         .actions(List.of("mediapackagev2:GetHeadObject", "mediapackagev2:GetObject"))
         .resources(List.of(origin.getOriginEndpointArn()))
         .conditions(Map.of(
                 "StringEquals", Map.of(
                         "aws:SourceArn", "arn:aws:cloudfront::123456789012:distribution/AAAAAAAAA")))
         .build());
 

Granting Permissions

Granting Ingest Access to MediaLive

To allow AWS Elemental MediaLive to ingest content into a MediaPackage channel, use the grants.ingest() method:

 Channel channel;
 IRole mediaLiveRole;
 
 
 // Grant MediaLive permission to ingest content
 channel.grants.ingest(mediaLiveRole);
 

CloudFront Integration

MediaPackage origin endpoints are designed to be used with Content Delivery Network (CDN) like Amazon CloudFront distributions. CloudFront provides caching, DDoS protection, and global content delivery for your streaming content.

To allow a CloudFront distribution to access a MediaPackage origin endpoint, add a resource policy with the CloudFront service principal:

 OriginEndpoint originEndpoint;
 Distribution distribution;
 
 
 originEndpoint.addToResourcePolicy(PolicyStatement.Builder.create()
         .sid("AllowCloudFrontServicePrincipal")
         .principals(List.of(new ServicePrincipal("cloudfront.amazonaws.com")))
         .effect(Effect.ALLOW)
         .actions(List.of("mediapackagev2:GetObject", "mediapackagev2:GetHeadObject"))
         .resources(List.of(originEndpoint.getOriginEndpointArn()))
         .conditions(Map.of(
                 "StringEquals", Map.of(
                         "aws:SourceArn", distribution.getDistributionArn())))
         .build());
 

You can complete the confirmation with an OAC (Origin Access Control) Policy on the CloudFront Distribution.

Manifest Configuration

MediaPackage V2 supports multiple manifest formats: HLS, Low-Latency HLS (LL-HLS), DASH, and Microsoft Smooth Streaming (MSS).

HLS Manifests

 Channel channel;
 
 
 OriginEndpoint.Builder.create(this, "Endpoint")
         .channel(channel)
         .segment(Segment.cmaf())
         .manifests(List.of(Manifest.hls(HlsManifestConfiguration.builder()
                 .manifestName("index")
                 .manifestWindow(Duration.seconds(60))
                 .programDateTimeInterval(Duration.seconds(60))
                 .scteAdMarkerHls(AdMarkerHls.DATERANGE)
                 .build())))
         .build();
 

Low-Latency HLS Manifests

 Channel channel;
 
 
 OriginEndpoint.Builder.create(this, "Endpoint")
         .channel(channel)
         .segment(Segment.cmaf())
         .manifests(List.of(Manifest.lowLatencyHLS(LowLatencyHlsManifestConfiguration.builder()
                 .manifestName("index")
                 .manifestWindow(Duration.seconds(30))
                 .programDateTimeInterval(Duration.seconds(5))
                 .childManifestName("child")
                 .build())))
         .build();
 

DASH Manifests

 Channel channel;
 
 
 OriginEndpoint.Builder.create(this, "Endpoint")
         .channel(channel)
         .segment(Segment.cmaf())
         .manifests(List.of(Manifest.dash(DashManifestConfiguration.builder()
                 .manifestName("index")
                 .manifestWindow(Duration.seconds(60))
                 .minBufferTime(Duration.seconds(30))
                 .minUpdatePeriod(Duration.seconds(10))
                 .segmentTemplateFormat(SegmentTemplateFormat.NUMBER_WITH_TIMELINE)
                 .periodTriggers(List.of(DashPeriodTriggers.AVAILS, DashPeriodTriggers.DRM_KEY_ROTATION))
                 .build())))
         .build();
 

MSS Manifests

 Channel channel;
 
 
 OriginEndpoint.Builder.create(this, "Endpoint")
         .channel(channel)
         .segment(Segment.ism())
         .manifests(List.of(Manifest.mss(MssManifestConfiguration.builder()
                 .manifestName("index")
                 .manifestWindow(Duration.seconds(60))
                 .manifestLayout(MssManifestLayout.COMPACT)
                 .build())))
         .build();
 

Multiple Manifests

You can configure multiple manifest formats for a single origin endpoint:

 Channel channel;
 
 
 OriginEndpoint.Builder.create(this, "Endpoint")
         .channel(channel)
         .segment(Segment.cmaf())
         .manifests(List.of(Manifest.hls(HlsManifestConfiguration.builder().manifestName("hls").build()), Manifest.dash(DashManifestConfiguration.builder().manifestName("dash").build())))
         .build();
 

| Segment type | Supported manifests | |--------|--------| | Segment.cmaf() | HLS, LL-HLS, DASH | | Segment.ts() | HLS, LL-HLS | | Segment.ism() | MSS |

Each origin endpoint has a single segment configuration. If you need segments with different configurations, use multiple origin endpoints on the same channel.

@see https://docs.aws.amazon.com/mediapackage/latest/userguide/endpoints-create.html

Manifest Filtering

Manifest filters control which variants are included in the manifest. Filters are type-safe and validated against the MediaPackage manifest filtering rules.

| Filter | Method | |--------|--------| | Audio / video bitrate | bitrate(), bitrateRange(), bitrateCombo() | | Audio channels, sample rate, video height, framerate, trickplay height | numeric(), numericList(), numericRange(), numericCombo() | | Audio codec | audioCodec(), audioCodecList() | | Video codec | videoCodec(), videoCodecList() | | Video dynamic range | videoDynamicRange(), videoDynamicRangeList() | | Trickplay type | trickplayType(), trickplayTypeList() | | Audio / subtitle language | text(), textList() | | Advanced patterns | custom() |

The following example creates an HD streaming endpoint that serves only H.264/H.265 content between 1–5 Mbps with stereo audio in English or French:

 Channel channel;
 
 
 OriginEndpoint.Builder.create(this, "Endpoint")
         .channel(channel)
         .segment(Segment.cmaf())
         .manifests(List.of(Manifest.hls(HlsManifestConfiguration.builder()
                 .manifestName("index")
                 .filterConfiguration(FilterConfiguration.builder()
                         .manifestFilter(List.of(ManifestFilter.bitrateRange(BitrateFilterKey.VIDEO_BITRATE, Bitrate.mbps(1), Bitrate.mbps(5)), ManifestFilter.numericRange(NumericFilterKey.VIDEO_HEIGHT, 720, 1080), ManifestFilter.videoCodecList(List.of(VideoCodec.H264, VideoCodec.H265)), ManifestFilter.numeric(NumericFilterKey.AUDIO_CHANNELS, 2), ManifestFilter.textList(TextFilterKey.AUDIO_LANGUAGE, List.of("en-US", "fr"))))
                         .timeDelay(Duration.seconds(30))
                         .build())
                 .build())))
         .build();
 

For advanced patterns that combine ranges and single values, use numericCombo() or bitrateCombo():

 Channel channel;
 
 
 OriginEndpoint.Builder.create(this, "Endpoint")
         .channel(channel)
         .segment(Segment.cmaf())
         .manifests(List.of(Manifest.hls(HlsManifestConfiguration.builder()
                 .manifestName("index")
                 .filterConfiguration(FilterConfiguration.builder()
                         .manifestFilter(List.of(ManifestFilter.numericCombo(NumericFilterKey.VIDEO_HEIGHT, List.of(NumericExpression.range(240, 360), NumericExpression.range(720, 1080), NumericExpression.value(1440)))))
                         .build())
                 .build())))
         .build();
 

DRM Settings

You can exclude session keys from HLS and LL-HLS multivariant playlists using the drmSettings filter configuration. This improves compatibility with legacy HLS clients and provides more granular access control:

 Channel channel;
 
 
 OriginEndpoint.Builder.create(this, "Endpoint")
         .channel(channel)
         .segment(Segment.cmaf())
         .manifests(List.of(Manifest.hls(HlsManifestConfiguration.builder()
                 .manifestName("index")
                 .filterConfiguration(FilterConfiguration.builder()
                         .drmSettings(List.of(DrmSettingsKey.EXCLUDE_SESSION_KEYS))
                         .build())
                 .build())))
         .build();
 

Start Tag Configuration

Configure where playback should start in HLS and LL-HLS manifests using the EXT-X-START tag:

 Channel channel;
 
 
 OriginEndpoint.Builder.create(this, "Endpoint")
         .channel(channel)
         .segment(Segment.cmaf())
         .manifests(List.of(Manifest.hls(HlsManifestConfiguration.builder()
                 .manifestName("index")
                 .startTag(StartTag.of(10))
                 .build())))
         .build();
 

Segment Configuration

Configure segment settings for your origin endpoint.

 Channel channel;
 
 
 OriginEndpoint.Builder.create(this, "TsEndpoint")
         .channel(channel)
         .segment(Segment.ts(TsSegmentProps.builder()
                 .duration(Duration.seconds(6))
                 .name("segment")
                 .includeDvbSubtitles(true)
                 .useAudioRenditionGroup(true)
                 .includeIframeOnlyStreams(false)
                 .scteFilter(List.of(ScteMessageType.BREAK, ScteMessageType.DISTRIBUTOR_ADVERTISEMENT))
                 .build()))
         .manifests(List.of(Manifest.hls(HlsManifestConfiguration.builder().manifestName("index").build())))
         .build();
 
 OriginEndpoint.Builder.create(this, "CmafEndpoint")
         .channel(channel)
         .segment(Segment.cmaf(CmafSegmentProps.builder()
                 .duration(Duration.seconds(6))
                 .name("segment")
                 .includeIframeOnlyStreams(true)
                 .scteFilter(List.of(ScteMessageType.DISTRIBUTOR_ADVERTISEMENT))
                 .build()))
         .manifests(List.of(Manifest.hls(HlsManifestConfiguration.builder().manifestName("index").build())))
         .build();
 
 OriginEndpoint.Builder.create(this, "Endpoint")
         .channel(channel)
         .segment(Segment.cmaf())
         .manifests(List.of(Manifest.hls(HlsManifestConfiguration.builder().manifestName("index").build())))
         .build();
 

Encryption and DRM

Protect your content with encryption using SPEKE (Secure Packager and Encoder Key Exchange). Each container type has its own encryption class with type-safe options:

CMAF Encryption

 Channel channel;
 IRole spekeRole;
 
 
 OriginEndpoint.Builder.create(this, "Endpoint")
         .channel(channel)
         .segment(Segment.cmaf(CmafSegmentProps.builder()
                 .encryption(CmafEncryption.speke(CmafSpekeEncryptionProps.builder()
                         .method(CmafEncryptionMethod.CBCS)
                         .drmSystems(List.of(CmafDrmSystem.FAIRPLAY, CmafDrmSystem.WIDEVINE))
                         .resourceId("my-content-id")
                         .url("https://example.com/speke")
                         .role(spekeRole)
                         .keyRotationInterval(Duration.seconds(300))
                         .audioPreset(PresetSpeke20Audio.PRESET_AUDIO_2)
                         .videoPreset(PresetSpeke20Video.PRESET_VIDEO_2)
                         .build()))
                 .build()))
         .manifests(List.of(Manifest.hls(HlsManifestConfiguration.builder().manifestName("index").build())))
         .build();
 

TS Encryption

 Channel channel;
 IRole spekeRole;
 
 
 OriginEndpoint.Builder.create(this, "TsEndpoint")
         .channel(channel)
         .segment(Segment.ts(TsSegmentProps.builder()
                 .encryption(TsEncryption.speke(TsSpekeEncryptionProps.builder()
                         .method(TsEncryptionMethod.SAMPLE_AES)
                         .resourceId("my-content-id")
                         .url("https://example.com/speke")
                         .role(spekeRole)
                         .build()))
                 .build()))
         .manifests(List.of(Manifest.hls(HlsManifestConfiguration.builder().manifestName("index").build())))
         .build();
 

TS encryption defaults the DRM system based on the method: FairPlay for SAMPLE_AES, Clear Key AES 128 for AES_128. You can override this with the drmSystems property using TsDrmSystem.

Content Key Encryption

You can add content key encryption by providing a certificate imported into AWS Certificate Manager. Your DRM key provider must support content key encryption for this to work:

 Channel channel;
 IRole spekeRole;
 ICertificate certificate;
 
 
 OriginEndpoint.Builder.create(this, "Endpoint")
         .channel(channel)
         .segment(Segment.cmaf(CmafSegmentProps.builder()
                 .encryption(CmafEncryption.speke(CmafSpekeEncryptionProps.builder()
                         .method(CmafEncryptionMethod.CBCS)
                         .drmSystems(List.of(CmafDrmSystem.FAIRPLAY))
                         .resourceId("my-content-id")
                         .url("https://example.com/speke")
                         .role(spekeRole)
                         .certificate(certificate)
                         .build()))
                 .build()))
         .manifests(List.of(Manifest.hls(HlsManifestConfiguration.builder().manifestName("index").build())))
         .build();
 

Excluding Segment DRM Metadata

For CMAF content, you can exclude DRM metadata from segments:

 Channel channel;
 IRole spekeRole;
 
 
 OriginEndpoint.Builder.create(this, "Endpoint")
         .channel(channel)
         .segment(Segment.cmaf(CmafSegmentProps.builder()
                 .encryption(CmafEncryption.speke(CmafSpekeEncryptionProps.builder()
                         .method(CmafEncryptionMethod.CBCS)
                         .drmSystems(List.of(CmafDrmSystem.FAIRPLAY))
                         .resourceId("my-content-id")
                         .url("https://example.com/speke")
                         .role(spekeRole)
                         .excludeSegmentDrmMetadata(true)
                         .build()))
                 .build()))
         .manifests(List.of(Manifest.hls(HlsManifestConfiguration.builder().manifestName("index").build())))
         .build();
 

ISM (Smooth Streaming) Encryption

ISM endpoints use CENC encryption with PlayReady. Audio and video presets are always SHARED, and key rotation is not supported. The DRM system defaults to PlayReady:

 Channel channel;
 IRole spekeRole;
 
 
 OriginEndpoint.Builder.create(this, "IsmEndpoint")
         .channel(channel)
         .segment(Segment.ism(IsmSegmentProps.builder()
                 .encryption(IsmEncryption.speke(IsmSpekeEncryptionProps.builder()
                         .resourceId("my-content-id")
                         .url("https://example.com/speke")
                         .role(spekeRole)
                         .build()))
                 .build()))
         .manifests(List.of(Manifest.mss(MssManifestConfiguration.builder().manifestName("index").build())))
         .build();
 

CloudWatch Metrics

MediaPackage V2 resources expose CloudWatch metrics for monitoring. You can create alarms and dashboards using these metrics:

 ChannelGroup channelGroup;
 Channel channel;
 OriginEndpoint endpoint;
 
 
 // Create a CloudWatch alarm on channel group egress bytes
 Alarm alarm = channelGroup.metricEgressBytes().createAlarm(this, "HighEgress", CreateAlarmOptions.builder()
         .threshold(1000)
         .evaluationPeriods(1)
         .build());
 
 // Monitor channel ingress response time
 channel.metricIngressResponseTime().createAlarm(this, "SlowIngress", CreateAlarmOptions.builder()
         .threshold(1000)
         .evaluationPeriods(2)
         .build());
 
 // Track origin endpoint request count
 Metric requestMetric = endpoint.metricEgressRequestCount(MetricOptions.builder()
         .statistic("sum")
         .period(Duration.minutes(5))
         .build());
 

Available metrics include:

  • metricIngressBytes() - Bytes ingested
  • metricEgressBytes() - Bytes delivered
  • metricIngressResponseTime() - Ingress response time (average)
  • metricEgressResponseTime() - Egress response time (average)
  • metricIngressRequestCount() - Number of ingress requests
  • metricEgressRequestCount() - Number of egress requests

All metrics support standard CloudWatch metric options for customizing period, statistic, and dimensions.