[Multimedia] Deprecate constructors related to ElmSharp (#4540)
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Remoting / WebRTC / MediaCustomSource.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 audio, video custom source.
26     /// </summary>
27     /// <seealso cref="WebRTC.AddSource"/>
28     /// <seealso cref="WebRTC.AddSources"/>
29     /// <since_tizen> 9 </since_tizen>
30     [EditorBrowsable(EditorBrowsableState.Never)]
31     public sealed class MediaCustomSource : MediaSource
32     {
33         /// <summary>
34         /// Initializes a new instance of the <see cref="MediaCustomSource"/> class.
35         /// </summary>
36         /// <since_tizen> 9 </since_tizen>
37         [EditorBrowsable(EditorBrowsableState.Never)]
38         public MediaCustomSource(MediaType type) : base(type) {}
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             var type = MediaType == MediaType.Video ? CustomMediaSourceType.Video : CustomMediaSourceType.Audio;
50
51             NativeWebRTC.AddCustomMediaSource(webRtc.Handle, type, out uint sourceId).
52                     ThrowIfFailed($"Failed to add {MediaType.ToString()} MediaCustomSource.");
53
54             WebRtc = webRtc;
55             SourceId = sourceId;
56         }
57
58         internal override void OnDetached(WebRTC webRtc)
59         {
60             NativeWebRTC.RemoveMediaSource(webRtc.Handle, SourceId.Value).
61                 ThrowIfFailed($"Failed to remove {MediaType.ToString()} MediaCustomSource.");
62
63             WebRtc = null;
64         }
65     }
66 }