Commit 034d6430 authored by Ghislain MARY's avatar Ghislain MARY

Handle all the video methods of MS2Tester in background task for Windows 10.

parent 418980ba
......@@ -10,11 +10,60 @@ namespace ms2_tester.Helpers
{
class MS2TesterHelper
{
public static async Task<OperationResult> StartVideoStreamAsync(String camera, String codec, String videoSize, UInt32 frameRate, UInt32 bitRate)
public static async Task<OperationResult> InitVideo()
{
AppServiceHelper appServiceHelper = new AppServiceHelper();
ValueSet message = new ValueSet();
message[BackgroundOperation.NewBackgroundRequest] = (int)BackgroundRequest.InitVideo;
ValueSet response = await appServiceHelper.SendMessageAsync(message);
if (response != null)
{
return ((OperationResult)(response[BackgroundOperation.Result]));
}
return OperationResult.Failed;
}
public static async Task<OperationResult> UninitVideo()
{
AppServiceHelper appServiceHelper = new AppServiceHelper();
ValueSet message = new ValueSet();
message[BackgroundOperation.NewBackgroundRequest] = (int)BackgroundRequest.UninitVideo;
ValueSet response = await appServiceHelper.SendMessageAsync(message);
if (response != null)
{
return ((OperationResult)(response[BackgroundOperation.Result]));
}
return OperationResult.Failed;
}
public static async Task<List<String>> GetVideoDevices()
{
AppServiceHelper appServiceHelper = new AppServiceHelper();
ValueSet message = new ValueSet();
message[BackgroundOperation.NewBackgroundRequest] = (int)BackgroundRequest.GetVideoDevices;
ValueSet response = await appServiceHelper.SendMessageAsync(message);
if ((response != null) && ((OperationResult)(response[BackgroundOperation.Result]) == OperationResult.Succeeded))
{
return ((IEnumerable<String>)response[BackgroundOperation.ReturnValue]).ToList<String>();
}
return null;
}
public static async Task<OperationResult> StartVideoStream(String swapChainPanelName, String camera, String codec, String videoSize, UInt32 frameRate, UInt32 bitRate)
{
AppServiceHelper appServiceHelper = new AppServiceHelper();
ValueSet message = new ValueSet();
message[StartVideoStreamArguments.SwapChainPanelName.ToString()] = swapChainPanelName;
message[StartVideoStreamArguments.Camera.ToString()] = camera;
message[StartVideoStreamArguments.Codec.ToString()] = codec;
message[StartVideoStreamArguments.VideoSize.ToString()] = videoSize;
......@@ -30,5 +79,71 @@ namespace ms2_tester.Helpers
return OperationResult.Failed;
}
public static async Task<OperationResult> StopVideoStream()
{
AppServiceHelper appServiceHelper = new AppServiceHelper();
ValueSet message = new ValueSet();
message[BackgroundOperation.NewBackgroundRequest] = (int)BackgroundRequest.StopVideoStream;
ValueSet response = await appServiceHelper.SendMessageAsync(message);
if (response != null)
{
return ((OperationResult)(response[BackgroundOperation.Result]));
}
return OperationResult.Failed;
}
public static async Task<OperationResult> ChangeCamera(String camera)
{
AppServiceHelper appServiceHelper = new AppServiceHelper();
ValueSet message = new ValueSet();
message[ChangeCameraArguments.Camera.ToString()] = camera;
message[BackgroundOperation.NewBackgroundRequest] = (int)BackgroundRequest.ChangeCamera;
ValueSet response = await appServiceHelper.SendMessageAsync(message);
if (response != null)
{
return ((OperationResult)(response[BackgroundOperation.Result]));
}
return OperationResult.Failed;
}
public static async Task<int> GetOrientation()
{
AppServiceHelper appServiceHelper = new AppServiceHelper();
ValueSet message = new ValueSet();
message[BackgroundOperation.NewBackgroundRequest] = (int)BackgroundRequest.GetOrientation;
ValueSet response = await appServiceHelper.SendMessageAsync(message);
if ((response != null) && ((OperationResult)(response[BackgroundOperation.Result]) == OperationResult.Succeeded))
{
return Convert.ToInt32(response[BackgroundOperation.ReturnValue]);
}
return 0;
}
public static async Task<OperationResult> SetOrientation(int degrees)
{
AppServiceHelper appServiceHelper = new AppServiceHelper();
ValueSet message = new ValueSet();
message[SetOrientationArguments.Degrees.ToString()] = degrees;
message[BackgroundOperation.NewBackgroundRequest] = (int)BackgroundRequest.SetOrientation;
ValueSet response = await appServiceHelper.SendMessageAsync(message);
if (response != null)
{
return ((OperationResult)(response[BackgroundOperation.Result]));
}
return OperationResult.Failed;
}
}
}
......@@ -24,39 +24,95 @@ namespace MS2TesterTasks
connection.RequestReceived += Connection_RequestReceived;
}
private void Connection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
private async void Connection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
var deferral = args.GetDeferral();
var response = new ValueSet();
response[BackgroundOperation.Result] = (int)OperationResult.Failed;
bool stop = false;
try
{
var request = args.Request;
var message = request.Message;
var message = args.Request.Message;
if (message.ContainsKey(BackgroundOperation.NewBackgroundRequest))
{
switch ((BackgroundRequest)message[BackgroundOperation.NewBackgroundRequest])
{
case BackgroundRequest.InitVideo:
CurrentOperation.AppRequest = args.Request;
CurrentOperation.Request = BackgroundRequest.InitVideo;
CurrentOperation.AppRequestDeferral = deferral;
CurrentOperation.InitVideo();
response[BackgroundOperation.Result] = (int)OperationResult.Succeeded;
break;
case BackgroundRequest.UninitVideo:
CurrentOperation.AppRequest = args.Request;
CurrentOperation.Request = BackgroundRequest.UninitVideo;
CurrentOperation.AppRequestDeferral = deferral;
CurrentOperation.UninitVideo();
response[BackgroundOperation.Result] = (int)OperationResult.Succeeded;
break;
case BackgroundRequest.GetVideoDevices:
CurrentOperation.AppRequest = args.Request;
CurrentOperation.Request = BackgroundRequest.GetVideoDevices;
CurrentOperation.AppRequestDeferral = deferral;
response[BackgroundOperation.ReturnValue] = CurrentOperation.GetVideoDevices().ToArray();
response[BackgroundOperation.Result] = (int)OperationResult.Succeeded;
break;
case BackgroundRequest.StartVideoStream:
CurrentOperation.AppRequest = args.Request;
CurrentOperation.Request = BackgroundRequest.StartVideoStream;
CurrentOperation.AppRequestDeferal = deferral;
CurrentOperation.AppRequestDeferral = deferral;
CurrentOperation.StartVideoStream(
message[StartVideoStreamArguments.SwapChainPanelName.ToString()] as String,
message[StartVideoStreamArguments.Camera.ToString()] as String,
message[StartVideoStreamArguments.Codec.ToString()] as String,
message[StartVideoStreamArguments.VideoSize.ToString()] as String,
Convert.ToUInt32(message[StartVideoStreamArguments.FrameRate.ToString()]),
Convert.ToUInt32(message[StartVideoStreamArguments.BitRate.ToString()]));
response[BackgroundOperation.Result] = (int)OperationResult.Succeeded;
break;
case BackgroundRequest.StopVideoStream:
CurrentOperation.AppRequest = args.Request;
CurrentOperation.Request = BackgroundRequest.StopVideoStream;
CurrentOperation.AppRequestDeferral = deferral;
CurrentOperation.StopVideoStream();
response[BackgroundOperation.Result] = (int)OperationResult.Succeeded;
break;
case BackgroundRequest.ChangeCamera:
CurrentOperation.AppRequest = args.Request;
CurrentOperation.Request = BackgroundRequest.ChangeCamera;
CurrentOperation.AppRequestDeferral = deferral;
CurrentOperation.ChangeCamera(message[ChangeCameraArguments.Camera.ToString()] as String);
response[BackgroundOperation.Result] = (int)OperationResult.Succeeded;
break;
case BackgroundRequest.GetOrientation:
CurrentOperation.AppRequest = args.Request;
CurrentOperation.Request = BackgroundRequest.GetOrientation;
CurrentOperation.AppRequestDeferral = deferral;
response[BackgroundOperation.ReturnValue] = CurrentOperation.GetOrientation();
response[BackgroundOperation.Result] = (int)OperationResult.Succeeded;
break;
case BackgroundRequest.SetOrientation:
CurrentOperation.AppRequest = args.Request;
CurrentOperation.Request = BackgroundRequest.SetOrientation;
CurrentOperation.AppRequestDeferral = deferral;
CurrentOperation.SetOrientation(Convert.ToInt32(message[SetOrientationArguments.Degrees.ToString()]));
response[BackgroundOperation.Result] = (int)OperationResult.Succeeded;
break;
default:
stop = true;
break;
}
}
}
finally
{
if (stop)
{
_deferral.Complete();
} else
{
await args.Request.SendResponseAsync(response);
}
}
}
......
......@@ -8,7 +8,14 @@ namespace MS2TesterTasks
{
public enum BackgroundRequest
{
InitVideo,
UninitVideo,
GetVideoDevices,
StartVideoStream,
StopVideoStream,
ChangeCamera,
GetOrientation,
SetOrientation,
// Always keep this as the last option
InValid
......@@ -16,6 +23,7 @@ namespace MS2TesterTasks
public enum StartVideoStreamArguments
{
SwapChainPanelName,
Camera,
Codec,
VideoSize,
......@@ -23,6 +31,16 @@ namespace MS2TesterTasks
BitRate
}
public enum ChangeCameraArguments
{
Camera
}
public enum SetOrientationArguments
{
Degrees
}
public enum OperationResult
{
Succeeded,
......@@ -46,8 +64,14 @@ namespace MS2TesterTasks
get { return _result; }
}
public static String ReturnValue
{
get { return _returnValue; }
}
const String _appServiceName = "MS2TesterTasks.AppService";
const String _newBackgroundRequest = "NewBackgroundRequest";
const String _result = "Result";
const String _returnValue = "ReturnValue";
}
}
......@@ -45,7 +45,7 @@ namespace MS2TesterTasks
}
}
public static AppServiceDeferral AppRequestDeferal
public static AppServiceDeferral AppRequestDeferral
{
set
{
......@@ -64,11 +64,67 @@ namespace MS2TesterTasks
}
public static void StartVideoStream(String camera, String codec, String videoSize, UInt32 frameRate, UInt32 bitRate)
public static void InitVideo()
{
lock (_lock)
{
ms2_tester_runtime_component.MS2Tester.Instance.startVideoStream(null, null, camera, codec, videoSize, frameRate, bitRate);
ms2_tester_runtime_component.MS2Tester.Instance.initVideo();
}
}
public static void UninitVideo()
{
lock (_lock)
{
ms2_tester_runtime_component.MS2Tester.Instance.uninitVideo();
}
}
public static List<String> GetVideoDevices()
{
lock (_lock)
{
return ms2_tester_runtime_component.MS2Tester.Instance.VideoDevices.ToList<String>();
}
}
public static void StartVideoStream(String swapChainPanelName, String camera, String codec, String videoSize, UInt32 frameRate, UInt32 bitRate)
{
lock (_lock)
{
ms2_tester_runtime_component.MS2Tester.Instance.startVideoStream(swapChainPanelName, camera, codec, videoSize, frameRate, bitRate);
}
}
public static void StopVideoStream()
{
lock (_lock)
{
ms2_tester_runtime_component.MS2Tester.Instance.stopVideoStream();
}
}
public static void ChangeCamera(String camera)
{
lock (_lock)
{
ms2_tester_runtime_component.MS2Tester.Instance.changeCamera(camera);
}
}
public static int GetOrientation()
{
lock (_lock)
{
return ms2_tester_runtime_component.MS2Tester.Instance.getOrientation();
}
}
public static void SetOrientation(int degrees)
{
lock (_lock)
{
ms2_tester_runtime_component.MS2Tester.Instance.setOrientation(degrees);
}
}
......
......@@ -5,7 +5,7 @@
xmlns:local="using:ms2_tester"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Loaded="Page_Loaded" Unloaded="Page_Unloaded">
mc:Ignorable="d">
<Page.BottomAppBar>
<CommandBar x:Name="CommandBar">
......
......@@ -7,6 +7,7 @@ using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Devices.Sensors;
using Windows.Foundation;
using Windows.Foundation.Collections;
......@@ -32,16 +33,8 @@ namespace ms2_tester
public VideoPage()
{
this.InitializeComponent();
MS2Tester.Instance.initVideo();
bool isSelected = true;
foreach (String device in MS2Tester.Instance.VideoDevices)
{
ComboBoxItem item = new ComboBoxItem();
item.Content = device;
item.IsSelected = isSelected;
isSelected = false;
CameraComboBox.Items.Add(item);
}
InitVideo();
FillCameraComboBox();
}
override protected void OnNavigatedTo(Windows.UI.Xaml.Navigation.NavigationEventArgs e)
......@@ -69,17 +62,6 @@ namespace ms2_tester
Window.Current.SizeChanged -= Current_SizeChanged;
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
_source = new libmswinrtvid.SwapChainPanelSource();
_source.Start(VideoSwapChainPanel);
}
private void Page_Unloaded(object sender, RoutedEventArgs e)
{
_source.Stop();
}
private async void OrientationSensor_OrientationChanged(SimpleOrientationSensor sender, SimpleOrientationSensorOrientationChangedEventArgs args)
{
// Keep previous orientation when the user puts its device faceup or facedown
......@@ -98,7 +80,7 @@ namespace ms2_tester
private void BackButton_Click(object sender, RoutedEventArgs e)
{
MS2Tester.Instance.uninitVideo();
UninitVideo();
((Frame)Window.Current.Content).GoBack();
}
......@@ -155,15 +137,96 @@ namespace ms2_tester
}
else
{
MS2Tester.Instance.stopVideoStream();
if (_source != null)
{
_source.Stop();
}
StopVideoStream();
}
}
private async void FillCameraComboBox()
{
bool isSelected = true;
List<String> videoDevices = await GetVideoDevices();
foreach (String device in videoDevices)
{
ComboBoxItem item = new ComboBoxItem();
item.Content = device;
item.IsSelected = isSelected;
isSelected = false;
CameraComboBox.Items.Add(item);
}
}
private async void InitVideo()
{
try
{
OperationResult result = await MS2TesterHelper.InitVideo();
if (result == OperationResult.Succeeded)
{
Debug.WriteLine("InitVideo: success");
} else
{
Debug.WriteLine("InitVideo: failure");
}
}
catch (Exception e)
{
Debug.WriteLine(String.Format("InitVideo: Exception {0}", e.Message));
}
}
private async void UninitVideo()
{
try
{
OperationResult result = await MS2TesterHelper.UninitVideo();
if (result == OperationResult.Succeeded)
{
Debug.WriteLine("UninitVideo: success");
}
else
{
Debug.WriteLine("UninitVideo: failure");
}
}
catch (Exception e)
{
Debug.WriteLine(String.Format("UninitVideo: Exception {0}", e.Message));
}
}
private async Task<List<String>> GetVideoDevices()
{
List<String> result = null;
try
{
result = await MS2TesterHelper.GetVideoDevices();
if (result != null)
{
Debug.WriteLine("GetVideoDevices: success");
}
else
{
Debug.WriteLine("GetVideoDevices: failure");
}
}
catch (Exception e)
{
Debug.WriteLine(String.Format("GetVideoDevices: Exception {0}", e.Message));
}
return result;
}
private async void StartVideoStream(String camera, String codec, String videoSize, UInt32 frameRate, UInt32 bitRate)
{
try
{
OperationResult result = await MS2TesterHelper.StartVideoStreamAsync(camera, codec, videoSize, frameRate, bitRate);
_source = new libmswinrtvid.SwapChainPanelSource();
_source.Start(VideoSwapChainPanel);
OperationResult result = await MS2TesterHelper.StartVideoStream(VideoSwapChainPanel.Name, camera, codec, videoSize, frameRate, bitRate);
if (result == OperationResult.Succeeded)
{
Debug.WriteLine("StartVideoStream: success");
......@@ -173,22 +236,93 @@ namespace ms2_tester
Debug.WriteLine("StartVideoStream: failure");
}
}
catch (Exception /*e*/)
catch (Exception e)
{
Debug.WriteLine(String.Format("StartVideoStream: Exception {0}", e.Message));
}
}
private async void StopVideoStream()
{
try
{
OperationResult result = await MS2TesterHelper.StopVideoStream();
if (result == OperationResult.Succeeded)
{
Debug.WriteLine("StopVideoStream: success");
}
else
{
Debug.WriteLine("StopVideoStream: failure");
}
}
catch (Exception e)
{
Debug.WriteLine(String.Format("StopVideoStream: Exception {0}", e.Message));
}
}
private async void ChangeCamera(String camera)
{
try
{
OperationResult result = await MS2TesterHelper.ChangeCamera(camera);
if (result == OperationResult.Succeeded)
{
Debug.WriteLine("ChangeCamera: success");
}
else
{
Debug.WriteLine("ChangeCamera: failure");
}
}
catch (Exception e)
{
Debug.WriteLine(String.Format("ChangeCamera: Exception {0}", e.Message));
}
}
private async Task<int> GetOrientation()
{
int result = 0;
try
{
result = await MS2TesterHelper.GetOrientation();
}
catch (Exception e)
{
Debug.WriteLine(String.Format("GetVideoDevices: Exception {0}", e.Message));
}
return result;
}
private async void SetOrientation(int degrees)
{
try
{
OperationResult result = await MS2TesterHelper.SetOrientation(degrees);
if (result == OperationResult.Succeeded)
{
Debug.WriteLine("SetOrientation: success");
}
else
{
Debug.WriteLine("SetOrientation: failure");
}
}
catch (Exception e)
{
//if (e.HResult == MethodCallUnexpectedTime)
//{
Debug.WriteLine("StartVideoStream: Async operation already in progress");
//}
Debug.WriteLine(String.Format("SetOrientation: Exception {0}", e.Message));
}
}
private void ChangeCameraButton_Click(object sender, RoutedEventArgs e)
{
String camera = (CameraComboBox.SelectedItem as ComboBoxItem).Content as String;
MS2Tester.Instance.changeCamera(camera);
ChangeCamera(camera);
}
private void SetVideoOrientation()
private async void SetVideoOrientation()
{
SimpleOrientation orientation = deviceOrientation;
if (displayInformation.NativeOrientation == DisplayOrientations.Portrait)
......@@ -228,9 +362,10 @@ namespace ms2_tester
break;
}
if (MS2Tester.Instance.getOrientation() != degrees)
int currentDegrees = await GetOrientation();
if (currentDegrees != degrees)
{
MS2Tester.Instance.setOrientation(degrees);
SetOrientation(degrees);
}
}
......
......@@ -205,7 +205,7 @@ void MS2Tester::uninitVideo()
wcstombs(cst, wst.c_str(), sizeof(cst))
void MS2Tester::startVideoStream(Platform::Object^ CaptureElement, Platform::Object^ MediaElement, Platform::String^ camera, Platform::String^ codec, Platform::String^ videoSize, unsigned int frameRate, unsigned int bitRate)
void MS2Tester::startVideoStream(Platform::String^ swapChainPanelName, Platform::String^ camera, Platform::String^ codec, Platform::String^ videoSize, unsigned int frameRate, unsigned int bitRate)
{
ms_filter_enable_statistics(TRUE);
ms_filter_reset_statistics();
......@@ -239,9 +239,7 @@ void MS2Tester::startVideoStream(Platform::Object^ CaptureElement, Platform::Obj
PayloadType *pt = rtp_profile_get_payload(&av_profile, payload);
pt->normal_bitrate = bitRate * 1000;
_videoStream = video_stream_new(20000, 0, FALSE);
RefToPtrProxy<Platform::Object^> *previewWindowId = new RefToPtrProxy<Platform::Object^>(CaptureElement);
video_stream_set_native_preview_window_id(_videoStream, previewWindowId);
RefToPtrProxy<Platform::Object^> *nativeWindowId = new RefToPtrProxy<Platform::Object^>(MediaElement);
RefToPtrProxy<Platform::String^> *nativeWindowId = new RefToPtrProxy<Platform::String^>(swapChainPanelName);
video_stream_set_native_window_id(_videoStream, nativeWindowId);
video_stream_set_display_filter_name(_videoStream, "MSWinRTDis");
video_stream_use_video_preset(_videoStream, "custom");
......
......@@ -24,7 +24,7 @@ namespace ms2_tester_runtime_component
void runAllToXml();
void initVideo();
void uninitVideo();
void startVideoStream(Platform::Object^ CaptureElement, Platform::Object^ MediaElement, Platform::String^ camera, Platform::String^ codec, Platform::String^ videoSize, unsigned int frameRate, unsigned int bitRate);
void startVideoStream(Platform::String^ swapChainPanelName, Platform::String^ camera, Platform::String^ codec, Platform::String^ videoSize, unsigned int frameRate, unsigned int bitRate);
void stopVideoStream();
int getOrientation() { return _deviceRotation; }
void setOrientation(int degrees);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment