WPF uses shared memory to play videos

Source: Internet
Author: User

In WPF, controls that do not inherit frameworkelement cannot be displayed on the WPF interface. windowsformshost must be used for nesting. For example, some third-party ocx, among them, the most depressing thing I often encounter is playing videos in special formats in WPF. I am doing video monitoring applications and facing various messy video formats, some videos can only be played in the vendor's library. It is conceivable that it is difficult to play these videos on WPF, because the WPF control has no handle, and windowsformshost is used, the control will be placed on top again, video menus and video overlays cannot be implemented.

Recently, I saw a C # open-source code that calls the VLC player, including the WPF control. After reading the code, I found that he actually reads each frame of the video, then, a shared memory is created to convert the video data into an interopbitmap. The interopbitmap enables developers to improve the rendering performance of non-wpf uis hosted by WPF In the interoperability solution and can be directly assigned to the image. source, so that the video can be played through the image, so that the video can be moved. Of course, the project uses the VLC decoding function. If we want to parse our own video format, you need to implement decoding and convert it to the composition of interopbitmap. If it is strong enough, you can directly change the VLC decoder. VLC is also open-source.

I plan to implement my own player by referring to the ideas and methods in the open source code. Generally, a third-party decoding library provides the following functions: handle video playback, playback control, and so on, I don't want to fully implement a player in WPF. That's not practical, because there are too many manufacturers' interfaces to handle. I just want to change the handle to the image to display the video. In order to implement this function, first, learn some necessary knowledge.

Memory ing API

// Create a memory ing [dllimport ("Kernel32", setlasterror = true)] public static extern intptr createfilemapping (intptr hfile, intptr lpattributes, pageaccess flprotect, int partition, int dwmaximumsizehigh, string lpname); // obtain the memory ing Address [dllimport ("success", setlasterror = true)] public static extern intptr mapviewoffile (intptr hfilemappingobject, filemapaccess dwdesiredaccess, uint dwfileoffsethigh, uint success, uint dwnumberofbytestomap); // release the memory ing [dllimport ("Kernel32", setlasterror = true)] public static extern bool unmapviewoffile (intptr lpbaseaddress ); // release the handle [dllimport ("Kernel32", setlasterror = true)] public static extern bool closehandle (intptr handle );

How to Get bitmap from the unmanaged memory using imaging. createbitmapsourcefrommemorysection Method

//// Summary: // return the hosted system. Windows. Media. imaging. bitmapsource Based on the provided unmanaged memory location. //// Parameter: // section: // pointer of the memory part. //// Pixelwidth: // specifies the integer of the bitmap width (in pixels. //// Pixelheight: // specifies the integer of the bitmap height (in pixels. //// Format: // an enumerated value. //// Stride: // the interval of the bitmap. //// Offset: // The memory stream byte offset from which the image starts. //// Return result: // The created system. Windows. Media. imaging. bitmapsource. [Securitycritical] public static bitmapsource createbitmapsourcefrommemorysection (intptr section, int pixelwidth, int pixelheight, pixelformat format, int stride, int offset );
The Type displayed in the createbitmapsourcefrommemorysection is converted to interopbitmap, and the dependency attribute can be bound to the image control;

The next step is how to map video data from the memory, which needs to be related to your video decoding provider. What format is the image of each frame obtained by him, usually there are several pixelformats. bgr32, pixelformats. pbgra32 and so on. The createbitmapsourcefrommemorysection is used to read the image from the memory according to the format and size. The memory is hashed out to the createbitmapsourcefrommemorysection at a frame, after processing and releasing the video, continue to the next frame. If the other party provides the callback function, it is best to process the video memory without callback. The video component will carry some interfaces.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.