Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem in audio streaming? #337

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Java Webcam Capture

This library allows you to use your build-in or external webcam directly from Java.
This library allows you to use your build-in or external webcam directly from Java. This fork adds streaming audio in addition to video.

[![Build Status](https://secure.travis-ci.org/sarxos/webcam-capture.png?branch=master)](http://travis-ci.org/sarxos/webcam-capture)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,98 @@
import java.awt.image.BufferedImage;
import java.net.InetSocketAddress;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xuggle.xuggler.IAudioSamples;

import us.sosia.video.stream.agent.ui.SingleVideoDisplayWindow;
import us.sosia.video.stream.handler.StreamFrameListener;

public class StreamClient {
/**
* @author kerr
* */
private final static Dimension dimension = new Dimension(320,240);
private final static SingleVideoDisplayWindow displayWindow = new SingleVideoDisplayWindow("Stream example",dimension);
protected final static Logger logger = LoggerFactory.getLogger(StreamClient.class);
public static void main(String[] args) {
//setup the videoWindow
displayWindow.setVisible(true);

//setup the connection
logger.info("setup dimension :{}",dimension);
StreamClientAgent clientAgent = new StreamClientAgent(new StreamFrameListenerIMPL(),dimension);
clientAgent.connect(new InetSocketAddress("localhost", 20000));
}


protected static class StreamFrameListenerIMPL implements StreamFrameListener{
private volatile long count = 0;
@Override
public void onFrameReceived(BufferedImage image) {
logger.info("frame received :{}",count++);
displayWindow.updateImage(image);
}

}

/**
* @author kerr
* */
private static SourceDataLine mLine;
private static boolean isFirst = true;
private final static Dimension dimension = new Dimension(320,240);
private final static SingleVideoDisplayWindow displayWindow = new SingleVideoDisplayWindow("Stream example",dimension);
protected final static Logger logger = LoggerFactory.getLogger(StreamClient.class);
public static void main(String[] args) {
//setup the videoWindow
displayWindow.setVisible(true);

//setup the connection
logger.info("setup dimension :{}",dimension);
StreamClientAgent clientAgent = new StreamClientAgent(new StreamFrameListenerIMPL(),dimension);
clientAgent.connect(new InetSocketAddress("localhost", 20000));
try {
openJavaSound();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}


protected static class StreamFrameListenerIMPL implements StreamFrameListener{
private volatile long count = 0;
@Override
public void onFrameReceived(BufferedImage image) {
logger.info("frame received :{}",count++);
displayWindow.updateImage(image);
}
@Override
public void onAudioRecieved(IAudioSamples samples) {
playJavaSound(samples);
}
}

private static void openJavaSound() throws LineUnavailableException
{
AudioFormat audioFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
44100.0F, 16, 2, 4, 44100, false);

AudioFormat format = new AudioFormat(44100,
16,
2,
true, /* xuggler defaults to signed 16 bit samples */
false);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
mLine = (SourceDataLine) AudioSystem.getLine(info);
/**
* if that succeeded, try opening the line.
*/
mLine.open(audioFormat);
/**
* And if that succeed, start the line.
*/
mLine.start();


}

private static synchronized void playJavaSound(IAudioSamples aSamples)
{
if (isFirst) {
isFirst = false;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

/**
* We're just going to dump all the samples into the line.
*/
byte[] rawBytes = aSamples.getData().getByteArray(0, aSamples.getSize());
mLine.write(rawBytes, 0, rawBytes.length);
}
}