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
|
import java.io.IOException;
import java.net.InetAddress;
import javax.media.*;
import javax.media.rtp.*;
import javax.media.format.*;
import javax.media.control.*;
import javax.media.protocol.*;
public class StreamingServer {
public static void main(String[] args) {
//Chemin du fichier
String FichierAdresse = "file:Test.mp3";
//Création du MediaLocator à partir du fichier
MediaLocator FichierLocator = new MediaLocator(FichierAdresse);
//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) {}
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é
if(suppFormats.length > 0) {
track[i].setFormat(suppFormats[0]);
System.err.println("Track " + i +" est transmis as :"+suppFormats[0]);
}
else {
track[i].setEnabled(false);
}
}
}
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("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];
String AdressIp = "localhost";
//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(AdressIp),port);
//Ajout de cette SessionAddress dans le RTPManager
rtpm[i].addTarget(destaddr);
System.err.println("Creation RTP session " + AdressIp + " 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