⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtpexporting.html

📁 奉献给多媒体java编程者们。JMF2.1.1最新版本的用户指南。JMF是java用于基于实时多媒体的开发工具
💻 HTML
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="GENERATOR" content="Quadralay WebWorks Publisher 5.0.2">
<meta name="TEMPLATEBASE" content="Portable HTML">
<meta name="LASTUPDATED" content="11/23/99 11:48:12">
<title>Importing and Exporting RTP Media Streams  </title>
</head>

<body link="#3366CC" vlink="#9999CC" text="#000000" alink="#0000CC" bgcolor="#FFFFFF"
background="images/backgrnd.gif">


<table width="100%" border="0" align="left" cellpadding="0" cellspacing="0">
  <tr>
    <td><a href="JMFTOC.html">CONTENTS</a> | 
    <a href="RTPSending.html">PREV </a> |
    <a href="RTPExtending.html">NEXT</a> |
    <a href="JMFIX.html">INDEX</a></td>
    <td align="right"><em>JMF 2.0 API Guide</em>
  </tr>
</table>

<p><br clear="all">
</p>

<hr align="left">

<blockquote>
<div align="right">
<a name="104017"> </a><font  size="3" face="Palatino, Times New Roman, Times, serif">11 <br></font>
</div>
<div align="right">
<h2>
  <a name="105074"> </a><font color="#003366" face="Palatino, Times New Roman, Times, serif">Importing and Exporting RTP Media Streams</font>
</h2>
</div>

<p>
  <a name="104305"> </a><font face="Palatino, Times New Roman, Times, serif">Many applications need to be able to read and write RTP streams. For example, conferencing application might record a conference and broadcast it at a later time, or telephony applications might transmit stored audio streams for announcement messages or hold music. </font>
</p>


<p>
  <a name="104306"> </a><font face="Palatino, Times New Roman, Times, serif">You can save RTP streams received from the network to a file using an RTP file writer <code>DataSink</code>. Similarly, you can read saved files and either present them locally or transmit them across the network. </font>
</p>


<h3>
  <a name="104235"> </a><font color="#003366" face="Palatino, Times New Roman, Times, serif">Reading RTP Media Streams from a File</font>
</h3>


<p>
  <a name="104343"> </a><font face="Palatino, Times New Roman, Times, serif">To read data from a file and present or transmit it, you can use a <code>MediaLocator</code> that identifies the file to construct a <code>DataSource</code>, or use the <code>MediaLocator</code> to directly construct your <code>Processor</code>. The file types that can be used for RTP transmissions depend on what codec plug-ins you have available to transcode and packetize the data into an RTP-specific format. </font>
</p>


<a name="104047"> </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="105359"> </a>Example 11-1:   Reading RTP streams from a file  (1 of 3)</font></i></b></caption>
  <tr>
    <td><font face="Palatino, Times New Roman, Times, serif"><pre>
