|
| 1 | +--- |
| 2 | +title: Azure Communication Services Quickstart - Create and manage a media composition |
| 3 | +titleSuffix: An Azure Communication Services quickstart |
| 4 | +description: In this quickstart, you'll learn how to create a media composition within your Azure Communication Services resource. |
| 5 | +services: azure-communication-services |
| 6 | +author: peiliu |
| 7 | +manager: alexokun |
| 8 | + |
| 9 | +ms.author: peiliu |
| 10 | +ms.date: 08/18/2022 |
| 11 | +ms.topic: quickstart |
| 12 | +ms.service: azure-communication-services |
| 13 | +ms.custom: mode-other |
| 14 | +--- |
| 15 | +# Quickstart: Create and manage a media composition resource |
| 16 | + |
| 17 | +[!INCLUDE [Private Preview Disclaimer](../../includes/private-preview-include-section.md)] |
| 18 | + |
| 19 | +Get started with Azure Communication Services by using the Communication Services C# Media Composition SDK to compose and stream videos. |
| 20 | + |
| 21 | +## Prerequisites |
| 22 | + |
| 23 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 24 | +- The latest version of [.NET Core SDK](https://dotnet.microsoft.com/download/dotnet-core) for your operating system. |
| 25 | +- An active Communication Services resource and connection string. [Create a Communication Services resource](../create-communication-resource.md). |
| 26 | + |
| 27 | +### Prerequisite check |
| 28 | + |
| 29 | +- In a terminal or command window, run the `dotnet` command to check that the .NET SDK is installed. |
| 30 | + |
| 31 | +## Set up the application environment |
| 32 | + |
| 33 | +To set up an environment for using media composition, take the steps in the following sections. |
| 34 | + |
| 35 | +### Create a new C# application |
| 36 | + |
| 37 | +1. In a console window, such as cmd, PowerShell, or Bash, use the `dotnet new` command to create a new console app with the name `MediaCompositionQuickstart`. This command creates a simple "Hello World" C# project with a single source file, **Program.cs**. |
| 38 | + |
| 39 | + ```console |
| 40 | + dotnet new console -o MediaCompositionQuickstart |
| 41 | + ``` |
| 42 | + |
| 43 | +1. Change your directory to the newly created app folder and use the `dotnet build` command to compile your application. |
| 44 | + |
| 45 | + ```console |
| 46 | + cd MediaCompositionQuickstart |
| 47 | + dotnet build |
| 48 | + ``` |
| 49 | + |
| 50 | +### Install the package |
| 51 | + |
| 52 | +1. While still in the application directory, install the Azure Communication Services MediaComposition SDK for .NET package by using the following command. |
| 53 | + |
| 54 | + ```console |
| 55 | + dotnet add package Azure.Communication.MediaCompositionQuickstart --version 1.0.0-beta.1 |
| 56 | + ``` |
| 57 | + |
| 58 | +1. Add a `using` directive to the top of **Program.cs** to include the `Azure.Communication` namespace. |
| 59 | + |
| 60 | + ```csharp |
| 61 | + using System; |
| 62 | + using System.Collections.Generic; |
| 63 | + |
| 64 | + using Azure; |
| 65 | + using Azure.Communication; |
| 66 | + using Azure.Communication.MediaComposition; |
| 67 | + ``` |
| 68 | + |
| 69 | +## Authenticate the media composition client |
| 70 | + |
| 71 | +Open **Program.cs** in a text editor and replace the body of the `Main` method with code to initialize a `MediaCompositionClient` with your connection string. The `MediaCompositionClient` will be used to create and manage media composition objects. |
| 72 | + |
| 73 | + You can find your Communication Services resource connection string in the Azure portal. For more information on connection strings, see [this page](../create-communication-resource.md#access-your-connection-strings-and-service-endpoints). |
| 74 | + |
| 75 | + |
| 76 | +```csharp |
| 77 | +// Find your Communication Services resource in the Azure portal |
| 78 | +var connectionString = "<connection_string>"; |
| 79 | +var mediaCompositionClient = new MediaCompositionClient(connectionString); |
| 80 | +``` |
| 81 | + |
| 82 | +## Create a media composition |
| 83 | + |
| 84 | +Create a new media composition by defining the `inputs`, `layout`, `outputs`, and a user-friendly `mediaCompositionId`. For more information on how to define the values, see [this page](./define-media-composition.md). These values are passed into the `CreateAsync` function exposed on the client. The code snippet below shows and example of defining a simple two by two grid layout: |
| 85 | + |
| 86 | +```csharp |
| 87 | +var layout = new GridLayout( |
| 88 | + rows: 2, |
| 89 | + columns: 2, |
| 90 | + inputIds: new List<List<string>> |
| 91 | + { |
| 92 | + new List<string> { "Jill", "Jack" }, new List<string> { "Jane", "Jerry" } |
| 93 | + }) |
| 94 | + { |
| 95 | + Resolution = new(1920, 1080) |
| 96 | + }; |
| 97 | + |
| 98 | +var inputs = new Dictionary<string, MediaInput>() |
| 99 | +{ |
| 100 | + ["Jill"] = new ParticipantInput |
| 101 | + ( |
| 102 | + id: new MicrosoftTeamsUserIdentifier("f3ba9014-6dca-4456-8ec0-fa03cfa2b7b7"), |
| 103 | + call: "teamsMeeting") |
| 104 | + { |
| 105 | + PlaceholderImageUri = "https://imageendpoint" |
| 106 | + }, |
| 107 | + ["Jack"] = new ParticipantInput |
| 108 | + ( |
| 109 | + id: new MicrosoftTeamsUserIdentifier("fa4337b5-f13a-41c5-a34f-f2aa46699b61"), |
| 110 | + call: "teamsMeeting") |
| 111 | + { |
| 112 | + PlaceholderImageUri = "https://imageendpoint" |
| 113 | + }, |
| 114 | + ["Jane"] = new ParticipantInput |
| 115 | + ( |
| 116 | + id: new MicrosoftTeamsUserIdentifier("2dd69470-dc25-49cf-b5c3-f562f08bf3b2"), |
| 117 | + call: "teamsMeeting" |
| 118 | + ) |
| 119 | + { |
| 120 | + PlaceholderImageUri = "https://imageendpoint" |
| 121 | + }, |
| 122 | + ["Jerry"] = new ParticipantInput |
| 123 | + ( |
| 124 | + id: new MicrosoftTeamsUserIdentifier("30e29fde-ac1c-448f-bb34-0f3448d5a677"), |
| 125 | + call: "teamsMeeting") |
| 126 | + { |
| 127 | + PlaceholderImageUri = "https://imageendpoint" |
| 128 | + }, |
| 129 | + ["teamsMeeting"] = new TeamsMeetingInput(teamsJoinUrl: "https://teamsJoinUrl") |
| 130 | +}; |
| 131 | + |
| 132 | +var outputs = new Dictionary<string, MediaOutput>() |
| 133 | +{ |
| 134 | + ["acsGroupCall"] = new GroupCallOutput("d12d2277-ffec-4e22-9979-8c0d8c13d193") |
| 135 | +}; |
| 136 | + |
| 137 | +var mediaCompositionId = "twoByTwoGridLayout" |
| 138 | +var response = await mediaCompositionClient.CreateAsync(mediaCompositionId, layout, inputs, outputs); |
| 139 | +``` |
| 140 | + |
| 141 | +You can use the `mediaCompositionId` to view or update the properties of a media composition object. Therefore, it is important to keep track of and persist the `mediaCompositionId` in your storage medium of choice. |
| 142 | + |
| 143 | +## Get properties of an existing media composition |
| 144 | + |
| 145 | +Retrieve the details of an existing media composition by referencing the `mediaCompositionId`. |
| 146 | + |
| 147 | +```C# Snippet:GetMediaComposition |
| 148 | +var gridMediaComposition = await mediaCompositionClient.GetAsync(mediaCompositionId); |
| 149 | +``` |
| 150 | + |
| 151 | +## Updates |
| 152 | + |
| 153 | +Updating the `layout` of a media composition can happen on-the-fly as the media composition is running. However, `input` updates while the media composition is running are not supported. The media composition will need to be stopped and restarted before any changes to the inputs are applied. |
| 154 | + |
| 155 | +### Update layout |
| 156 | + |
| 157 | +Updating the `layout` can be issued by passing in the new `layout` object and the `mediaCompositionId`. For example, we can update the grid layout to an auto-grid layout following the snippet below: |
| 158 | + |
| 159 | +```csharp |
| 160 | +var layout = new AutoGridLayout(new List<string>() { "teamsMeeting" }) |
| 161 | +{ |
| 162 | + Resolution = new(720, 480), |
| 163 | +}; |
| 164 | + |
| 165 | +var response = await mediaCompositionClient.UpdateLayoutAsync(mediaCompositionId, layout); |
| 166 | +``` |
| 167 | + |
| 168 | +### Upsert or remove inputs |
| 169 | + |
| 170 | +To upsert inputs from the media composition object, use the `UpsertInputsAsync` function exposed in the client. |
| 171 | + |
| 172 | +```csharp |
| 173 | +var inputsToUpsert = new Dictionary<string, MediaInput>() |
| 174 | +{ |
| 175 | + ["James"] = new ParticipantInput |
| 176 | + ( |
| 177 | + id: new MicrosoftTeamsUserIdentifier("f3ba9014-6dca-4456-8ec0-fa03cfa2b70p"), |
| 178 | + call: "teamsMeeting" |
| 179 | + ) |
| 180 | + { |
| 181 | + PlaceholderImageUri = "https://imageendpoint" |
| 182 | + } |
| 183 | +}; |
| 184 | + |
| 185 | +var response = await mediaCompositionClient.UpsertInputsAsync(mediaCompositionId, inputsToUpsert); |
| 186 | +``` |
| 187 | + |
| 188 | +You can also explicitly remove inputs from the list. |
| 189 | +```csharp |
| 190 | +var inputIdsToRemove = new List<string>() |
| 191 | +{ |
| 192 | + "Jane", "Jerry" |
| 193 | +}; |
| 194 | +var response = await mediaCompositionClient.RemoveInputsAsync(mediaCompositionId, inputIdsToRemove); |
| 195 | +``` |
| 196 | + |
| 197 | +### Upsert or remove outputs |
| 198 | + |
| 199 | +To upsert outputs, you can use the `UpsertOutputsAsync` function from the client. |
| 200 | +```csharp |
| 201 | +var outputsToUpsert = new Dictionary<string, MediaOutput>() |
| 202 | +{ |
| 203 | + ["youtube"] = new RtmpOutput("key", new(1920, 1080), "rtmp://a.rtmp.youtube.com/live2") |
| 204 | +}; |
| 205 | + |
| 206 | +var response = await mediaCompositionClient.UpsertOutputsAsync(mediaCompositionId, outputsToUpsert); |
| 207 | +``` |
| 208 | + |
| 209 | +You can remove outputs by following the snippet below: |
| 210 | +```csharp |
| 211 | +var outputIdsToRemove = new List<string>() |
| 212 | +{ |
| 213 | + "acsGroupCall" |
| 214 | +}; |
| 215 | +var response = await mediaCompositionClient.RemoveOutputsAsync(mediaCompositionId, outputIdsToRemove); |
| 216 | +``` |
| 217 | + |
| 218 | +## Start running a media composition |
| 219 | + |
| 220 | +After defining the media composition with the correct properties, you can start composing the media by calling the `StartAsync` function using the `mediaCompositionId`. |
| 221 | + |
| 222 | +```csharp |
| 223 | +var compositionSteamState = await mediaCompositionClient.StartAsync(mediaCompositionId); |
| 224 | +``` |
| 225 | + |
| 226 | +## Stop running a media composition |
| 227 | + |
| 228 | +To stop a media composition, call the `StopAsync` function using the `mediaCompositionId`. |
| 229 | + |
| 230 | +```csharp |
| 231 | +var compositionSteamState = await mediaCompositionClient.StopAsync(mediaCompositionId); |
| 232 | +``` |
| 233 | + |
| 234 | +## Delete a media composition |
| 235 | + |
| 236 | +If you wish to delete a media composition, you may issue a delete request: |
| 237 | +```csharp |
| 238 | +await mediaCompositionClient.DeleteAsync(mediaCompositionId); |
| 239 | +``` |
| 240 | + |
| 241 | +## Object model |
| 242 | + |
| 243 | +The table below lists the main properties of media composition objects: |
| 244 | + |
| 245 | +| Name | Description | |
| 246 | +|-----------------------|-------------------------------------------| |
| 247 | +| `mediaCompositionId` | Media composition identifier that can be a user-friendly string. Must be unique across a Communication Service resource. | |
| 248 | +| `layout` | Specifies how the media sources will be composed into a single frame. | |
| 249 | +| `inputs` | Defines which media sources will be used in the layout composition. | |
| 250 | +| `outputs` | Defines where to send the composed streams to.| |
| 251 | + |
| 252 | +## Next steps |
| 253 | + |
| 254 | +In this section you learned how to: |
| 255 | +> [!div class="checklist"] |
| 256 | +> - Create a new media composition |
| 257 | +> - Get the properties of a media composition |
| 258 | +> - Update layout |
| 259 | +> - Upsert and remove inputs |
| 260 | +> - Upsert and remove outputs |
| 261 | +> - Start and stop a media composition |
| 262 | +> - Delete a media composition |
| 263 | +
|
| 264 | +You may also want to: |
| 265 | + - Learn about [media composition concept](../../concepts/voice-video-calling//media-comp.md) |
| 266 | + - Learn about [how to define a media composition](./define-media-composition.md) |
0 commit comments