设置音频数据捕获(capture)和提交(render)缓冲区的数值
问题:
给音频信息的捕获和提交设置缓冲区数值.小一些的缓冲区会减少时钟周期,但是也会增加传输的次数.这些书只需要在不同的系统上测试.
解决方案:
音频缓冲区控制(AudioBufferControl)包含于数据源(DataSource)和播放器(Player)中.捕获音频缓冲区数值(Capture Buffer Size)由数据
源控制,提交缓冲区数值(render buffer size)由播放器控制.
requirements
related classes
how to run this sample
source code
requirements
平台 JDK 1.1.6 或更高版本
JMF API: 2.0 或更高版本
Implementation AJ(All Java),WPP(Windows Performance Pack),SPP(Solaris Performance Pack)
相关的类
javax.media.Player
javax.media.control.BufferControl
如何执行这个例子
java AudioBufferControl [-c <capture buf size in millisecs>] [-r <render buf size in millisecs>] <URL>
例如: java AudioBufferControl -c 62 -r 400 javasound://44100
代码
import java.awt.*;
import javax.media.*;
import javax.media.control.TrackControl;
import javax.media.control.BufferControl;
import javax.media.format.*;
import javax.media.protocol.*;
import javax.media.protocol.DataSource;
/**
* Sample program to playback the input URL and set the audio capture
* and rendering buffer sizes.
*/
public class AudioBufferControl extends Frame implements ControllerListener {
Processor p;
Object waitSync = new Object();
boolean stateTransitionOK = true;
/**
* Given a DataSource, create a processor and use that processor
* as a player to playback the media.
*/
public boolean open(DataSource ds, int captureBufSize, int renderBufSize) {
System.err.println("create processor for: " + ds.getContentType());
// 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(captureBufSize);
try {
p = Manager.createProcessor(ds);
} catch (Exception e) {
System.err.println("Failed to create a processor from the given DataSource: " + e);
return false;
}
p.addControllerListener(this);
// Put the Processor into configured state.
p.configure();
if (!waitForState(p.Configured)) {
System.err.println("Failed to configure the processor.");
return false;
}
// So I can use it as a player.
p.setContentDescriptor(null);
p.realize();
if (!waitForState(p.Realized)) {
System.err.println("Failed to realize the processor.");
return false;
}
// After the processor has been realized, we can now set the
// renderer's buffer size. We need to do this before the
// processor is prefetched.
// We need to loop the array of controls to make sure that we
// are setting the size of the correct buffer control since
// the DataSource's controls are also included in the list.
Control cs[] = p.getControls();
Object owner;
for (int i = 0; i < cs.length; i++) {
if (cs[i] instanceof Owned && cs[i] instanceof BufferControl) {
owner = ((Owned)cs[i]).getOwner();
if (owner instanceof Renderer) {
((BufferControl)cs[i]).setBufferLength(renderBufSize);
}
}
}
// Prefetch the processor.
p.prefetch();
if (!waitForState(p.Prefetched)) {
System.err.println("Failed to prefetch the processor.");
return false;
}
// Display the visual & control component if there's one.
setLayout(new BorderLayout());
Component cc;
Component vc;
if ((vc = p.getVisualComponent()) != null) {
add("Center", vc);
}
if ((cc = p.getControlPanelComponent()) != null) {
add("South", cc);
}
// Start the processor.
p.start();
setVisible(true);
return true;
}
public void addNotify() {
super.addNotify();
pack();
}
/**
* Block until the processor has transitioned to the given state.
* Return false if the transition failed.
*/
boolean waitForState(int state) {
synchronized (waitSync) {
try {
while (p.getState() < state && stateTransitionOK)
waitSync.wait();
} catch (Exception e) {}
}
return stateTransitionOK;
}
/**
* Controller Listener.
*/
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) {
p.setMediaTime(new Time(0));
//p.start();
//p.close();
//System.exit(0);
} else if (evt instanceof SizeChangeEvent) {
}
}
// Default buffer size in milli-seconds.
static final int DEF_CAPTURE_SIZE = 62;
static final int DEF_RENDER_SIZE = 400;
/**
* Main program
*/
public static void main(String [] args) {
String mlStr = null;
int captureBufSize = DEF_CAPTURE_SIZE;
int renderBufSize = DEF_RENDER_SIZE;
int i = 0;
while (i < args.length) {
if (args[i].equals("-c")) {
i++;
if (i >= args.length) {
prUsage();
System.exit(0);
}
try {
captureBufSize = Integer.parseInt(args[i]);
} catch (NumberFormatException e) {
prUsage();
System.exit(0);
}
} else if (args[i].equals("-r")) {
i++;
if (i >= args.length) {
prUsage();
System.exit(0);
}
try {
renderBufSize = Integer.parseInt(args[i]);
} catch (NumberFormatException e) {
prUsage();
System.exit(0);
}
} else {
mlStr = args[i];
}
i++;
}
if (mlStr == null) {
prUsage();
System.exit(0);
}
MediaLocator ml;
if ((ml = new MediaLocator(mlStr)) == null) {
System.err.println("Cannot build media locator from: " + mlStr);
prUsage();
System.exit(0);
}
DataSource ds = null;
// Create a DataSource given the media locator.
try {
ds = Manager.createDataSource(ml);
} catch (Exception e) {
System.err.println("Cannot create DataSource from: " + ml);
System.exit(0);
}
AudioBufferControl abc = new AudioBufferControl();
if (!abc.open(ds, captureBufSize, renderBufSize))
System.exit(0);
}
static void prUsage() {
System.err.println("Usage: java AudioBufferControl [-c <capture buf size in millisecs>] [-r <render buf size in
millisecs>] <url>");
}
}