📄 rtppresenting.html
字号:
<tr>
<td><font face="Palatino, Times New Roman, Times, serif"><pre>
<a name="112423"> </a><code> // This is the same datasource received from
</code><a name="112403"> </a><code> // NewReceiveStreamEvent and used to create the
</code><a name="112404"> </a><code> // initial rtp player
</code><a name="112239"> </a><code>
</code><a name="112240"> </a><code> rtpsource.connect();
</code><a name="112241"> </a><code> newplayer = Manager.createPlayer(rtpsource);
</code><a name="112242"> </a><code>
</code><a name="112243"> </a><code> if (newplayer == null) {
</code><a name="112244"> </a><code> System.err.println("Could not create player");
</code><a name="112245"> </a><code> return;
</code><a name="112246"> </a><code> }
</code><a name="112247"> </a><code>
</code><a name="112248"> </a><code> newplayer.addControllerListener(listener);
</code><a name="112249"> </a><code> newplayer.realize();
</code><a name="112250"> </a><code>
</code><a name="112251"> </a><code> // when the new player is realized, retrieve its
</code><a name="112252"> </a><code> // visual and control components
</code><a name="112253"> </a><code> } catch (Exception e) {
</code><a name="112254"> </a><code> System.err.println("could not create player");
</code><a name="112255"> </a><code> }
</code><a name="112256"> </a><code> }
</code><a name="112257"> </a><code> }
</code><a name="112158"> </a><code> }
</code></pre>
</font></td>
</tr>
</table>
<br></font>
<h4>
<a name="107995"> </a><font color="#003366" face="Palatino, Times New Roman, Times, serif">Controlling Buffering of Incoming RTP Streams</font>
</h4>
<p>
<a name="107996"> </a><font face="Palatino, Times New Roman, Times, serif">You can control the RTP receiver buffer through the <code>BufferControl</code> exported by the <code>SessionManager</code>. This control enables you to set two parameters, buffer length and threshold.</font>
</p>
<p>
<a name="107999"> </a><font face="Palatino, Times New Roman, Times, serif">The buffer length is the size of the buffer maintained by the receiver. The threshold is the minimum amount of data that is to be buffered by the control before pushing data out or allowing data to be pulled out (jitter buffer). Data will only be available from this object when this minimum threshold has been reached. If the amount of data buffered falls below this threshold, data will again be buffered until the threshold is reached.</font>
</p>
<p>
<a name="108061"> </a><font face="Palatino, Times New Roman, Times, serif">The buffer length and threshold values are specified in milliseconds. The number of audio packets or video frames buffered depends on the format of the incoming stream. Each receive stream maintains its own default and maximum values for both the buffer length and minimum threshold. (The default and maximum buffer lengths are implementation dependent.)</font>
</p>
<p>
<a name="108074"> </a><font face="Palatino, Times New Roman, Times, serif">To get the <code>BufferControl</code> for a session, you call <code>getControl</code> on the <code>SessionManager</code>. You can retrieve a GUI <code>Component</code> for the <code>BufferControl</code> by calling <code>getControlComponent</code>.</font>
</p>
<h3>
<a name="107083"> </a><font color="#003366" face="Palatino, Times New Roman, Times, serif">Presenting RTP Streams with RTPSocket </font>
</h3>
<p>
<a name="107084"> </a><font face="Palatino, Times New Roman, Times, serif">RTP is transport-protocol independent. By using <code>RTPSocket</code>, you can stream RTP from any underlying network. The format of the RTP socket is designed to have both a data and a control channel. Each channel has an input and output stream to stream data into and out of the underlying network. </font>
</p>
<p>
<a name="107099"> </a><font face="Palatino, Times New Roman, Times, serif"><code>SessionManager</code> expects to receive individual RTP packets from the <code>RTPSocket</code>. Users are responsible for streaming individual RTP packets to the <code>RTPSocket. </code></font>
</p>
<p>
<a name="107108"> </a><font face="Palatino, Times New Roman, Times, serif">To play an RTP stream from the <code>RTPSocket</code>, you pass the socket to M<code>anager.createPlayer</code> to construct the <code>Player</code>. Alternatively, you could construct a <code>Player</code> by calling <code>createPlayer(MediaLocator)</code> and passing in a <code>MediaLocator</code> with a new protocol that is a variant of RTP, <code>"rtpraw"</code>. For example:</font>
</p>
<pre>
<a name="108195"> </a><code>Manager.createPlayer(new MediaLocator("rtpraw://"));
</code></pre>
<p>
<a name="105534"> </a><font face="Palatino, Times New Roman, Times, serif">According to the JMF Player creation mechanism, <code>Manager</code> will attempt to construct the <code>DataSource defined</code> in:</font>
</p>
<pre>
<a name="107189"> </a><code><protocol package-prefix>.media.protocol.rtpraw.DataSource
</code></pre>
<p>
<a name="107190"> </a><font face="Palatino, Times New Roman, Times, serif">This must be the <code>RTPSocket</code>. The content of the <code>RTPsocket</code> should be set to <code>rtpraw. Manager </code>will then attempt to create a player of type<code> <content-prefix>.media.content.rptraw.Handler</code> and set the <code>RTPSocket</code> on it.</font>
</p>
<p>
<a name="107966"> </a><font face="Palatino, Times New Roman, Times, serif">Note: The <code>RTPSocket</code> created at <code><protocol package-prefix>.media.protocol.rtpraw.DataSource </code>is your own implementation of <code>RTPSocket</code>. The JMF API does not define a default implementation of <code>RTPSocket</code>. The implementation of <code>RTPSocket</code> is dependent on the underlying transport protocol that you are using. Your <code>RTPSocket</code> class must be located at <code><protocol package-prefix>.media.protocol.rtpraw.DataSource </code>and its control and data channel streams must be set as shown in the following example.</font>
</p>
<p>
<a name="107980"> </a><font face="Palatino, Times New Roman, Times, serif"><code>RTPControl</code> interfaces for the <code>RTPSocket</code> can be used to add dynamic payload information to the RTP session manager.</font>
</p>
<p>
<a name="107217"> </a><font face="Palatino, Times New Roman, Times, serif">The following example implements an RTP over UDP player that can receive RTP UDP packets and stream them to the <code>Player</code> or session manager, which is not aware of the underlying network/transport protocol. This sample uses the interfaces defined in <code>javax.media.rtp.RTPSocket </code>and its related classes.</font>
</p>
<a name="105787"> </a><font size="1" face="Palatino, Times New Roman, Times, serif">
<table border="1" bordercolorlight="#FFFFFF" bordercolordark="#000000"
cellpadding="5" cellspacing="0">
<caption><b><i><font face="Palatino, Times New Roman, Times, serif"><a name="110866"> </a>Example 9-6: RTPSocketPlayer (1 of 6)</font></i></b></caption>
<tr>
<td><font face="Palatino, Times New Roman, Times, serif"><pre>
<a name="113043"> </a>import java.io.*;
<a name="113044"> </a>import java.net.*;
<a name="113045"> </a>import java.util.*;
<a name="113046"> </a>
<a name="113047"> </a>import javax.media.*;
<a name="113048"> </a>import javax.media.format.*;
<a name="113049"> </a>import javax.media.protocol.*;
<a name="113050"> </a>import javax.media.rtp.*;
<a name="113051"> </a>import javax.media.rtp.event.*;
<a name="113052"> </a>import javax.media.rtp.rtcp.*;
<a name="113053"> </a>
<a name="113054"> </a>public class RTPSocketPlayer implements ControllerListener {
<a name="113055"> </a> // ENTER THE FOLLOWING SESSION PARAMETERS FOR YOUR RTP SESSION
<a name="113056"> </a>
<a name="113057"> </a> // RTP Session address, multicast, unicast or broadcast address
<a name="113058"> </a> String address = "224.144.251.245";
<a name="113059"> </a>
<a name="113060"> </a> // RTP Session port
<a name="113061"> </a> int port = 49150;
<a name="113062"> </a>
<a name="113063"> </a> // Media Type i.e. one of audio or video
<a name="113064"> </a> String media = "audio";
<a name="113065"> </a>
<a name="113066"> </a> // DO NOT MODIFY ANYTHING BELOW THIS LINE
<a name="113067"> </a>
<a name="113068"> </a> // The main rtpsocket abstraction which we will create and send
<a name="113069"> </a> // to the Manager for appropriate handler creation
<a name="113070"> </a> RTPSocket rtpsocket = null;
<a name="113071"> </a>
<a name="113072"> </a> // The control RTPPushDataSource of the above RTPSocket
<a name="113073"> </a> RTPPushDataSource rtcpsource = null;
<a name="113074"> </a>
<a name="113075"> </a> // The GUI to handle the player
<a name="113076"> </a> // PlayerWindow playerWindow;
<a name="113077"> </a>
<a name="113078"> </a> // The handler created for the RTP session,
<a name="113079"> </a> // as returned by the Manager
<a name="113080"> </a> Player player;
<a name="113081"> </a>
<a name="113082"> </a> // maximum size of buffer for UDP receive from the sockets
<a name="113083"> </a> private int maxsize = 2000;
<a name="113085"> </a>
<a name="113086"> </a> UDPHandler rtp = null;
<a name="113087"> </a> UDPHandler rtcp = null;
<a name="113088"> </a>
<a name="113089"> </a> public RTPSocketPlayer() {
<a name="113090"> </a> // create the RTPSocket
<a name="112467"> </a> rtpsocket = new RTPSocket();
</pre>
</font></td>
</tr>
<tr>
<td><font face="Palatino, Times New Roman, Times, serif"><pre>
<a name="113100"> </a> // set its content type :
<a name="113101"> </a> // rtpraw/video for a video session
<a name="113102"> </a> // rtpraw/audio for an audio session
<a name="113103"> </a> String content = "rtpraw/" + media;
<a name="113104"> </a> rtpsocket.setContentType(content);
<a name="113105"> </a>
<a name="113106"> </a> // set the RTP Session address and port of the RTP data
<a name="113107"> </a> rtp = new UDPHandler(address, port);
<a name="113108"> </a>
<a name="113109"> </a> // set the above UDP Handler to be the
<a name="113110"> </a> // sourcestream of the rtpsocket
<a name="113111"> </a> rtpsocket.setOutputStream(rtp);
<a name="113112"> </a>
<a name="113113"> </a> // set the RTP Session address and port of the RTCP data
<a name="113114"> </a> rtcp = new UDPHandler(address, port +1);
<a name="113115"> </a>
<a name="113116"> </a> // get a handle over the RTCP Datasource so that we can
<a name="113117"> </a> // set the sourcestream and deststream of this source
<a name="113118"> </a> // to the rtcp udp handler we created above.
<a name="113119"> </a> rtcpsource = rtpsocket.getControlChannel();
<a name="113120"> </a>
<a name="113121"> </a> // Since we intend to send RTCP packets from the
<a name="113122"> </a> // network to the session manager and vice-versa, we need
<a name="113123"> </a> // to set the RTCP UDP handler as both the input and output
<a name="113124"> </a> // stream of the rtcpsource.
<a name="113125"> </a> rtcpsource.setOutputStream(rtcp);
<a name="113126"> </a> rtcpsource.setInputStream(rtcp);
<a name="113127"> </a>
<a name="113128"> </a> // connect the RTP socket data source before
<a name="113129"> </a> // creating the player
<a name="113130"> </a> try {
<a name="113131"> </a> rtpsocket.connect();
<a name="113132"> </a> player = Manager.createPlayer(rtpsocket);
<a name="113133"> </a> rtpsocket.start();
<a name="113134"> </a> } catch (NoPlayerException e) {
<a name="113135"> </a> System.err.println(e.getMessage());
<a name="113136"> </a> e.printStackTrace();
<a name="113137"> </a> return;
<a name="113138"> </a> }
<a name="113139"> </a> catch (IOException e) {
<a name="113140"> </a> System.err.println(e.getMessage());
<a name="113141"> </a> e.printStackTrace();
<a name="113142"> </a> return;
<a name="113143"> </a> }
<a name="113144"> </a>
<a name="113145"> </a> if (player != null) {
<a name="113146"> </a> player.addControllerListener(this);
<a name="113147"> </a> // send this player to out playerwindow
<a name="113148"> </a> // playerWindow = new PlayerWindow(player);
<a name="113149"> </a> }
<a name="113150"> </a> }
<a name="111097"> </a>
</pre>
</font></td>
</tr>
<tr>
<td><font face="Palatino, Times New Roman, Times, serif"><pre>
<a name="113157"> </a> public synchronized void controllerUpdate(ControllerEvent ce) {
<a name="113158"> </a> if ((ce instanceof DeallocateEvent) ||
<a name="113159"> </a> (ce instanceof ControllerErrorEvent)) {
<a name="113160"> </a>
<a name="113161"> </a> // stop udp handlers
<a name="113162"> </a> if (rtp != null) rtp.close();
<a name="113163"> </a>
<a name="113164"> </a> if (rtcp != null) rtcp.close();
<a name="113165"> </a> }
<a name="113166"> </a> }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -