private void CreateMediaObjects() { // Create our encoding profile based on the size of the item // TODO: This only really makes sense for monitors, we need // to change this to make sense in all cases. int width = _captureItem.Size.Width; int height = _captureItem.Size.Height; // Describe our input: uncompressed BGRA8 buffers comming in at the monitor's refresh rate // TODO: We pick 60Hz here because it applies to most monitors. However this should be // more robust. var videoProperties = VideoEncodingProperties.CreateUncompressed(MediaEncodingSubtypes.Bgra8, (uint)width, (uint)height); _videoDescriptor = new VideoStreamDescriptor(videoProperties); _videoDescriptor.EncodingProperties.FrameRate.Numerator = c_frameRateN; _videoDescriptor.EncodingProperties.FrameRate.Denominator = c_frameRateD; _videoDescriptor.EncodingProperties.Bitrate = (uint)(c_frameRateN * c_frameRateD * width * height * 4); // Create our MediaStreamSource _mediaStreamSource = new MediaStreamSource(_videoDescriptor); _mediaStreamSource.BufferTime = TimeSpan.FromSeconds(0); _mediaStreamSource.Starting += OnMediaStreamSourceStarting; _mediaStreamSource.SampleRequested += OnMediaStreamSourceSampleRequested; // Create our device manager _mediaGraphicsDevice = MediaGraphicsDevice.CreateFromMediaStreamSource(_mediaStreamSource); _mediaGraphicsDevice.RenderingDevice = _device; // Create our transcoder _transcoder = new MediaTranscoder(); _transcoder.HardwareAccelerationEnabled = true; }
protected override async void OnNavigatedTo(NavigationEventArgs e) { DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape; // // Doing all that video processing is too much for low-end phones like the Lumia 520 // Pick-and-choose which piece should run // VideoPreview.MediaFailed += VideoPreview_MediaFailed; //var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/video.cvmpilj.mjpg")); //var stream = await file.OpenAsync(FileAccessMode.Read); //var source = await HttpMjpegCaptureSource.CreateFromStreamAsync(stream, "myboundary"); var source = await HttpMjpegCaptureSource.CreateFromUriAsync("http://216.123.238.208/axis-cgi/mjpg/video.cgi?camera&resolution=640x480"); VideoPreview.SetMediaStreamSource(source.Source); var settings = new MediaCaptureInitializationSettings { StreamingCaptureMode = StreamingCaptureMode.Video }; await settings.SelectVideoDeviceAsync(VideoDeviceSelection.BackOrFirst); _capture = new MediaCapture(); await _capture.InitializeAsync(settings); var graphicsDevice = MediaGraphicsDevice.CreateFromMediaCapture(_capture); var previewProps = (VideoEncodingProperties)_capture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); TextLog.Text += String.Format("Preview: {0} {1}x{2} {3}fps\n", previewProps.Subtype, previewProps.Width, previewProps.Height, previewProps.FrameRate.Numerator / (float)previewProps.FrameRate.Denominator); TextLog.Text += "Creating MediaSamplePresenter from SurfaceImageSource\n"; var image = new SurfaceImageSource((int)previewProps.Width, (int)previewProps.Height); ImagePreview.Source = image; _imagePresenter = ImagePresenter.CreateFromSurfaceImageSource(image, graphicsDevice, (int)previewProps.Width, (int)previewProps.Height); TextLog.Text += "Creating MediaSamplePresenter from SwapChainPanel\n"; _swapChainPresenter = ImagePresenter.CreateFromSwapChainPanel( SwapChainPreview, graphicsDevice, (int)previewProps.Width, (int)previewProps.Height ); TextLog.Text += "Creating MediaReader\n"; _mediaReader = await MediaReader.CreateFromMediaCaptureAsync(_capture, AudioInitialization.Deselected, VideoInitialization.Bgra8); TextLog.Text += "Starting video loop\n"; var ignore = Task.Run(() => VideoLoop()); }
public void CS_WP_N_Basic() { ExecuteOnUIThread(async() => { var capture = new MediaCapture(); await capture.InitializeAsync(new MediaCaptureInitializationSettings { StreamingCaptureMode = StreamingCaptureMode.Video }); var graphicsDevice = MediaGraphicsDevice.CreateFromMediaCapture(capture); var previewProps = (VideoEncodingProperties)capture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); var image = new SurfaceImageSource((int)previewProps.Width, (int)previewProps.Height); var imagePresenter = ImagePresenter.CreateFromSurfaceImageSource( image, graphicsDevice, (int)previewProps.Width, (int)previewProps.Height ); var panel = new SwapChainPanel(); var swapChainPresenter = ImagePresenter.CreateFromSwapChainPanel( panel, graphicsDevice, (int)previewProps.Width, (int)previewProps.Height ); var readerProps = VideoEncodingProperties.CreateUncompressed(MediaEncodingSubtypes.Bgra8, previewProps.Width, previewProps.Height); readerProps.FrameRate.Numerator = previewProps.FrameRate.Numerator; readerProps.FrameRate.Denominator = previewProps.FrameRate.Denominator; var captureReader = await CaptureReader.CreateAsync( capture, new MediaEncodingProfile { Video = readerProps }); using (MediaSample2D sample = (MediaSample2D)await captureReader.GetVideoSampleAsync()) { swapChainPresenter.Present(sample); imagePresenter.Present(sample); var folder = await KnownFolders.PicturesLibrary.CreateFolderAsync("MediaCaptureReaderTests", CreationCollisionOption.OpenIfExists); var file = await folder.CreateFileAsync("CS_WP_N_Basic.jpg", CreationCollisionOption.ReplaceExisting); await sample.SaveToFileAsync(file, ImageCompression.Jpeg); } }); }