<a name="105654"> </a><code>        // Create a Processor for the selected file. Exit if the 
</code><a name="105655"> </a><code>        // Processor cannot be created.
</code><a name="105656"> </a><code>        try { 
</code><a name="105657"> </a><code>            String url= "file:/home/foo/foo.au";
</code><a name="105658"> </a><code>
</code><a name="105659"> </a><code>            processor 
</code><a name="105660"> </a><code>              = Manager.createProcessor( new MediaLocator(url)); 
</code><a name="105661"> </a><code>        } catch (IOException e) { 
</code><a name="105400"> </a><code>            System.exit(-1); 
</code></pre>
</font></td>
  </tr>
  <tr>
    <td><font face="Palatino, Times New Roman, Times, serif"><pre>
<a name="105746"> </a><code>        } catch (NoProcessorException e) { 
</code><a name="105747"> </a><code>            System.exit(-1); 
</code><a name="105748"> </a><code>        } 
</code><a name="105749"> </a><code>        
</code><a name="105750"> </a><code>        // configure the processor
</code><a name="105751"> </a><code>        processor.configure(); 
</code><a name="105752"> </a><code>
</code><a name="105753"> </a><code>        // Block until the Processor has been configured 
</code><a name="105754"> </a><code>
</code><a name="105755"> </a><code>        TrackControl track[] = processor.getTrackControls(); 
</code><a name="105756"> </a><code>        
</code><a name="105757"> </a><code>        boolean encodingOk = false;
</code><a name="105758"> </a><code>        
</code><a name="105759"> </a><code>        // Go through the tracks and try to program one of them to
</code><a name="105760"> </a><code>        // output ulaw data. 
</code><a name="105761"> </a><code>        for (int i = 0; i &lt; track.length; i++) { 
</code><a name="105762"> </a><code>            if (!encodingOk &amp;&amp; track[i] instanceof FormatControl) { 
</code><a name="105763"> </a><code>                       
</code><a name="105764"> </a><code>                if (((FormatControl)track[i]).
</code><a name="105765"> </a><code>                    setFormat( new AudioFormat(AudioFormat.ULAW_RTP, 
</code><a name="105766"> </a><code>                                               8000, 
</code><a name="105767"> </a><code>                                               8, 
</code><a name="105768"> </a><code>                                               1)) == null) {
</code><a name="105769"> </a><code>
</code><a name="105770"> </a><code>                    track[i].setEnabled(false); 
</code><a name="105771"> </a><code>                }
</code><a name="105772"> </a><code>                else {
</code><a name="105773"> </a><code>                    encodingOk = true; 
</code><a name="105774"> </a><code>                }
</code><a name="105775"> </a><code>            } 
</code><a name="105776"> </a><code>            else { 
</code><a name="105777"> </a><code>                // we could not set this track to ulaw, so disable it 
</code><a name="105778"> </a><code>                track[i].setEnabled(false); 
</code><a name="105779"> </a><code>            } 
</code><a name="105780"> </a><code>        }
</code><a name="105781"> </a><code>        
</code><a name="105782"> </a><code>        // At this point, we have determined where we can send out 
</code><a name="105783"> </a><code>        // ulaw data or not. 
</code><a name="105784"> </a><code>        // realize the processor 
</code><a name="105785"> </a><code>        
</code><a name="105786"> </a><code>        if (encodingOk) { 
</code><a name="105787"> </a><code>            processor.realize(); 
</code><a name="105788"> </a><code>            
</code><a name="105789"> </a><code>            // block until realized. 
</code><a name="105790"> </a><code>            // get the output datasource of the processor and exit 
</code><a name="105791"> </a><code>            // if we fail 
</code><a name="105792"> </a><code>            DataSource ds = null;
</code><a name="105793"> </a><code>            
</code><a name="105794"> </a><code>            try { 
</code><a name="105795"> </a><code>                ds = processor.getDataOutput(); 
</code><a name="105796"> </a><code>            } catch (NotRealizedError e) { 
</code><a name="105797"> </a><code>                System.exit(-1);
</code><a name="105798"> </a><code>            }
</code></pre>
</font></td>
  </tr>
  <tr>
    <td><font face="Palatino, Times New Roman, Times, serif"><pre>
<a name="105807"> </a><code>            // hand this datasource to manager for creating an RTP 
</code><a name="105808"> </a><code>            // datasink.
</code><a name="105809"> </a><code>            // our RTP datasink will multicast the audio 
</code><a name="105810"> </a><code>            
</code><a name="105811"> </a><code>            try {
</code><a name="105812"> </a><code>                String url= "rtp://224.144.251.104:49150/audio/1";
</code><a name="105813"> </a><code>
</code><a name="105814"> </a><code>                MediaLocator m = new MediaLocator(url);
</code><a name="105815"> </a><code>
</code><a name="105816"> </a><code>                DataSink d = Manager.createDataSink(ds, m);
</code><a name="105817"> </a><code>
</code><a name="105818"> </a><code>                d.open();
</code><a name="105819"> </a><code>                d.start(); 
</code><a name="105820"> </a><code>            } catch (Exception e) {
</code><a name="105821"> </a><code>                System.exit(-1);
</code><a name="105822"> </a><code>            }
</code><a name="105504"> </a><code>        }
</code></pre>
</font></td>
  </tr>
</table>



<br></font>


<h3>
  <a name="105356"> </a><font color="#003366" face="Palatino, Times New Roman, Times, serif">Exporting RTP Media Streams</font>
</h3>


<p>
  <a name="104382"> </a><font face="Palatino, Times New Roman, Times, serif">RTP streams received from the network can be stored as well as presented. To write the data to a file, you retrieve the <code>DataSource</code> from the <code>ReceiveStream</code> and use it to create a file writing <code>DataSink</code> through the <code>Manager</code>.</font>
</p>


<p>
  <a name="104385"> </a><font face="Palatino, Times New Roman, Times, serif">If you want to transcode the data before storing it, you can use the <code>DataSource</code> retrieved from the <code>ReceiveStream</code> to construct a <code>Processor</code>. You then:</font>
</p>

