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

📄 rtpsending.html

📁 奉献给多媒体java编程者们。JMF2.1.1最新版本的用户指南。JMF是java用于基于实时多媒体的开发工具
💻 HTML
📖 第 1 页 / 共 5 页
字号:
</code><a name="110008"> </a><code>        
</code><a name="110009"> </a><code>       TrackControl track[] = processor.getTrackControls(); 
</code><a name="110010"> </a><code>       
</code><a name="110011"> </a><code>       boolean encodingOk = false;
</code><a name="110012"> </a><code>       
</code><a name="110013"> </a><code>       // Go through the tracks and try to program one of them to
</code><a name="110014"> </a><code>       // output gsm data. 
</code><a name="110015"> </a><code>       
</code><a name="110016"> </a><code>        for (int i = 0; i &lt; track.length; i++) { 
</code><a name="107984"> </a><code>            if (!encodingOk &amp;&amp; track[i] instanceof FormatControl) {  
</code></pre>
</font></td>
  </tr>
  <tr>
    <td><font face="Palatino, Times New Roman, Times, serif"><pre>
<a name="108048"> </a><code>                if (((FormatControl)track[i]).
</code><a name="108049"> </a><code>                    setFormat( new AudioFormat(AudioFormat.GSM_RTP, 
</code><a name="108050"> </a><code>                                               8000, 
</code><a name="108051"> </a><code>                                               8, 
</code><a name="108052"> </a><code>                                               1)) == null) {
</code><a name="108053"> </a>
<a name="110027"> </a><code>                   track[i].setEnabled(false); 
</code><a name="110028"> </a><code>                }
</code><a name="110029"> </a><code>                else {
</code><a name="110030"> </a><code>                    encodingOk = true; 
</code><a name="110031"> </a><code>                }
</code><a name="110032"> </a><code>            } else { 
</code><a name="110033"> </a><code>                // we could not set this track to gsm, so disable it 
</code><a name="110034"> </a><code>                track[i].setEnabled(false); 
</code><a name="110035"> </a><code>            } 
</code><a name="110036"> </a><code>        }
</code><a name="110037"> </a><code>        
</code><a name="110038"> </a><code>        // At this point, we have determined where we can send out 
</code><a name="110039"> </a><code>        // gsm data or not. 
</code><a name="110040"> </a><code>        // realize the processor 
</code><a name="110041"> </a><code>        if (encodingOk) { 
</code><a name="110042"> </a><code>            processor.realize(); 
</code><a name="110043"> </a><code>            // block until realized. 
</code><a name="110044"> </a><code>            // get the output datasource of the processor and exit 
</code><a name="110045"> </a><code>            // if we fail 
</code><a name="110046"> </a><code>            DataSource ds = null;
</code><a name="110047"> </a><code>            
</code><a name="110048"> </a><code>            try { 
</code><a name="110049"> </a><code>                ds = processor.getDataOutput(); 
</code><a name="110050"> </a><code>            } catch (NotRealizedError e) { 
</code><a name="110051"> </a><code>                System.exit(-1);
</code><a name="110052"> </a><code>            }
</code><a name="110053"> </a>
<a name="110054"> </a><code>            // hand this datasource to manager for creating an RTP 
</code><a name="110055"> </a><code>            // datasink our RTP datasimnk will multicast the audio 
</code><a name="110056"> </a><code>            try {
</code><a name="110057"> </a><code>                String url= "rtp://224.144.251.104:49150/audio/1";
</code><a name="110058"> </a>
<a name="110059"> </a><code>                MediaLocator m = new MediaLocator(url);
</code><a name="110060"> </a>
<a name="110061"> </a><code>                DataSink d = Manager.createDataSink(ds, m);
</code><a name="110062"> </a>
<a name="110063"> </a><code>                d.open();
</code><a name="110064"> </a><code>                d.start(); 
</code><a name="110065"> </a><code>            } catch (Exception e) {
</code><a name="110066"> </a><code>                System.exit(-1);
</code><a name="110067"> </a><code>            }     
</code><a name="108093"> </a><code>        }    
</code></pre>
</font></td>
  </tr>
</table>



<br></font>


<h3>
  <a name="105131"> </a><font color="#003366" face="Palatino, Times New Roman, Times, serif">Transmitting RTP Data with the Session Manager</font>
