protected synchronized void encodeImage(BufferedImage image) {
if (!isEncoding)
return;
int errorNumber;
IPacket packet = IPacket.make();
long now = System.currentTimeMillis();
if (firstTimestamp == -1)
firstTimestamp = now;
if (converter == null || lastRenderedPictureSize == null
|| image.getWidth() != lastRenderedPictureSize.width
|| image.getHeight() != lastRenderedPictureSize.height) {
lastRenderedPictureSize = new Dimension(image.getWidth(), image
.getHeight());
// close old converter if necessary
if (converter != null)
converter.delete();
try {
// converter =
// ConverterFactory.createConverter("XUGGLER-BGR-24",
// PIXELFORMAT, width, height, bgrImage.getWidth(), bgrImage
// .getHeight());
converter = ConverterFactory.createConverter("XUGGLER-BGR-24",
pixelformat, width, height, width, height);
} catch (UnsupportedOperationException e) {
videoSharingSession.reportError(new EncodingException(e));
return;
}
}
image = Decoder.resample(image, new Dimension(width, height));
long timeStamp = (now - firstTimestamp) * 1000; // convert to
// microseconds
com.xuggle.xuggler.IVideoPicture outFrame = converter.toPicture(
convertToType(image, BufferedImage.TYPE_3BYTE_BGR), timeStamp);
if ((errorNumber = coder.encodeVideo(packet, outFrame, 0)) < 0) {
videoSharingSession.reportError(new EncodingException(IError.make(
errorNumber).getDescription()));
return;
}
if (packet.isComplete()) {
if ((errorNumber = container.writePacket(packet)) < 0) {
videoSharingSession.reportError(new EncodingException(IError
.make(errorNumber).getDescription()));
return;
}