1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
| import javax.media.*;
import javax.media.format.*;
import javax.media.protocol.*;
import javax.media.control.*;
import java.util.*;
import java.io.*;
import javax.media.rtp.*;
import java.net.InetAddress;
import javax.media.control.BufferControl;
public class RtpServer {
/**
* @param args
*/
public static void main(String[] args) {
int i, j;
int nCount=0;
CaptureDeviceInfo infoCaptureDevice;
Format arrFormats [];
// TODO Auto-generated method stub
CaptureDeviceInfo di=null;
System.out.println("test");
// Get the CaptureDeviceInfo for the live audio capture device
Vector deviceList = CaptureDeviceManager.getDeviceList(new
AudioFormat("linear", 22050, 8, 1));
if (deviceList.size()>0)
{
nCount=deviceList.size();
System.out.println("Enumerating devicelist");
for ( i = 0; i < nCount; i++ ) {
infoCaptureDevice = (CaptureDeviceInfo) deviceList.elementAt ( i );
arrFormats = infoCaptureDevice.getFormats ();
for ( j = 0; j < arrFormats.length; j++ ) {
if ( arrFormats[j] instanceof AudioFormat ) {
//vectorAudioDevices.addElement ( infoCaptureDevice );
System.out.println(infoCaptureDevice);
break;
}
}
}
}else{System.exit(0);}
/*cdiAudio = getAudioDevice();
if ( cdiAudio != null && isAudioDeviceUsed() ) {
deviceURL = cdiAudio.getLocator ();
if ( strValue.length() > 0 )
strValue = strValue + " & ";
strValue = strValue + deviceURL.toString ();
}*/
di = (CaptureDeviceInfo) deviceList.elementAt(0);
// Create a Player for the capture device: */
/*try{
Player p = Manager.createPlayer(di.getLocator());
p.start();
} catch (IOException e) {
} catch (NoPlayerException e) {}*/
// Création du MediaLocator à partir du fichier
MediaLocator FichierLocator = di.getLocator();
// Déclaration du processeur
Processor FichierCessor = null;
DataSource ds=null;
try{
//Création du Processor à partir du MediaLocator
try{
ds = Manager.createDataSource(FichierLocator);
}
catch(Exception e){}
// Check to see if there's a buffer control on the data source.
// It could be that we are using a capture data source.
Control c = (Control)ds.getControl("javax.media.control.BufferControl");
if (c != null){
((BufferControl)c).setBufferLength(1);
((BufferControl)c).setMinimumThreshold(2);
((BufferControl)c).setEnabledThreshold(true);
}
FichierCessor = Manager.createProcessor(ds);
//FichierCessor = Manager.createProcessor(ds);
//Appel des fonctions qui vont permettre le lancement du flux RTP
configure(FichierCessor);
SetSupportedFormat(FichierCessor);
//passer dans l'etat realized du processor
realize(FichierCessor);
//start
Demarre(FichierCessor);
launchRTPManager(FichierCessor);
}
catch(IOException e)
{
System.out.println("Erreur : "+e.getMessage());
}
catch(NoProcessorException e)
{
System.out.println("Erreur : "+e.getMessage());
}
}
public static Processor configure(Processor p)
{
//Attendre tant que le Processor n'est pas configuré.
while(p.getState() < Processor.Configured)
{
//Configuration du Processor
p.configure();
}
return p;
}
public static void SetSupportedFormat(Processor p)
{
//On met la description du contenu de sortie à RAW_RTP
ContentDescriptor cd = new ContentDescriptor(ContentDescriptor.RAW_RTP);
p.setContentDescriptor(cd);
//On obtient les différentes pistes du processor
TrackControl track[] = p.getTrackControls();
for(int i = 0; i < track.length; i++)
{
//on obtient les formats supportés pour cette piste
Format suppFormats[] = track[i].getSupportedFormats();
//Si il y a au moins un format supporté
//System.out.println();
for ( int t=0; t < suppFormats.length;t++)
{
System.out.println("RTP supported format: "+suppFormats[t]);
}
if(suppFormats.length > 0)
{
track[i].setFormat(suppFormats[5]);
System.err.println("Track " + i +" est transmis as :"+suppFormats[5]);
if (track[i].getFormat() instanceof AudioFormat) {
int packetRate = 5;
PacketSizeControl pktCtrl = (PacketSizeControl) p.getControl(PacketSizeControl.class.getName());
if (pktCtrl != null) {
try {
pktCtrl.setPacketSize(getPacketSize(track[i].getFormat(), packetRate));
//pktCtrl.setPacketSize(pktCtrl.getPacketSize());
//pktCtrl.setPacketSize(1);
}
catch (IllegalArgumentException e) {
pktCtrl.setPacketSize(80);
// Do nothing
}
}
}
}
else
{
track[i].setEnabled(false);
}
}
}
private static int getPacketSize(Format codecFormat, int milliseconds) throws IllegalArgumentException {
String encoding = codecFormat.getEncoding();
if (encoding.equalsIgnoreCase(AudioFormat.GSM) ||
encoding.equalsIgnoreCase(AudioFormat.GSM_RTP)) {
return milliseconds * 4; // 1 byte per millisec
}
else if (encoding.equalsIgnoreCase(AudioFormat.ULAW) ||
encoding.equalsIgnoreCase(AudioFormat.ULAW_RTP)) {
return milliseconds * 8;
}
else {
throw new IllegalArgumentException("Unknown codec type");
}
}
public static Processor realize(Processor p)
{
//Attendre tant que le Processor n'est pas réalisé.
while(p.getState() < Processor.Realized)
{
//Réalisation du Processor
p.realize();
}
return p;
}
public static void Demarre(Processor p)
{
//Demarrage du Processor
p.start();
System.err.println("Processor started");
}
public static void launchRTPManager(Processor p)
{
//Creation du DataSource correspondant au Processor
DataSource OutputSource = p.getDataOutput();
PushBufferDataSource pbds = (PushBufferDataSource)OutputSource;
PushBufferStream pbss[] = pbds.getStreams();
//Nouvelle Instance d'un RTPManager
RTPManager rtpm[] = new RTPManager[pbss.length];
//System.out.println("taille:" +pbss.length);
//RTPManager rtpm;
for(int i=0; i < pbss.length;i++)
{
try{
rtpm[i] = RTPManager.newInstance();
int port = 22224 + 2*i;
//Création d'une SessionAddress
SessionAddress localaddr = new SessionAddress(InetAddress.getLocalHost(),port);
//Initialisation du RTPManager
rtpm[i].initialize(localaddr);
//Création d'une SessionAddress
SessionAddress destaddr = new SessionAddress(InetAddress.getByName("192.168.1.11"),6666);
//Ajout de cette SessionAddress dans le RTPManager
rtpm[i].addTarget(destaddr);
System.err.println("Creation RTP session 192.168.1.12 port : "+port);
//Creation d'un SendStream à partir du DataSource
SendStream ss2 = rtpm[i].createSendStream(OutputSource,i);
//Demarrage du SendStream
ss2.start();
System.out.println("Started ");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
} |
Partager