feat(Windows): Adds Windows support to react-native-video

This PR adds react-native-windows support to react-native-video.  The Video component is implemented with a XAML MediaPlayerElement.  Most of the features implemented by Android (and some additional ones) are implemented by Windows.

Known issues and missing features include:
* onReadyForDisplay event
* local URI convention (e.g., "broadchurch" in examples changed to require("./broadchurch.mp4")
* `playableDuration` in `onVideoProgress` event is always 0.0
* `playInBackground` is not yet supported
* Volume settings are applied, but the UWP control does not handle it properly
This commit is contained in:
Eric Rozell
2016-11-09 11:31:42 -08:00
parent 605f4cf070
commit 8cc1dbda4f
32 changed files with 1895 additions and 2 deletions

View File

@@ -0,0 +1,29 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ReactNativeVideo")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ReactNativeVideo")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: ComVisible(false)]

View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This file contains Runtime Directives, specifications about types your application accesses
through reflection and other dynamic code patterns. Runtime Directives are used to control the
.NET Native optimizer and ensure that it does not remove code accessed by your library. If your
library does not do any reflection, then you generally do not need to edit this file. However,
if your library reflects over types, especially types passed to it or derived from its types,
then you should write Runtime Directives.
The most common use of reflection in libraries is to discover information about types passed
to the library. Runtime Directives have three ways to express requirements on types passed to
your library.
1. Parameter, GenericParameter, TypeParameter, TypeEnumerableParameter
Use these directives to reflect over types passed as a parameter.
2. SubTypes
Use a SubTypes directive to reflect over types derived from another type.
3. AttributeImplies
Use an AttributeImplies directive to indicate that your library needs to reflect over
types or methods decorated with an attribute.
For more information on writing Runtime Directives for libraries, please visit
http://go.microsoft.com/fwlink/?LinkID=391919
-->
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Library Name="ReactNativeVideo">
<!-- add directives for your library here -->
</Library>
</Directives>

View File

@@ -0,0 +1,116 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{E8F5F57F-757E-4237-AD23-F7A8755427CD}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ReactNativeVideo</RootNamespace>
<AssemblyName>ReactNativeVideo</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion>10.0.14393.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.10586.0</TargetPlatformMinVersion>
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<PlatformTarget>x86</PlatformTarget>
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM'">
<PlatformTarget>ARM</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\ARM\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>ARM</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|ARM'">
<PlatformTarget>ARM</PlatformTarget>
<OutputPath>bin\ARM\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>ARM</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<PlatformTarget>x64</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<PlatformTarget>x64</PlatformTarget>
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<!-- A reference to the entire .Net Framework and Windows SDK are automatically included -->
<None Include="project.json" />
</ItemGroup>
<ItemGroup>
<Compile Include="ReactVideoPackage.cs" />
<Compile Include="ReactVideoView.cs" />
<Compile Include="ReactVideoViewManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReactVideoEventType.cs" />
<Compile Include="ReactVideoEventTypeExtensions.cs" />
<EmbeddedResource Include="Properties\ReactNativeVideo.rd.xml" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\example\node_modules\react-native-windows\ReactWindows\ReactNative\ReactNative.csproj">
<Project>{c7673ad5-e3aa-468c-a5fd-fa38154e205c}</Project>
<Name>ReactNative</Name>
</ProjectReference>
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
<VisualStudioVersion>14.0</VisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -0,0 +1,15 @@
namespace ReactNativeVideo
{
enum ReactVideoEventType
{
LoadStart,
Load,
Error,
Progress,
Seek,
End,
Stalled,
Resume,
ReadyForDisplay,
}
}

View File

@@ -0,0 +1,36 @@
using System;
using static System.FormattableString;
namespace ReactNativeVideo
{
static class ReactVideoEventTypeExtensions
{
public static string GetEventName(this ReactVideoEventType eventType)
{
switch (eventType)
{
case ReactVideoEventType.LoadStart:
return "onVideoLoadStart";
case ReactVideoEventType.Load:
return "onVideoLoad";
case ReactVideoEventType.Error:
return "onVideoError";
case ReactVideoEventType.Progress:
return "onVideoProgress";
case ReactVideoEventType.Seek:
return "onVideoSeek";
case ReactVideoEventType.End:
return "onVideoEnd";
case ReactVideoEventType.Stalled:
return "onPlaybackStalled";
case ReactVideoEventType.Resume:
return "onPlaybackResume";
case ReactVideoEventType.ReadyForDisplay:
return "onReadyForDisplay";
default:
throw new NotSupportedException(
Invariant($"No event name added for event type '{eventType}'."));
}
}
}
}

