[WebRTC] Add new MediaFileSource APIs (#3917)
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Remoting / WebRTC / MediaFileSource.cs
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Diagnostics;
19 using NativeWebRTC = Interop.NativeWebRTC;
20
21 namespace Tizen.Multimedia.Remoting
22 {
23     /// <summary>
24     /// Represents a media source with contents read from a file.
25     /// </summary>
26     /// <remarks>
27     /// Depending on where the source file is located either the media storage privilege (http://tizen.org/privilege/mediastorage) is required or<br/>
28     /// the external storage privilege(http://tizen.org/privilege/externalstorage) is required.
29     /// </remarks>
30     /// <seealso cref="WebRTC.AddSource"/>
31     /// <seealso cref="WebRTC.AddSources"/>
32     /// <since_tizen> 10 </since_tizen>
33     public sealed class MediaFileSource : MediaSource
34     {
35         private string _path;
36
37         /// <summary>
38         /// Initializes a new instance of the <see cref="MediaFileSource"/> class.
39         /// </summary>
40         /// <param name="type">The <see cref="MediaType"/> of file source.</param>
41         /// <param name="path">The file path.</param>
42         /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
43         /// <since_tizen> 10 </since_tizen>
44         public MediaFileSource(MediaType type, string path) : base(type)
45         {
46             _path = path ?? throw new ArgumentNullException(nameof(path), "path is null");
47         }
48
49         internal override void OnAttached(WebRTC webRtc)
50         {
51             Debug.Assert(webRtc != null);
52
53             if (WebRtc != null)
54             {
55                 throw new InvalidOperationException("The source is has already been assigned to another WebRTC.");
56             }
57
58             NativeWebRTC.AddMediaSource(webRtc.Handle, MediaSourceType.File, out uint sourceId).
59                 ThrowIfFailed("Failed to add MediaFileSource.");
60
61             NativeWebRTC.SetFileSourcePath(webRtc.Handle, sourceId, _path).
62                 ThrowIfFailed("Failed to set path for MediaFileSource.");
63
64             WebRtc = webRtc;
65             SourceId = sourceId;
66         }
67
68         internal override void OnDetached(WebRTC webRtc)
69         {
70             NativeWebRTC.RemoveMediaSource(webRtc.Handle, SourceId.Value).
71                 ThrowIfFailed("Failed to remove MediaFileSource.");
72
73             WebRtc = null;
74         }
75     }
76 }