2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Diagnostics;
20 using System.Threading;
21 using System.Threading.Tasks;
22 using Native = Interop.ScreenMirroring;
24 namespace Tizen.Multimedia.Remoting
27 /// Provides the ability to connect to and disconnect from a screen mirroring source,
28 /// start, pause, and resume the screen mirroring as a sink.
30 /// <since_tizen> 4 </since_tizen>
31 public class ScreenMirroring : IDisposable, IDisplayable<ScreenMirroringErrorCode>
33 private const string Feature = "http://tizen.org/feature/network.wifi.direct.display";
35 private const uint _port = 2022;
36 private const uint _portMax = 65535;
38 private IntPtr _handle;
40 private AtomicState _state;
42 private bool _disposed = false;
44 internal IntPtr Handle
54 private static bool IsSupported()
56 return System.Information.TryGetValue(Feature, out bool isSupported) ? isSupported : false;
60 /// Initializes a new instance of the ScreenMirroring class.
62 /// <feature>http://tizen.org/feature/network.wifi.direct.display</feature>
63 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
64 /// <since_tizen> 4 </since_tizen>
65 public ScreenMirroring()
67 if (IsSupported() == false)
69 throw new PlatformNotSupportedException(
70 $"The feature({Feature}) is not supported on the current device.");
73 Native.Create(out _handle).ThrowIfError("Failed to create ScreenMirroring.");
75 _state = new AtomicState();
77 AudioInfo = new ScreenMirroringAudioInfo(this);
78 VideoInfo = new ScreenMirroringVideoInfo(this);
80 RegisterStateChangedEvent();
84 /// Finalizes an instance of the ScreenMirroring class.
92 /// Occurs when the state is changed.
94 /// <since_tizen> 4 </since_tizen>
95 public event EventHandler<ScreenMirroringStateChangedEventArgs> StateChanged;
98 /// Occurs when an error occurs.
100 /// <since_tizen> 4 </since_tizen>
101 public event EventHandler<ScreenMirroringErrorOccurredEventArgs> ErrorOccurred;
103 #region Display support
105 private Display _display;
107 private void DetachDisplay()
109 if (_display != null)
111 _display.SetOwner(null);
116 private void SetDisplay(Display display)
120 throw new ArgumentNullException(nameof(Display));
123 display.SetOwner(this);
124 display.ApplyTo(this).ThrowIfError("Failed to set display.");
129 ScreenMirroringErrorCode IDisplayable<ScreenMirroringErrorCode>.ApplyEvasDisplay(DisplayType type,
130 ElmSharp.EvasObject evasObject)
132 Debug.Assert(Enum.IsDefined(typeof(DisplayType), type));
134 return Native.SetDisplay(Handle, (int)type, evasObject);
137 ScreenMirroringErrorCode IDisplayable<ScreenMirroringErrorCode>.ApplyEcoreWindow(IntPtr windowHandle)
139 return Native.SetEcoreDisplay(Handle, windowHandle);
144 /// Gets the negotiated audio info.
146 /// <since_tizen> 4 </since_tizen>
147 public ScreenMirroringAudioInfo AudioInfo { get; }
150 /// Gets the negotiated video info.
152 /// <since_tizen> 4 </since_tizen>
153 public ScreenMirroringVideoInfo VideoInfo { get; }
155 private bool IsConnected
159 return _state.IsOneOf(ScreenMirroringState.Connected, ScreenMirroringState.Playing,
160 ScreenMirroringState.Paused);
164 internal void ThrowIfNotConnected()
168 if (IsConnected == false)
170 throw new InvalidOperationException("ScreenMirroring is not connected.");
175 /// Prepares the screen mirroring with the specified display.
178 /// The state must be <see cref="ScreenMirroringState.Idle"/>.<br/>
180 /// All supported resolutions will be candidates.
182 /// <param name="display">The display where the mirroring will be played on.</param>
183 /// <exception cref="ArgumentException">
184 /// <paramref name="display"/> has already been assigned to another.
186 /// <exception cref="ArgumentNullException"><paramref name="display"/> is null.</exception>
187 /// <exception cref="InvalidOperationException">
188 /// The current state is not in the valid.<br/>
190 /// An internal error occurs.
192 /// <exception cref="ObjectDisposedException">The <see cref="ScreenMirroring"/> has already been disposed.</exception>
193 /// <since_tizen> 4 </since_tizen>
194 public void Prepare(Display display)
196 PrepareCore(display, (ScreenMirroringResolutions)0);
200 /// Prepares the screen mirroring with the specified display and resolutions.
203 /// The state must be <see cref="ScreenMirroringState.Idle"/>.
205 /// <param name="display">The display where the mirroring will be played on.</param>
206 /// <param name="resolutions">The desired resolutions.</param>
207 /// <exception cref="ArgumentException">
208 /// <paramref name="resolutions"/> contain invalid flags.<br/>
210 /// <paramref name="display"/> has already been assigned to another.
212 /// <exception cref="ArgumentNullException"><paramref name="display"/> is null.</exception>
213 /// <exception cref="InvalidOperationException">
214 /// The current state is not in the valid.<br/>
216 /// An internal error occurs.
218 /// <exception cref="ObjectDisposedException">The <see cref="ScreenMirroring"/> has already been disposed.</exception>
219 /// <since_tizen> 4 </since_tizen>
220 public void Prepare(Display display, ScreenMirroringResolutions resolutions)
222 ValidationUtil.ValidateFlagsEnum(resolutions, (ScreenMirroringResolutions)((1 << 7) - 1), nameof(resolutions));
224 PrepareCore(display, resolutions);
227 private void PrepareCore(Display display, ScreenMirroringResolutions resolutions)
229 ValidateState(ScreenMirroringState.Idle);
231 Native.SetResolution(Handle, resolutions).ThrowIfError("Failed to set resolutions.");
237 Native.Prepare(Handle).ThrowIfError("Failed to prepare.");
246 private Task RunAsync(Func<IntPtr, ScreenMirroringErrorCode> func, string failMessage)
248 var tcs = new TaskCompletionSource<bool>();
252 var ret = func(Handle);
254 if (ret == ScreenMirroringErrorCode.None)
260 tcs.SetException(ret.AsException(failMessage));
268 /// Creates the connection and ready for receiving data from a mirroring source.
270 /// <param name="sourceIp">The source ip address to connect.</param>
272 /// The state must be <see cref="ScreenMirroringState.Prepared"/> state by
273 /// <see cref="Prepare(Display, ScreenMirroringResolutions)"/>.<br/>
274 /// The default port number is 2022.<br/>
275 /// If you want to connect using different port number, please use <see cref="ConnectAsync(string, uint)"/>.
277 /// <returns>A task that represents the asynchronous operation.</returns>
278 /// <privilege>http://tizen.org/privilege/internet</privilege>
279 /// <exception cref="ArgumentException"><paramref name="sourceIp"/> is a zero-length string, contains only white space.</exception>
280 /// <exception cref="ArgumentNullException"><paramref name="sourceIp"/> is null.</exception>
281 /// <exception cref="InvalidOperationException">
282 /// The current state is not in the valid.<br/>
284 /// An internal error occurs.
286 /// <exception cref="ObjectDisposedException">The <see cref="ScreenMirroring"/> has already been disposed.</exception>
287 /// <exception cref="UnauthorizedAccessException">Caller does not have required permission.</exception>
288 /// <seealso cref="ConnectAsync(string, uint)"/>
289 /// <since_tizen> 4 </since_tizen>
290 public Task ConnectAsync(string sourceIp)
292 return ConnectAsync(sourceIp, _port);
296 /// Creates the connection and ready for receiving data from a mirroring source with the given <paramref name="port"/>.
298 /// <param name="sourceIp">The source ip address to connect.</param>
299 /// <param name="port">The port number to connect. The max value is 65535.</param>
301 /// The state must be <see cref="ScreenMirroringState.Prepared"/> state by
302 /// <see cref="Prepare(Display, ScreenMirroringResolutions)"/>.
304 /// <returns>A task that represents the asynchronous operation.</returns>
305 /// <privilege>http://tizen.org/privilege/internet</privilege>
306 /// <exception cref="ArgumentException">
307 /// <paramref name="sourceIp"/> is a zero-length string, contains only white space.<br/>
309 /// <paramref name="port"/> is greater than port max value(65535).
311 /// <exception cref="ArgumentNullException"><paramref name="sourceIp"/> is null.</exception>
312 /// <exception cref="InvalidOperationException">
313 /// The current state is not in the valid.<br/>
315 /// An internal error occurs.
317 /// <exception cref="ObjectDisposedException">The <see cref="ScreenMirroring"/> has already been disposed.</exception>
318 /// <exception cref="UnauthorizedAccessException">Caller does not have required permission.</exception>
319 /// <since_tizen> 9 </since_tizen>
320 public Task ConnectAsync(string sourceIp, uint port)
322 if (sourceIp == null)
324 throw new ArgumentNullException(nameof(sourceIp));
326 if (string.IsNullOrWhiteSpace(sourceIp))
328 throw new ArgumentException($"{nameof(sourceIp)} is a zero-length string.", nameof(sourceIp));
332 throw new ArgumentException($"{nameof(port)} is greater than max port value(65535).", nameof(port));
335 ValidateState(ScreenMirroringState.Prepared);
337 Native.SetIpAndPort(Handle, sourceIp, port.ToString()).ThrowIfError("Failed to set ip.");
339 return RunAsync(Native.Connect, "Failed to connect.");
343 /// Starts mirroring from the source.
346 /// The state must be <see cref="ScreenMirroringState.Connected"/> state by
347 /// <see cref="ConnectAsync(string)"/>.
349 /// <returns>A task that represents the asynchronous operation.</returns>
350 /// <privilege>http://tizen.org/privilege/internet</privilege>
351 /// <exception cref="InvalidOperationException">
352 /// The current state is not in the valid.<br/>
354 /// An internal error occurs.
356 /// <exception cref="ObjectDisposedException">The <see cref="ScreenMirroring"/> has already been disposed.</exception>
357 /// <exception cref="UnauthorizedAccessException">Caller does not have required permission.</exception>
358 /// <since_tizen> 4 </since_tizen>
359 public Task StartAsync()
361 ValidateState(ScreenMirroringState.Connected);
363 return RunAsync(Native.Start, "Failed to start.");
367 /// Pauses mirroring from the source.
370 /// The state must be <see cref="ScreenMirroringState.Playing"/> state by
371 /// <see cref="StartAsync"/>.
373 /// <returns>A task that represents the asynchronous operation.</returns>
374 /// <privilege>http://tizen.org/privilege/internet</privilege>
375 /// <exception cref="InvalidOperationException">
376 /// The current state is not in the valid.<br/>
378 /// An internal error occurs.
380 /// <exception cref="ObjectDisposedException">The <see cref="ScreenMirroring"/> has already been disposed.</exception>
381 /// <exception cref="UnauthorizedAccessException">Caller does not have required permission.</exception>
382 /// <since_tizen> 4 </since_tizen>
383 public Task PauseAsync()
385 ValidateState(ScreenMirroringState.Playing);
387 return RunAsync(Native.Pause, "Failed to pause.");
391 /// Resumes mirroring from the source.
394 /// The state must be <see cref="ScreenMirroringState.Paused"/> state by
395 /// <see cref="PauseAsync"/>.
397 /// <returns>A task that represents the asynchronous operation.</returns>
398 /// <privilege>http://tizen.org/privilege/internet</privilege>
399 /// <exception cref="InvalidOperationException">
400 /// The current state is not in the valid.<br/>
402 /// An internal error occurs.
404 /// <exception cref="ObjectDisposedException">The <see cref="ScreenMirroring"/> has already been disposed.</exception>
405 /// <exception cref="UnauthorizedAccessException">Caller does not have required permission.</exception>
406 /// <since_tizen> 4 </since_tizen>
407 public Task ResumeAsync()
409 ValidateState(ScreenMirroringState.Paused);
411 return RunAsync(Native.Resume, "Failed to resume.");
415 /// Disconnects from the source.
418 /// The state must be <see cref="ScreenMirroringState.Connected"/>,
419 /// <see cref="ScreenMirroringState.Playing"/> or <see cref="ScreenMirroringState.Paused"/>.
421 /// <privilege>http://tizen.org/privilege/internet</privilege>
422 /// <exception cref="InvalidOperationException">
423 /// The current state is not in the valid.<br/>
425 /// An internal error occurs.
427 /// <exception cref="ObjectDisposedException">The <see cref="ScreenMirroring"/> has already been disposed.</exception>
428 /// <exception cref="UnauthorizedAccessException">Caller does not have required permission.</exception>
429 /// <since_tizen> 4 </since_tizen>
430 public void Disconnect()
432 ValidateState(ScreenMirroringState.Connected, ScreenMirroringState.Playing,
433 ScreenMirroringState.Paused);
435 Native.Disconnect(Handle).ThrowIfError("Failed to disconnect.");
439 /// Unprepares the screen mirroring.
442 /// The state must be <see cref="ScreenMirroringState.Prepared"/>,
443 /// or <see cref="ScreenMirroringState.Disconnected"/>.
445 /// <exception cref="InvalidOperationException">
446 /// The current state is not in the valid.<br/>
448 /// An internal error occurs.
450 /// <exception cref="ObjectDisposedException">The <see cref="ScreenMirroring"/> has already been disposed.</exception>
451 /// <since_tizen> 4 </since_tizen>
452 public void Unprepare()
454 ValidateState(ScreenMirroringState.Prepared, ScreenMirroringState.Disconnected);
456 Native.Unprepare(Handle).ThrowIfError("Failed to reset.");
461 private void ThrowIfDisposed()
465 throw new ObjectDisposedException(nameof(ScreenMirroring));
470 /// Releases all resource used by the <see cref="ScreenMirroring"/> object.
473 /// Call <see cref="Dispose()"/> when you are finished using the <see cref="ScreenMirroring"/>.
474 /// The <see cref="Dispose()"/> method leaves the <see cref="ScreenMirroring"/> in an unusable
475 /// state. After calling <see cref="Dispose()"/>, you must release all references to the
476 /// <see cref="ScreenMirroring"/> so the garbage collector can reclaim the memory that the
477 /// <see cref="ScreenMirroring"/> was occupying.
479 /// <since_tizen> 4 </since_tizen>
480 public void Dispose()
483 GC.SuppressFinalize(this);
487 /// Releases the resources used by the ScreenMirroring.
489 /// <param name="disposing">
490 /// true to release both managed and unmanaged resources; false to release only unmanaged resources.
492 /// <since_tizen> 4 </since_tizen>
493 protected virtual void Dispose(bool disposing)
499 if (_handle != IntPtr.Zero)
501 Native.Destroy(_handle);
502 _handle = IntPtr.Zero;
509 private Native.StateChangedCallback _stateChangedCallback;
511 private void RegisterStateChangedEvent()
513 _stateChangedCallback = (error, state, _) =>
515 var prevState = _state.Value;
517 _state.Value = state;
519 if (prevState != state)
521 StateChanged?.Invoke(this, new ScreenMirroringStateChangedEventArgs(state));
525 if (error != ScreenMirroringErrorCode.None)
527 ErrorOccurred?.Invoke(this, new ScreenMirroringErrorOccurredEventArgs(
528 ScreenMirroringError.InvalidOperation));
532 Native.SetStateChangedCb(Handle, _stateChangedCallback).
533 ThrowIfError("Failed to initialize StateChanged event.");
536 private void ValidateState(params ScreenMirroringState[] required)
538 Debug.Assert(required.Length > 0);
542 throw new ObjectDisposedException(nameof(ScreenMirroring));
545 var curState = _state.Value;
546 if (!required.Contains(curState))
548 throw new InvalidOperationException($"The screen mirroring is not in a valid state. " +
549 $"Current State : { curState }, Valid State : { string.Join(", ", required) }.");
555 internal class AtomicState
561 _value = (int)ScreenMirroringState.Idle;
564 public ScreenMirroringState Value
568 return (ScreenMirroringState)Interlocked.CompareExchange(ref _value, 0, 0);
572 Interlocked.Exchange(ref _value, (int)value);
576 public bool IsOneOf(params ScreenMirroringState[] states)
578 return states.Contains(Value);