1 7 8 package org.jboss.media.engine.audio; 9 10 import java.io.File ; 11 import java.io.IOException ; 12 import java.net.MalformedURLException ; 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 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 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 67 public int getBitRate() 68 { 69 return m_bitRate; 70 } 71 72 77 public String getEncoding() 78 { 79 return m_encoding; 80 } 81 82 87 public void setBitRate(int bitRate) 88 { 89 m_bitRate = bitRate; 90 } 91 92 97 public void setEncoding(String encoding) 98 { 99 m_encoding = encoding; 100 } 101 102 107 public int getResolution() 108 { 109 return m_resolution; 110 } 111 112 117 public void setResolution(int resolution) 118 { 119 m_resolution = resolution; 120 } 121 122 126 public int getChannels() 127 { 128 return m_channels; 129 } 130 131 135 public void setChannels(int channels) 136 { 137 m_channels = channels; 138 } 139 140 public void publish() 141 throws 142 NoProcessorException, 143 MalformedURLException , 144 IOException , 145 NoDataSinkException 146 { 147 File file = new File (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 160 configurePluginGraph(); 161 162 boolean encodingOk = checkForEncoding(m_encoding); 166 if (encodingOk) 167 { 168 if (!stateHelper.realize(10000)) 170 { 171 log.error("Failed to realize the processor."); 172 } 173 174 DataSource ds = null; 177 178 ds = m_processor.getDataOutput(); 179 180 String 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 210 private boolean checkForEncoding(String encoding) 211 { 212 TrackControl track[] = m_processor.getTrackControls(); 213 boolean encodingOk = false; 214 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 track[i].setEnabled(false); 240 } 241 } 242 243 return encodingOk; 244 } 245 246 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 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 |