[Multimedia] Deprecate constructors related to ElmSharp (#4540)
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Remoting / WebRTC / MediaMicrophoneSource.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 microphone source.
25     /// </summary>
26     /// <remarks>The recorder privilege(http://tizen.org/privilege/recorder) is required.</remarks>
27     /// <seealso cref="WebRTC.AddSource"/>
28     /// <seealso cref="WebRTC.AddSources"/>
29     /// <since_tizen> 9 </since_tizen>
30     public sealed class MediaMicrophoneSource : MediaSource
31     {
32         /// <summary>
33         /// Initializes a new instance of the <see cref="MediaMicrophoneSource"/> class.
34         /// </summary>
35         /// <since_tizen> 9 </since_tizen>
36         public MediaMicrophoneSource() : base(MediaType.Audio) {}
37
38         internal override void OnAttached(WebRTC webRtc)
39         {
40             Debug.Assert(webRtc != null);
41             if (WebRtc != null)
42             {
43                 Log.Error(WebRTCLog.Tag, "The source is has already been assigned to another WebRTC.");
44                 throw new InvalidOperationException("The source is has already been assigned to another WebRTC.");
45             }
46
47             NativeWebRTC.AddMediaSource(webRtc.Handle, MediaSourceType.Microphone, out uint sourceId).
48                 ThrowIfFailed("Failed to add MediaMicrophoneSource.");
49
50             WebRtc = webRtc;
51             SourceId = sourceId;
52         }
53
54         internal override void OnDetached(WebRTC webRtc)
55         {
56             NativeWebRTC.RemoveMediaSource(webRtc.Handle, SourceId.Value).
57                 ThrowIfFailed("Failed to remove MediaMicrophoneSource.");
58
59             WebRtc = (WebRTC)null;
60         }
61
62         /// <summary>
63         /// Applies the audio stream policy to <see cref="MediaMicrophoneSource"/>.
64         /// </summary>
65         /// <param name="policy">The <see cref="AudioStreamPolicy"/> to apply.</param>
66         /// <remarks>
67         /// The WebRTC must be in the <see cref="WebRTCState.Idle"/> state.<br/>
68         /// <br/>
69         /// <see cref="WebRTC"/> does not support all <see cref="AudioStreamType"/>.<br/>
70         /// Supported types are <see cref="AudioStreamType.Media"/>, <see cref="AudioStreamType.VoiceRecognition"/>,
71         /// <see cref="AudioStreamType.Voip"/>, <see cref="AudioStreamType.MediaExternalOnly"/>.
72         /// </remarks>
73         /// <exception cref="ObjectDisposedException">
74         ///     The WebRTC has already been disposed.<br/>
75         ///     -or-<br/>
76         ///     <paramref name="policy"/> has already been disposed.
77         /// </exception>
78         /// <exception cref="InvalidOperationException">The WebRTC is not in the valid state.</exception>
79         /// <exception cref="ArgumentNullException"><paramref name="policy"/> is null.</exception>
80         /// <exception cref="NotSupportedException">
81         ///     <see cref="AudioStreamType"/> of <paramref name="policy"/> is not supported on the current platform.
82         /// </exception>
83         /// <seealso cref="AudioStreamPolicy"/>
84         /// <since_tizen> 9 </since_tizen>
85         public void ApplyAudioStreamPolicy(AudioStreamPolicy policy)
86         {
87             if (policy == null)
88             {
89                 throw new ArgumentNullException(nameof(policy), "policy is null");
90             }
91
92             WebRtc.ValidateWebRTCState(WebRTCState.Idle);
93
94             var ret = NativeWebRTC.SetAudioStreamPolicyToMicrophoneSource(WebRtc.Handle, SourceId.Value, policy.Handle);
95
96             if (ret == WebRTCErrorCode.InvalidArgument)
97             {
98                 throw new NotSupportedException("The specified policy is not supported on the current system.");
99             }
100
101             ret.ThrowIfFailed("Failed to set the audio stream policy to the WebRTC");
102         }
103     }
104 }