[Multimedia] Deprecate constructors related to ElmSharp (#4540)
[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         /// <summary>
50         /// Gets or sets the looping mode of the file source.
51         /// </summary>
52         /// <value>
53         /// true if the transfer starts again from the beginning of the file source after reaching the end of the file; otherwise, false\n
54         /// The default value is false.
55         /// </value>
56         /// <exception cref="InvalidOperationException">MediaSource is not attached yet.</exception>
57         /// <exception cref="ObjectDisposedException">The WebRTC has already been disposed.</exception>
58         /// <since_tizen> 10 </since_tizen>
59         public bool IsLooping
60         {
61             get
62             {
63                 if (!SourceId.HasValue)
64                 {
65                     return false;
66                 }
67
68                 NativeWebRTC.GetFileSourceLooping(WebRtc.Handle, SourceId.Value, out bool isLooping).
69                     ThrowIfFailed("Failed to get looping mode");
70
71                 return isLooping;
72             }
73             set
74             {
75                 if (!SourceId.HasValue)
76                 {
77                     throw new InvalidOperationException("MediaSource is not attached yet. Call AddSource() first.");
78                 }
79
80                 NativeWebRTC.SetFileSourceLooping(WebRtc.Handle, SourceId.Value, value).
81                     ThrowIfFailed("Failed to set looping mode");
82             }
83         }
84
85         internal override void OnAttached(WebRTC webRtc)
86         {
87             Debug.Assert(webRtc != null);
88
89             if (WebRtc != null)
90             {
91                 throw new InvalidOperationException("The source is has already been assigned to another WebRTC.");
92             }
93
94             NativeWebRTC.AddMediaSource(webRtc.Handle, MediaSourceType.File, out uint sourceId).
95                 ThrowIfFailed("Failed to add MediaFileSource.");
96
97             NativeWebRTC.SetFileSourcePath(webRtc.Handle, sourceId, _path).
98                 ThrowIfFailed("Failed to set path for MediaFileSource.");
99
100             WebRtc = webRtc;
101             SourceId = sourceId;
102         }
103
104         internal override void OnDetached(WebRTC webRtc)
105         {
106             NativeWebRTC.RemoveMediaSource(webRtc.Handle, SourceId.Value).
107                 ThrowIfFailed("Failed to remove MediaFileSource.");
108
109             WebRtc = null;
110         }
111     }
112 }