10933f925b21b6905e29ef261d9f396dbd1a9974
[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.ComponentModel;
19 using System.Diagnostics;
20 using NativeWebRTC = Interop.NativeWebRTC;
21
22 namespace Tizen.Multimedia.Remoting
23 {
24     /// <summary>
25     /// Represents a file source.
26     /// </summary>
27     /// <remarks>
28     /// The media storage privilege(http://tizen.org/privilege/mediastorage) is required.<br/>
29     /// The external storage privilege(http://tizen.org/privilege/externalstorage) is required.
30     /// </remarks>
31     /// <seealso cref="WebRTC.AddSource"/>
32     /// <seealso cref="WebRTC.AddSources"/>
33     /// <since_tizen> 9 </since_tizen>
34     [EditorBrowsable(EditorBrowsableState.Never)]
35     public sealed class MediaFileSource : MediaSource
36     {
37         private string _path;
38
39         /// <summary>
40         /// Initializes a new instance of the <see cref="MediaFileSource"/> class.
41         /// </summary>
42         /// <param name="type">The <see cref="MediaType"/> of file source.</param>
43         /// <param name="path">The file path.</param>
44         /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
45         /// <since_tizen> 9 </since_tizen>
46         [EditorBrowsable(EditorBrowsableState.Never)]
47         public MediaFileSource(MediaType type, string path) : base(type)
48         {
49             _path = path ?? throw new ArgumentNullException(nameof(path), "path is null");
50         }
51
52         internal override void OnAttached(WebRTC webRtc)
53         {
54             Debug.Assert(webRtc != null);
55
56             if (WebRtc != null)
57             {
58                 throw new InvalidOperationException("The source is has already been assigned to another WebRTC.");
59             }
60
61             NativeWebRTC.AddMediaSource(webRtc.Handle, MediaSourceType.File, out uint sourceId).
62                 ThrowIfFailed("Failed to add MediaFileSource.");
63
64             NativeWebRTC.SetFileSourcePath(webRtc.Handle, sourceId, _path).
65                 ThrowIfFailed("Failed to set path for MediaFileSource.");
66
67             WebRtc = webRtc;
68             SourceId = sourceId;
69         }
70
71         internal override void OnDetached(WebRTC webRtc)
72         {
73             NativeWebRTC.RemoveMediaSource(webRtc.Handle, SourceId.Value).
74                 ThrowIfFailed("Failed to remove MediaFileSource.");
75
76             WebRtc = null;
77         }
78     }
79 }