[Multimedia] Deprecate constructors related to ElmSharp (#4540)
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Remoting / WebRTC / MediaCameraSource.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 camera source.
25     /// </summary>
26     /// <remarks>The camera privilege(http://tizen.org/privilege/camera) is required.</remarks>
27     /// <seealso cref="WebRTC.AddSource"/>
28     /// <seealso cref="WebRTC.AddSources"/>
29     /// <since_tizen> 9 </since_tizen>
30     public sealed class MediaCameraSource : MediaSource
31     {
32         /// <summary>
33         /// Initializes a new instance of the <see cref="MediaCameraSource"/> class.
34         /// </summary>
35         /// <since_tizen> 9 </since_tizen>
36         public MediaCameraSource() : base(MediaType.Video) {}
37
38         /// <summary>
39         /// Gets or sets the camera device id.
40         /// </summary>
41         /// <value>A value that specifies the camera device id.</value>
42         /// <exception cref="InvalidOperationException">MediaSource is not attached yet.</exception>
43         /// <exception cref="ObjectDisposedException">The WebRTC has already been disposed.</exception>
44         /// <since_tizen> 10 </since_tizen>
45         public uint CameraDeviceId
46         {
47             get
48             {
49                 if (!SourceId.HasValue)
50                 {
51                     throw new InvalidOperationException("MediaSource is not attached yet. Call AddSource() first.");
52                 }
53
54                 NativeWebRTC.GetCameraDeviceId(WebRtc.Handle, SourceId.Value, out uint deviceId).
55                     ThrowIfFailed("Failed to get camera device id");
56
57                 return deviceId;
58             }
59             set
60             {
61                 if (!SourceId.HasValue)
62                 {
63                     throw new InvalidOperationException("MediaSource is not attached yet. Call AddSource() first.");
64                 }
65
66                 NativeWebRTC.SetCameraDeviceId(WebRtc.Handle, SourceId.Value, value).
67                     ThrowIfFailed("Failed to set camera device id");
68             }
69         }
70
71         internal override void OnAttached(WebRTC webRtc)
72         {
73             Debug.Assert(webRtc != null);
74
75             if (WebRtc != null)
76             {
77                 throw new InvalidOperationException("The source is has already been assigned to another WebRTC.");
78             }
79
80             NativeWebRTC.AddMediaSource(webRtc.Handle, MediaSourceType.Camera, out uint sourceId).
81                 ThrowIfFailed("Failed to add MediaCameraSource.");
82
83             WebRtc = webRtc;
84             SourceId = sourceId;
85         }
86
87         internal override void OnDetached(WebRTC webRtc)
88         {
89             NativeWebRTC.RemoveMediaSource(webRtc.Handle, SourceId.Value).
90                 ThrowIfFailed("Failed to remove MediaCameraSource.");
91
92             WebRtc = null;
93         }
94     }
95 }