</h3>


<p>
  <a name="105134"> </a><font face="Palatino, Times New Roman, Times, serif">The basic process for transmitting RTP data with the session manager is:</font>
</p>

<ol type="1">
  <li value="1"><a name="105135"> </a><font face="Palatino, Times New Roman, Times, serif">Create a JMF <code>Processor</code> and set each track format to an RTP-specific format. </font>
  <li value="2"><a name="105136"> </a><font face="Palatino, Times New Roman, Times, serif">Retrieve the output <code>DataSource</code> from the <code>Processor</code>. </font>
  <li value="3"><a name="105137"> </a><font face="Palatino, Times New Roman, Times, serif">Call <code>createSendStream</code> on a previously created and initialized <code>SessionManager</code>, passing in the <code>DataSource</code> and a stream index. The session manager creates a <code>SendStream</code> for the specified <code>SourceStream</code>. </font>
  <li value="4"><a name="106981"> </a><font face="Palatino, Times New Roman, Times, serif">Start the session manager by calling <code>SessionManager</code> <code>startSession</code>.</font>
  <li value="5"><a name="105138"> </a><font face="Palatino, Times New Roman, Times, serif">Control the transmission through the <code>SendStream</code> methods. A <code>SendStreamListener</code> can be registered to listen to events on the <code>SendStream</code>.</font>
</ol>

<h4>
  <a name="105159"> </a><font color="#003366" face="Palatino, Times New Roman, Times, serif">Creating a Send Stream</font>
</h4>


<p>
  <a name="105160"> </a><font face="Palatino, Times New Roman, Times, serif">Before the session manager can transmit data, it needs to know where to get the data to transmit. When you construct a new <code>SendStream, </code>you hand the <code>SessionManager</code> the <code>DataSource</code> from which it will acquire the data. Since a <code>DataSource</code> can contain multiple streams, you also need to specify the index of the stream to be sent in this session. You can create multiple send streams by passing different <code>DataSources</code> to<code> createSendStream</code> or by specifying different stream indexes.</font>
</p>


<p>
  <a name="106080"> </a><font face="Palatino, Times New Roman, Times, serif">The session manager queries the format of the <code>SourceStream</code> to determine if it has a registered payload type for this format. If the format of the data is not an RTP format or a payload type cannot be located for the RTP format, an <code>UnSupportedFormatException</code> is thrown with the appropriate message. Dynamic payloads can be associated with an RTP format using the <code>SessionManager</code> <code>addFormat</code> method </font>
</p>


<h5>
  <a name="106158"> </a><i><font color="#003366" face="Palatino, Times New Roman, Times, serif">Using Cloneable Data Sources</font></i>
</h5>


<p>
  <a name="106159"> </a><font face="Palatino, Times New Roman, Times, serif">Many RTP usage scenarios involve sending a stream over multiple RTP sessions or encoding a stream into multiple formats and sending them over multiple RTP sessions. When a stream encoded in a single format has to be sent over multiple RTP sessions, you need to clone the <code>DataSource</code> output from the <code>Processor</code> from which data is being captured. This is done by creating a cloneable <code>DataSource</code> through the <code>Manager</code> and calling <code>getClone</code> on the cloneable <code>DataSource</code>. A new <code>Processor</code> can be created from each cloned <code>DataSource</code>, its tracks encoded in the desired format, and the stream sent out over an RTP session.</font>
</p>


<h5>
  <a name="106160"> </a><i><font color="#003366" face="Palatino, Times New Roman, Times, serif">Using Merging Data Sources</font></i>
</h5>


<p>
  <a name="106161"> </a><font face="Palatino, Times New Roman, Times, serif">If you want to mix multiple media streams of the same type (such as audio) into a single stream going out from one source, you need to use an RTP mixer. If the streams to be mixed originate from multiple <code>DataSource</code>s, you can create a <code>MergingDataSource</code> from the separate <code>DataSource</code>s and hand it to the <code>SessionManager</code> to create the stream.</font>
</p>


<h4>
  <a name="105162"> </a><font color="#003366" face="Palatino, Times New Roman, Times, serif">Controlling a Send Stream</font>
</h4>


