JMF Video Transmission

Source: Internet
Author: User
Java media architecture (JMF) java media architecture (JMF) is an exciting Universal API that allows Java developers to process media in many different ways. This Guide provides an overview of some of the main features of JMF by using examples of work. After reading this guide, you will understand the main playback functions in the JMF architecture. You can also use JMF correctly, using existing examples and source code that can be extended for more special features. This Guide contains the following topics: · download and install JMF main JMF classes and their applications in the JMF Architecture · play local media files · Create a GUI for media access and operations) · transmitting media through the network · receiving operations and processing of almost all media types through the network can be achieved through JMF. A comprehensive discussion of all features provided by JMF has exceeded the scope of this Guide. We will use three simple media applications to learn the building modules of this framework. By using this method, this Guide prepares you for learning and implementing more special applications in the future. Should I use this guide? This guide will show you the basics of using JMF. To do this, we will create three examples of Independent jobs. Each example is based on the previous example to show different aspects of JMF functionality. The example in this Guide assumes that you have used and are familiar with the Java programming language. In addition to the Java core and JMF classes, we will use some java AWT and swing classes (used to create guis) and some Java Network classes (used to transmit media over the network ). Familiarity with the GUI and network helps you better understand your point of view and examples, but it is not necessary to read this guide. The Learning example is as follows: a simple audio player (helloworld application of JMF ): in this character interface, the player simply enters the name of a media file in the command line to play most audio types. The demo of this audio player shows the JMF-specific classes in general. · A graphical Media Player: we will use the JMF built-in interface components to establish a graphical interface. Therefore, we must have some graphic interface programming experience in this exercise. This media generator uses Java AWT and swing classes to display graphical components for users. · A media broadcast application: This application allows a local media file to be transmitted over the network. This program can flexibly transfer media to only the specified network node, or to all nodes in a subnetwork. This demo uses some Java Network APIs to transmit media in the network. As part of the third exercise, we will modify the graphical player so that it can receive and play media. Jump to page 23 to view the resources, articles, guides, and other reference object lists. This will help you learn more about the topics included in this Guide. To install the sample program in this Guide, you need the following tools and components: · Java 2 platform, Standard Edition, compiling and running the demo program · java media framework, version 2.1.1a or later · A sound card that has been installed and configured with an appropriate number · one or more Test Machines · demo source code file in mediaplayer. the last demo application in jar shows the application of JMF in the network. If needed, this demo can run on an independent machine, where the transmitter and receiver are used. However, to observe that all JMF functions are used in the network, you still need at least two connected machines. Resources on page 23 can download the Java 2 platform, complete source code files, and other tools required to complete this guide. Download the installation file to install JMF on your computer. The first step is to download the installation file on the JMF homepage. It also includes the JMF source code and API documentation links. The resources on page 23 contains a link to download JMF. Currently, JMF is available in windows, Solaris, Linux, and other versions, and can run a pure Java version on any computer with virtual machines. To increase performance, you need to download a version that suits your operating system. Any code written and compiled in one JMF version can be easily transplanted to another operating system. For example, if you download a Solaris JMF and compile a class, these classes can be used on Linux without any problems. You can choose to download the pure Java version or the cross-platform JMF version. These versions do not use database files specific to the operating system. If no suitable JMF version is available for the operating system, the cross-platform version is a good choice. After JMF is installed and the JMF installer is downloaded, double-click the installer icon. Most installers have an option to install the local library to the system directory. For example, the Windows Installer has an option "Move DLLs to Windows/system directory .". It is best to select this option because it ensures that the library files of these operating systems can be correctly installed during installation. You also need to select a project to update the classpath and PATH variables of the system. If these options are disabled, You need to introduce the jmf jar file in classpath when compiling and running the sample program in this Guide. About the author Eric Olson's software engineer at retek Inc. It has four years of working experience on the Java platform and rich experience in different Java-based technologies, including JMF, Jini, Jiro, JSP, Servlets, and ejbs. Eric graduated from st. Paul, MN's st. Thomas University and obtained a degree in computer science. He works in the sanfrancisco project team of IBM and is responsible for Websphere Business components. He also works for imation Corp. To store applications. He is developing web-based software solutions for the retail industry. In his spare time, Eric and Paul Monday collaborated on stereo beacon-a distributed point-to-point JMF-based media player. Contact Eric zpalffy@yahoo.com. Section 2. A simple audio player browsing in this section, we will perform the first exercise of creating a simple audio player. This example introduces the manager class and the player interface, both of which are important parts for creating most JMF-based applications. In this example, the function is to play a local audio file on the Character interface. We will learn the source code and understand the tasks of each line. After completing this section, you will have a JMF-based demo program that can play multiple audio files, including MP3, WAV, and AU. The source code category after this exercise can be used to query the simpleaudioplayer. Java file. Some calls are included in the simpleaudioplayer class. All necessary classes must be introduced in the first few lines: Import javax. media. *; import Java. io. file; import Java. io. ioexception; import java.net. URL; import java.net. malformedurlexception; The javax. A media package is one of multiple packages defined by JMF. Javax. Media is a core package, including defining the manager class and player interface. In this section, we mainly learn the manager class and player interface, and the remaining javax. Media classes are placed in the following sections. In addition to javax. Media declarations, the above code snippets introduce some input declarations for creating media players. The player interface creates a public class simpleaudioplayer in the following code snippet and defines a player variable: public class simpleaudioplayer {private player audioplayer = NULL. The term player sounds familiar, because it is based on our public audio or video player. In fact, the example of this interface is like a real copy of them. Players reveals functional methods involved in a physical media player (such as a stereo speaker system or VCR. For example, a JMF media player can start and end a media stream. In this section, we will use the start and end features of player. It is very easy to create a player on a file and use JMF to obtain a player instance of a specific media file. The manager class creates many special interface types in the same factory in JMF, including the player interface. Therefore, the manager class is responsible for creating a player instance. For example, public simpleaudioplayer (URL) throws ioexception, noplayerexception, cannotrealizeexception public simpleaudioplayer (File file) throws ioexception, noplayerexception, cannotrealizeexception after reading the code in this section, you can note that the manager class contains other methods for creating a player instance. We will study some of them, such as the instantiation of datasource or medialocator in the subsequent sections. The player status JMF defines different statuses that may exist for a large number of player instances. As shown in the following figure: · prefetched · prefetching · realized · realizing · started · unrealized use these statuses because media usage is often resource intensive and many methods revealed by JMF objects are not blocked, asynchronous notifications that allow the status of a series of event listening changes. For example, a player must pass the prefetched and realized statuses before it can be started. Since these changes take some time to complete, the JMF media application can allocate a thread to initialize and create a player instance, and then continue other operations. When player is ready, it notifies the application that its status has changed. In a simple program like ours, the type of versatility is not very important. For this reason, the manager class also provides some useful methods for creating realized player. Call a createrealizedplayer () method to block the call thread until the player reaches the realized state. To call a method for creating a player without blocking, we use a createplayer () method in the Manager class. The following code creates a realized player that we need to use in the example program: audioplayer = manager. createrealizedplayer (URL); start and stop a player. Setting the start or stop of a player instance is like calling a simple authentication method of player, as shown below: Public void play () public void stop () calls the play () method in the simpleaudioplayer class to call the START () method of the player instance. After calling this method, you can hear the sound file of the local speaker. Similarly, the STOP () method stops the player and closes the Player Object. For reading and playing local media files, disabling the player instance to release all resources is a useful method. This is a simple example. Disabling player is an acceptable way to terminate a session. However, in actual applications, you need to be careful to disable it before removing the player. Once you have disabled the player, you must create a new player instance (wait for its status to change) before playing a media file again ). Create a simpleaudioplayer. The Media Player application must contain a main () method that can be called by entering commands in the command prompt line. In this main () method, we will call the method for creating simpleaudioplayer: file audiofile = new file (ARGs [0]); simpleaudioplayer player = new simpleaudioplayer (audiofile ); the only thing before playing an audio file is to call the method play () of the created audio player, as shown below: Player. play (); to stop and clear the audio player, the main () method should also be called as follows: Player. stop (); compile and run simpleaudioplayer by entering javac simpleaudioplayer in the command prompt line. java to compile the sample program. The created file simpleaudioplayer. Class is in the current working directory. Then, type the following command in the command prompt line to run the example program: Java simpleaudioplayer audiofile to replace audiofile with the audio file on your local machine. All relative file names are compared to the current working directory. You will see some flag information about the currently playing file. To terminate the playback, press Enter. If compilation fails, make sure that the jmf jar file is correctly included in the classpath environment variable. Section 3. JMF User Interface Component Video Playback in the previous section, we learned how to create an application for playing audio files through the character interface. One of the most important features of JMF is that you do not need to understand the media file format to configure the media player. Everything is built in. For example, in the previous example, when MP3 format is required, we do not need to create a special player for an MP3 file. As you will see in this section, operations on video files are equally effective. JMF provides details about all media file interfaces. The biggest difference between processing video media and audio is that we must create a display screen that can play a video. Fortunately, JMF can process a lot of this information. As in the previous example, we will create a Player Object and use many visual components to directly create our visual media browser from the JMF object. In this section, we will learn two examples. In this section, we & aposll walk through the second example application. Please refer to mediaplayerframe. Java in the source code distribution of the following exercises. In this section, we will create an application that can display and run local audio and video media. As part of the exercise, we will study some gui components built into JMF. Familiarity with AWT and swing will help you understand this example, but this is not necessary. Unless you need to directly involve the jmf gui components, or we will not detail the source code. You can find detailed descriptions not involved here in the source code annotations. Many concepts, classes, and methods used in this example are similar to those in the first example. The basic operations for creating a player are mostly the same. The biggest difference is that we need to study the Player Object more deeply, especially when we need to obtain media information from the player. The example of how to start a video player is designed to run in the same way as the audio playback example, but this example needs to be built on the GUI. As in the previous section, we first call the application through the media file name. The application then displays a form with operable media components. In the row starting with mediaplayerframe, we defined the class and extended the class javax. Swing. jframe class. This is how to make a media player look like a separate form on the desktop. After any client program creates a playback object, it can be displayed by calling the show () method defined in the jframe class. Below is a screen where mediaplayerframe is playing an MPEG movie: There are some methods to obtain the GUI Component player interface to obtain the involved options of the selected visual component. In mediaplayerframe, we use the following components: · player. getvisualcomponent () is a visual component used to play all the video media. · Player. getcontrolpanelcomponent () is a visual component (including start, stop, and playback) that operates the timeline. It also contains useful information about media streams. · Player. getgaincontrol (). getcontrolcomponent () is a visual component for operating Volume (increase. The getgaincontrol () method returns a gaincontrol instance that can be used to change the increase level of a program. The preceding interface method of the visualization component returns an instance of the Java. AWT. component class. No instances are visible components that can be loaded to our forms. These components are directly related to the player, so the processing of all the visual elements on these components will change accordingly after the player plays the media. Before adding these components to our form, make sure they are not empty. Because not all media players include every visual component, we only need to add relevant player components. For example, an audio player has no visual components, so getvisualcomponent () returns NULL. You do not want to add visual components to the audio player form. Get Special Control of a media player instance. You can also use the getcontrol () and getcontrols () Methods to expose the control. getcontrols () returns a control object set, while getcontrol () return a control. Different player types can be used as special operations to expose the specified media type from the control set, or to obtain the transmission mechanism of the media. If you are writing a player that only supports certain media types, you need to rely on some control objects available in the player instance. Since our player is very abstract and designed to play a variety of different media types, we simply expose all the control objects for users. If you find any extended control set, you can use the getcontrolcomponent () method to add the corresponding visible control to the tag panel. In this way, you can observe all the components on the player. The following code snippet exposes all the control objects to the user: Control [] controls = player. getcontrols (); For (INT I = 0; I <controls. length; I ++)} In order to enable a real application to use the control instance to do something useful (except to display visual components ), the application needs to know the special type of the control and allocate it. Then, applications can use these controls to control media programs. For example, if you know that the media you often use exposes javax. media. control. you can use the qualitycontrol interface to change the attribute settings on the qualitycontrol interface. The biggest difference between using a medialocator in our new Gui-based Media Player and our first simple player is that we use a medialocator object instead of a URL to create a player instance, as shown below: public void setmedialocator (medialocator Locator) throws ioexception, noplayerexception, cannotrealizeexception. We will discuss the cause of this change in a later chapter. Currently, the medialocator object and URL descriptions on the resource site on the network are considered very similar. In fact, you can create a medialocator from a URL or get a URL from medialocator. Create a medialocator in a URL of our new Media Player and use this medialocator to create a player through the file. Compile and run mediaplayerframe by entering javac mediaplayerframe. Java in the command prompt line to compile the sample program. A file named mediaplayerframe. class will be created in the working directory. Type the following in the command prompt line to run the example program: Java mediaplayerframe mediafile. You need to replace mediafile with a media file on your local machine (either audio or video files ). All relative file names are relative to the current working directory. You will see a window showing the GUI control set for controlling media files. For more information about the list of audio and video files supported by JMF, see resources on 23 pages. If the initial compilation fails, make sure that the jmf jar file is included in the current classpath environment variable. Mediaplayerframe is playing the screen of an MPEG video file that you saw before this section. The screen below shows that an audio player is playing an MP3 file: to learn more examples in this exercise, view the complete mediaplayerframe source code. Section 4. JMF concept JMF architecture you have seen how easy it is to use JMF to play local media files. Now let's move back, let's take a look at how JMF creates a large image of such mature media-based applications and how JMF creates such mature media-based applications. It is meaningless to fully understand the JMF architecture. This section will give you a general idea about how advanced JMF components are combined to create what you want. The component structure of JMF is very flexible. Its components can be divided into three parts: Input describes a media used as an input when the process is resting. · Process executes certain input activities. A process has a clear input and output. A large number of processes are available and can be used for one input or a batch of input. These processes can be linked, and the output of one process is used for the input of another process. In this style, a large number of processes may be applied to an input. (This period is optional-the two examples we started do not contain real data processes, but only one input from the file and one output through player .) · Output describes certain media destinations. From these descriptions, you can imagine that the JMF component architecture sounds like a typical stereo system or VCR. It is easy to imagine that using JMF is like opening a TV or adjusting the sound style in a stereo speaker system. For example, recording simple movements of favorite TV programs can be based on these components: · input is a television broadcasting stream, and audio and video are shipped on the same channel. · Process is a recording device (that is, a VCR or many digital devices) that converts analog or digital audio and video broadcast streams into formats suitable for copying to tapes or other media. · Output records formatted tracks (audio and video) to some types of media. The JMF data processing mode illustrates the JMF data processing module in a piece and provides examples for each type: using this mode, it is easy to understand the previous two examples, input audio and video from the file and output it to the local computer. In the subsequent sections, we will also talk about some JMF network functions that spread and receive audio media. The processing model example links the input, processing, and output modes of JMF. We can start to imagine that many media-based operations may be completed through JMF. For example, convert a media type to another type and store its output to a new file. For example, we want to convert an audio file in WAV format to MP3 without damaging the original file. The following process mode illustration shows how to start the conversion: the input in this example is a WAV file. It is processed by a media format conversion tool and output to a new file. Now let's take a look at every step of this mode in jmf api. We use the input, processing, and output modes as the conceptual roadmap. In JMF input, a medialocator object is generally used to describe an input. As previously specified, medialocator looks and acts like a URL, so that it can uniquely identify a resource on the network. In fact, it is entirely possible to create a medialocator using a URL. We did this in the first two examples. For our media conversion example, we need to create a medialocator file to describe the original WAV file. As we will see in later chapters, a medialocator can also be used to describe a media stream across networks. In this case, medialocator describes the propagated URL, which is similar to a web resource specified by the URL, used to replace the file specified by a local file system to create medialocator. A different medialocator and a URL requires proper java.net. urlstreamhandler to be installed in the system to successfully create a URL object. The purpose of stream processing is to process the stream type described by the URL. This is not required for a medialocator object. For example, our next application uses the Real-Time Transport Protocol (RTP) to transmit audio over the network. Because most systems do not install urlstreamhandler for the RTP protocol, creating a URL object fails. In this application, only the medialocator object will succeed. For more information about URL objects and creating and registering a urlstreamhandler, see the JDK help documentation (view 23-page resources ). When we use JMF, the processor component of the application is described by the processor interface instance. You need to be familiar with processor, which is extended to the player interface. Because processor inherits the direct player interface, it also inherits all available attributes from player. In addition, processor adds two attributes: grouping and configured. These extended attributes (and associated methods) are used for communication when processor collects information from the input stream. In our final example program, we will create a processor to Convert audio in MP3 encoding format into a format suitable for transmitting on the network. Later, we will discuss how to create a simple processor. There are a few methods for JMF output to describe the output status of the processing mode in JMF. The simplest (and we will use it in the last example) is the javax. Media. datasink interface. A datasink reads media content and sends it to some destinations. During the most initial audio format conversion in this section, the MP3 (output) file will be described by datasink. In our last example, we will use a datasink to actually spread audio media over the network. A datasink is completed by specifying a datasource (input to datasink) and a medialocator (output to datasink) in the Manager class. A datasource instance description can be used for input data of players, processors, and datasinks. The output of a processing machine is also described as a datasource object. This is why processors can connect to each other and perform multiple operations in the same media data. This is also the reason why processor output can be used as input by player or datasink (it can pass the media to the output destination ). The final target file of a datasink is described by a medialocator object. As before, medialocator describes a network resource. This is where a media stream will be transmitted. Section 5. The characteristics of many friendly networks of the communication and receiving media JMF and real-time transmission protocol (RTP) are directly established in JMF, which makes it easy for client programs to transmit and receive media over the network. When a user on a network wants to receive media streams of any type, it does not need to wait for all broadcasts to be downloaded to the machine before watching the media; users can watch broadcasts in real time. This concept is put forward in streaming media. Through Streaming Media, a network client can receive audio broadcast from other machines or even obtain live video broadcasts that are happening. Ietf rfc 1889 defines the real-time transmission protocol (RTP ). Rapid and reliable data transmission over the network is extremely time sensitive. RTP is used in JMF to provide users with methods to transmit media streams to other network nodes. In this section, we will learn about our last example program. Here, you will learn how to transfer an MP3 file stored on one machine to another machine on the same network. The actual MP3 source file is not removed from the master computer, nor is it copied to another machine. In fact, it will be converted into a file format transmitted using RTP and sent over the network. Once received by a client, the source file (now in the form of RTP packets) can be transmitted again. This is a format that can be played on the receiving machine. View the source code in the mediatransmitter. Java file and learn the following exercises. To set the processing mode, we can discuss our final example based on the processing mode defined in the previous chapter. On the transmission machine, the processing mode looks like this: in fact, the mediatransmitter object Source Code includes the following three lines: Private medialocator = NULL; private datasink = NULL; private processor mediaprocessor = NULL; these three instance variables can be directly mapped to the previous processing mode chart, as shown below: · The mediaprocessor variable is our processor; it will be responsible for converting audio files from the MP3 file mode to a format suitable for transmission through the RTP protocol. · The datasink variable is our output block. · When creating a datasink, We need to specify a medialocator file, which is the target file of datasink. When we run datasink, our processed media will be transmitted to the location specified in medialocator.

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.