|
From: <mar...@us...> - 2003-03-13 23:47:39
|
Update of /cvsroot/madsserv/madsserv/src/server/processor
In directory sc8-pr-cvs1:/tmp/cvs-serv28323
Modified Files:
Reencode.java ThreadReencode.java
Log Message:
Module utilisable mais l_encodage dans un format voulu n_est pas encore implemente
Index: Reencode.java
===================================================================
RCS file: /cvsroot/madsserv/madsserv/src/server/processor/Reencode.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** Reencode.java 13 Mar 2003 01:53:00 -0000 1.8
--- Reencode.java 13 Mar 2003 23:47:25 -0000 1.9
***************
*** 1,405 ****
! /**
! * Projet Madsserv
! * Reencode.java
! *
! * Module de conversion des fichiers multimedia.
! * Reencode un fichier multimedia dans un format choisi.
! *
! * @author Jean-Baptiste Mariotte
! */
!
! import java.awt.*;
! import java.io.File;
! import javax.media.*;
! import javax.media.Format;
! import javax.media.format.*;
! import javax.media.protocol.*;
! import javax.media.protocol.DataSource;
! import javax.media.control.*;
! import java.io.IOException;
!
! /**
! * Classe Reencode
! */
! public class Reencode implements ControllerListener {
!
! /**
! * Champs de la classe Reencode
! */
! public Streaming streamingRef; // Reference du module Streaming frere.
! public Transition transitionRef; //Reference du module Transition frere.
! public boolean multiMedia = false; // Indique si le fichier est mulimedia.
! public boolean reencoding = false; // Indique s'il y a conversion en cours.
! public ThreadReencode formerThreadReencode = null; // Precedent thread de conversion.
! public ThreadReencode currentThreadReencode = null; // Thread de conversion actuel.
!
! private String inputURL = null; // Adresse du fichier demande.
! private int clientId = -1; // Numero du client demandeur.
! private MediaLocator inputML = null; // MediaLocator du fichier a convertir.
! private DataSource inputDS = null; // DataSource contenant le fichier a convertir.
! private TrackControl[] inputTC = null; // TrackControl permettant d'acceder aux differentes pistes du media.
! private Format[] inputFormat = null; // Vecteur de Format pour stocker les formats des pistes.
! private AudioFormat originalAudioFormat = null; // Format audio original du fichier a convertir.
! private VideoFormat originalVideoFormat = null; // Format video original du fichier a convertir.
! private AudioFormat reencodingAudioFormat = null; // Format audio de reencodage du fichier.
! private VideoFormat reencodingVideoFormat = null; // Format video de reencodage du fichier.
! private Time startTime; // Instant de debut de conversion.
!
!
! /**
! * Constructeur de la classe Reencode.
! * @param clientId l'ID du client associe au module
! * @param inputURL l'URL demande par le client
! */
! Reencode(int clientId, String inputURL)
! {
! // Stockage de l'ID du client correspondant a l'instantiation.
! this.clientId = clientId;
! if(this.clientId < 0)
! {
! System.err.println("L'ID du client n'a pas été transmis correctement.");
! System.exit(0);
! }
! // Stockage de l'URL demande correspondant a l'instantiation.
! this.inputURL = inputURL;
! if(this.inputURL == null)
! {
! System.err.println("L'URL à lire n'a pas été transmis correctement.");
! System.exit(0);
! }
! System.err.println("Module de conversion construit\n");
!
! // Conversion de l'URL en MediaLocator.
! if((inputML = createMediaLocator(inputURL)) == null)
! {
! System.err.println("Impossible de construire le MediaLocator à partir de : " + inputURL);
! System.exit(0);
! }
!
! // Determination des formats d'origine du fichier demande.
! // Pour cela, on cree un Processor que l'on place dans l'etat configure.
! // Source inspire de la classe Transcode publiee par Sun.
! // http://java.sun.com/products/java-media/jmf/2.1.1/solutions/Transcode.html
! Processor p = null;
!
! try
! {
! p = Manager.createProcessor(inputML);
! System.err.println("- create processor for: " + inputML);
! }
! catch (Exception e)
! {
! System.err.println("Yikes! Cannot create a processor from the given url: " + e);
! }
!
! p.addControllerListener(this);
!
! // Put the Processor into configured state.
! p.configure();
! if (!waitForState(p, p.Configured))
! {
! System.err.println("Failed to configure the processor.");
! }
!
! // A l'aide de TrackControl, on accede aux differentes pistes du media.
! try
! {
! if ( (inputTC = p.getTrackControls()) == null)
! {
! System.err.println("Il n'y a aucune piste dans le fichier !");
! }
! else
! {
! if(inputTC.length > 2)
! {
! System.err.println("Il y a trop de pistes dans le fichier !");
! }
! else
! {
! inputFormat = new Format[inputTC.length];
! if(inputTC.length == 1)
! {
! System.err.println("Il n'y a qu'une seule piste");
! setOriginalAudioFormat( (AudioFormat) inputFormat[0]); //FIXME si video sans son
! }
! else //FIXME si pistes video et son dans un autre ordre
! {
! inputFormat[0] = inputTC[0].getFormat();
! System.err.println("Format piste n 1 : " + inputFormat[0].toString());
! setOriginalVideoFormat( (VideoFormat) inputFormat[0]);
! inputFormat[1] = inputTC[1].getFormat();
! System.err.println("Format piste n 2 : " + inputFormat[1].toString());
! setOriginalAudioFormat( (AudioFormat) inputFormat[1]);
! multiMedia = true;
! }
! }
! }
! }
! catch (NotConfiguredError e) {}
!
! p.removeControllerListener(this);
! }
!
! /**
! * Lancement de la conversion
! */
! public void start()
! {
! if(!reencoding)
! {
! if (multiMedia) { // Fichier audio et video
! currentThreadReencode = new ThreadReencode(inputML,
! reencodingAudioFormat,
! reencodingVideoFormat, startTime, this);
! }
! else { // Fichier audio simple
! currentThreadReencode = new ThreadReencode(inputML,
! reencodingAudioFormat, startTime, this);
! }
! currentThreadReencode.run();
! streamingRef.setNewStream(currentThreadReencode.getOutput());
! }
! else
! {
! formerThreadReencode = currentThreadReencode;
! if (multiMedia) { // Fichier audio et video
! currentThreadReencode = new ThreadReencode(inputML,
! reencodingAudioFormat,
! reencodingVideoFormat, startTime, this);
! }
! else { // Fichier audio simple
! currentThreadReencode = new ThreadReencode(inputML,
! reencodingAudioFormat, startTime, this);
! }
! currentThreadReencode.run();
!
! // On attend que le Processor du nouveau thread soit started
! Processor currentProcessor = null;
! currentProcessor = currentThreadReencode.processor;
! stateTransitionOK = true;
! waitForState(currentProcessor, currentProcessor.Started);
!
! // On transmet au module Streaming l'adresse du DataSource de sortie
! streamingRef.setNewStream(currentThreadReencode.getOutput());
! }
! }
!
! /**
! * Arret du ThreadReencode precedent
! * @return -1 si erreur
! * @return 0 si arret du precedent thread
! */
! public int stopFormerThreadReencode()
! {
! if(formerThreadReencode == null)
! {
! System.err.println("Le précédent ThreadReencode est déjà arrêté ou n'a pas encore été affecté.");
! return -1;
! }
! else // on arrete le processor et met la reference du ThreadReencode precedent a null
! {
! formerThreadReencode.stopProcessing();
! formerThreadReencode = null;
! return 0;
! }
! }
!
! /**
! * Retourne l'ID du client correspondant au module instancie
! * @return l'ID du client correspondant au module instancie
! */
! public int getClientId()
! {
! return this.clientId;
! }
!
! /**
! * Retourne le format video original du fichier demande.
! * @return le format video original du fichier
! */
! public VideoFormat getOriginalVideoFormat()
! {
! return this.originalVideoFormat;
! }
!
! /**
! * Retourne le format audio original du fichier demande.
! * @return le format audio original du fichier
! */
! public AudioFormat getOriginalAudioFormat()
! {
! return this.originalAudioFormat;
! }
!
! /**
! * Definit le format video original du fichier demande.
! * @param originalVideoFormat le format video original du fichier
! */
! private void setOriginalVideoFormat(VideoFormat originalVideoFormat)
! {
! this.originalVideoFormat = originalVideoFormat;
! System.out.println("Format vidéo original du fichier : " + this.originalVideoFormat.toString());
! }
!
! /**
! * Definit le format audio original du fichier demande.
! * @param originalAudioFormat le format audio original du fichier
! */
! private void setOriginalAudioFormat(AudioFormat originalAudioFormat)
! {
! this.originalAudioFormat = originalAudioFormat;
! System.out.println("Format audio original du fichier : " + this.originalAudioFormat.toString());
! }
!
! /**
! * Definit le format vidéo de reencodage du fichier demande.
! * @param reencodingVideoFormat le nouveau format vidéo de réencodage
! * @return 0 si mise a jour effectuee
! * @return -1 si erreur
! */
! public int setReencodingVideoFormat(VideoFormat reencodingVideoFormat)
! {
! if(reencodingVideoFormat != null)
! {
! this.reencodingVideoFormat = reencodingVideoFormat;
! System.err.println("Nouveau format video recu : " + reencodingVideoFormat.toString());
! return 0;
! }
! else
! {
! System.err.println("Pas de nouveau format video recu !");
! return -1;
! }
! }
!
! /**
! * Definit le format audio de reencodage du fichier demande.
! * @param reencodingAudioFormat le nouveau format audio de réencodage
! * @return 0 si mise a jour effectuee
! * @return -1 si erreur
! */
! public int setReencodingAudioFormat(AudioFormat reencodingAudioFormat)
! {
! if(reencodingAudioFormat != null)
! {
! this.reencodingAudioFormat = reencodingAudioFormat;
! System.err.println("Nouveau format video recu : " + reencodingAudioFormat.toString());
! return 0;
! }
! else
! {
! System.err.println("Pas de nouveau format audio recu !");
! return -1;
! }
! }
!
! /**
! * Definit le numero de l'image du changement de conversion.
! * @param startTime l'instant du changement de conversion
! */
! public void setReencodingTime(Time startTime)
! {
! this.startTime = startTime;
! }
!
! /**
! * Definit la reference du module Streaming correspondant au clientId.
! * @param streamingRef la reference du module Streaming correspondant au meme client
! */
! public void setStreamingRef(Streaming streamingRef)
! {
! this.streamingRef = streamingRef;
! }
!
! /**
! * Definit la reference du module Transition correspondant au clientId.
! * @param transitionRef la reference du module Transition correspondant au meme client
! */
! public void setTransitionRef(Transition transitionRef)
! {
! this.transitionRef = transitionRef;
! }
!
!
! /**
! * Cree un media locator a partir de l'URL (String) demandee.
! * Source tire de la classe Transcode publiee par Sun.
! * http://java.sun.com/products/java-media/jmf/2.1.1/solutions/Transcode.html
! * @param url une url de fichier multimedia
! * @return un MediaLocator
! */
! static MediaLocator createMediaLocator(String url)
! {
! MediaLocator ml;
!
! if (url.indexOf(":") > 0 && (ml = new MediaLocator(url)) != null)
! return ml;
!
! if (url.startsWith(File.separator)) {
! if ((ml = new MediaLocator("file:" + url)) != null)
! return ml;
! } else {
! String file = "file:" + System.getProperty("user.dir") + File.separator + url;
! if ((ml = new MediaLocator(file)) != null)
! return ml;
! }
!
! return null;
! }
!
! Object waitSync = new Object();
! boolean stateTransitionOK = true;
!
! /**
! * Block until the processor has transitioned to the given state.
! * Return false if the transition failed.
! * Source tire de la classe Transcode publiee par Sun.
! * http://java.sun.com/products/java-media/jmf/2.1.1/solutions/Transcode.html
! * @param p un Processor
! * @param state un int representant l'etat du Processor p
! * @return un booleen qui indique que le Processor est dans le bon etat
! */
! boolean waitForState(Processor p, int state) {
! synchronized (waitSync) {
! try {
! while (p.getState() < state && stateTransitionOK)
! waitSync.wait();
! } catch (Exception e) {}
! }
! return stateTransitionOK;
! }
!
! /**
! * Controller Listener.
! * Source tire de la classe Transcode publiee par Sun.
! * http://java.sun.com/products/java-media/jmf/2.1.1/solutions/Transcode.html
! */
! public void controllerUpdate(ControllerEvent evt) {
!
! if (evt instanceof ConfigureCompleteEvent ||
! evt instanceof RealizeCompleteEvent ||
! evt instanceof PrefetchCompleteEvent) {
! synchronized (waitSync) {
! stateTransitionOK = true;
! waitSync.notifyAll();
! }
! } else if (evt instanceof ResourceUnavailableEvent) {
! synchronized (waitSync) {
! stateTransitionOK = false;
! waitSync.notifyAll();
! }
! } else if (evt instanceof EndOfMediaEvent) {
! evt.getSourceController().close();
! } else if (evt instanceof MediaTimeSetEvent) {
! System.err.println("- mediaTime set: " +
! ((MediaTimeSetEvent)evt).getMediaTime().getSeconds());
! } else if (evt instanceof StopAtTimeEvent) {
! System.err.println("- stop at time: " +
! ((StopAtTimeEvent)evt).getMediaTime().getSeconds());
! evt.getSourceController().close();
! }
! }
!
! public void stop() {}; //FIXME
!
! }
\ No newline at end of file
--- 1,406 ----
! /**
! * Projet Madsserv
! * Reencode.java
! *
! * Module de conversion des fichiers multimedia.
! * Reencode un fichier multimedia dans un format choisi.
! *
! * @author Jean-Baptiste Mariotte
! */
!
! import java.awt.*;
! import java.io.File;
! import javax.media.*;
! import javax.media.Format;
! import javax.media.format.*;
! import javax.media.protocol.*;
! import javax.media.protocol.DataSource;
! import javax.media.control.*;
! import java.io.IOException;
!
! /**
! * Classe Reencode
! */
! public class Reencode implements ControllerListener {
!
! /**
! * Champs de la classe Reencode
! */
! public Streaming streamingRef; // Reference du module Streaming frere.
! public Transition transitionRef; //Reference du module Transition frere.
! public boolean multiMedia = false; // Indique si le fichier est mulimedia.
! public boolean reencoding = false; // Indique s'il y a conversion en cours.
! public ThreadReencode formerThreadReencode = null; // Precedent thread de conversion.
! public ThreadReencode currentThreadReencode = null; // Thread de conversion actuel.
!
! private String inputURL = null; // Adresse du fichier demande.
! private int clientId = -1; // Numero du client demandeur.
! private MediaLocator inputML = null; // MediaLocator du fichier a convertir.
! private DataSource inputDS = null; // DataSource contenant le fichier a convertir.
! private TrackControl[] inputTC = null; // TrackControl permettant d'acceder aux differentes pistes du media.
! private Format[] inputFormat = null; // Vecteur de Format pour stocker les formats des pistes.
! private AudioFormat originalAudioFormat = null; // Format audio original du fichier a convertir.
! private VideoFormat originalVideoFormat = null; // Format video original du fichier a convertir.
! private AudioFormat reencodingAudioFormat = null; // Format audio de reencodage du fichier.
! private VideoFormat reencodingVideoFormat = null; // Format video de reencodage du fichier.
! private Time startTime = new Time(0); // Instant de debut de conversion.
!
!
! /**
! * Constructeur de la classe Reencode.
! * @param clientId l'ID du client associe au module
! * @param inputURL l'URL demande par le client
! */
! Reencode(int clientId, String inputURL)
! {
! // Stockage de l'ID du client correspondant a l'instantiation.
! this.clientId = clientId;
! if(this.clientId < 0)
! {
! System.err.println("L'ID du client n'a pas été transmis correctement.");
! System.exit(0);
! }
! // Stockage de l'URL demande correspondant a l'instantiation.
! this.inputURL = inputURL;
! if(this.inputURL == null)
! {
! System.err.println("L'URL à lire n'a pas été transmis correctement.");
! System.exit(0);
! }
! System.err.println("Module de conversion construit\n");
!
! // Conversion de l'URL en MediaLocator.
! if((inputML = createMediaLocator(inputURL)) == null)
! {
! System.err.println("Impossible de construire le MediaLocator à partir de : " + inputURL);
! System.exit(0);
! }
!
! // Determination des formats d'origine du fichier demande.
! // Pour cela, on cree un Processor que l'on place dans l'etat configure.
! // Source inspire de la classe Transcode publiee par Sun.
! // http://java.sun.com/products/java-media/jmf/2.1.1/solutions/Transcode.html
! Processor p = null;
!
! try
! {
! p = Manager.createProcessor(inputML);
! System.err.println("- create processor for: " + inputML);
! }
! catch (Exception e)
! {
! System.err.println("Yikes! Cannot create a processor from the given url: " + e);
! }
!
! p.addControllerListener(this);
!
! // Put the Processor into configured state.
! p.configure();
! if (!waitForState(p, p.Configured))
! {
! System.err.println("Failed to configure the processor.");
! }
!
! // A l'aide de TrackControl, on accede aux differentes pistes du media.
! try
! {
! if ( (inputTC = p.getTrackControls()) == null)
! {
! System.err.println("Il n'y a aucune piste dans le fichier !");
! }
! else
! {
! if(inputTC.length > 2)
! {
! System.err.println("Il y a trop de pistes dans le fichier !");
! }
! else
! {
! inputFormat = new Format[inputTC.length];
! if(inputTC.length == 1)
! {
! inputFormat[0] = inputTC[0].getFormat();
! System.err.println("Il n'y a qu'une seule piste");
! setOriginalAudioFormat( (AudioFormat) inputFormat[0]); //FIXME si video sans son
! }
! else // Il y a deux pistes, la video est la premiere
! {
! inputFormat[0] = inputTC[0].getFormat();
! System.err.println("Format piste n 1 : " + inputFormat[0].toString());
! setOriginalVideoFormat( (VideoFormat) inputFormat[0]);
! inputFormat[1] = inputTC[1].getFormat();
! System.err.println("Format piste n 2 : " + inputFormat[1].toString());
! setOriginalAudioFormat( (AudioFormat) inputFormat[1]);
! multiMedia = true;
! }
! }
! }
! }
! catch (NotConfiguredError e) {}
!
! p.removeControllerListener(this);
! }
!
! /**
! * Lancement de la conversion
! */
! public void start()
! {
! if(!reencoding)
! {
! if (multiMedia) { // Fichier audio et video
! currentThreadReencode = new ThreadReencode(inputML,
! reencodingAudioFormat,
! reencodingVideoFormat, startTime, this);
! }
! else { // Fichier audio simple
! currentThreadReencode = new ThreadReencode(inputML,
! reencodingAudioFormat, startTime, this);
! }
! currentThreadReencode.run();
! streamingRef.setNewStream(currentThreadReencode.getOutput());
! }
! else
! {
! formerThreadReencode = currentThreadReencode;
! if (multiMedia) { // Fichier audio et video
! currentThreadReencode = new ThreadReencode(inputML,
! reencodingAudioFormat,
! reencodingVideoFormat, startTime, this);
! }
! else { // Fichier audio simple
! currentThreadReencode = new ThreadReencode(inputML,
! reencodingAudioFormat, startTime, this);
! }
! currentThreadReencode.run();
!
! // On attend que le Processor du nouveau thread soit started
! Processor currentProcessor = null;
! currentProcessor = currentThreadReencode.processor;
! stateTransitionOK = true;
! waitForState(currentProcessor, currentProcessor.Started);
!
! // On transmet au module Streaming l'adresse du DataSource de sortie
! streamingRef.setNewStream(currentThreadReencode.getOutput());
! }
! }
!
! /**
! * Arret du ThreadReencode precedent
! * @return -1 si erreur
! * @return 0 si arret du precedent thread
! */
! public int stopFormerThreadReencode()
! {
! if(formerThreadReencode == null)
! {
! System.err.println("Le précédent ThreadReencode est déjà arrêté ou n'a pas encore été affecté.");
! return -1;
! }
! else // on arrete le processor et met la reference du ThreadReencode precedent a null
! {
! formerThreadReencode.stopProcessing();
! formerThreadReencode = null;
! return 0;
! }
! }
!
! /**
! * Retourne l'ID du client correspondant au module instancie
! * @return l'ID du client correspondant au module instancie
! */
! public int getClientId()
! {
! return this.clientId;
! }
!
! /**
! * Retourne le format video original du fichier demande.
! * @return le format video original du fichier
! */
! public VideoFormat getOriginalVideoFormat()
! {
! return this.originalVideoFormat;
! }
!
! /**
! * Retourne le format audio original du fichier demande.
! * @return le format audio original du fichier
! */
! public AudioFormat getOriginalAudioFormat()
! {
! return this.originalAudioFormat;
! }
!
! /**
! * Definit le format video original du fichier demande.
! * @param originalVideoFormat le format video original du fichier
! */
! private void setOriginalVideoFormat(VideoFormat originalVideoFormat)
! {
! this.originalVideoFormat = originalVideoFormat;
! System.out.println("Format vidéo original du fichier : " + this.originalVideoFormat.toString());
! }
!
! /**
! * Definit le format audio original du fichier demande.
! * @param originalAudioFormat le format audio original du fichier
! */
! private void setOriginalAudioFormat(AudioFormat originalAudioFormat)
! {
! this.originalAudioFormat = originalAudioFormat;
! System.out.println("Format audio original du fichier : " + this.originalAudioFormat.toString());
! }
!
! /**
! * Definit le format vidéo de reencodage du fichier demande.
! * @param reencodingVideoFormat le nouveau format vidéo de réencodage
! * @return 0 si mise a jour effectuee
! * @return -1 si erreur
! */
! public int setReencodingVideoFormat(VideoFormat reencodingVideoFormat)
! {
! if(reencodingVideoFormat != null)
! {
! this.reencodingVideoFormat = reencodingVideoFormat;
! System.err.println("Nouveau format video recu : " + reencodingVideoFormat.toString());
! return 0;
! }
! else
! {
! System.err.println("Pas de nouveau format video recu !");
! return -1;
! }
! }
!
! /**
! * Definit le format audio de reencodage du fichier demande.
! * @param reencodingAudioFormat le nouveau format audio de réencodage
! * @return 0 si mise a jour effectuee
! * @return -1 si erreur
! */
! public int setReencodingAudioFormat(AudioFormat reencodingAudioFormat)
! {
! if(reencodingAudioFormat != null)
! {
! this.reencodingAudioFormat = reencodingAudioFormat;
! System.err.println("Nouveau format audio recu : " + reencodingAudioFormat.toString());
! return 0;
! }
! else
! {
! System.err.println("Pas de nouveau format audio recu !");
! return -1;
! }
! }
!
! /**
! * Definit le numero de l'image du changement de conversion.
! * @param startTime l'instant du changement de conversion
! */
! public void setReencodingTime(Time startTime)
! {
! this.startTime = startTime;
! }
!
! /**
! * Definit la reference du module Streaming correspondant au clientId.
! * @param streamingRef la reference du module Streaming correspondant au meme client
! */
! public void setStreamingRef(Streaming streamingRef)
! {
! this.streamingRef = streamingRef;
! }
!
! /**
! * Definit la reference du module Transition correspondant au clientId.
! * @param transitionRef la reference du module Transition correspondant au meme client
! */
! public void setTransitionRef(Transition transitionRef)
! {
! this.transitionRef = transitionRef;
! }
!
!
! /**
! * Cree un media locator a partir de l'URL (String) demandee.
! * Source tire de la classe Transcode publiee par Sun.
! * http://java.sun.com/products/java-media/jmf/2.1.1/solutions/Transcode.html
! * @param url une url de fichier multimedia
! * @return un MediaLocator
! */
! static MediaLocator createMediaLocator(String url)
! {
! MediaLocator ml;
!
! if (url.indexOf(":") > 0 && (ml = new MediaLocator(url)) != null)
! return ml;
!
! if (url.startsWith(File.separator)) {
! if ((ml = new MediaLocator("file:" + url)) != null)
! return ml;
! } else {
! String file = "file:" + System.getProperty("user.dir") + File.separator + url;
! if ((ml = new MediaLocator(file)) != null)
! return ml;
! }
!
! return null;
! }
!
! Object waitSync = new Object();
! boolean stateTransitionOK = true;
!
! /**
! * Block until the processor has transitioned to the given state.
! * Return false if the transition failed.
! * Source tire de la classe Transcode publiee par Sun.
! * http://java.sun.com/products/java-media/jmf/2.1.1/solutions/Transcode.html
! * @param p un Processor
! * @param state un int representant l'etat du Processor p
! * @return un booleen qui indique que le Processor est dans le bon etat
! */
! boolean waitForState(Processor p, int state) {
! synchronized (waitSync) {
! try {
! while (p.getState() < state && stateTransitionOK)
! waitSync.wait();
! } catch (Exception e) {}
! }
! return stateTransitionOK;
! }
!
! /**
! * Controller Listener.
! * Source tire de la classe Transcode publiee par Sun.
! * http://java.sun.com/products/java-media/jmf/2.1.1/solutions/Transcode.html
! */
! public void controllerUpdate(ControllerEvent evt) {
!
! if (evt instanceof ConfigureCompleteEvent ||
! evt instanceof RealizeCompleteEvent ||
! evt instanceof PrefetchCompleteEvent) {
! synchronized (waitSync) {
! stateTransitionOK = true;
! waitSync.notifyAll();
! }
! } else if (evt instanceof ResourceUnavailableEvent) {
! synchronized (waitSync) {
! stateTransitionOK = false;
! waitSync.notifyAll();
! }
! } else if (evt instanceof EndOfMediaEvent) {
! evt.getSourceController().close();
! } else if (evt instanceof MediaTimeSetEvent) {
! System.err.println("- mediaTime set: " +
! ((MediaTimeSetEvent)evt).getMediaTime().getSeconds());
! } else if (evt instanceof StopAtTimeEvent) {
! System.err.println("- stop at time: " +
! ((StopAtTimeEvent)evt).getMediaTime().getSeconds());
! evt.getSourceController().close();
! }
! }
!
! public void stop() {}; //FIXME
!
! }
Index: ThreadReencode.java
===================================================================
RCS file: /cvsroot/madsserv/madsserv/src/server/processor/ThreadReencode.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** ThreadReencode.java 13 Mar 2003 01:53:00 -0000 1.3
--- ThreadReencode.java 13 Mar 2003 23:47:26 -0000 1.4
***************
*** 1,376 ****
! /**
! * Projet Madsserv
! * ThreadReencode.java
! *
! * Thread de conversion du fichier multimedia.
! *
! * @author Jean-Baptiste Mariotte
! */
!
! import java.awt.*;
! import java.io.File;
! import java.io.IOException;
! import java.util.Vector;
! import javax.media.*;
! import javax.media.Format;
! import javax.media.format.*;
! import javax.media.protocol.*;
! import javax.media.protocol.DataSource;
! import javax.media.control.*;
! import javax.media.rtp.*;
! import javax.media.rtp.rtcp.*;
! import com.sun.media.rtp.*;
!
!
! /**
! * Classe ThreadReencode
! */
! class ThreadReencode extends Thread
! {
! /**
! * Champs de la classe ThreadReencode
! */
! public Processor processor = null; // Processor qui execute le reencodage.
! private Reencode reencodeRef = null; // Reference du module Reencode pere.
! private MediaLocator inputML = null; // MediaLocator du fichier a reencoder.
! private DataSource dataOutput; // DataSource de sortie.
! private AudioFormat reencodingAudioFormat = null; // Format audio de reencodage.
! private VideoFormat reencodingVideoFormat = null; // Format video de reencodage.
! private Time startTime; // Instant de debut du reencodage.
!
! /**
! * Constructeur du thread de reencodage pour un media sonore uniquement.
! * @param inputML MediaLocator du fichier a reencoder
! * @param reencodingAudioFormat format audio de reencodage du fichier
! * @param startTime instant de debut du reencodage
! * @param reencodeRef reference du module Reencode pere
! */
! public ThreadReencode (MediaLocator inputML, AudioFormat reencodingAudioFormat,
! Time startTime, Reencode reencodeRef)
! {
! this.inputML = inputML;
! this.reencodingAudioFormat = reencodingAudioFormat;
! this.startTime = startTime;
! this.reencodeRef = reencodeRef;
! }
!
! /**
! * Constructeur du thread de reencodage pour un media audio et video.
! * @param inputML MediaLocator du fichier a reencoder
! * @param reencodingAudioFormat format audio de reencodage du fichier
! * @param reencodingVideoFormat format video de reencodage du fichier
! * @param startTime instant de debut du reencodage
! * @param reencodeRef reference du module Reencode pere
! */
! public ThreadReencode (MediaLocator inputML, AudioFormat reencodingAudioFormat,
! VideoFormat reencodingVideoFormat, Time startTime,
! Reencode reencodeRef)
! {
! this.inputML = inputML;
! this.reencodingAudioFormat = reencodingAudioFormat;
! this.reencodingVideoFormat = reencodingVideoFormat;
! this.startTime = startTime;
! this.reencodeRef = reencodeRef;
! }
!
! /**
! * Methode run lancant le thread de conversion.
! * Pour cela, on cree un Processor que l'on place dans l'etat configure.
! * Source inspire de la classe Transcode publiee par Sun.
! * http://java.sun.com/products/java-media/jmf/2.1.1/solutions/Transcode.html
! */
! public void run()
! {
! String resultat;
!
! resultat = this.startProcessing();
! reencodeRef.reencoding = true;
! System.err.println("Résultat : " + resultat);
! }
!
! /**
! * Source inspire de la classe AVTransmit2 publiee par Sun.
! * http://java.sun.com/products/java-media/jmf/2.1.1/solutions/AVTransmit.html
! */
! public DataSource getOutput() {
! return dataOutput;
! }
!
! private String createProcessor() {
! if (inputML == null)
! return "Locator is null";
!
! DataSource ds;
! DataSource clone;
!
! try {
! ds = javax.media.Manager.createDataSource(inputML);
! } catch (Exception e) {
! return "Couldn't create DataSource";
! }
!
! // Try to create a processor to handle the input media locator
! try {
! processor = javax.media.Manager.createProcessor(ds);
! } catch (NoProcessorException npe) {
! return "Couldn't create processor";
! } catch (IOException ioe) {
! return "IOException creating processor";
! }
!
! // Wait for it to configure
! boolean result = waitForState(processor, Processor.Configured);
! if (result == false)
! return "Couldn't configure processor";
!
! // Get the tracks from the processor
! TrackControl [] tracks = processor.getTrackControls();
!
! // Do we have atleast one track?
! if (tracks == null || tracks.length < 1)
! return "Couldn't find tracks in processor";
!
! // Set the output content descriptor to RAW_RTP
! // This will limit the supported formats reported from
! // Track.getSupportedFormats to only valid RTP formats.
! ContentDescriptor cd = new ContentDescriptor(ContentDescriptor.RAW_RTP);
! processor.setContentDescriptor(cd);
!
! Format supported[];
! Format chosen;
! boolean atLeastOneTrack = false;
!
! // Program the tracks.
! for (int i = 0; i < tracks.length; i++) {
! Format format = tracks[i].getFormat();
! if (tracks[i].isEnabled()) {
! System.out.println("Track " + i + " is enabled");
! supported = tracks[i].getSupportedFormats();
!
! // We've set the output content to the RAW_RTP.
! // So all the supported formats should work with RTP.
! // We'll just pick the first one.
!
! if (supported.length > 0) {
! if (supported[0] instanceof VideoFormat) {
! // For video formats, we should double check the
! // sizes since not all formats work in all sizes.
! chosen = checkForVideoSizes(tracks[i].getFormat(),
! supported[0]); //FIXME mettre reencodingVideoFormat
! } else
! chosen = reencodingAudioFormat;
! tracks[i].setFormat(chosen);
! System.err.println("Track " + i + " is set to transmit as:");
! System.err.println(" " + chosen);
! atLeastOneTrack = true;
! } else
! tracks[i].setEnabled(false);
! } else {
! tracks[i].setEnabled(false);
! System.out.println("Track " + i + " isn't enabled");
! }
! }
!
! if (!atLeastOneTrack)
! return "Couldn't set any of the tracks to a valid RTP format";
!
! // Realize the processor. This will internally create a flow
! // graph and attempt to create an output datasource for JPEG/RTP
! // audio frames.
! result = waitForState(processor, Controller.Realized);
! if (result == false)
! return "Couldn't realize processor";
!
! // Set the JPEG quality to .5.
! setJPEGQuality(processor, 0.5f);
!
! // Get the output data source of the processor
! dataOutput = processor.getDataOutput();
!
! return null;
! }
!
! /**
! * Lance le Processor charge de la conversion
! * @return le message de creation du Processor
! */
! public synchronized String startProcessing() {
! String result;
!
! // Create a processor for the specified media locator
! // and program it to output JPEG/RTP
! result = createProcessor();
! if (result != null)
! return result;
!
! // Start the transmission
! processor.start();
!
! return null;
! }
!
! /**
! * Arrete le Processor en cours
! */
! public void stopProcessing() {
! synchronized (this) {
! if (processor != null) {
! processor.stop();
! processor.close();
! processor = null;
! }
! }
! }
!
! /**
! * For JPEG and H263, we know that they only work for particular
! * sizes. So we'll perform extra checking here to make sure they
! * are of the right sizes.
! */
! Format checkForVideoSizes(Format original, Format supported) {
!
! int width, height;
! Dimension size = ((VideoFormat)original).getSize();
! Format jpegFmt = new Format(VideoFormat.JPEG_RTP);
! Format h263Fmt = new Format(VideoFormat.H263_RTP);
!
! if (supported.matches(jpegFmt)) {
! // For JPEG, make sure width and height are divisible by 8.
! width = (size.width % 8 == 0 ? size.width :
! (int)(size.width / 8) * 8);
! height = (size.height % 8 == 0 ? size.height :
! (int)(size.height / 8) * 8);
! } else if (supported.matches(h263Fmt)) {
! // For H.263, we only support some specific sizes.
! if (size.width < 128) {
! width = 128;
! height = 96;
! } else if (size.width < 176) {
! width = 176;
! height = 144;
! } else {
! width = 352;
! height = 288;
! }
! } else {
! // We don't know this particular format. We'll just
! // leave it alone then.
! return supported;
! }
!
! return (new VideoFormat(null,
! new Dimension(width, height),
! Format.NOT_SPECIFIED,
! null,
! Format.NOT_SPECIFIED)).intersects(supported);
! }
!
!
! /**
! * Setting the encoding quality to the specified value on the JPEG encoder.
! * 0.5 is a good default.
! */
! void setJPEGQuality(Player p, float val) {
!
! Control cs[] = p.getControls();
! QualityControl qc = null;
! VideoFormat jpegFmt = new VideoFormat(VideoFormat.JPEG);
!
! // Loop through the controls to find the Quality control for
! // the JPEG encoder.
! for (int i = 0; i < cs.length; i++) {
!
! if (cs[i] instanceof QualityControl &&
! cs[i] instanceof Owned) {
! Object owner = ((Owned)cs[i]).getOwner();
!
! // Check to see if the owner is a Codec.
! // Then check for the output format.
! if (owner instanceof Codec) {
! Format fmts[] = ((Codec)owner).getSupportedOutputFormats(null);
! for (int j = 0; j < fmts.length; j++) {
! if (fmts[j].matches(jpegFmt)) {
! qc = (QualityControl)cs[i];
! qc.setQuality(val);
! System.err.println("- Setting quality to " +
! val + " on " + qc);
! break;
! }
! }
! }
! if (qc != null)
! break;
! }
! }
! }
!
!
! /****************************************************************
! * Convenience methods to handle processor's state changes.
! ****************************************************************/
!
! private Integer stateLock = new Integer(0);
! private boolean failed = false;
!
! Integer getStateLock() {
! return stateLock;
! }
!
! void setFailed() {
! failed = true;
! }
!
! private synchronized boolean waitForState(Processor p, int state) {
! p.addControllerListener(new StateListener());
! failed = false;
!
! // Call the required method on the processor
! if (state == Processor.Configured) {
! p.configure();
! } else if (state == Processor.Realized) {
! p.realize();
! }
!
! // Wait until we get an event that confirms the
! // success of the method, or a failure event.
! // See StateListener inner class
! while (p.getState() < state && !failed) {
! synchronized (getStateLock()) {
! try {
! getStateLock().wait();
! } catch (InterruptedException ie) {
! return false;
! }
! }
! }
!
! if (failed)
! return false;
! else
! return true;
! }
!
! /****************************************************************
! * Inner Classes
! ****************************************************************/
!
! class StateListener implements ControllerListener {
!
! public void controllerUpdate(ControllerEvent ce) {
!
! // If there was an error during configure or
! // realize, the processor will be closed
! if (ce instanceof ControllerClosedEvent)
! setFailed();
!
! // All controller events, send a notification
! // to the waiting thread in waitForState method.
! if (ce instanceof ControllerEvent) {
! synchronized (getStateLock()) {
! getStateLock().notifyAll();
! }
! }
! }
! }
!
! }
\ No newline at end of file
--- 1,385 ----
! /**
! * Projet Madsserv
! * ThreadReencode.java
! *
! * Thread de conversion du fichier multimedia.
! *
! * @author Jean-Baptiste Mariotte
! */
!
! import java.awt.*;
! import java.io.File;
! import java.io.IOException;
! import java.util.Vector;
! import javax.media.*;
! import javax.media.Format;
! import javax.media.format.*;
! import javax.media.protocol.*;
! import javax.media.protocol.DataSource;
! import javax.media.control.*;
! import javax.media.rtp.*;
! import javax.media.rtp.rtcp.*;
! import com.sun.media.rtp.*;
!
!
! /**
! * Classe ThreadReencode
! */
! class ThreadReencode extends Thread
! {
! /**
! * Champs de la classe ThreadReencode
! */
! public Processor processor = null; // Processor qui execute le reencodage.
! private Reencode reencodeRef = null; // Reference du module Reencode pere.
! private MediaLocator inputML = null; // MediaLocator du fichier a reencoder.
! private DataSource dataOutput; // DataSource de sortie.
! private AudioFormat reencodingAudioFormat = null; // Format audio de reencodage.
! private VideoFormat reencodingVideoFormat = null; // Format video de reencodage.
! private Time startTime; // Instant de debut du reencodage.
!
! /**
! * Constructeur du thread de reencodage pour un media sonore uniquement.
! * @param inputML MediaLocator du fichier a reencoder
! * @param reencodingAudioFormat format audio de reencodage du fichier
! * @param startTime instant de debut du reencodage
! * @param reencodeRef reference du module Reencode pere
! */
! public ThreadReencode (MediaLocator inputML, AudioFormat reencodingAudioFormat,
! Time startTime, Reencode reencodeRef)
! {
! this.inputML = inputML;
! this.reencodingAudioFormat = reencodingAudioFormat;
! this.startTime = startTime;
! this.reencodeRef = reencodeRef;
! }
!
! /**
! * Constructeur du thread de reencodage pour un media audio et video.
! * @param inputML MediaLocator du fichier a reencoder
! * @param reencodingAudioFormat format audio de reencodage du fichier
! * @param reencodingVideoFormat format video de reencodage du fichier
! * @param startTime instant de debut du reencodage
! * @param reencodeRef reference du module Reencode pere
! */
! public ThreadReencode (MediaLocator inputML, AudioFormat reencodingAudioFormat,
! VideoFormat reencodingVideoFormat, Time startTime,
! Reencode reencodeRef)
! {
! this.inputML = inputML;
! this.reencodingAudioFormat = reencodingAudioFormat;
! this.reencodingVideoFormat = reencodingVideoFormat;
! this.startTime = startTime;
! this.reencodeRef = reencodeRef;
! }
!
! /**
! * Methode run lancant le thread de conversion.
! * Pour cela, on cree un Processor que l'on place dans l'etat configure.
! * Source inspire de la classe Transcode publiee par Sun.
! * http://java.sun.com/products/java-media/jmf/2.1.1/solutions/Transcode.html
! */
! public void run()
! {
! String resultat;
!
! resultat = this.startProcessing();
! reencodeRef.reencoding = true;
! System.err.println("Résultat : " + resultat);
! }
!
! /**
! * Source inspire de la classe AVTransmit2 publiee par Sun.
! * http://java.sun.com/products/java-media/jmf/2.1.1/solutions/AVTransmit.html
! */
! public DataSource getOutput() {
! return dataOutput;
! }
!
! private String createProcessor() {
! if (inputML == null)
! return "Locator is null";
!
! DataSource ds;
! DataSource clone;
!
! try {
! ds = javax.media.Manager.createDataSource(inputML);
! } catch (Exception e) {
! return "Couldn't create DataSource";
! }
!
! // Try to create a processor to handle the input media locator
! try {
! processor = javax.media.Manager.createProcessor(ds);
! } catch (NoProcessorException npe) {
! return "Couldn't create processor";
! } catch (IOException ioe) {
! return "IOException creating processor";
! }
!
! // Wait for it to configure
! boolean result = waitForState(processor, Processor.Configured);
! if (result == false)
! return "Couldn't configure processor";
!
! // Get the tracks from the processor
! TrackControl [] tracks = processor.getTrackControls();
!
! // Do we have atleast one track?
! if (tracks == null || tracks.length < 1)
! return "Couldn't find tracks in processor";
!
! // Set the output content descriptor to RAW_RTP
! // This will limit the supported formats reported from
! // Track.getSupportedFormats to only valid RTP formats.
! ContentDescriptor cd = new ContentDescriptor(ContentDescriptor.RAW_RTP);
! processor.setContentDescriptor(cd);
!
! Format supported[];
! Format chosen;
! boolean atLeastOneTrack = false;
!
! // Program the tracks.
! for (int i = 0; i < tracks.length; i++) {
! Format format = tracks[i].getFormat();
! if (tracks[i].isEnabled()) {
! System.out.println("Track " + i + " is enabled");
! supported = tracks[i].getSupportedFormats();
!
! // We've set the output content to the RAW_RTP.
! // So all the supported formats should work with RTP.
! // We'll just pick the first one.
!
! if (supported.length > 0) {
! if (supported[0] instanceof VideoFormat) {
! // For video formats, we should double check the
! // sizes since not all formats work in all sizes.
! chosen = checkForVideoSizes(tracks[i].getFormat(),
! supported[0]); //FIXME mettre reencodingVideoFormat
! } else
! //chosen = reencodingAudioFormat;
! chosen = supported[0];
! tracks[i].setFormat(chosen);
! System.err.println("Track " + i + " is set to transmit as:");
! System.err.println(" " + chosen);
! atLeastOneTrack = true;
! } else
! tracks[i].setEnabled(false);
! } else {
! tracks[i].setEnabled(false);
! System.out.println("Track " + i + " isn't enabled");
! }
! }
!
! if (!atLeastOneTrack)
! return "Couldn't set any of the tracks to a valid RTP format";
! System.out.println("C'est pas encore planté ! l. 176");
! // Realize the processor. This will internally create a flow
! // graph and attempt to create an output datasource for JPEG/RTP
! // audio frames.
! result = waitForState(processor, Controller.Realized);
! if (result == false)
! return "Couldn't realize processor";
!
! // Set the JPEG quality to .5.
! setJPEGQuality(processor, 0.5f);
!
! // Commencer a encoder a partir de l'instant voulu
! if (startTime.getSeconds() > 0)
! {
! processor.setMediaTime(startTime);
! }
!
! // Get the output data source of the processor
! dataOutput = processor.getDataOutput();
!
! return null;
! }
!
! /**
! * Lance le Processor charge de la conversion
! * @return le message de creation du Processor
! */
! public synchronized String startProcessing() {
! String result;
!
! // Create a processor for the specified media locator
! // and program it to output JPEG/RTP
! result = createProcessor();
! if (result != null)
! return result;
!
! // Start the transmission
! processor.start();
!
! return null;
! }
!
! /**
! * Arrete le Processor en cours
! */
! public void stopProcessing() {
! synchronized (this) {
! if (processor != null) {
! processor.stop();
! processor.close();
! processor = null;
! }
! }
! }
!
! /**
! * For JPEG and H263, we know that they only work for particular
! * sizes. So we'll perform extra checking here to make sure they
! * are of the right sizes.
! */
! Format checkForVideoSizes(Format original, Format supported) {
!
! int width, height;
! Dimension size = ((VideoFormat)original).getSize();
! Format jpegFmt = new Format(VideoFormat.JPEG_RTP);
! Format h263Fmt = new Format(VideoFormat.H263_RTP);
!
! if (supported.matches(jpegFmt)) {
! // For JPEG, make sure width and height are divisible by 8.
! width = (size.width % 8 == 0 ? size.width :
! (int)(size.width / 8) * 8);
! height = (size.height % 8 == 0 ? size.height :
! (int)(size.height / 8) * 8);
! } else if (supported.matches(h263Fmt)) {
! // For H.263, we only support some specific sizes.
! if (size.width < 128) {
! width = 128;
! height = 96;
! } else if (size.width < 176) {
! width = 176;
! height = 144;
! } else {
! width = 352;
! height = 288;
! }
! } else {
! // We don't know this particular format. We'll just
! // leave it alone then.
! return supported;
! }
!
! return (new VideoFormat(null,
! new Dimension(width, height),
! Format.NOT_SPECIFIED,
! null,
! Format.NOT_SPECIFIED)).intersects(supported);
! }
!
!
! /**
! * Setting the encoding quality to the specified value on the JPEG encoder.
! * 0.5 is a good default.
! */
! void setJPEGQuality(Player p, float val) {
!
! Control cs[] = p.getControls();
! QualityControl qc = null;
! VideoFormat jpegFmt = new VideoFormat(VideoFormat.JPEG);
!
! // Loop through the controls to find the Quality control for
! // the JPEG encoder.
! for (int i = 0; i < cs.length; i++) {
!
! if (cs[i] instanceof QualityControl &&
! cs[i] instanceof Owned) {
! Object owner = ((Owned)cs[i]).getOwner();
!
! // Check to see if the owner is a Codec.
! // Then check for the output format.
! if (owner instanceof Codec) {
! Format fmts[] = ((Codec)owner).getSupportedOutputFormats(null);
! for (int j = 0; j < fmts.length; j++) {
! if (fmts[j].matches(jpegFmt)) {
! qc = (QualityControl)cs[i];
! qc.setQuality(val);
! System.err.println("- Setting quality to " +
! val + " on " + qc);
! break;
! }
! }
! }
! if (qc != null)
! break;
! }
! }
! }
!
!
! /****************************************************************
! * Convenience methods to handle processor's state changes.
! ****************************************************************/
!
! private Integer stateLock = new Integer(0);
! private boolean failed = false;
!
! Integer getStateLock() {
! return stateLock;
! }
!
! void setFailed() {
! failed = true;
! }
!
! private synchronized boolean waitForState(Processor p, int state) {
! p.addControllerListener(new StateListener());
! failed = false;
!
! // Call the required method on the processor
! if (state == Processor.Configured) {
! p.configure();
! } else if (state == Processor.Realized) {
! System.out.println("On va réaliser le processor");
! p.realize();
! System.out.println("Le processor est realized");
! }
!
! // Wait until we get an event that confirms the
! // success of the method, or a failure event.
! // See StateListener inner class
! while (p.getState() < state && !failed) {
! synchronized (getStateLock()) {
! try {
! getStateLock().wait();
! } catch (InterruptedException ie) {
! return false;
! }
! }
! }
!
! if (failed)
! return false;
! else
! return true;
! }
!
! /****************************************************************
! * Inner Classes
! ****************************************************************/
!
! class StateListener implements ControllerListener {
!
! public void controllerUpdate(ControllerEvent ce) {
!
! // If there was an error during configure or
! // realize, the processor will be closed
! if (ce instanceof ControllerClosedEvent)
! setFailed();
!
! // All controller events, send a notification
! // to the waiting thread in waitForState method.
! if (ce instanceof ControllerEvent) {
! synchronized (getStateLock()) {
! getStateLock().notifyAll();
! }
! }
! }
! }
!
! }
|