AppService.cs 7.15 KB
Newer Older
Ghislain MARY's avatar
Ghislain MARY committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.AppService;
using Windows.ApplicationModel.Background;
using Windows.Foundation.Collections;

namespace MS2TesterTasks
{
    public sealed class AppService : IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            AppServiceTriggerDetails triggerDetail = taskInstance.TriggerDetails as AppServiceTriggerDetails;
            _deferral = taskInstance.GetDeferral();

            // Register for Task Cancel callback
            taskInstance.Canceled += TaskInstance_Canceled;

            AppServiceConnection connection = triggerDetail.AppServiceConnection;
            _connection = connection;
            connection.RequestReceived += Connection_RequestReceived;
        }

        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 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.AppRequestDeferral = deferral;
                            CurrentOperation.StartVideoStream(
                                message[StartVideoStreamArguments.VideoSwapChainPanelName.ToString()] as String,
                                message[StartVideoStreamArguments.PreviewSwapChainPanelName.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()]),
72 73
                                Convert.ToUInt32(message[StartVideoStreamArguments.BitRate.ToString()]),
                                Convert.ToBoolean(message[StartVideoStreamArguments.UsePreviewStream.ToString()]));
Ghislain MARY's avatar
Ghislain MARY committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
                            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);
                }
            }
        }

        private void TaskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
        {
            if (_deferral != null)
            {
                _deferral.Complete();
            }
        }

        private AppServiceConnection _connection;
        private BackgroundTaskDeferral _deferral;
    }
}