/
MediaFileSource.cs
executable file
·208 lines (180 loc) · 8.3 KB
/
MediaFileSource.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Diagnostics;
using NativeWebRTC = Interop.NativeWebRTC;
namespace Tizen.Multimedia.Remoting
{
/// <summary>
/// Represents a media source with contents read from a file.
/// </summary>
/// <remarks>
/// Depending on where the source file is located either the media storage privilege (http://tizen.org/privilege/mediastorage) is required or<br/>
/// the external storage privilege(http://tizen.org/privilege/externalstorage) is required.
/// </remarks>
/// <seealso cref="WebRTC.AddSource"/>
/// <seealso cref="WebRTC.AddSources"/>
/// <since_tizen> 10 </since_tizen>
public sealed class MediaFileSource : MediaSource
{
private string _path;
/// <summary>
/// Initializes a new instance of the <see cref="MediaFileSource"/> class.
/// </summary>
/// <param name="path">The file path.</param>
/// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
/// <since_tizen> 10 </since_tizen>
public MediaFileSource(string path) : base()
{
_path = path ?? throw new ArgumentNullException(nameof(path), "path is null");
}
/// <summary>
/// Gets or sets the looping mode of the file source.
/// </summary>
/// <value>
/// true if the transfer starts again from the beginning of the file source after reaching the end of the file; otherwise, false\n
/// The default value is false.
/// </value>
/// <exception cref="InvalidOperationException">MediaSource is not attached yet.</exception>
/// <exception cref="ObjectDisposedException">The WebRTC has already been disposed.</exception>
/// <since_tizen> 10 </since_tizen>
public bool IsLooping
{
get
{
if (!SourceId.HasValue)
{
return false;
}
NativeWebRTC.GetFileSourceLooping(WebRtc.Handle, SourceId.Value, out bool isLooping).
ThrowIfFailed("Failed to get looping mode");
return isLooping;
}
set
{
if (!SourceId.HasValue)
{
throw new InvalidOperationException("MediaSource is not attached yet. Call AddSource() first.");
}
NativeWebRTC.SetFileSourceLooping(WebRtc.Handle, SourceId.Value, value).
ThrowIfFailed("Failed to set looping mode");
}
}
/// <summary>
/// Gets the transceiver direction for receiving media stream.
/// </summary>
/// <remarks>The default value is <see cref="TransceiverDirection.SendRecv"/></remarks>
/// <param name="type">The media type.</param>
/// <returns>The transceiver direction.</returns>
/// <exception cref="InvalidOperationException">MediaSource is not attached yet.</exception>
/// <exception cref="ObjectDisposedException">The WebRTC has already been disposed.</exception>
/// <since_tizen> 10 </since_tizen>
public TransceiverDirection GetTransceiverDirection(MediaType type)
{
if (!SourceId.HasValue)
{
throw new InvalidOperationException("MediaSource is not attached yet. Call AddSource() first.");
}
NativeWebRTC.GetTransceiverDirection(WebRtc.Handle, SourceId.Value, type, out TransceiverDirection direction).
ThrowIfFailed("Failed to get transceiver direction.");
return direction;
}
/// <summary>
/// Sets the transceiver direction for receiving media stream.
/// </summary>
/// <remarks>
/// The WebRTC must be in the <see cref="WebRTCState.Idle"/> state when transceiver direction is set.
/// </remarks>
/// <param name="type">The media type.</param>
/// <param name="direction">The transceiver direction.</param>
/// <exception cref="InvalidOperationException">
/// MediaSource is not attached yet.<br/>
/// -or-<br/>
/// The WebRTC is not in the valid state.
/// </exception>
/// <exception cref="ObjectDisposedException">The WebRTC has already been disposed.</exception>
/// <since_tizen> 10 </since_tizen>
public void SetTransceiverDirection(MediaType type, TransceiverDirection direction)
{
if (!SourceId.HasValue)
{
throw new InvalidOperationException("MediaSource is not attached yet. Call AddSource() first.");
}
WebRtc.ValidateWebRTCState(WebRTCState.Idle);
NativeWebRTC.SetTransceiverDirection(WebRtc.Handle, SourceId.Value, type, direction).
ThrowIfFailed("Failed to set transceiver direction");
}
/// <summary>
/// Gets the pause status of media file source.
/// </summary>
/// <remarks>The default value is false.</remarks>
/// <param name="type">The media type.</param>
/// <returns>The pause status.</returns>
/// <exception cref="InvalidOperationException">MediaSource is not attached yet.</exception>
/// <exception cref="ObjectDisposedException">The WebRTC has already been disposed.</exception>
/// <since_tizen> 10 </since_tizen>
public bool GetPause(MediaType type)
{
if (!SourceId.HasValue)
{
throw new InvalidOperationException("MediaSource is not attached yet. Call AddSource() first.");
}
WebRtc.ValidateWebRTCState(WebRTCState.Idle);
NativeWebRTC.GetPause(WebRtc.Handle, SourceId.Value, MediaType, out bool isPaused).
ThrowIfFailed("Failed to get pause");
return isPaused;
}
/// <summary>
/// Sets the pause status of media file source.
/// </summary>
/// <param name="type">The media type.</param>
/// <param name="isPaused">The pause status.</param>
/// <exception cref="InvalidOperationException">MediaSource is not attached yet.</exception>
/// <exception cref="ObjectDisposedException">The WebRTC has already been disposed.</exception>
/// <since_tizen> 10 </since_tizen>
public void SetPause(MediaType type, bool isPaused)
{
if (!SourceId.HasValue)
{
throw new InvalidOperationException("MediaSource is not attached yet. Call AddSource() first.");
}
WebRtc.ValidateWebRTCState(WebRTCState.Idle);
NativeWebRTC.SetPause(WebRtc.Handle, SourceId.Value, type, isPaused).
ThrowIfFailed("Failed to set pause");
}
internal override void OnAttached(WebRTC webRtc)
{
Debug.Assert(webRtc != null);
if (WebRtc != null)
{
throw new InvalidOperationException("The source is has already been assigned to another WebRTC.");
}
NativeWebRTC.AddMediaSource(webRtc.Handle, MediaSourceType.File, out uint sourceId).
ThrowIfFailed("Failed to add MediaFileSource.");
NativeWebRTC.SetFileSourcePath(webRtc.Handle, sourceId, _path).
ThrowIfFailed("Failed to set path for MediaFileSource.");
WebRtc = webRtc;
SourceId = sourceId;
}
internal override void OnDetached(WebRTC webRtc)
{
NativeWebRTC.RemoveMediaSource(webRtc.Handle, SourceId.Value).
ThrowIfFailed("Failed to remove MediaFileSource.");
WebRtc = null;
}
internal override MediaSourceType MediaSourceType => MediaSourceType.File;
}
}