From: <to...@us...> - 2007-02-15 21:11:17
|
Revision: 62 http://techne-dev.svn.sourceforge.net/techne-dev/?rev=62&view=rev Author: tonit Date: 2007-02-15 13:11:16 -0800 (Thu, 15 Feb 2007) Log Message: ----------- transfered project to maven2/maven-bundle-plugin project Added Paths: ----------- sandbox/tonit/techne.samples.audio.player/.classpath sandbox/tonit/techne.samples.audio.player/.project sandbox/tonit/techne.samples.audio.player/pom.xml sandbox/tonit/techne.samples.audio.player/src/ sandbox/tonit/techne.samples.audio.player/src/main/ sandbox/tonit/techne.samples.audio.player/src/main/java/ sandbox/tonit/techne.samples.audio.player/src/main/java/techne/ sandbox/tonit/techne.samples.audio.player/src/main/java/techne/App.java sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/ sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/ sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/Activator.java sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/MusicSource.java sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/PlayerControl.java sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/ sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/JavaZoomMusicPlayer.java sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/JavaZoomPlayerThread.java sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/PlayerImpl.java sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/PlayerState.java sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/SingleTrackMusicSource.java sandbox/tonit/techne.samples.audio.player/src/test/ sandbox/tonit/techne.samples.audio.player/src/test/java/ sandbox/tonit/techne.samples.audio.player/src/test/java/techne/ sandbox/tonit/techne.samples.audio.player/src/test/java/techne/AppTest.java sandbox/tonit/techne.samples.audio.player/target/ Added: sandbox/tonit/techne.samples.audio.player/.classpath =================================================================== --- sandbox/tonit/techne.samples.audio.player/.classpath (rev 0) +++ sandbox/tonit/techne.samples.audio.player/.classpath 2007-02-15 21:11:16 UTC (rev 62) @@ -0,0 +1,13 @@ +<classpath> + <classpathentry kind="src" path="src/main/java"/> + <classpathentry kind="src" path="src/test/java" output="target/test-classes"/> + <classpathentry kind="output" path="target/classes"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="var" path="M2_REPO/org/apache/felix/org.osgi.compendium/0.9.0-incubator-SNAPSHOT/org.osgi.compendium-0.9.0-incubator-SNAPSHOT.jar"/> + <classpathentry kind="var" path="M2_REPO/org/javazoom/org.javazoom.jl/1.0-SNAPSHOT/org.javazoom.jl-1.0-SNAPSHOT.jar"/> + <classpathentry kind="var" path="M2_REPO/org/apache/felix/javax.servlet/0.9.0-incubator-SNAPSHOT/javax.servlet-0.9.0-incubator-SNAPSHOT.jar"/> + <classpathentry kind="var" path="M2_REPO/org/apache/felix/org.apache.felix.shell/0.9.0-incubator-SNAPSHOT/org.apache.felix.shell-0.9.0-incubator-SNAPSHOT.jar"/> + <classpathentry kind="var" path="M2_REPO/org/apache/felix/org.osgi.core/0.9.0-incubator-SNAPSHOT/org.osgi.core-0.9.0-incubator-SNAPSHOT.jar"/> + <classpathentry kind="var" path="M2_REPO/org/apache/felix/org.osgi.foundation/0.9.0-incubator-SNAPSHOT/org.osgi.foundation-0.9.0-incubator-SNAPSHOT.jar"/> + <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/> +</classpath> \ No newline at end of file Added: sandbox/tonit/techne.samples.audio.player/.project =================================================================== --- sandbox/tonit/techne.samples.audio.player/.project (rev 0) +++ sandbox/tonit/techne.samples.audio.player/.project 2007-02-15 21:11:16 UTC (rev 62) @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>techne.samples.audio.player</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> Added: sandbox/tonit/techne.samples.audio.player/pom.xml =================================================================== --- sandbox/tonit/techne.samples.audio.player/pom.xml (rev 0) +++ sandbox/tonit/techne.samples.audio.player/pom.xml 2007-02-15 21:11:16 UTC (rev 62) @@ -0,0 +1,51 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>techne</groupId> + <artifactId>techne.samples.audio.player</artifactId> + <packaging>bundle</packaging> + <version>1.0</version> + <name>techne.samples.audio.player</name> + <url>http://maven.apache.org</url> + <dependencies> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.osgi.core</artifactId> + <version>0.9.0-incubator-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.osgi.compendium</artifactId> + <version>0.9.0-incubator-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.javazoom</groupId> + <artifactId>org.javazoom.jl</artifactId> + <version>1.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>3.8.1</version> + <scope>test</scope> + </dependency> + </dependencies> + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <instructions> + <Bundle-SymbolicName>techne.samples.audio</Bundle-SymbolicName> + <Export-Package>techne.audio.player</Export-Package> + <Private-Package>techne.audio.player.internal</Private-Package> + <Bundle-Activator>techne.audio.player.Activator</Bundle-Activator> + <Service-Component>techne.audio.player.internal.JavaZoomMusicPlayer;musicSource=techne.audio.player.MusicSource;provide:=techne.audio.player.PlayerControl</Service-Component> + </instructions> + </configuration> + </plugin> + </plugins> + </build> +</project> Added: sandbox/tonit/techne.samples.audio.player/src/main/java/techne/App.java =================================================================== --- sandbox/tonit/techne.samples.audio.player/src/main/java/techne/App.java (rev 0) +++ sandbox/tonit/techne.samples.audio.player/src/main/java/techne/App.java 2007-02-15 21:11:16 UTC (rev 62) @@ -0,0 +1,13 @@ +package techne; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} Added: sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/Activator.java =================================================================== --- sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/Activator.java (rev 0) +++ sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/Activator.java 2007-02-15 21:11:16 UTC (rev 62) @@ -0,0 +1,24 @@ +package techne.audio.player; + +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; + +public class Activator implements BundleActivator { + + public void start(BundleContext ctx) throws Exception { + System.out.println("Audio ON"); + try { + System.out.println("Component header: " + + ctx.getBundle().getHeaders().get("Service-Component")); + + System.out.println("LogReader wired!"); + } catch (Exception e) { + System.out.println("Cannot wire to logreader"); + } + } + + public void stop(BundleContext arg0) throws Exception { + System.out.println("Audio OFF"); + } + +} Added: sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/MusicSource.java =================================================================== --- sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/MusicSource.java (rev 0) +++ sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/MusicSource.java 2007-02-15 21:11:16 UTC (rev 62) @@ -0,0 +1,10 @@ +package techne.audio.player; + +import java.io.InputStream; + +public interface MusicSource { + InputStream next(); + boolean hasNext(); + InputStream current(); + +} Added: sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/PlayerControl.java =================================================================== --- sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/PlayerControl.java (rev 0) +++ sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/PlayerControl.java 2007-02-15 21:11:16 UTC (rev 62) @@ -0,0 +1,8 @@ +package techne.audio.player; + +public interface PlayerControl { + void play(); + void stop(); + void pause(); + int getState(); +} Added: sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/JavaZoomMusicPlayer.java =================================================================== --- sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/JavaZoomMusicPlayer.java (rev 0) +++ sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/JavaZoomMusicPlayer.java 2007-02-15 21:11:16 UTC (rev 62) @@ -0,0 +1,82 @@ +package techne.audio.player.internal; + +import org.osgi.service.component.ComponentContext; + +import techne.audio.player.MusicSource; +import techne.audio.player.PlayerControl; + +/** + * + * Currently it just supports a single track. Later it will accept mutiple + * tracks. Those track shoudl come from a MusicSource impl. Thus, the controls + * will likely to be extended (foward,backward) + * + * + * @author tmenzel + * + */ +public class JavaZoomMusicPlayer implements PlayerControl { + private JavaZoomPlayerThread thread; + private MusicSource musicSource; + private ComponentContext context; + + public JavaZoomMusicPlayer() { + } + + protected void activate(ComponentContext context) { + this.context = context; + // reference hopefully injected already? + if (musicSource != null) { + thread = new JavaZoomPlayerThread(); + thread.setMusicSource(musicSource); + } else { + System.out.println("There is no musicSource for some reason.. :-("); + } + } + + public void deactivate(ComponentContext context) throws Exception { + if (thread != null) { + thread.changeState(PlayerState.STATE_OFF); + thread.close(); + } + System.out.println("Player Service is down"); + } + + public void pause() { + thread.changeState(PlayerState.STATE_PAUSE); + } + + public void play() { + if (thread == null) { + thread = new JavaZoomPlayerThread(); + thread.setMusicSource(musicSource); + } + + if (thread != null) { + if (!thread.isAlive()) { + System.out.println("start player thread.."); + thread.start(); + } + thread.changeState(PlayerState.STATE_PLAY); + } + } + + public void stop() { + if (thread != null) { + thread.changeState(PlayerState.STATE_IDLE); + } + } + + public MusicSource getMusicSource() { + return musicSource; + } + + public void setMusicSource(MusicSource source) { + this.musicSource = source; + } + + public int getState() { + return thread.getCurrentPlayerState(); + } + +} Added: sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/JavaZoomPlayerThread.java =================================================================== --- sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/JavaZoomPlayerThread.java (rev 0) +++ sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/JavaZoomPlayerThread.java 2007-02-15 21:11:16 UTC (rev 62) @@ -0,0 +1,81 @@ +package techne.audio.player.internal; + +import techne.audio.player.MusicSource; + +/** + * * Thread that represents a ready to go music player To control it, use the + * PlayerControls + */ +public class JavaZoomPlayerThread extends Thread { + MusicSource musicSource; + private PlayerImpl pla; + + private int state = PlayerState.STATE_IDLE; + private int lastpos = 0; + + public synchronized void changeState(int state) { + System.out.println("SWITCHING STATE FROM " + this.state + " to " + + state + " at pos " + lastpos); + this.state = state; + if (state != PlayerState.STATE_PLAY) { + // break event + if (pla != null) { + pla.pause(); + + System.out.println("adding " + pla.getPosition() + " to " + + lastpos); + lastpos += pla.getPosition(); + } + } + } + + public void run() { + try { + while (state != PlayerState.STATE_OFF) { + if (this.state == PlayerState.STATE_PLAY) { + if (pla == null || pla.isComplete()) { + if (musicSource.hasNext()) { + pla = new PlayerImpl(musicSource.next()); + pla.play(0, Integer.MAX_VALUE); + } else { + changeState(PlayerState.STATE_IDLE); + } + } else { + // resume + System.out.println("resume at pos " + lastpos); + // resume requires re-opening stream..:-( + pla = new PlayerImpl(musicSource.current()); + pla.play(lastpos, Integer.MAX_VALUE); + } + } + } + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + System.out.println(e.getMessage()); + throw new RuntimeException(e); + } + System.out.println("MusicPlayer Thread closed!"); + } + + public void close() { + changeState(PlayerState.STATE_OFF); + pla.close(); + } + + public MusicSource getMusicSource() { + return musicSource; + } + + + + public void setMusicSource(MusicSource musicSource) { + this.musicSource = musicSource; + } + + public int getCurrentPlayerState() { + return state; + } + + +} Added: sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/PlayerImpl.java =================================================================== --- sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/PlayerImpl.java (rev 0) +++ sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/PlayerImpl.java 2007-02-15 21:11:16 UTC (rev 62) @@ -0,0 +1,222 @@ +package techne.audio.player.internal; + +import java.io.InputStream; + +import javazoom.jl.decoder.Bitstream; +import javazoom.jl.decoder.BitstreamException; +import javazoom.jl.decoder.Decoder; +import javazoom.jl.decoder.Header; +import javazoom.jl.decoder.JavaLayerException; +import javazoom.jl.decoder.SampleBuffer; +import javazoom.jl.player.AudioDevice; +import javazoom.jl.player.FactoryRegistry; + +public class PlayerImpl { + + /** + * The MPEG audio bitstream. + */ + // javac blank final bug. + /* final */private Bitstream bitstream; + + /** + * The MPEG audio decoder. + */ + /* final */private Decoder decoder; + + /** + * The AudioDevice the audio samples are written to. + */ + private AudioDevice audio; + + /** + * Has the player been closed? + */ + private boolean closed = false; + + /** + * Has the player played back all frames from the stream? + */ + private boolean complete = false; + + private int lastPosition = 0; + + /** + * Creates a new <code>Player</code> instance. + */ + public PlayerImpl(InputStream stream) throws JavaLayerException { + bitstream = new Bitstream(stream); + decoder = new Decoder(); + + FactoryRegistry r = FactoryRegistry.systemRegistry(); + audio = r.createAudioDevice(); + + audio.open(decoder); + } + + public void play() throws JavaLayerException { + play(Integer.MAX_VALUE); + } + + public boolean play(final int start, final int end) { + boolean ret = true; + boolean res = true; + int offset = start; + try { + while (offset-- > 0 && ret) { + ret = skipFrame(); + } + res = play(end - start); + + } catch (JavaLayerException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + throw new RuntimeException(e); + } + return res; + } + + /** + * skips over a single frame + * + * @return false if there are no more frames to decode, true otherwise. + */ + protected boolean skipFrame() throws JavaLayerException { + Header h = bitstream.readFrame(); + if (h == null) + return false; + bitstream.closeFrame(); + return true; + } + + /** + * Plays a number of MPEG audio frames. + * + * @param frames + * The number of frames to play. + * @return true if the last frame was played, or false if there are more + * frames. + */ + public boolean play(int frames) throws JavaLayerException { + boolean ret = true; + + while (frames-- > 0 && ret) { + ret = decodeFrame(); + } + + if (!ret) { + // last frame, ensure all data flushed to the audio device. + AudioDevice out = audio; + if (out != null) { + out.flush(); + synchronized (this) { + complete = (!closed); + close(); + } + } + } + return ret; + } + + /** + * Cloases this player. Any audio currently playing is stopped immediately. + */ + public synchronized void close() { + AudioDevice out = audio; + if (out != null) { + closed = true; + audio = null; + // this may fail, so ensure object state is set up before + // calling this method. + out.close(); + lastPosition = out.getPosition(); + try { + bitstream.close(); + } catch (BitstreamException ex) { + } + } + } + + /** + * Returns the completed status of this player. + * + * @return true if all available MPEG audio frames have been decoded, or + * false otherwise. + */ + public synchronized boolean isComplete() { + return complete; + } + + /** + * Retrieves the position in milliseconds of the current audio sample being + * played. This method delegates to the <code> + * AudioDevice</code> that is + * used by this player to sound the decoded audio samples. + */ + public int getPosition() { + int position = lastPosition; + + AudioDevice out = audio; + if (out != null) { + position = out.getPosition(); + } + return position; + } + + /** + * Decodes a single frame. + * + * @return true if there are no more frames to decode, false otherwise. + */ + protected boolean decodeFrame() throws JavaLayerException { + try { + AudioDevice out = audio; + if (out == null) { + return false; + } + Header h = bitstream.readFrame(); + if (h == null) { + return false; + } + // sample buffer set when decoder constructed + SampleBuffer output = (SampleBuffer) decoder.decodeFrame(h, + bitstream); + + synchronized (this) { + out = audio; + if (out != null) { + out.write(output.getBuffer(), 0, output.getBufferLength()); + } + } + + bitstream.closeFrame(); + } catch (RuntimeException ex) { + throw new JavaLayerException("Exception decoding audio frame", ex); + } + /* + * catch (IOException ex) { System.out.println("exception decoding audio + * frame: "+ex); return false; } catch (BitstreamException bitex) { + * System.out.println("exception decoding audio frame: "+bitex); return + * false; } catch (DecoderException decex) { + * System.out.println("exception decoding audio frame: "+decex); return + * false; } + */ + return true; + } + + public void pause() { + AudioDevice out = audio; + if (out != null) { + closed = true; + audio = null; + // this may fail, so ensure object state is set up before + // calling this method. + out.close(); + lastPosition = out.getPosition(); + try { + bitstream.close(); + } catch (BitstreamException ex) { + } + } + } +} Added: sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/PlayerState.java =================================================================== --- sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/PlayerState.java (rev 0) +++ sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/PlayerState.java 2007-02-15 21:11:16 UTC (rev 62) @@ -0,0 +1,9 @@ +package techne.audio.player.internal; + +public class PlayerState { + public static final int STATE_IDLE = 0; + public static final int STATE_OFF = -1; + + public static final int STATE_PLAY = 1; + public static final int STATE_PAUSE = 2; +} Added: sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/SingleTrackMusicSource.java =================================================================== --- sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/SingleTrackMusicSource.java (rev 0) +++ sandbox/tonit/techne.samples.audio.player/src/main/java/techne/audio/player/internal/SingleTrackMusicSource.java 2007-02-15 21:11:16 UTC (rev 62) @@ -0,0 +1,52 @@ +package techne.audio.player.internal; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +import techne.audio.player.MusicSource; + +public class SingleTrackMusicSource implements MusicSource { + boolean repeat = false; + private URL url; + // count of next() requests + int count = 0; + + public SingleTrackMusicSource(URL url) { + this.url = url; + } + + public boolean hasNext() { + return (repeat || count == 0); + } + + public InputStream next() { + synchronized (this) { + if (count == 0) { + count++; + try { + System.out.println("MusicSource: giving " + url.toExternalForm()); + return url.openConnection().getInputStream(); + } catch (IOException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + } + } + return null; + } + + public void setRepeat(boolean rep) { + this.repeat = rep; + } + + public InputStream current() { + try { + return url.openConnection().getInputStream(); + } catch (IOException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + } + +} Added: sandbox/tonit/techne.samples.audio.player/src/test/java/techne/AppTest.java =================================================================== --- sandbox/tonit/techne.samples.audio.player/src/test/java/techne/AppTest.java (rev 0) +++ sandbox/tonit/techne.samples.audio.player/src/test/java/techne/AppTest.java 2007-02-15 21:11:16 UTC (rev 62) @@ -0,0 +1,38 @@ +package techne; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |