private void StartVideoStreamingThread()
        {
            IAsyncAction asyncAction = Windows.System.Threading.ThreadPool.RunAsync(
                async(workItem) =>
            {
                while (true)
                {
                    //UnityEngine.Debug.Log("Check new frame");

                    if (!_frameReadyFlag)
                    {
                        await System.Threading.Tasks.Task.Delay(30);
                        continue;
                    }

                    {
                        byte[] imageData = null;     // after compression

                        long dataTime = 0, compressedTime = 0;
                        if (!Const.LOAD_IMAGES)
                        {
                            imageData = _imageDataRaw;
                            // Compress to JPEG bytes
                            //imageData = await Bitmap2JPEG(_imageDataRaw);
                        }
                        else
                        {
                        }

                        // Stream image to the server
                        if (_tokenController.GetCurrentToken() > 0)
                        {
                            _tokenController.DecreaseToken();
                            _tokenController.LogSentPacket(_frameID, dataTime, compressedTime, _cameraToWorldMatrix, _projectionMatrix);
                            await StreamImageAsync(imageData, _frameID);
                        }

                        _frameReadyFlag   = false;
                        _isDoingTimerTask = false;
                    }
                }
            });
        }
示例#2
0
        /// <summary>
        /// Gets the current preview frame as a SoftwareBitmap
        /// </summary>
        /// <returns></returns>
        private async Task StreamPreviewFrameAsync(long frameID)
        {
            //            long startTime = GetTimeMillis();
            long dataTime = 0, compressedTime = 0;

            byte[] imageData;
            if (!Const.LOAD_IMAGES)
            {
                // Get information about the preview
                var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;

                // Create the video frame to request a SoftwareBitmap preview frame
                var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height);

                // Capture the preview frame
                using (var currentFrame = await _mediaCapture.GetPreviewFrameAsync(videoFrame))
                {
                    // Collect the resulting frame
                    SoftwareBitmap frame = currentFrame.SoftwareBitmap;

                    // Compress to JPEG bytes
                    imageData = await Bitmap2JPEG(frame);

                    // Show the frame (as is, no rotation is being applied)
                    // Create a SoftwareBitmapSource to display the SoftwareBitmap to the user
                    //                var sbSource = new SoftwareBitmapSource();
                    //                await sbSource.SetBitmapAsync(frame);
                    //                PreviewFrameImage.Source = sbSource;
                }
            }
            else
            {
                _indexImageFile = (int)(frameID % _imageFiles.LongCount());
                using (IRandomAccessStreamWithContentType stream = await _imageFiles[_indexImageFile].OpenReadAsync())
                {
                    imageData = new byte[stream.Size];
                    using (DataReader reader = new DataReader(stream))
                    {
                        await reader.LoadAsync((uint)stream.Size);

                        reader.ReadBytes(imageData);
                    }
                }
            }

            // Stream image to the server
            if (_tokenController.GetCurrentToken() > 0)
            {
                if (Const.IS_EXPERIMENT)
                {
                    dataTime = GetTimeMillis();
                    var unused = await Bitmap2JPEG(_imageBitmapsCompress[_indexImageFileCompress]);

                    _indexImageFileCompress = (_indexImageFileCompress + 1) % _imageFileCompressLength;
                    compressedTime          = GetTimeMillis();
                }
                _tokenController.DecreaseToken();
                _tokenController.LogSentPacket(frameID, dataTime, compressedTime);
                await StreamImageAsync(imageData, frameID);
            }
        }