[NUI] Add IsXamlUsing flag to Camera, ItemView and etc
[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 BindableProperty VideoProperty = null;
33         internal static void SetInternalVideoProperty(BindableObject bindable, object oldValue, object newValue)
34         {
35             var videoView = (VideoView)bindable;
36             if (newValue != null)
37             {
38                 Tizen.NUI.Object.SetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.VIDEO, new Tizen.NUI.PropertyValue((PropertyMap)newValue));
39             }
40         }
41         internal static object GetInternalVideoProperty(BindableObject bindable)
42         {
43             var videoView = (VideoView)bindable;
44             PropertyMap temp = new PropertyMap();
45             Tizen.NUI.Object.GetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.VIDEO).Get(temp);
46             return temp;
47         }
48
49         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
50         [EditorBrowsable(EditorBrowsableState.Never)]
51         public static BindableProperty LoopingProperty = null;
52         internal static void SetInternalLoopingProperty(BindableObject bindable, object oldValue, object newValue)
53         {
54             var videoView = (VideoView)bindable;
55             if (newValue != null)
56             {
57                 Tizen.NUI.Object.SetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.LOOPING, new Tizen.NUI.PropertyValue((bool)newValue));
58             }
59         }
60         internal static object GetInternalLoopingProperty(BindableObject bindable)
61         {
62             var videoView = (VideoView)bindable;
63             bool temp = false;
64             Tizen.NUI.Object.GetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.LOOPING).Get(out temp);
65             return temp;
66         }
67
68         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
69         [EditorBrowsable(EditorBrowsableState.Never)]
70         public static BindableProperty MutedProperty = null;
71         internal static void SetInternalMutedProperty(BindableObject bindable, object oldValue, object newValue)
72         {
73             var videoView = (VideoView)bindable;
74             if (newValue != null)
75             {
76                 Tizen.NUI.Object.SetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.MUTED, new Tizen.NUI.PropertyValue((bool)newValue));
77             }
78         }
79         internal static object GetInternalMutedProperty(BindableObject bindable)
80         {
81             var videoView = (VideoView)bindable;
82             bool temp = false;
83             Tizen.NUI.Object.GetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.MUTED).Get(out temp);
84             return temp;
85         }
86
87         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
88         [EditorBrowsable(EditorBrowsableState.Never)]
89         public static BindableProperty VolumeProperty = null;
90         internal static void SetInternalVolumeProperty(BindableObject bindable, object oldValue, object newValue)
91         {
92             var videoView = (VideoView)bindable;
93             if (newValue != null)
94             {
95                 Tizen.NUI.Object.SetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.VOLUME, new Tizen.NUI.PropertyValue((PropertyMap)newValue));
96             }
97         }
98         internal static object GetInternalVolumeProperty(BindableObject bindable)
99         {
100             var videoView = (VideoView)bindable;
101             PropertyMap temp = new PropertyMap();
102             Tizen.NUI.Object.GetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.VOLUME).Get(temp);
103             return temp;
104         }
105
106         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
107         [EditorBrowsable(EditorBrowsableState.Never)]
108         public static BindableProperty UnderlayProperty = null;
109         internal static void SetInternalUnderlayProperty(BindableObject bindable, object oldValue, object newValue)
110         {
111             var videoView = (VideoView)bindable;
112             if (newValue != null)
113             {
114                 Tizen.NUI.Object.SetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.UNDERLAY, new Tizen.NUI.PropertyValue((bool)newValue));
115             }
116         }
117         internal static object GetInternalUnderlayProperty(BindableObject bindable)
118         {
119             var videoView = (VideoView)bindable;
120             bool temp = false;
121             Tizen.NUI.Object.GetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.UNDERLAY).Get(out temp);
122             return temp;
123         }
124
125         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
126         [EditorBrowsable(EditorBrowsableState.Never)]
127         public static BindableProperty ResourceUrlProperty = null;
128         internal static void SetInternalResourceUrlProperty(BindableObject bindable, object oldValue, object newValue)
129         {
130             var videoView = (VideoView)bindable;
131             if (newValue != null)
132             {
133                 Tizen.NUI.Object.SetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.VIDEO, new Tizen.NUI.PropertyValue((string)newValue));
134             }
135         }
136         internal static object GetInternalResourceUrlProperty(BindableObject bindable)
137         {
138             var videoView = (VideoView)bindable;
139             string temp;
140             Tizen.NUI.Object.GetProperty((HandleRef)videoView.SwigCPtr, VideoView.Property.VIDEO).Get(out temp);
141             return temp;
142         }
143
144         private FinishedCallbackDelegate videoViewFinishedCallbackDelegate;
145         private EventHandler<FinishedEventArgs> videoViewFinishedEventHandler;
146
147
148         static VideoView()
149         {
150             if (NUIApplication.IsUsingXaml)
151             {
152                 VideoProperty = BindableProperty.Create(nameof(Video), typeof(PropertyMap), typeof(VideoView), null,
153                     propertyChanged: SetInternalVideoProperty, defaultValueCreator: GetInternalVideoProperty);
154
155                 LoopingProperty = BindableProperty.Create(nameof(Looping), typeof(bool), typeof(VideoView), false,
156                     propertyChanged: SetInternalLoopingProperty, defaultValueCreator: GetInternalLoopingProperty);
157
158                 MutedProperty = BindableProperty.Create(nameof(Muted), typeof(bool), typeof(VideoView), false,
159                     propertyChanged: SetInternalMutedProperty, defaultValueCreator: GetInternalMutedProperty);
160
161                 VolumeProperty = BindableProperty.Create(nameof(Volume), typeof(PropertyMap), typeof(VideoView), null,
162                     propertyChanged: SetInternalVolumeProperty, defaultValueCreator: GetInternalVolumeProperty);
163
164                 UnderlayProperty = BindableProperty.Create(nameof(Underlay), typeof(bool), typeof(VideoView), false,
165                     propertyChanged: SetInternalUnderlayProperty, defaultValueCreator: GetInternalUnderlayProperty);
166
167                 ResourceUrlProperty = BindableProperty.Create(nameof(ResourceUrl), typeof(string), typeof(VideoView), string.Empty,
168                     propertyChanged: SetInternalResourceUrlProperty, defaultValueCreator: GetInternalResourceUrlProperty);
169             }
170         }
171
172         /// <summary>
173         /// Creates an initialized VideoView.
174         /// </summary>
175         /// <since_tizen> 3 </since_tizen>
176         public VideoView() : this(Interop.VideoView.New(), true)
177         {
178             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
179         }
180
181         /// <summary>
182         /// Creates an initialized VideoView.<br />
183         /// If the string is empty, VideoView will not display anything.<br />
184         /// </summary>
185         /// <param name="url">The URL of the video resource to display.</param>
186         /// <since_tizen> 3 </since_tizen>
187         public VideoView(string url) : this(Interop.VideoView.New(url), true)
188         {
189             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
190         }
191
192         /// <summary>
193         /// Creates an initialized VideoView.<br />
194         /// If the string is empty, VideoView will not display anything.<br />
195         /// </summary>
196         /// <param name="swCodec">Video rendering by H/W codec if false.</param>
197         [EditorBrowsable(EditorBrowsableState.Never)]
198         public VideoView(bool swCodec) : this(Interop.VideoView.New(swCodec), true)
199         {
200             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
201         }
202
203         /// <summary>
204         /// Creates an initialized VideoView.<br />
205         /// If the string is empty, VideoView will not display anything.<br />
206         /// </summary>
207         /// <param name="url">The URL of the video resource to display.</param>
208         /// <param name="swCodec">Video rendering by H/W codec if false.</param>
209         [EditorBrowsable(EditorBrowsableState.Never)]
210         public VideoView(string url, bool swCodec) : this(Interop.VideoView.New(url, swCodec), true)
211         {
212             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
213         }
214
215         /// <summary>
216         /// Hidden API (Inhouse API).
217         /// Using Uri class to provide safe service and secure API.
218         /// Creates an initialized VideoView.
219         /// If the string is empty, VideoView will not display anything.
220         /// </summary>
221         /// <param name="uri">The URI of the video resource to display.</param>
222         /// <param name="swCodec">Video rendering by H/W codec if false.</param>
223         [EditorBrowsable(EditorBrowsableState.Never)]
224         public VideoView(Uri uri, bool swCodec) : this(Interop.VideoView.New((uri == null) ? String.Empty : uri.AbsoluteUri, swCodec), true)
225         {
226             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
227         }
228
229         internal VideoView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
230         {
231         }
232
233         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
234         private delegate void FinishedCallbackDelegate(IntPtr data);
235
236         /// <summary>
237         /// Event for the finished signal which can be used to subscribe or unsubscribe the event handler
238         /// The finished signal is emitted when a video playback has finished.<br />
239         /// </summary>
240         /// <since_tizen> 3 </since_tizen>
241         public event EventHandler<FinishedEventArgs> Finished
242         {
243             add
244             {
245                 if (videoViewFinishedEventHandler == null)
246                 {
247                     videoViewFinishedCallbackDelegate = (OnFinished);
248                     FinishedSignal().Connect(videoViewFinishedCallbackDelegate);
249                 }
250                 videoViewFinishedEventHandler += value;
251             }
252             remove
253             {
254                 videoViewFinishedEventHandler -= value;
255                 if (videoViewFinishedEventHandler == null && FinishedSignal().Empty() == false)
256                 {
257                     FinishedSignal().Disconnect(videoViewFinishedCallbackDelegate);
258                 }
259             }
260         }
261
262         /// <summary>
263         /// Video file setting type of PropertyMap.
264         /// </summary>
265         /// <since_tizen> 3 </since_tizen>
266         public PropertyMap Video
267         {
268             get
269             {
270                 if (NUIApplication.IsUsingXaml)
271                 {
272                     return (PropertyMap)GetValue(VideoProperty);
273                 }
274                 else
275                 {
276                     return (PropertyMap)GetInternalVideoProperty(this);
277                 }
278             }
279             set
280             {
281                 if (NUIApplication.IsUsingXaml)
282                 {
283                     SetValue(VideoProperty, value);
284                 }
285                 else
286                 {
287                     SetInternalVideoProperty(this, null, value);
288                 }
289                 NotifyPropertyChanged();
290             }
291         }
292
293         /// <summary>
294         /// The looping status, true or false.
295         /// </summary>
296         /// <since_tizen> 3 </since_tizen>
297         public bool Looping
298         {
299             get
300             {
301                 if (NUIApplication.IsUsingXaml)
302                 {
303                     return (bool)GetValue(LoopingProperty);
304                 }
305                 else
306                 {
307                     return (bool)GetInternalLoopingProperty(this);
308                 }
309             }
310             set
311             {
312                 if (NUIApplication.IsUsingXaml)
313                 {
314                     SetValue(LoopingProperty, value);
315                 }
316                 else
317                 {
318                     SetInternalLoopingProperty(this, null, value);
319                 }
320                 NotifyPropertyChanged();
321             }
322         }
323
324         /// <summary>
325         /// The mute status, true or false.
326         /// </summary>
327         /// <since_tizen> 3 </since_tizen>
328         public bool Muted
329         {
330             get
331             {
332                 if (NUIApplication.IsUsingXaml)
333                 {
334                     return (bool)GetValue(MutedProperty);
335                 }
336                 else
337                 {
338                     return (bool)GetInternalMutedProperty(this);
339                 }
340             }
341             set
342             {
343                 if (NUIApplication.IsUsingXaml)
344                 {
345                     SetValue(MutedProperty, value);
346                 }
347                 else
348                 {
349                     SetInternalMutedProperty(this, null, value);
350                 }
351                 NotifyPropertyChanged();
352             }
353         }
354
355         /// <summary>
356         /// The left and the right volume scalar as float type, PropertyMap with two values ( "left" and "right" ).
357         /// </summary>
358         /// <since_tizen> 3 </since_tizen>
359         public PropertyMap Volume
360         {
361             get
362             {
363                 if (NUIApplication.IsUsingXaml)
364                 {
365                     return (PropertyMap)GetValue(VolumeProperty);
366                 }
367                 else
368                 {
369                     return (PropertyMap)GetInternalVolumeProperty(this);
370                 }
371             }
372             set
373             {
374                 if (NUIApplication.IsUsingXaml)
375                 {
376                     SetValue(VolumeProperty, value);
377                 }
378                 else
379                 {
380                     SetInternalVolumeProperty(this, null, value);
381                 }
382                 NotifyPropertyChanged();
383             }
384         }
385
386         /// <summary>
387         /// Video rendering by underlay, true or false.<br />
388         /// This shows video composited underneath the window by the system. This means it may ignore rotation of the video-view.
389         /// </summary>
390         /// <since_tizen> 4 </since_tizen>
391         public bool Underlay
392         {
393             get
394             {
395
396                 if (NUIApplication.IsUsingXaml)
397                 {
398                     return (bool)GetValue(UnderlayProperty);
399                 }
400                 else
401                 {
402                     return (bool)GetInternalUnderlayProperty(this);
403                 }
404             }
405             set
406             {
407                 if (NUIApplication.IsUsingXaml)
408                 {
409                     SetValue(UnderlayProperty, value);
410                 }
411                 else
412                 {
413                     SetInternalUnderlayProperty(this, null, value);
414                 }
415                 NotifyPropertyChanged();
416             }
417         }
418
419         /// <summary>
420         /// Video file URL as string type.
421         /// </summary>
422         /// <since_tizen> 4 </since_tizen>
423         public string ResourceUrl
424         {
425             get
426             {
427                 if (NUIApplication.IsUsingXaml)
428                 {
429                     return (string)GetValue(ResourceUrlProperty);
430                 }
431                 else
432                 {
433                     return (string)GetInternalResourceUrlProperty(this);
434                 }
435             }
436             set
437             {
438                 if (NUIApplication.IsUsingXaml)
439                 {
440                     SetValue(ResourceUrlProperty, value);
441                 }
442                 else
443                 {
444                     SetInternalResourceUrlProperty(this, null, value);
445                 }
446                 NotifyPropertyChanged();
447             }
448         }
449
450         /// <summary>
451         /// Starts the video playback.
452         /// </summary>
453         /// <since_tizen> 3 </since_tizen>
454         public void Play()
455         {
456             Interop.VideoView.Play(SwigCPtr);
457             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
458         }
459
460         /// <summary>
461         /// Pauses the video playback.
462         /// </summary>
463         /// <since_tizen> 3 </since_tizen>
464         public void Pause()
465         {
466             Interop.VideoView.Pause(SwigCPtr);
467             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
468         }
469
470         /// <summary>
471         /// Stops the video playback.
472         /// </summary>
473         /// <since_tizen> 3 </since_tizen>
474         public void Stop()
475         {
476             Interop.VideoView.Stop(SwigCPtr);
477             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
478         }
479
480         /// <summary>
481         /// Seeks forward by the specified number of milliseconds.
482         /// </summary>
483         /// <param name="millisecond">The position for forward playback.</param>
484         /// <since_tizen> 3 </since_tizen>
485         public void Forward(int millisecond)
486         {
487             Interop.VideoView.Forward(SwigCPtr, millisecond);
488             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
489         }
490
491         /// <summary>
492         /// Seeks backward by the specified number of milliseconds.
493         /// </summary>
494         /// <param name="millisecond">The position for backward playback.</param>
495         /// <since_tizen> 3 </since_tizen>
496         public void Backward(int millisecond)
497         {
498             Interop.VideoView.Backward(SwigCPtr, millisecond);
499             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
500         }
501
502         /// <summary>
503         /// Get native player handle.
504         /// <example>
505         /// How to get native player handle
506         /// <code>
507         /// VideoView videoView = new VideoView();
508         /// videoView.ResourceUrl = "some video path";
509         /// var handle = videoView.NativeHandle;
510         /// if(handle.IsInvalid == false)
511         /// {
512         ///     IntPtr nativeHandle = handle.DangerousGetHandle();
513         ///     // do something with nativeHandle
514         /// }
515         /// </code>
516         /// </example>
517         /// </summary>
518         /// <since_tizen> 9 </since_tizen>
519         public SafeHandle NativeHandle
520         {
521             get
522             {
523                 return new NUI.SafeNativePlayerHandle(this);
524             }
525         }
526
527         internal VideoViewSignal FinishedSignal()
528         {
529             VideoViewSignal ret = new VideoViewSignal(Interop.VideoView.FinishedSignal(SwigCPtr), false);
530             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
531             return ret;
532         }
533
534         /// <summary>
535         /// Dispose.
536         /// </summary>
537         /// <param name="type">DisposeTypes</param>
538         /// <since_tizen> 3 </since_tizen>
539         protected override void Dispose(DisposeTypes type)
540         {
541             if (disposed)
542             {
543                 return;
544             }
545
546             //Release your own unmanaged resources here.
547             //You should not access any managed member here except static instance.
548             //because the execution order of Finalizes is non-deterministic.
549
550             if (this != null && videoViewFinishedCallbackDelegate != null)
551             {
552                 FinishedSignal().Disconnect(videoViewFinishedCallbackDelegate);
553             }
554
555             base.Dispose(type);
556         }
557
558         /// This will not be public opened.
559         [EditorBrowsable(EditorBrowsableState.Never)]
560         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
561         {
562             Interop.VideoView.DeleteVideoView(swigCPtr);
563         }
564
565         // Callback for VideoView Finished signal
566         private void OnFinished(IntPtr data)
567         {
568             if (videoViewFinishedEventHandler != null)
569             {
570                 FinishedEventArgs e = new FinishedEventArgs();
571
572                 // Populate all members of "e" (FinishedEventArgs) with real data
573                 e.VideoView = Registry.GetManagedBaseHandleFromNativePtr(data) as VideoView;
574                 //here we send all data to user event handlers
575                 videoViewFinishedEventHandler(this, e);
576             }
577         }
578
579         /// <summary>
580         /// Event arguments that passed via the finished signal.
581         /// </summary>
582         /// <since_tizen> 3 </since_tizen>
583         public class FinishedEventArgs : EventArgs
584         {
585             private VideoView videoView;
586
587             /// <summary>
588             /// The view for video playback and display.
589             /// </summary>
590             /// <since_tizen> 3 </since_tizen>
591             public VideoView VideoView
592             {
593                 get
594                 {
595                     return videoView;
596                 }
597                 set
598                 {
599                     videoView = value;
600                 }
601             }
602         }
603
604         internal new class Property
605         {
606             internal static readonly int VIDEO = Interop.VideoView.VideoGet();
607             internal static readonly int LOOPING = Interop.VideoView.LoopingGet();
608             internal static readonly int MUTED = Interop.VideoView.MutedGet();
609             internal static readonly int VOLUME = Interop.VideoView.VolumeGet();
610             internal static readonly int UNDERLAY = Interop.VideoView.UnderlayGet();
611         }
612
613         internal System.IntPtr GetNativePlayerHandle()
614         {
615             var ret = Interop.VideoView.GetNativePlayerHandle(SwigCPtr);
616             NUILog.Debug($"NativePlayerHandle=0x{ret:X}");
617             return ret;
618         }
619     }
620
621     /// <summary>
622     /// Contains and encapsulates Native Player handle.
623     /// </summary>
624     [EditorBrowsable(EditorBrowsableState.Never)]
625     [Obsolete("This has been deprecated in API9 and will be removed in API11. Use VideoView.NativeHandle instead.")]
626     public class SafeNativePlayerHandle : SafeHandle
627     {
628         /// <summary>
629         /// Constructor, null handle is set.
630         /// </summary>
631         [EditorBrowsable(EditorBrowsableState.Never)]
632         public SafeNativePlayerHandle() : base(global::System.IntPtr.Zero, false)
633         {
634         }
635
636         /// <summary>
637         /// Constructor, Native player handle is set to handle.
638         /// </summary>
639         [EditorBrowsable(EditorBrowsableState.Never)]
640         public SafeNativePlayerHandle(VideoView videoView) : base(global::System.IntPtr.Zero, false)
641         {
642             if (videoView != null)
643             {
644                 SetHandle(videoView.GetNativePlayerHandle());
645             }
646         }
647
648         /// <summary>
649         /// Null check if the handle is valid or not.
650         /// </summary>
651         [EditorBrowsable(EditorBrowsableState.Never)]
652         public override bool IsInvalid
653         {
654             get
655             {
656                 return handle == global::System.IntPtr.Zero;
657             }
658         }
659         /// <summary>
660         /// Release handle itself.
661         /// </summary>
662         /// <returns>true when released successfully.</returns>
663         [EditorBrowsable(EditorBrowsableState.Never)]
664         protected override bool ReleaseHandle()
665         {
666             SetHandle(global::System.IntPtr.Zero);
667             return true;
668         }
669     }
670 }