View File

@@ -0,0 +1,30 @@
using ReactNative.Bridge;
using ReactNative.Modules.Core;
using ReactNative.UIManager;
using System;
using System.Collections.Generic;
namespace ReactNativeVideo
{
public class ReactVideoPackage : IReactPackage
{
public IReadOnlyList<Type> CreateJavaScriptModulesConfig()
{
return Array.Empty<Type>();
}
public IReadOnlyList<INativeModule> CreateNativeModules(ReactContext reactContext)
{
return Array.Empty<INativeModule>();
}
public IReadOnlyList<IViewManager> CreateViewManagers(ReactContext reactContext)
{
return new List<IViewManager>
{
new ReactVideoViewManager(),
};
}
}
}

View File

@@ -0,0 +1,355 @@
using Newtonsoft.Json.Linq;
using ReactNative.UIManager;
using ReactNative.UIManager.Events;
using System;
using Windows.ApplicationModel.Core;
using Windows.Media.Core;
using Windows.Media.Playback;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace ReactNativeVideo
{
class ReactVideoView : MediaPlayerElement, IDisposable
{
public const string EVENT_PROP_SEEK_TIME = "seekTime";
private readonly DispatcherTimer _timer;
private bool _isSourceSet;
private string _uri;
private bool _isLoopingEnabled;
private bool _isPaused;
private bool _isMuted;
private bool _isUserControlEnabled;
private bool _isCompleted;
private double _volume;
private double _rate;
public ReactVideoView()
{
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(250.0);
_timer.Start();
}
public new string Source
{
set
{
_uri = value;
base.Source = MediaSource.CreateFromUri(new Uri(_uri));
_isSourceSet = true;
ApplyModifiers();
SubscribeEvents();
}
}
public bool IsLoopingEnabled
{
set
{
_isLoopingEnabled = value;
if (_isSourceSet)
{
MediaPlayer.IsLoopingEnabled = _isLoopingEnabled;
}
}
}
public bool IsMuted
{
set
{
_isMuted = value;
if (_isSourceSet)
{
MediaPlayer.IsMuted = _isMuted;
}
}
}
public bool IsPaused
{
set
{
_isPaused = value;
if (_isSourceSet)
{
if (_isPaused)
{
MediaPlayer.Pause();
}
else
{
MediaPlayer.Play();
}
}
}
}
public bool IsUserControlEnabled
{
set
{
_isUserControlEnabled = value;
if (_isSourceSet)
{
MediaPlayer.SystemMediaTransportControls.IsEnabled = _isUserControlEnabled;
}
}
}
public double Volume
{
set
{
_volume = value;
if (_isSourceSet)
{
MediaPlayer.Volume = _volume;
}
}
}
public double Rate
{
set
{
_rate = value;
if (_isSourceSet)
{
MediaPlayer.PlaybackSession.PlaybackRate = _rate;
}
}
}
public double ProgressUpdateInterval
{
set
{
_timer.Interval = TimeSpan.FromSeconds(value);
}
}
public void Seek(double seek)
{
if (_isSourceSet)
{
MediaPlayer.PlaybackSession.Position = TimeSpan.FromSeconds(seek);
}
}
public void Dispose()
{
if (_isSourceSet)
{
_timer.Tick -= OnTick;
MediaPlayer.SourceChanged -= OnSourceChanged;
MediaPlayer.MediaOpened -= OnMediaOpened;
MediaPlayer.MediaFailed -= OnMediaFailed;
MediaPlayer.MediaEnded -= OnMediaEnded;
MediaPlayer.PlaybackSession.BufferingStarted -= OnBufferingStarted;
MediaPlayer.PlaybackSession.BufferingEnded -= OnBufferingEnded;
MediaPlayer.PlaybackSession.SeekCompleted -= OnSeekCompleted;
}
_timer.Stop();
}
private void ApplyModifiers()
{
IsLoopingEnabled = _isLoopingEnabled;
IsMuted = _isMuted;
IsPaused = _isPaused;
IsUserControlEnabled = _isUserControlEnabled;
Volume = _volume;
Rate = _rate;
}
private void SubscribeEvents()
{
_timer.Tick += OnTick;
MediaPlayer.SourceChanged += OnSourceChanged;
MediaPlayer.MediaOpened += OnMediaOpened;
MediaPlayer.MediaFailed += OnMediaFailed;
MediaPlayer.MediaEnded += OnMediaEnded;
MediaPlayer.PlaybackSession.BufferingStarted += OnBufferingStarted;
MediaPlayer.PlaybackSession.BufferingEnded += OnBufferingEnded;
MediaPlayer.PlaybackSession.SeekCompleted += OnSeekCompleted;
}
private void OnTick(object sender, object e)
{
if (_isSourceSet && !_isCompleted && !_isPaused)
{
this.GetReactContext()
.GetNativeModule<UIManagerModule>()
.EventDispatcher
.DispatchEvent(
new ReactVideoEvent(
ReactVideoEventType.Progress.GetEventName(),
this.GetTag(),
new JObject
{
{ "currentTime", MediaPlayer.PlaybackSession.Position.TotalSeconds },
{ "playableDuration", 0.0 /* TODO */ }
}));
}
}
private void OnSourceChanged(MediaPlayer sender, object args)
{
this.GetReactContext()
.GetNativeModule<UIManagerModule>()
.EventDispatcher
.DispatchEvent(
new ReactVideoEvent(
ReactVideoEventType.LoadStart.GetEventName(),
this.GetTag(),
new JObject
{
{ "src", this._uri }
}));
}
private void OnMediaOpened(MediaPlayer sender, object args)
{
RunOnDispatcher(delegate
{
var width = MediaPlayer.PlaybackSession.NaturalVideoWidth;
var height = MediaPlayer.PlaybackSession.NaturalVideoHeight;
var orientation = (width > height) ? "landscape" : "portrait";
var size = new JObject
{
{ "width", width },
{ "height", height },
{ "orientation", orientation }
};
this.GetReactContext().GetNativeModule<UIManagerModule>().EventDispatcher.DispatchEvent(new ReactVideoView.ReactVideoEvent(ReactVideoEventType.Load.GetEventName(), this.GetTag(), new JObject
{
{ "duration", MediaPlayer.PlaybackSession.NaturalDuration.TotalSeconds },
{ "currentTime", MediaPlayer.PlaybackSession.Position.TotalSeconds },
{ "naturalSize", size },
{ "canPlayFastForward", false },
{ "canPlaySlowForward", false },
{ "canPlaySlow", false },
{ "canPlayReverse", false },
{ "canStepBackward", false },
{ "canStepForward", false }
}));
});
}
private void OnMediaFailed(MediaPlayer sender, MediaPlayerFailedEventArgs args)
{
var errorData = new JObject
{
{ "what", args.Error.ToString() },
{ "extra", args.ErrorMessage }
};
this.GetReactContext()
.GetNativeModule<UIManagerModule>()
.EventDispatcher
.DispatchEvent(
new ReactVideoEvent(
ReactVideoEventType.Error.GetEventName(),
this.GetTag(),
new JObject
{
{ "error", errorData }
}));
}
private void OnMediaEnded(MediaPlayer sender, object args)
{
_isCompleted = true;
this.GetReactContext()
.GetNativeModule<UIManagerModule>()
.EventDispatcher
.DispatchEvent(
new ReactVideoEvent(
ReactVideoEventType.End.GetEventName(),
this.GetTag(),
null));
}
private void OnBufferingStarted(MediaPlaybackSession sender, object args)
{
this.GetReactContext()
.GetNativeModule<UIManagerModule>()
.EventDispatcher
.DispatchEvent(
new ReactVideoEvent(
ReactVideoEventType.Stalled.GetEventName(),
this.GetTag(),
new JObject()));
}
private void OnBufferingEnded(MediaPlaybackSession sender, object args)
{
this.GetReactContext()
.GetNativeModule<UIManagerModule>()
.EventDispatcher
.DispatchEvent(
new ReactVideoEvent(
ReactVideoEventType.Resume.GetEventName(),
this.GetTag(),
new JObject()));
}
private void OnSeekCompleted(MediaPlaybackSession sender, object args)
{
this.GetReactContext()
.GetNativeModule<UIManagerModule>()
.EventDispatcher.DispatchEvent(
new ReactVideoEvent(
ReactVideoEventType.Seek.GetEventName(),
this.GetTag(),
new JObject()));
}
private static async void RunOnDispatcher(DispatchedHandler action)
{
await CoreApplication.GetCurrentView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, action).AsTask().ConfigureAwait(false);
}
class ReactVideoEvent : Event
{
private readonly string _eventName;
private readonly JObject _eventData;
public ReactVideoEvent(string eventName, int viewTag, JObject eventData)
: base(viewTag, TimeSpan.FromTicks(Environment.TickCount))
{
_eventName = eventName;
_eventData = eventData;
}
public override string EventName
{
get
{
return _eventName;
}
}
public override bool CanCoalesce
{
get
{
return false;
}
}
public override void Dispatch(RCTEventEmitter eventEmitter)
{
eventEmitter.receiveEvent(ViewTag, EventName, _eventData);
}
}
}
}

