/
CallRecording.cs
634 lines (600 loc) · 26.7 KB
/
CallRecording.cs
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
namespace Azure.Communication.CallAutomation
{
/// <summary>
/// The Azure Communication Services Call Recording client.
/// </summary>
public class CallRecording
{
internal readonly string _resourceEndpoint;
internal readonly ContentDownloader _contentDownloader;
internal readonly ClientDiagnostics _clientDiagnostics;
internal readonly HttpPipeline _pipeline;
internal readonly CallRecordingRestClient _callRecordingRestClient;
internal CallRecording(string resourceEndpoint, CallRecordingRestClient callRecordingRestClient, ClientDiagnostics clientDiagnostics, HttpPipeline httpPipeline)
{
_resourceEndpoint = resourceEndpoint;
_callRecordingRestClient = callRecordingRestClient;
_contentDownloader = new(this);
_clientDiagnostics = clientDiagnostics;
_pipeline = httpPipeline;
}
/// <summary>Initializes a new instance of <see cref="CallRecording"/> for mocking.</summary>
protected CallRecording()
{
_resourceEndpoint = null;
_callRecordingRestClient = null;
_contentDownloader = new(this);
_clientDiagnostics = null;
_pipeline = null;
}
/// <summary>
/// Start recording of the call.
/// </summary>
/// <param name="options">Options for start recording</param>
/// <param name="cancellationToken">The cancellation token.</param>
public virtual Response<RecordingStateResult> Start(StartRecordingOptions options, CancellationToken cancellationToken = default)
{
if (options == null)
throw new ArgumentNullException(nameof(options));
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(CallRecording)}.{nameof(Start)}");
scope.Start();
try
{
StartCallRecordingRequestInternal request = new(CallLocatorSerializer.Serialize(options.CallLocator))
{
RecordingStateCallbackUri = options.RecordingStateCallbackUri?.AbsoluteUri,
RecordingChannelType = options.RecordingChannel,
RecordingContentType = options.RecordingContent,
RecordingFormatType = options.RecordingFormat,
PauseOnStart = options.PauseOnStart,
};
if (options.RecordingStorage != null)
{
// This is required only when blob storage in use
if (options.RecordingStorage is AzureBlobContainerRecordingStorage blobStorage)
{
request.ExternalStorage = new RecordingStorageInternal(blobStorage.RecordingStorageKind, blobStorage.RecordingDestinationContainerUri);
}
}
if (options.AudioChannelParticipantOrdering != null && options.AudioChannelParticipantOrdering.Any())
{
foreach (var c in options.AudioChannelParticipantOrdering)
{
request.AudioChannelParticipantOrdering.Add(CommunicationIdentifierSerializer.Serialize(c));
}
}
if (options.ChannelAffinity != null && options.ChannelAffinity.Any())
{
foreach (var c in options.ChannelAffinity)
{
ChannelAffinityInternal newChannelAffinity = new ChannelAffinityInternal(CommunicationIdentifierSerializer.Serialize(c.Participant));
if (c.Channel != null)
{
newChannelAffinity.Channel = c.Channel;
}
request.ChannelAffinity.Add(newChannelAffinity);
}
}
return _callRecordingRestClient.StartRecording(request, cancellationToken: cancellationToken);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Start recording of the call.
/// </summary>
/// <param name="options">Options for start recording</param>
/// <param name="cancellationToken">The cancellation token.</param>
public virtual async Task<Response<RecordingStateResult>> StartAsync(StartRecordingOptions options, CancellationToken cancellationToken = default)
{
if (options == null)
throw new ArgumentNullException(nameof(options));
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(CallRecording)}.{nameof(Start)}");
scope.Start();
try
{
StartCallRecordingRequestInternal request = new(CallLocatorSerializer.Serialize(options.CallLocator))
{
RecordingStateCallbackUri = options.RecordingStateCallbackUri?.AbsoluteUri,
RecordingChannelType = options.RecordingChannel,
RecordingContentType = options.RecordingContent,
RecordingFormatType = options.RecordingFormat,
PauseOnStart = options.PauseOnStart,
};
if (options.RecordingStorage != null)
{
// This is required only when blob storage in use
if (options.RecordingStorage is AzureBlobContainerRecordingStorage blobStorage)
{
request.ExternalStorage = new RecordingStorageInternal(blobStorage.RecordingStorageKind, blobStorage.RecordingDestinationContainerUri);
}
}
if (options.AudioChannelParticipantOrdering != null && options.AudioChannelParticipantOrdering.Any())
{
foreach (var c in options.AudioChannelParticipantOrdering)
{
request.AudioChannelParticipantOrdering.Add(CommunicationIdentifierSerializer.Serialize(c));
}
};
if (options.ChannelAffinity != null && options.ChannelAffinity.Any())
{
foreach (var c in options.ChannelAffinity)
{
ChannelAffinityInternal newChannelAffinity = new ChannelAffinityInternal(CommunicationIdentifierSerializer.Serialize(c.Participant));
if (c.Channel != null)
{
newChannelAffinity.Channel = c.Channel;
}
request.ChannelAffinity.Add(newChannelAffinity);
}
}
return await _callRecordingRestClient.StartRecordingAsync(request, cancellationToken: cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Get the current recording state by recording id.
/// </summary>
/// <param name="recordingId">The recording id to get the state of.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public virtual Response<RecordingStateResult> GetState(string recordingId, CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(CallRecording)}.{nameof(GetState)}");
scope.Start();
try
{
return _callRecordingRestClient.GetRecordingProperties(
recordingId: recordingId,
cancellationToken: cancellationToken
);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Get the current recording state by recording id.
/// </summary>
/// <param name="recordingId">The recording id to get the state of.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public virtual async Task<Response<RecordingStateResult>> GetStateAsync(string recordingId, CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(CallRecording)}.{nameof(GetState)}");
scope.Start();
try
{
return await _callRecordingRestClient.GetRecordingPropertiesAsync(
recordingId: recordingId,
cancellationToken: cancellationToken
).ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Stop recording of the call.
/// </summary>
/// <param name="recordingId">The recording id to stop.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public virtual Response Stop(string recordingId, CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(CallRecording)}.{nameof(Stop)}");
scope.Start();
try
{
return _callRecordingRestClient.StopRecording(
recordingId: recordingId,
cancellationToken: cancellationToken
);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Stop recording of the call.
/// </summary>
/// <param name="recordingId">The recording id to stop.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public virtual async Task<Response> StopAsync(string recordingId, CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(CallRecording)}.{nameof(Stop)}");
scope.Start();
try
{
return await _callRecordingRestClient.StopRecordingAsync(
recordingId: recordingId,
cancellationToken: cancellationToken
).ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Pause recording of the call.
/// </summary>
/// <param name="recordingId">The recording id to pause.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public virtual async Task<Response> PauseAsync(string recordingId, CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(CallRecording)}.{nameof(Pause)}");
scope.Start();
try
{
return await _callRecordingRestClient.PauseRecordingAsync(
recordingId: recordingId,
cancellationToken: cancellationToken
).ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Pause recording of the call.
/// </summary>
/// <param name="recordingId">The recording id to pause.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public virtual Response Pause(string recordingId, CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(CallRecording)}.{nameof(Pause)}");
scope.Start();
try
{
return _callRecordingRestClient.PauseRecording(
recordingId: recordingId,
cancellationToken: cancellationToken
);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Resume recording of the call.
/// </summary>
/// <param name="recordingId">The recording id to pause.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public virtual async Task<Response> ResumeAsync(string recordingId, CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(CallRecording)}.{nameof(Resume)}");
scope.Start();
try
{
return await _callRecordingRestClient.ResumeRecordingAsync(
recordingId: recordingId,
cancellationToken: cancellationToken
).ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// resume recording of the call.
/// </summary>
/// <param name="recordingId">The recording id to resume.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public virtual Response Resume(string recordingId, CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(CallRecording)}.{nameof(Resume)}");
scope.Start();
try
{
return _callRecordingRestClient.ResumeRecording(
recordingId: recordingId,
cancellationToken: cancellationToken
);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// The <see cref="DownloadStreamingAsync(Uri, HttpRange, CancellationToken)"/>
/// operation downloads the recording's content.
///
/// </summary>
/// <param name="sourceLocation">
/// Recording's content's url location.
/// </param>
/// <param name="range">
/// If provided, only download the bytes of the content in the specified range.
/// If not provided, download the entire content.
/// </param>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be canceled.
/// </param>
/// <returns>
/// A <see cref="Response{Stream}"/> containing the
/// downloaded content.
/// </returns>
/// <remarks>
/// A <see cref="RequestFailedException"/> will be thrown if
/// a failure occurs.
/// </remarks>
public virtual async Task<Response<Stream>> DownloadStreamingAsync(
Uri sourceLocation,
HttpRange range = default,
CancellationToken cancellationToken = default) =>
await _contentDownloader.DownloadStreamingInternal(
sourceLocation,
range,
async: true,
cancellationToken)
.ConfigureAwait(false);
/// <summary>
/// The <see cref="DownloadStreaming(Uri, HttpRange, CancellationToken)"/>
/// operation downloads the recording's content.
///
/// </summary>
/// <param name="sourceLocation">
/// Recording's content's url location.
/// </param>
/// <param name="range">
/// If provided, only download the bytes of the content in the specified range.
/// If not provided, download the entire content.
/// </param>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be canceled.
/// </param>
/// <returns>
/// A <see cref="Response{Stream}"/> containing the
/// downloaded content.
/// </returns>
/// <remarks>
/// A <see cref="RequestFailedException"/> will be thrown if
/// a failure occurs.
/// </remarks>
public virtual Response<Stream> DownloadStreaming(
Uri sourceLocation,
HttpRange range = default,
CancellationToken cancellationToken = default) =>
_contentDownloader.DownloadStreamingInternal(
sourceLocation,
range,
async: false,
cancellationToken)
.EnsureCompleted();
/// <summary>
/// The <see cref="DownloadTo(Uri, Stream, ContentTransferOptions, CancellationToken)"/>
/// operation downloads the specified content using parallel requests,
/// and writes the content to <paramref name="destinationStream"/>.
/// </summary>
/// <param name="sourceLocation">
/// A <see cref="Uri"/> with the Recording's content's url location.
/// </param>
/// <param name="destinationStream">
/// A <see cref="Stream"/> to write the downloaded content to.
/// </param>
/// <param name="transferOptions">
/// Optional <see cref="ContentTransferOptions"/> to configure
/// parallel transfer behavior.
/// </param>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be canceled.
/// </param>
/// <returns>
/// A <see cref="Response"/> describing the operation.
/// </returns>
/// <remarks>
/// A <see cref="RequestFailedException"/> will be thrown if
/// a failure occurs.
/// </remarks>
public virtual Response DownloadTo(Uri sourceLocation, Stream destinationStream,
ContentTransferOptions transferOptions = default, CancellationToken cancellationToken = default) =>
_contentDownloader.StagedDownloadAsync(sourceLocation, destinationStream, transferOptions, async: false, cancellationToken: cancellationToken).EnsureCompleted();
/// <summary>
/// The <see cref="DownloadToAsync(Uri, Stream, ContentTransferOptions, CancellationToken)"/>
/// operation downloads the specified content using parallel requests,
/// and writes the content to <paramref name="destinationStream"/>.
/// </summary>
/// <param name="sourceLocation">
/// A <see cref="Uri"/> with the Recording's content's url location.
/// </param>
/// <param name="destinationStream">
/// A <see cref="Stream"/> to write the downloaded content to.
/// </param>
/// <param name="transferOptions">
/// Optional <see cref="ContentTransferOptions"/> to configure
/// parallel transfer behavior.
/// </param>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be canceled.
/// </param>
/// <returns>
/// A <see cref="Response"/> describing the operation.
/// </returns>
/// <remarks>
/// A <see cref="RequestFailedException"/> will be thrown if
/// a failure occurs.
/// </remarks>
public virtual async Task<Response> DownloadToAsync(Uri sourceLocation, Stream destinationStream, ContentTransferOptions transferOptions = default, CancellationToken cancellationToken = default) =>
await _contentDownloader.StagedDownloadAsync(sourceLocation, destinationStream, transferOptions, async: true, cancellationToken: cancellationToken).ConfigureAwait(false);
/// <summary>
/// The <see cref="DownloadTo(Uri, string, ContentTransferOptions, CancellationToken)"/>
/// operation downloads the specified content using parallel requests,
/// and writes the content to <paramref name="destinationPath"/>.
/// </summary>
/// <param name="sourceLocation">
/// A <see cref="Uri"/> with the Recording's content's url location.
/// </param>
/// <param name="destinationPath">
/// A file path to write the downloaded content to.
/// </param>
/// <param name="transferOptions">
/// Optional <see cref="ContentTransferOptions"/> to configure
/// parallel transfer behavior.
/// </param>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be canceled.
/// </param>
/// <returns>
/// A <see cref="Response"/> describing the operation.
/// </returns>
/// <remarks>
/// A <see cref="RequestFailedException"/> will be thrown if
/// a failure occurs.
/// </remarks>
public virtual Response DownloadTo(Uri sourceLocation, string destinationPath,
ContentTransferOptions transferOptions = default, CancellationToken cancellationToken = default)
{
using Stream destination = File.Create(destinationPath);
return _contentDownloader.StagedDownloadAsync(sourceLocation, destination, transferOptions,
async: false, cancellationToken: cancellationToken).EnsureCompleted();
}
/// <summary>
/// The <see cref="DownloadToAsync(Uri, string, ContentTransferOptions, CancellationToken)"/>
/// operation downloads the specified content using parallel requests,
/// and writes the content to <paramref name="destinationPath"/>.
/// </summary>
/// <param name="sourceLocation">
/// A <see cref="Uri"/> with the Recording's content's url location.
/// </param>
/// <param name="destinationPath">
/// A file path to write the downloaded content to.
/// </param>
/// <param name="transferOptions">
/// Optional <see cref="ContentTransferOptions"/> to configure
/// parallel transfer behavior.
/// </param>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be canceled.
/// </param>
/// <returns>
/// A <see cref="Response"/> describing the operation.
/// </returns>
/// <remarks>
/// A <see cref="RequestFailedException"/> will be thrown if
/// a failure occurs.
/// </remarks>
public virtual async Task<Response> DownloadToAsync(Uri sourceLocation, string destinationPath,
ContentTransferOptions transferOptions = default, CancellationToken cancellationToken = default)
{
using Stream destination = File.Create(destinationPath);
return await _contentDownloader.StagedDownloadAsync(sourceLocation, destination, transferOptions,
async: true, cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// The <see cref="Delete(Uri, CancellationToken)"/>
/// operation deletes the specified content from storage.
/// </summary>
/// <param name="recordingLocation">
/// A <see cref="Uri"/> with the Recording's content's url location.
/// </param>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be canceled.
/// </param>
/// <returns>
/// A <see cref="Response"/> describing the operation.
/// </returns>
/// <remarks>
/// A <see cref="RequestFailedException"/> will be thrown if
/// a failure occurs.
/// </remarks>
public virtual Response Delete(Uri recordingLocation, CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(CallRecording)}.{nameof(Delete)}");
scope.Start();
try
{
HttpMessage message = AmsDirectRequestHelpers.GetHttpMessage(this, recordingLocation, RequestMethod.Delete);
_pipeline.Send(message, cancellationToken);
switch (message.Response.Status)
{
case 200:
return message.Response;
default:
throw new RequestFailedException(message.Response);
}
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// The <see cref="DeleteAsync(Uri, CancellationToken)"/>
/// operation deletes the specified content from storage
/// using parallel requests.
/// </summary>
/// <param name="recordingLocation">
/// A <see cref="Uri"/> with the Recording's content's url location.
/// </param>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be canceled.
/// </param>
/// <returns>
/// A <see cref="Response"/> describing the operation.
/// </returns>
/// <remarks>
/// A <see cref="RequestFailedException"/> will be thrown if
/// a failure occurs.
/// </remarks>
public virtual async Task<Response> DeleteAsync(Uri recordingLocation, CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(CallRecording)}.{nameof(Delete)}");
scope.Start();
try
{
HttpMessage message = AmsDirectRequestHelpers.GetHttpMessage(this, recordingLocation, RequestMethod.Delete);
await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
switch (message.Response.Status)
{
case 200:
return message.Response;
default:
throw new RequestFailedException(message.Response);
}
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
}
}