<ol type="1">
  <li value="1"><a name="104388"> </a><font face="Palatino, Times New Roman, Times, serif">Set the track formats to perform the desired encoding.</font>
  <li value="2"><a name="104389"> </a><font face="Palatino, Times New Roman, Times, serif">Get the output <code>DataSource</code> from the <code>Processor</code>.</font>
  <li value="3"><a name="104390"> </a><font face="Palatino, Times New Roman, Times, serif">Construct an RTP file writer with the <code>DataSource</code>.</font>
</ol>

<p>
  <a name="104414"> </a><font face="Palatino, Times New Roman, Times, serif">In the following example, whenever a new stream is created in the session:</font>
</p>

<ol type="1">
  <li value="1"><a name="104415"> </a><font face="Palatino, Times New Roman, Times, serif">The stream is retrieved from <code>NewReceiveStreamEvent</code>.</font>
  <li value="2"><a name="104416"> </a><font face="Palatino, Times New Roman, Times, serif">The <code>DataSource</code> is acquired from the <code>ReceiveStream</code>.</font>
  <li value="3"><a name="104474"> </a><font face="Palatino, Times New Roman, Times, serif">The <code>DataSource</code> is passed to the <code>Manager.createDataSink</code> method along with a <code>MediaLocator</code> that identifies the file where we want to store the data.</font>
</ol>

<p>
  <a name="104821"> </a><font face="Palatino, Times New Roman, Times, serif">This example handles a single track. To write a file that contains both audio and video tracks, you need to retrieve the audio and video streams from the separate session managers and create a merging <code>DataSource</code> that carries both of the streams. Then you hand the merged <code>DataSource</code> to <code>Manager.createDataSink.</code> </font>
</p>


<a name="104677"> </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="105519"> </a>Example 11-2:   Writing an RTP stream to a file</font></i></b></caption>
  <tr>
    <td><font face="Palatino, Times New Roman, Times, serif"><pre>
<a name="105827"> </a><code>    public void update(ReceiveStreamEvent event) {
</code><a name="105828"> </a><code>        // find the source session manager for this event
</code><a name="105829"> </a><code>        SessionManager source = (SessionManager)event.getSource();
</code><a name="105830"> </a><code>       
</code><a name="105831"> </a><code>        // create a filewriter datasink if a new ReceiveStream 
</code><a name="105832"> </a><code>        // is detected
</code><a name="105833"> </a><code>        if (event instanceof NewReceiveStreamEvent) {
</code><a name="105834"> </a><code>            String cname = "Java Media Player";
</code><a name="105835"> </a><code>            ReceiveStream stream = null;
</code><a name="105836"> </a><code>            
</code><a name="105837"> </a><code>            try {
</code><a name="105838"> </a><code>                // get the ReceiveStream
</code><a name="105839"> </a><code>                stream =((NewReceiveStreamEvent)event)
</code><a name="105840"> </a><code>                        .getReceiveStream();
</code><a name="105841"> </a><code>
</code><a name="105842"> </a><code>                Participant part = stream.getParticipant();
</code><a name="105843"> </a><code>
</code><a name="105844"> </a><code>                // get the ReceiveStream datasource
</code><a name="105845"> </a><code>                DataSource dsource = stream.getDataSource();
</code><a name="105846"> </a><code>
</code><a name="105847"> </a><code>                // hand this datasource over to a file datasink
</code><a name="105848"> </a><code>                MediaLocator f = new MediaLocator("file://foo.au");
</code><a name="105849"> </a><code>
</code><a name="105850"> </a><code>                Manager.createDataSink(dsource, f);    
</code><a name="105851"> </a><code>            } catch (Exception e) {
</code><a name="105852"> </a><code>                System.err.println("newReceiveStreamEvent exception " 
</code><a name="105853"> </a><code>                                   + e.getMessage());
</code><a name="105854"> </a><code>                return;
</code><a name="105855"> </a><code>            }
</code><a name="105856"> </a><code>        }
</code><a name="105521"> </a><code>    }
</code></pre>
</font></td>
  </tr>
</table>



      <br></font>
</blockquote>
<br clear="all">
<hr>
<a href="JMFTOC.html">CONTENTS</a> | 
<a href="RTPSending.html">PREV </a> |
<a href="RTPExtending.html">NEXT</a> |
<a href="JMFIX.html">INDEX</a></td>
<br>
<hr>
<em>
<a href="copyright.html">Copyright</a> &copy;
1998-1999 Sun Microsystems, Inc. All Rights Reserved.
</em>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -