Call mplayer in GTK

Source: Internet
Author: User
Tags gstreamer

 

I. mplayer

1. What is mplayer?
Mplayer is a multimedia player in Linux. It is easy to use and supports common video and music formats.

2. Why is mplayer used?
In Linux, multimedia players mainly use xine, mplayer, and gstreamer.

1) mplayer is easy to install and use. It is generally called through the command line and then controlled by pressing the key (space is paused, left and right keys are seek). Its built-in graphical interface is not very useful.

2) xine consists of LIB and UI (the two installation packages must be consistent). Generally, you can use xine-UI to play media. The LIB provides a complete interface to facilitate secondary development, library usage is relatively complex

3) gstreamer is a playback framework with some decoding libraries. It is said that the structure is good, but it is troublesome for ordinary people to install, use, and develop.

3. How to Use mplayer

1) play normal files
$ Mplayer xxx.mp3
$ Mplayer XXX. Avi

2) playback media files
$ Mkfifo/tmp/media // create an MPS queue File
$ Cat XXX. MpEG>/tmp/Media & // write media file data to one end of the MPs queue
$ Mplayer/tmp/media-Cache 3000 // mplayer starts playing after loading 3 M data
Note that you must set up a slow charge to support small-scale indexes when reading data.
Similarly, streaming media can be played (for example, when one end of the media data is received from the service and the other end is read and played by mplayer)

3) play in slave mode
In slave mode, mplayer is no longer controlled by the buttons. You can use a media transcoding queue to send control commands to mplayer.
It is often used when mplayer is called by another program and played in the background.
$ Mkfifo/tmp/CMD
$ Mplayer-slave-quiet-input file =/tmp/cmd xxx. MpEG &
$ Echo "Pause">/tmp/CMD
Run the following command to view available control commands:
$ Mplayer-input cmdlist

Mplayer 1.0rc2-4.1.2 (c) 2000-2007 mplayer team
CPU: SGI MIPS
Creating config file: //. mplayer/config
Seek float [integer]
Edl_mark
Audio_delay float [integer]
Speed_incr float
Speed_mult float
Speed_set float
Quit [integer]
Pause
Frame_step
Pt_step integer [integer]
Pt_up_step integer [integer]
Alt_src_step integer
Loop integer [integer]
Sub_delay float [integer]
Sub_step integer [integer]
OSD [integer]
Osd_show_text string [integer] [integer]
Osd_show_property_te string [integer] [integer]
Volume float [integer]
Balance float [integer]
Use_master
Mute [integer]
Contrast integer [integer]
Gamma integer [integer]
Brightness integer [integer]
Hue integer [integer]
Saturation integer [integer]
Frame_drop [integer]
Sub_pos integer [integer]
Sub_alignment [integer]
Sub_visibility [integer]
Sub_load string
Sub_remove [integer]
Vobsub_lang [integer]
Sub_select [integer]
Sub_log
Sub_scale float [integer]
Get_percent_pos
Get_time_pos
Get_time_length
Get_file_name
Get_video_codec
Get_video_bitrate
Get_video_resolution
Get_audio_codec
Get_audio_bitrate
Get_audio_samples
Get_meta_title
Get_meta_artist
Get_meta_album
Get_meta_year
Get_meta_comment
Get_meta_track
Get_meta_genre
Switch_audio [integer]
TV _start_scan
TV _step_channel integer
TV _step_norm
TV _step_chanlist
TV _set_channel string
TV _last_channel
TV _set_freq float
TV _step_freq float
TV _set_norm string
TV _set_brightness integer [integer]
TV _set_contrast integer [integer]
TV _set_hue integer [integer]
TV _set_saturation integer [integer]
Forced_subs_only [integer]
Dvb_set_channel integer
Switch_ratio [float]
Vo_fullscreen [integer]
Vo_ontop [integer]
File_filter integer
Vo_rootwin [integer]
Vo_border [integer]
Screenshot [integer]
Panscan float [integer]
Switch_vsync [integer]
LoadFile string [integer]
Loadlist string [integer]
Run string
Change_rectangle integer
Get_vo_fullscreen
Get_sub_visibility
Key_down_event integer
Set_property string
Get_property string
Step_property string [float] [integer]
Seek_chapter integer [integer]
Set_mouse_pos integer

4. mplayer-related Gui

1) gmplayer: the built-in GUI of mplayer
Generally, the mplayer installed in the RPM package does not contain gmplayer.
To use gmplayer, You need to download the source code and add the option-enable-Gui during compilation. After compilation and installation, the executable program gmplayer is generated.
To run gmplayer properly, you also need to install the skin
The control interface and playback interface of gmplayer are in two windows, which are not very useful.

2) kmplayer: playback software based on the mplayer Program
The control interface and the playback interface are implemented in the same window. It looks like a storm video.
It only supports some mplayer functions, such as setting the cache function, which cannot be implemented.
There are limitations and many codes, so it is inconvenient to modify and add the desired functions.

3) Advantages of self-built mplayer Gui

A) do the simplest mplayer Gui: We can use less than 200 lines of code, use the gtk gui, and provide mplayer with a simple gui (pause/seek/vol and other functions ), the control interface and playback interface are in the same window.

B) It supports all mplayer parameters and functions

C) It can be closely integrated with your own applications.

D. do not compile or modify the mplayer source code.

Ii. Call mplayer in GTK
Mplayer can be combined with many graphic interface tools (including framebuffer). We use GTK as an example because GTK implementation is relatively simple (similarly to QT, X, and framebuffer)

1. Combined with the GTK window

1) the media played by mplayer is displayed in the specified window.
In the slave mode of mplayer, the parameter-wid allows the video to be output to the specified form (xwindowid must be specified. In the XWindow desktop system, each window is assigned a unique ID when it is created, it is equivalent to a pointer)
Run the following code to obtain the xwindowid of the gtk window:
Window1 = gtk_window_new (gtk_window_toplevel );
Gtk_widget_show (window1 );
Xid = gdk_x11_drawable_get_xid (window1-> window );
During playback, the XId is added to the end of-wid in decimal format.

2) the media played by mplayer coexist with other GTK controls.
Although the above method can be used to specify the mplayer display window, you cannot set the position of the video displayed in this window so that the video can be played throughout the window, if you want to display the control at the same time on the screen, the problem will occur. We use the darwingarea control to solve this problem.
Although the drawingarea control is also located in a certain part of the gtkwindow, it has a different xwindowid from the gtkwindow. We use the following method to make the mplayer display in the partial part of the window (10, 10,200,150)
Window1 = gtk_window_new (gtk_window_toplevl );
Fixed1 = gtk_fixed_new ();
Gtk_container_add (gtk_container (window1), fixed1 );
Drawingarea1 = gtk_drawing_area_new ();
Gtk_fixed_put (gtk_fixed (fixed1), drawingarea1, 10, 10 );
Gtk_widget_set_size_request (drawingarea1, 200,150 );
Gtk_widget_show (drawingarea1 );
Gtk_widget_show (fixed1 );
Gtk_widget_show (window1 );
Xid = gtk_x11_drawable_get_xid (drawingarea1-> window );

2. Command Control mplayer
The slave mode mentioned above allows the program to control the playing of mplayer through the pipeline, and use the system function in the program to run shell commands, such as pausing
System ("echo pause>/tmp/CMD ");

3. Get mplayer progress information
The control command get_percent_pos allows mplayer to print the current progress (the current progress is the output information of mplayer). It starts with ans_percent_position. We can use the read method of popen to run mplayer, in this way, the mplayer output information is obtained and analyzed, as shown in figure
If (Pp = popen ("mplayer-wid XXXX-slave-input file =/tmp/CMD", "R "))! = NULL)
{
While (! Feof (PP ))
{
Data = gets (STR, 256, pp );
If (strncmp (STR, "ans_percent_position =", 21) = 0)
{
Pos = atoi (STR + 21)/100.0;
// Set the progress bar
}
}
Pclose (PP );
}
This method continuously reads data may require threads. You can also use io_channel to analyze mplayer output.
Start a timer at the same time and execute the following code every second to get the progress information every second.
System ("echo get_percent_pos>/tmp/CMD ");

4. Note

1) when sending control commands to mplayer, note that any command will interrupt the current pause status.

2) capture the sigpipe signal. Because the program communicates with mplayer through the pipeline, the program may receive the sigpipe signal when the mplayer exits. Otherwise, the program may not exit normally.

3) if multiple threads are used when mplayer is run, do not forget to use the gdk_thread_xxx function to lock Resources in the interface of the new thread to avoid conflicts.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zhangbinka/archive/2009/07/01/4313057.aspx

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.