[NUI] Remove upcast
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / VideoView.cs
1 /*
2  * Copyright(c) 2017 Samsung Electronics Co., Ltd.
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.Runtime.InteropServices;
20 using Tizen.NUI.Binding;
21
22 namespace Tizen.NUI.BaseComponents
23 {
24     /// <summary>
25     /// VideoView is a control for video playback and display.
26     /// </summary>
27     /// <since_tizen> 3 </since_tizen>
28     public class VideoView : View
29     {
30         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
31         [EditorBrowsable(EditorBrowsableState.Never)]
32         public static readonly BindableProperty VideoProperty = BindableProperty.Create(nameof(Video), typeof(PropertyMap), typeof(VideoView), null, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
33         {
34             var videoView = (VideoView)bindable;
35             if (newValue != null)
36             {
37                 Tizen.NUI.Object.SetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.VIDEO, new Tizen.NUI.PropertyValue((PropertyMap)newValue));
38             }
39         }),
40         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
41         {
42             var videoView = (VideoView)bindable;
43             PropertyMap temp = new PropertyMap();
44             Tizen.NUI.Object.GetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.VIDEO).Get(temp);
45             return temp;
46         }));
47         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
48         [EditorBrowsable(EditorBrowsableState.Never)]
49         public static readonly BindableProperty LoopingProperty = BindableProperty.Create(nameof(Looping), typeof(bool), typeof(VideoView), false, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
50         {
51             var videoView = (VideoView)bindable;
52             if (newValue != null)
53             {
54                 Tizen.NUI.Object.SetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.LOOPING, new Tizen.NUI.PropertyValue((bool)newValue));
55             }
56         }),
57         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
58         {
59             var videoView = (VideoView)bindable;
60             bool temp = false;
61             Tizen.NUI.Object.GetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.LOOPING).Get(out temp);
62             return temp;
63         }));
64         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
65         [EditorBrowsable(EditorBrowsableState.Never)]
66         public static readonly BindableProperty MutedProperty = BindableProperty.Create(nameof(Muted), typeof(bool), typeof(VideoView), false, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
67         {
68             var videoView = (VideoView)bindable;
69             if (newValue != null)
70             {
71                 Tizen.NUI.Object.SetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.MUTED, new Tizen.NUI.PropertyValue((bool)newValue));
72             }
73         }),
74         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
75         {
76             var videoView = (VideoView)bindable;
77             bool temp = false;
78             Tizen.NUI.Object.GetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.MUTED).Get(out temp);
79             return temp;
80         }));
81         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
82         [EditorBrowsable(EditorBrowsableState.Never)]
83         public static readonly BindableProperty VolumeProperty = BindableProperty.Create(nameof(Volume), typeof(PropertyMap), typeof(VideoView), null, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
84         {
85             var videoView = (VideoView)bindable;
86             if (newValue != null)
87             {
88                 Tizen.NUI.Object.SetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.VOLUME, new Tizen.NUI.PropertyValue((PropertyMap)newValue));
89             }
90         }),
91         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
92         {
93             var videoView = (VideoView)bindable;
94             PropertyMap temp = new PropertyMap();
95             Tizen.NUI.Object.GetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.VOLUME).Get(temp);
96             return temp;
97         }));
98         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
99         [EditorBrowsable(EditorBrowsableState.Never)]
100         public static readonly BindableProperty UnderlayProperty = BindableProperty.Create(nameof(Underlay), typeof(bool), typeof(VideoView), false, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
101         {
102             var videoView = (VideoView)bindable;
103             if (newValue != null)
104             {
105                 Tizen.NUI.Object.SetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.UNDERLAY, new Tizen.NUI.PropertyValue((bool)newValue));
106             }
107         }),
108         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
109         {
110             var videoView = (VideoView)bindable;
111             bool temp = false;
112             Tizen.NUI.Object.GetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.UNDERLAY).Get(out temp);
113             return temp;
114         }));
115         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
116         [EditorBrowsable(EditorBrowsableState.Never)]
117         public static readonly BindableProperty ResourceUrlProperty = BindableProperty.Create(nameof(ResourceUrl), typeof(string), typeof(VideoView), string.Empty, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
118         {
119             var videoView = (VideoView)bindable;
120             if (newValue != null)
121             {
122                 Tizen.NUI.Object.SetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.VIDEO, new Tizen.NUI.PropertyValue((string)newValue));
123             }
124         }),
125         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
126         {
127             var videoView = (VideoView)bindable;
128             string temp;
129             Tizen.NUI.Object.GetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.VIDEO).Get(out temp);
130             return temp;
131         }));
132
133         private FinishedCallbackDelegate _videoViewFinishedCallbackDelegate;
134         private EventHandler<FinishedEventArgs> _videoViewFinishedEventHandler;
135
136         /// <summary>
137         /// Creates an initialized VideoView.
138         /// </summary>
139         /// <since_tizen> 3 </since_tizen>
140         public VideoView() : this(Interop.VideoView.New(), true)
141         {
142             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
143         }
144
145         /// <summary>
146         /// Creates an initialized VideoView.<br />
147         /// If the string is empty, VideoView will not display anything.<br />
148         /// </summary>
149         /// <param name="url">The URL of the video resource to display.</param>
150         /// <since_tizen> 3 </since_tizen>
151         public VideoView(string url) : this(Interop.VideoView.New(url), true)
152         {
153             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
154         }
155
156         /// <summary>
157         /// Hidden API (Inhouse API).
158         /// Using Uri class to provide safe sevice and secure API.
159         /// Creates an initialized VideoView.
160         /// If the string is empty, VideoView will not display anything.
161         /// </summary>
162         /// <param name="uri">The URI of the video resource to display.</param>
163         [EditorBrowsable(EditorBrowsableState.Never)]
164         public VideoView(Uri uri) : this(Interop.VideoView.New((uri == null) ? String.Empty : uri.AbsoluteUri), true)
165         {
166             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
167         }
168
169         /// <summary>
170         /// Creates an initialized VideoView.<br />
171         /// If the string is empty, VideoView will not display anything.<br />
172         /// </summary>
173         /// <param name="swCodec">Video rendering by H/W codec if false.</param>
174         [EditorBrowsable(EditorBrowsableState.Never)]
175         public VideoView(bool swCodec) : this(Interop.VideoView.New(swCodec), true)
176         {
177             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
178         }
179
180         /// <summary>
181         /// Creates an initialized VideoView.<br />
182         /// If the string is empty, VideoView will not display anything.<br />
183         /// </summary>
184         /// <param name="url">The URL of the video resource to display.</param>
185         /// <param name="swCodec">Video rendering by H/W codec if false.</param>
186         [EditorBrowsable(EditorBrowsableState.Never)]
187         public VideoView(string url, bool swCodec) : this(Interop.VideoView.New(url, swCodec), true)
188         {
189             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
190         }
191
192         /// <summary>
193         /// Hidden API (Inhouse API).
194         /// Using Uri class to provide safe sevice and secure API.
195         /// Creates an initialized VideoView.
196         /// If the string is empty, VideoView will not display anything.
197         /// </summary>
198         /// <param name="uri">The URI of the video resource to display.</param>
199         /// <param name="swCodec">Video rendering by H/W codec if false.</param>
200         [EditorBrowsable(EditorBrowsableState.Never)]
201         public VideoView(Uri uri, bool swCodec) : this(Interop.VideoView.New((uri == null) ? String.Empty : uri.AbsoluteUri, swCodec), true)
202         {
203             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
204         }
205
206         internal VideoView(VideoView videoView) : this(Interop.VideoView.NewVideoView(VideoView.getCPtr(videoView)), true)
207         {
208             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
209         }
210
211         internal VideoView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
212         {
213         }
214
215         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
216         private delegate void FinishedCallbackDelegate(IntPtr data);
217
218         /// <summary>
219         /// Event for the finished signal which can be used to subscribe or unsubscribe the event handler
220         /// The finished signal is emitted when a video playback has finished.<br />
221         /// </summary>
222         /// <since_tizen> 3 </since_tizen>
223         public event EventHandler<FinishedEventArgs> Finished
224         {
225             add
226             {
227                 if (_videoViewFinishedEventHandler == null)
228                 {
229                     _videoViewFinishedCallbackDelegate = (OnFinished);
230                     FinishedSignal().Connect(_videoViewFinishedCallbackDelegate);
231                 }
232                 _videoViewFinishedEventHandler += value;
233             }
234             remove
235             {
236                 _videoViewFinishedEventHandler -= value;
237                 if (_videoViewFinishedEventHandler == null && FinishedSignal().Empty() == false)
238                 {
239                     FinishedSignal().Disconnect(_videoViewFinishedCallbackDelegate);
240                 }
241             }
242         }
243
244         /// <summary>
245         /// Video file setting type of PropertyMap.
246         /// </summary>
247         /// <since_tizen> 3 </since_tizen>
248         public PropertyMap Video
249         {
250             get
251             {
252                 return (PropertyMap)GetValue(VideoProperty);
253             }
254             set
255             {
256                 SetValue(VideoProperty, value);
257                 NotifyPropertyChanged();
258             }
259         }
260
261         /// <summary>
262         /// The looping status, true or false.
263         /// </summary>
264         /// <since_tizen> 3 </since_tizen>
265         public bool Looping
266         {
267             get
268             {
269                 return (bool)GetValue(LoopingProperty);
270             }
271             set
272             {
273                 SetValue(LoopingProperty, value);
274                 NotifyPropertyChanged();
275             }
276         }
277
278         /// <summary>
279         /// The mute status, true or false.
280         /// </summary>
281         /// <since_tizen> 3 </since_tizen>
282         public bool Muted
283         {
284             get
285             {
286                 return (bool)GetValue(MutedProperty);
287             }
288             set
289             {
290                 SetValue(MutedProperty, value);
291                 NotifyPropertyChanged();
292             }
293         }
294
295         /// <summary>
296         /// The left and the right volume scalar as float type, PropertyMap with two values ( "left" and "right" ).
297         /// </summary>
298         /// <since_tizen> 3 </since_tizen>
299         public PropertyMap Volume
300         {
301             get
302             {
303                 return (PropertyMap)GetValue(VolumeProperty);
304             }
305             set
306             {
307                 SetValue(VolumeProperty, value);
308                 NotifyPropertyChanged();
309             }
310         }
311
312         /// <summary>
313         /// Video rendering by underlay, true or false.<br />
314         /// This shows video composited underneath the window by the system. This means it may ignore rotation of the video-view.
315         /// </summary>
316         /// <since_tizen> 4 </since_tizen>
317         public bool Underlay
318         {
319             get
320             {
321                 return (bool)GetValue(UnderlayProperty);
322             }
323             set
324             {
325                 SetValue(UnderlayProperty, value);
326                 NotifyPropertyChanged();
327             }
328         }
329
330         /// <summary>
331         /// Video file URL as string type.
332         /// </summary>
333         /// <since_tizen> 4 </since_tizen>
334         public string ResourceUrl
335         {
336             get
337             {
338                 return (string)GetValue(ResourceUrlProperty);
339             }
340             set
341             {
342                 SetValue(ResourceUrlProperty, value);
343                 NotifyPropertyChanged();
344             }
345         }
346
347         /// <summary>
348         /// Starts the video playback.
349         /// </summary>
350         /// <since_tizen> 3 </since_tizen>
351         public void Play()
352         {
353             Interop.VideoView.Play(SwigCPtr);
354             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
355         }
356
357         /// <summary>
358         /// Pauses the video playback.
359         /// </summary>
360         /// <since_tizen> 3 </since_tizen>
361         public void Pause()
362         {
363             Interop.VideoView.Pause(SwigCPtr);
364             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
365         }
366
367         /// <summary>
368         /// Stops the video playback.
369         /// </summary>
370         /// <since_tizen> 3 </since_tizen>
371         public void Stop()
372         {
373             Interop.VideoView.Stop(SwigCPtr);
374             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
375         }
376
377         /// <summary>
378         /// Seeks forward by the specified number of milliseconds.
379         /// </summary>
380         /// <param name="millisecond">The position for forward playback.</param>
381         /// <since_tizen> 3 </since_tizen>
382         public void Forward(int millisecond)
383         {
384             Interop.VideoView.Forward(SwigCPtr, millisecond);
385             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
386         }
387
388         /// <summary>
389         /// Seeks backward by the specified number of milliseconds.
390         /// </summary>
391         /// <param name="millisecond">The position for backward playback.</param>
392         /// <since_tizen> 3 </since_tizen>
393         public void Backward(int millisecond)
394         {
395             Interop.VideoView.Backward(SwigCPtr, millisecond);
396             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
397         }
398
399         internal VideoViewSignal FinishedSignal()
400         {
401             VideoViewSignal ret = new VideoViewSignal(Interop.VideoView.FinishedSignal(SwigCPtr), false);
402             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
403             return ret;
404         }
405
406         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(VideoView obj)
407         {
408             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.SwigCPtr;
409         }
410
411         /// <summary>
412         /// Dispose.
413         /// </summary>
414         /// <param name="type">DisposeTypes</param>
415         /// <since_tizen> 3 </since_tizen>
416         protected override void Dispose(DisposeTypes type)
417         {
418             if (disposed)
419             {
420                 return;
421             }
422
423             //Release your own unmanaged resources here.
424             //You should not access any managed member here except static instance.
425             //because the execution order of Finalizes is non-deterministic.
426
427             if (this != null && _videoViewFinishedCallbackDelegate != null)
428             {
429                 FinishedSignal().Disconnect(_videoViewFinishedCallbackDelegate);
430             }
431
432             base.Dispose(type);
433         }
434
435         /// This will not be public opened.
436         [EditorBrowsable(EditorBrowsableState.Never)]
437         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
438         {
439             Interop.VideoView.DeleteVideoView(swigCPtr);
440         }
441
442         // Callback for VideoView Finished signal
443         private void OnFinished(IntPtr data)
444         {
445             FinishedEventArgs e = new FinishedEventArgs();
446
447             // Populate all members of "e" (FinishedEventArgs) with real data
448             e.VideoView = Registry.GetManagedBaseHandleFromNativePtr(data) as VideoView;
449
450             if (_videoViewFinishedEventHandler != null)
451             {
452                 //here we send all data to user event handlers
453                 _videoViewFinishedEventHandler(this, e);
454             }
455         }
456
457         /// <summary>
458         /// Event arguments that passed via the finished signal.
459         /// </summary>
460         /// <since_tizen> 3 </since_tizen>
461         public class FinishedEventArgs : EventArgs
462         {
463             private VideoView _videoView;
464
465             /// <summary>
466             /// The view for video playback and display.
467             /// </summary>
468             /// <since_tizen> 3 </since_tizen>
469             public VideoView VideoView
470             {
471                 get
472                 {
473                     return _videoView;
474                 }
475                 set
476                 {
477                     _videoView = value;
478                 }
479             }
480         }
481
482         internal new class Property
483         {
484             internal static readonly int VIDEO = Interop.VideoView.VideoGet();
485             internal static readonly int LOOPING = Interop.VideoView.LoopingGet();
486             internal static readonly int MUTED = Interop.VideoView.MutedGet();
487             internal static readonly int VOLUME = Interop.VideoView.VolumeGet();
488             internal static readonly int UNDERLAY = Interop.VideoView.UnderlayGet();
489         }
490
491         internal System.IntPtr GetNativePlayerHandle()
492         {
493             var ret = Interop.VideoView.GetNativePlayerHandle(SwigCPtr);
494             NUILog.Debug($"NativePlayerHandle=0x{ret:X}");
495             return ret;
496         }
497     }
498
499     /// <summary>
500     /// Contains and encapsulates Native Player handle.
501     /// </summary>
502     [EditorBrowsable(EditorBrowsableState.Never)]
503     public class SafeNativePlayerHandle : SafeHandle
504     {
505         /// <summary>
506         /// Contructor, null handle is set.
507         /// </summary>
508         [EditorBrowsable(EditorBrowsableState.Never)]
509         public SafeNativePlayerHandle() : base(global::System.IntPtr.Zero, false)
510         {
511         }
512
513         /// <summary>
514         /// Contructor, Native player handle is set to handle.
515         /// </summary>
516         [EditorBrowsable(EditorBrowsableState.Never)]
517         public SafeNativePlayerHandle(VideoView videoView) : base(global::System.IntPtr.Zero, false)
518         {
519             if (videoView != null)
520             {
521                 SetHandle(videoView.GetNativePlayerHandle());
522             }
523         }
524
525         /// <summary>
526         /// Null check if the handle is valid or not.
527         /// </summary>
528         [EditorBrowsable(EditorBrowsableState.Never)]
529         public override bool IsInvalid
530         {
531             get
532             {
533                 return handle == global::System.IntPtr.Zero;
534             }
535         }
536         /// <summary>
537         /// Release handle itself.
538         /// </summary>
539         /// <returns>true when released successfully.</returns>
540         [EditorBrowsable(EditorBrowsableState.Never)]
541         protected override bool ReleaseHandle()
542         {
543             SetHandle(global::System.IntPtr.Zero);
544             return true;
545         }
546     }
547 }