<p>
  <a name="105163"> </a><font face="Palatino, Times New Roman, Times, serif">You use the <code>RTPStream</code> <code>start</code> and <code>stop</code> methods to control a <code>SendStream</code>. Starting a <code>SendStream</code> begins data transfer over the network and stopping a <code>SendStream</code> indicates halts the data transmission. To begin an RTP transmission, each <code>SendStream</code> needs to be started. </font>
</p>


<p>
  <a name="106133"> </a><font face="Palatino, Times New Roman, Times, serif">Starting or stopping a send stream triggers the corresponding action on its <code>DataSource</code>. However, if the <code>DataSource</code> is started independently while the <code>SendStream</code> is stopped, data will be dropped (<code>PushBufferDataSource</code>) or not pulled (<code>PullBufferDataSource</code>) by the session manager. During this time, no data will be transmitted over the network.</font>
</p>


<h4>
  <a name="104197"> </a><font color="#003366" face="Palatino, Times New Roman, Times, serif">Sending Captured Audio Out in a Single Session</font>
</h4>


<p>
  <a name="104198"> </a><font face="Palatino, Times New Roman, Times, serif"><a href="RTPSending.html#108111">Example 10-2</a> captures mono audio data and sends it out on an RTP session.</font>
</p>


<a name="108108"> </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="108111"> </a>Example 10-2:   Sending captured audio out on a single session  (1 of 3)</font></i></b></caption>
  <tr>
    <td><font face="Palatino, Times New Roman, Times, serif"><pre>
<a name="108144"> </a><code>        // First, we'll need a DataSource that captures live audio: 
</code><a name="108145"> </a><code>       
</code><a name="108146"> </a><code>        AudioFormat format = new AudioFormat(AudioFormat.ULAW, 
</code><a name="108147"> </a><code>                                             8000, 
</code><a name="108148"> </a><code>                                             8, 
</code><a name="108149"> </a><code>                                             1); 
</code><a name="108150"> </a><code> 
</code><a name="108151"> </a><code>        Vector devices= CaptureDeviceManager.getDeviceList( format);
</code><a name="108152"> </a>
<a name="108153"> </a><code>        CaptureDeviceInfo di= null;
</code><a name="110081"> </a><code>        if (devices.size() &gt; 0) {
</code><a name="110082"> </a><code>             di = (CaptureDeviceInfo) devices.elementAt( 0);
</code><a name="110083"> </a><code>        }
</code></pre>
</font></td>
  </tr>
  <tr>
    <td><font face="Palatino, Times New Roman, Times, serif"><pre>
<a name="108962"> </a><code>        else {
</code><a name="108963"> </a><code>            // exit if we could not find the relevant capture device.
</code><a name="108964"> </a><code>            System.exit(-1); 
</code><a name="108965"> </a><code>        }
</code><a name="108165"> </a><code>        // Create a processor for this capture device &amp; exit if we 
</code><a name="108166"> </a><code>        // cannot create it 
</code><a name="108167"> </a><code>        try { 
</code><a name="108168"> </a><code>            Processor p = Manager.createProcessor(di.getLocator()); 
</code><a name="108169"> </a><code>        } catch (IOException e) { 
</code><a name="108170"> </a><code>            System.exit(-1); 
</code><a name="108171"> </a><code>        } catch (NoProcessorException e) { 
</code><a name="108172"> </a><code>            System.exit(-1); 
</code><a name="108173"> </a><code>        } 
</code><a name="108174"> </a>
<a name="108175"> </a><code>        // at this point, we have succesfully created the processor. 
</code><a name="108176"> </a><code>        // Realize it and block until it is configured. 
</code><a name="108177"> </a><code>       
</code><a name="108178"> </a><code>        processor.configure(); 
</code><a name="108179"> </a><code>       
</code><a name="108180"> </a><code>        // block until it has been configured 
</code><a name="108181"> </a><code>       
</code><a name="108182"> </a><code>        processor.setContentDescriptor( 
</code><a name="108183"> </a><code>            new ContentDescriptor( ContentDescriptor.RAW));
</code><a name="108184"> </a><code>       
</code><a name="108185"> </a><code>        TrackControl track[] = processor.getTrackControls();
</code><a name="108186"> </a><code>       
</code><a name="108187"> </a><code>        boolean encodingOk = false;

⌨️ 快捷键说明

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