KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > media > engine > audio > RtpAudioMediaPublisher


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.media.engine.audio;
9
10 import java.io.File JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.net.MalformedURLException JavaDoc;
13
14 import javax.media.Codec;
15 import javax.media.DataSink;
16 import javax.media.Manager;
17 import javax.media.MediaLocator;
18 import javax.media.NoDataSinkException;
19 import javax.media.NoProcessorException;
20 import javax.media.NotConfiguredError;
21 import javax.media.Processor;
22 import javax.media.UnsupportedPlugInException;
23 import javax.media.control.FormatControl;
24 import javax.media.control.TrackControl;
25 import javax.media.format.AudioFormat;
26 import javax.media.protocol.DataSource;
27
28 import jmapps.util.StateHelper;
29
30 import org.jboss.media.engine.MediaPluginGraph;
31 import org.jboss.media.engine.MediaPublisher;
32 import org.jboss.media.engine.MediaPublisherMBean;
33
34 /**
35  * @version <tt>$Revision: 1.3 $</tt>
36  * @author <a HREF="mailto:spyridon_samothrakis@yahoo.com">Spyridon Samothrakis</a>
37  *
38  * @jmx.mbean extends="org.jboss.media.engine.MediaPublisherMBean"
39  */

40 public class RtpAudioMediaPublisher
41    extends MediaPublisher
42    implements MediaPublisherMBean, RtpAudioMediaPublisherMBean
43 {
44
45    private int m_bitRate;
46    private int m_resolution;
47    private String JavaDoc m_encoding;
48    private int m_channels;
49
50    protected Processor m_processor;
51    protected DataSink m_rtpTransmitter;
52
53    protected AudioGenericPluginSupport m_plugin;
54
55    public RtpAudioMediaPublisher()
56    {
57       m_plugin = new AudioGenericPluginSupport();
58    }
59
60    // Accessors :)
61
///////////////////////////////////////////////////////////////////////////////
62
/**
63     * Returns the bitRate.
64     * @return int
65     * @jmx:managed-attribute
66     */

67    public int getBitRate()
68    {
69       return m_bitRate;
70    }
71
72    /**
73     * Returns the encoding.
74     * @return String
75     * @jmx:managed-attribute
76     */

77    public String JavaDoc getEncoding()
78    {
79       return m_encoding;
80    }
81
82    /**
83     * Sets the bitRate.
84     * @param bitRate The bitRate to set
85     * @jmx:managed-attribute
86     */

87    public void setBitRate(int bitRate)
88    {
89       m_bitRate = bitRate;
90    }
91
92    /**
93     * Sets the encoding.
94     * @param encoding The encoding to set
95     * @jmx:managed-attribute
96     */

97    public void setEncoding(String JavaDoc encoding)
98    {
99       m_encoding = encoding;
100    }
101
102    /**
103     * Returns the resolution.
104     * @return int
105     * @jmx:managed-attribute
106     */

107    public int getResolution()
108    {
109       return m_resolution;
110    }
111
112    /**
113     * Sets the resolution.
114     * @param resolution The resolution to set
115     * @jmx:managed-attribute
116     */

117    public void setResolution(int resolution)
118    {
119       m_resolution = resolution;
120    }
121
122    /**
123     * @return
124     * @jmx:managed-attribute
125     */

126    public int getChannels()
127    {
128       return m_channels;
129    }
130
131    /**
132     * @param string
133     * @jmx:managed-attribute
134     */

135    public void setChannels(int channels)
136    {
137       m_channels = channels;
138    }
139
140    public void publish()
141       throws
142          NoProcessorException,
143          MalformedURLException JavaDoc,
144          IOException JavaDoc,
145          NoDataSinkException
146    {
147       // Give me a processor
148
// For the moment just crush and burn . No exception handling
149
File JavaDoc file = new File JavaDoc(getFileName());
150       m_processor = Manager.createProcessor(new MediaLocator(file.toURL()));
151       StateHelper stateHelper;
152       stateHelper = new StateHelper(m_processor);
153
154       if (!stateHelper.configure(10000))
155       {
156          log.error("Failed to configure the processor.");
157       }
158       //wait until configured and get ready to fight
159

160       configurePluginGraph();
161
162       // At this point, we have determined where we can send out
163
// ulaw data or not.
164
// realize the processor
165
boolean encodingOk = checkForEncoding(m_encoding);
166       if (encodingOk)
167       {
168          // block until realized.
169
if (!stateHelper.realize(10000))
170          {
171             log.error("Failed to realize the processor.");
172          }
173
174          // get the output datasource of the processor and exit
175
// if we fail
176
DataSource ds = null;
177
178          ds = m_processor.getDataOutput();
179
180          String JavaDoc url = "rtp://" + getHost() + ":" + getPort() + getContext();
181          MediaLocator m = new MediaLocator(url);
182          m_rtpTransmitter = Manager.createDataSink(ds, m);
183          log.debug(m_rtpTransmitter);
184          m_rtpTransmitter.open();
185          m_rtpTransmitter.start();
186
187          ds.start();
188          m_processor.start();
189       }
190
191       log.info("Transmission of " + getFileName() + " " + "started");
192    }
193
194    public void stop()
195    {
196       if (m_processor != null)
197       {
198          m_processor.stop();
199          m_processor.close();
200          m_processor = null;
201          m_rtpTransmitter.close();
202          m_rtpTransmitter = null;
203       }
204
205       log.info("Transmission of " + getFileName() + " " + "stopped");
206    }
207
208    // Helper functions
209

210    private boolean checkForEncoding(String JavaDoc encoding)
211    {
212       TrackControl track[] = m_processor.getTrackControls();
213       boolean encodingOk = false;
214       // Go through the tracks and try to program one of them to
215
// output ulaw data.
216
for (int i = 0; i < track.length; i++)
217       {
218          if (!encodingOk && track[i] instanceof FormatControl)
219          {
220             if (((FormatControl) track[i])
221                .setFormat(
222                   new AudioFormat(
223                      getEncoding(),
224                      getResolution(),
225                      getBitRate(),
226                      getChannels()))
227                == null)
228             {
229                track[i].setEnabled(false);
230             }
231             else
232             {
233                encodingOk = true;
234             }
235          }
236          else
237          {
238             // we could not set this track to correct encoding, so disable it
239
track[i].setEnabled(false);
240          }
241       }
242
243       return encodingOk;
244    }
245
246    /* (non-Javadoc)
247     * @see org.jboss.media.engine.MediaPublisher#addPluginGraph(org.jboss.media.engine.MediaPluginGraph)
248     */

249    public void addPluginGraph(MediaPluginGraph pg)
250    {
251       m_plugin.setPluginGraph(pg);
252    }
253
254    private void configurePluginGraph()
255    {
256
257       if (m_plugin == null)
258          return;
259       // add the plugins
260
TrackControl tc[] = m_processor.getTrackControls();
261
262       if (tc == null)
263       {
264          log.error("Failed to obtain track controls from the processor.");
265       }
266       else
267       {
268          TrackControl audioTrack = null;
269          for (int i = 0; i < tc.length; i++)
270          {
271             if (tc[i].getFormat() instanceof AudioFormat)
272             {
273                audioTrack = tc[i];
274                break;
275             }
276          }
277          try
278          {
279             audioTrack.setCodecChain(new Codec[] { m_plugin });
280          }
281          catch (UnsupportedPlugInException e)
282          {
283             log.error("The processor does not support effects.");
284          }
285          catch (NotConfiguredError e)
286          {
287             log.error("Processor not configured", e);
288          }
289       }
290    }
291
292 }
Popular Tags