7be1574674a4aed9933da999c0767f20b112daf8
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Remoting / WebRTC / MediaScreenSource.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 screen source.
25     /// </summary>
26     /// <remarks>
27     /// MediaScreenSource is not allowed to be used by third-party applications due to the security reasons. (Since Tizen 7.0)
28     /// </remarks>
29     /// <seealso cref="WebRTC.AddSource"/>
30     /// <seealso cref="WebRTC.AddSources"/>
31     /// <since_tizen> 9 </since_tizen>
32     public sealed class MediaScreenSource : MediaSource
33     {
34         /// <summary>
35         /// Initializes a new instance of the <see cref="MediaScreenSource"/> class.
36         /// </summary>
37         /// <since_tizen> 9 </since_tizen>
38         public MediaScreenSource() : base(MediaType.Video) {}
39
40         internal override void OnAttached(WebRTC webRtc)
41         {
42             Debug.Assert(webRtc != null);
43
44             if (WebRtc != null)
45             {
46                 throw new InvalidOperationException("The source is has already been assigned to another WebRTC.");
47             }
48
49             NativeWebRTC.AddMediaSource(webRtc.Handle, MediaSourceType.Screen, out uint sourceId).
50                 ThrowIfFailed("Failed to add MediaScreenSource.");
51
52             WebRtc = webRtc;
53             SourceId = sourceId;
54         }
55
56         internal override void OnDetached(WebRTC webRtc)
57         {
58             NativeWebRTC.RemoveMediaSource(webRtc.Handle, SourceId.Value).
59                 ThrowIfFailed("Failed to remove MediaScreenSource.");
60
61             WebRtc = null;
62         }
63
64         internal override MediaSourceType MediaSourceType => MediaSourceType.Screen;
65     }
66 }