View File

@@ -0,0 +1,137 @@
using Newtonsoft.Json.Linq;
using ReactNative.UIManager;
using ReactNative.UIManager.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
namespace ReactNativeVideo
{
class ReactVideoViewManager : SimpleViewManager<ReactVideoView>
{
public override string Name
{
get
{
return "RCTVideo";
}
}
public override IReadOnlyDictionary<string, object> ExportedViewConstants
{
get
{
return new Dictionary<string, object>
{
{ "ScaleNone", ((int)Stretch.None).ToString() },
{ "ScaleToFill", ((int)Stretch.UniformToFill).ToString() },
{ "ScaleAspectFit", ((int)Stretch.Uniform).ToString() },
{ "ScaleAspectFill", ((int)Stretch.Fill).ToString() },
};
}
}
public override IReadOnlyDictionary<string, object> ExportedCustomDirectEventTypeConstants
{
get
{
var events = new Dictionary<string, object>();
var eventTypes = Enum.GetValues(typeof(ReactVideoEventType)).OfType<ReactVideoEventType>();
foreach (var eventType in eventTypes)
{
events.Add(eventType.GetEventName(), new Dictionary<string, object>
{
{ "registrationName", eventType.GetEventName() },
});
}
return events;
}
}
[ReactProp("src")]
public void SetSource(ReactVideoView view, JObject source)
{
view.Source = source.Value<string>("uri");
}
[ReactProp("resizeMode")]
public void SetResizeMode(ReactVideoView view, string resizeMode)
{
view.Stretch = (Stretch)int.Parse(resizeMode);
}
[ReactProp("repeat")]
public void SetRepeat(ReactVideoView view, bool repeat)
{
view.IsLoopingEnabled = repeat;
}
[ReactProp("paused")]
public void SetPaused(ReactVideoView view, bool paused)
{
view.IsPaused = paused;
}
[ReactProp("muted")]
public void SetMuted(ReactVideoView view, bool muted)
{
view.IsMuted = muted;
}
[ReactProp("volume", DefaultDouble = 1.0)]
public void SetVolume(ReactVideoView view, double volume)
{
view.Volume = volume;
}
[ReactProp("seek")]
public void SetSeek(ReactVideoView view, double? seek)
{
if (seek.HasValue)
{
view.Seek(seek.Value);
}
}
[ReactProp("rate", DefaultDouble = 1.0)]
public void SetPlaybackRate(ReactVideoView view, double rate)
{
view.Rate = rate;
}
[ReactProp("playInBackground")]
public void SetPlayInBackground(ReactVideoView view, bool playInBackground)
{
throw new NotImplementedException("Play in background has not been implemented on Windows.");
}
[ReactProp("controls")]
public void SetControls(ReactVideoView view, bool controls)
{
view.IsUserControlEnabled = controls;
}
[ReactProp("progressUpdateInterval")]
public void SetProgressUpdateInterval(ReactVideoView view, double progressUpdateInterval)
{
view.ProgressUpdateInterval = progressUpdateInterval;
}
public override void OnDropViewInstance(ThemedReactContext reactContext, ReactVideoView view)
{
base.OnDropViewInstance(reactContext, view);
view.Dispose();
}
protected override ReactVideoView CreateViewInstance(ThemedReactContext reactContext)
{
return new ReactVideoView
{
HorizontalAlignment = HorizontalAlignment.Stretch,
};
}
}
}

View File

@@ -0,0 +1,17 @@
{
"dependencies": {
"Microsoft.NETCore.UniversalWindowsPlatform": "5.2.2",
"Newtonsoft.Json": "9.0.1"
},
"frameworks": {
"uap10.0": {}
},
"runtimes": {
"win10-arm": {},
"win10-arm-aot": {},
"win10-x86": {},
"win10-x86-aot": {},
"win10-x64": {},
"win10-x64-aot": {}
}
}