i am using this code to publish audio to red5 server and testing it via publisher demo to see if i am able to hear what i am publshing.
the packets are getting sent successully but unable to hear audio. please help me out i am not able to figure out the error.
package testx;
import com.xuggle.ferry.IBuffer;
import com.xuggle.xuggler.*;
import com.xuggle.xuggler.IAudioSamples.Format;
import javax.sound.sampled.*;
public class Testx{
private static IContainer outContainer;
private static IContainerFormat outContainerFormat;
private static IStreamCoder outAudioCoder;
private static IStream outAudioStream;
public static void main(String args[])
{
String urlOut ="rtmp://localhost:1935/oflaDemo/xxx";
outContainer = IContainer.make();
outContainerFormat = IContainerFormat.make();
outContainerFormat.setOutputFormat("flv", urlOut, null);
int retVal = outContainer.open(urlOut, IContainer.Type.WRITE, outContainerFormat);
if (retVal < 0) {
System.out.println("Could not open output container");
return;
}
outAudioStream = outContainer.addNewStream(1);
outAudioCoder = outAudioStream.getStreamCoder();
ICodec cdc = ICodec.findEncodingCodec(ICodec.ID.CODEC_ID_AAC);
outAudioCoder.setCodec(cdc);
outAudioCoder.setSampleRate(44100);
outAudioCoder.setChannels(1);
outAudioCoder.setFrameRate(IRational.make(44100,1));
outAudioCoder.setSampleFormat(Format.FMT_S16);
outAudioCoder.setBitRate(64000);
retVal = outAudioCoder.open(null,null);
if (retVal < 0) {
System.out.println("Could not open audio coder");
return;
}
retVal = outContainer.writeHeader();
if (retVal < 0) {
System.out.println("Could not write output FLV header: ");
return ;
}
AudioFormat audioFormat = new AudioFormat(44100,
(int)16,
1,
true, /* xuggler defaults to signed 16 bit samples */
false);
TargetDataLine line = null;
DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
if (!AudioSystem.isLineSupported(info)) {
return;
}
// Obtain and open the line.
try {
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(audioFormat);
} catch (LineUnavailableException ex) {
return;
}
// Begin audio capture.
line.start();
long lastPos_out=0;
long audtioTime =0;
long startTime = System.nanoTime();
while(1)
{
byte[] data = new byte[line.getBufferSize()/5];
int sz = line.read(data, 0, data.length);
if(sz>0)
{
long nanoTs = System.nanoTime()-startTime;
IBuffer iBuf = IBuffer.make(null,data,0,sz);
System.out.println(iBuf.toString());
IAudioSamples smp = IAudioSamples.make(iBuf, 1,IAudioSamples.Format.FMT_S16);
smp.setComplete(true,sz/2,44100,1,Format.FMT_S16, nanoTs / 1000);
int samplesConsumed = 0;
while(samplesConsumed<smp.getNumSamples()){
IPacket packet= IPacket.make();
samplesConsumed +=outAudioCoder.encodeAudio(packet, smp, samplesConsumed);
if (packet.isComplete()) {
// packet.setKeyPacket(true);
packet.setPosition(lastPos_out);
packet.setStreamIndex(1);
lastPos_out+=packet.getSize();
outContainer.writePacket(packet);
}
}
}
// try {
// Thread.sleep((long)10000);
//} catch (InterruptedException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
// }
}
}
}
Solved it, my microphone volume was very low and set the rate to 16khz
Related
This class is part of the multi user application and it's used to view documents, however this does not work when the file size is over 20 MB.
The issue is here:
raf = new RandomAccessFile(this._file, "rw");
byte[] b = new byte[128000];
I have also tried to change it to byte[] b = new byte[(int)raf]; but it did not work.
import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
class PersistentByteServerV2$Runner extends Thread {
private ReaderWriterLock _rwl;
private File _file;
private InputStream _is;
private long _bytesRead;
private boolean _locksAcquired = true;
public PersistentByteServerV2$Runner(ReaderWriterLock rwl, File file,
InputStream is) {
this._rwl = rwl;
try {
this._rwl.startWrite();
this._rwl.startRead();
} catch (InterruptedException var5) {
this._locksAcquired = false;
}
this._file = file;
this._is = is;
this._bytesRead = 0 L;
}
public void run() {
RandomAccessFile raf = null;
if (this._locksAcquired) {
try {
raf = new RandomAccessFile(this._file, "rw");
byte[] b = new byte[128000]; // the value is set to this, as changing it to raf length does not work.
long len = 0 L;
do {
len = (long) this._is.read(b); //InputStream
if (len <= 0 L) {
break;
}
raf.write(b, 0, (int) len); //write to the random access file
if (this._bytesRead == 0 L) {
this._rwl.endRead();
}
this._bytesRead += len;
} while (len > -1 L);
} catch (Throwable var18) {;
} finally {
try {
raf.close();
} catch (Throwable var17) {;
}
try {
this._is.close();
} catch (Throwable var16) {;
}
if (this._rwl.isWriterDown()) {
this._rwl.endWrite();
}
if (this._rwl.isReaderDown()) {
this._rwl.endRead();
}
}
}
}
public long getBytesRead() {
try {
this._rwl.startRead();
this._rwl.endRead();
} catch (InterruptedException var2) {
return -1 L;
}
return this._bytesRead;
}
}
My Mediarecorder gives me a PCM File as an output when I record the phone's microphone. Now when trying to listen to this File that it created all I hear is static and I think, if I have understood correctly, I get a PCM file from Mediarecorder not AAC and I need to add ADTS header to the PCM to be able to listen to it.
I have seen threads with custom Encoders but I can not seem to figure out where and what I need to do with them.
I make an output File from microphone recoridng like this:
private static final int CHANNEL = AudioFormat.CHANNEL_IN_MONO;
private static final int AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private static final int SAMPLE_RATE = 44100; //44.1kHz
private static final int BUFFER_SIZE = 2048;
public Status status = Status.IDLE;
private AudioRecordingHandler arh;
private File outputFile;
private Context context;
/**
* Starts script for running. Needs output file to work!
*/
public void start() {
if (outputFile == null) { return; }
System.out.println("Start reading stream...");
aacEncoder = new AACEncoder(SAMPLE_RATE, micOutputPCM);
new Thread(new Runnable() {
#Override
public void run() {
record.startRecording();
byte[] data = new byte[BUFFER_SIZE];
float[] audioFloatBuffer = new float[BUFFER_SIZE/2];
Yin y = new Yin(SAMPLE_RATE, BUFFER_SIZE/2);
while(status == Status.RECORDING) {
record.read(data, 0, BUFFER_SIZE);
audioFloatBuffer = ConverterUtil.toFloatArray(data, 0, audioFloatBuffer,
0, audioFloatBuffer.length);
PitchDetectionResult pdr = y.getPitch(audioFloatBuffer);
aacEncoder.writeIntoOutputfile(data);
arh.handlePitch(pdr.getPitch());
}
aacEncoder.stopEncoding();
}
}).start();
}
/**
* Stops script
*/
public void stop() {
status = Status.IDLE;
record.stop();
arh.finishedRecording(micOutputPCM);
}
Here is how I get the byte[] from the File and where I try to encode the ADTS header to them.
public static File addHeaderToAac(File micOutputPCM, File output) throws IOException {
byte[] pcmFile = fullyReadFileToBytes(micOutputPCM);
int bufferSize = 2048;
//addADTSHeader to byte[] and return a File object
return fileWithADTSHeader;
}
public static byte[] fullyReadFileToBytes(File f) throws IOException {
int size = (int) f.length();
byte bytes[] = new byte[size];
byte tmpBuff[] = new byte[size];
FileInputStream fis= new FileInputStream(f);;
try {
int read = fis.read(bytes, 0, size);
if (read < size) {
int remain = size - read;
while (remain > 0) {
read = fis.read(tmpBuff, 0, remain);
System.arraycopy(tmpBuff, 0, bytes, size - remain, read);
remain -= read;
}
}
} catch (IOException e){
throw e;
} finally {
fis.close();
}
return bytes;
}
My question is, does anyone have an Encoder that can accept a File or byte[] or ByteStream as an input and return a File.
Because ultimately I want to make a mp4parser AACTrackImpl, which can be found here : https://github.com/sannies/mp4parser
AACTrackImpl aacTrack2 = new MP3TrackImpl(new FileDataSourceImpl(micOutputPCM));
Also If I am missing some important details about how to convert and what I should do to be able to play it then that information will also be useful.
If I need provide more information in order to answer this question, then I will gladly do so.
Edit:
I've been trying to make an encoder that would do what I need, but so far I have had no success.
public static File addHeaderToAac(File pcmFile1, File output, Context context) throws IOException {
byte[] pcmFile = fullyReadFileToBytes(pcmFile1);
int bufferSize = 2048;
AACEncoder encoder = new AACEncoder(44100, output);
encoder.encodeAudioFrameToAAC(pcmFile);
return output;
}
I am trying to encode the PCM to AAC with this encoder, but this encoder writes the output file to memory, but I need an object. And when I give it my byte[] it also gives me an error :
W/System.err: at java.nio.ByteBuffer.put(ByteBuffer.java:642)
And the error is coming from this line :
inputBuf.put(frameData);
Finally, my encoder:
public class AACEncoder {
final String TAG = "UEncoder Processor";
final int sampleRate;
File outputFile;
FileOutputStream fos;
final int TIMEOUT_USEC = 10000 ;
MediaCodec encoder;
boolean isEncoderRunning = false;
boolean outputDone = false;
MediaCodec.BufferInfo info;
public AACEncoder(final int sampleRate, File outputFile) {
this.sampleRate = sampleRate;
this.info = new MediaCodec.BufferInfo();
this.outputFile = outputFile;
openFileStream();
initEncoder();
}
/**
* Initializes CrappyEncoder for AAC-LC (Low complexity)
* #throws Exception
*/
public void initEncoder() {
try {
encoder = MediaCodec.createEncoderByType("audio/mp4a-latm");
MediaFormat format = new MediaFormat();
format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
format.setInteger(MediaFormat.KEY_SAMPLE_RATE, sampleRate);
format.setInteger(MediaFormat.KEY_BIT_RATE, 128000);
format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
} catch (IOException ex) {
Log.e(TAG, "Failed to create CrappyEncoder");
ex.printStackTrace();
}
}
int generateIndex = 0;
public void encodeAudioFrameToAAC(byte[] frameData) {
if (encoder == null) return;
if (!isEncoderRunning) {
encoder.start();
isEncoderRunning = true;
}
ByteBuffer[] encoderInputBuffers = encoder.getInputBuffers();
if (fos != null) {
int inputBufIndex = encoder.dequeueInputBuffer(TIMEOUT_USEC);
if (inputBufIndex >= 0) {
long ptsUsec = (System.currentTimeMillis() * 1000) / 10000;
if (outputDone) {
encoder.queueInputBuffer(inputBufIndex, 0, 0, ptsUsec,
MediaCodec.BUFFER_FLAG_END_OF_STREAM);
} else {
ByteBuffer inputBuf = encoderInputBuffers[inputBufIndex];
inputBuf.clear();
inputBuf.put(frameData);
encoder.queueInputBuffer(inputBufIndex, 0, frameData.length, ptsUsec, 0);
}
generateIndex++;
}
tryEncodeOutputBuffer();
}
checkIfOutputDone();
}
/**
* Gets data from output buffer and encodes it to
* AAC-LC encoding with ADTS header attached before every frame
*/
private void tryEncodeOutputBuffer() {
ByteBuffer[] encoderOutputBuffers = encoder.getOutputBuffers();
//If >= 0 then valid response
int encoderStatus = encoder.dequeueOutputBuffer(info, TIMEOUT_USEC);
if (encoderStatus >= 0) {
ByteBuffer encodedData = encoderOutputBuffers[encoderStatus];
encodedData.position(info.offset);
encodedData.limit(info.offset + info.size + 7);
byte[] data = new byte[info.size + 7];
addADTStoPacket(data, info.size + 7);
encodedData.get(data, 7, info.size);
encodedData.position(info.offset);
writeIntoOutputfile(data);
encoder.releaseOutputBuffer(encoderStatus, false);
}
}
private void checkIfOutputDone() {
if (outputDone) {
if (fos != null) {
try {
fos.close();
} catch (IOException ioe) {
Log.w(TAG, "failed closing debug file");
throw new RuntimeException(ioe);
}
fos = null;
}
}
}
/**
* Add ADTS header at the beginning of each and every AAC packet.
* This is needed as MediaCodec CrappyEncoder generates a packet of raw
* AAC data.
*
* Note the packetLen must count in the ADTS header itself.
**/
private void addADTStoPacket(byte[] packet, int packetLen) {
int profile = 2; //AAC LC
//39=MediaCodecInfo.CodecProfileLevel.AACObjectELD;
int freqIdx = 4; //44.1KHz
int chanCfg = 2; //CPE
// fill in ADTS data
packet[0] = (byte)0xFF;
packet[1] = (byte)0xF9;
packet[2] = (byte)(((profile-1)<<6) + (freqIdx<<2) +(chanCfg>>2));
packet[3] = (byte)(((chanCfg&3)<<6) + (packetLen>>11));
packet[4] = (byte)((packetLen&0x7FF) >> 3);
packet[5] = (byte)(((packetLen&7)<<5) + 0x1F);
packet[6] = (byte)0xFC;
}
private void openFileStream() {
fos = null;
try {
fos = new FileOutputStream(outputFile, false);
} catch (FileNotFoundException e) {
Log.e("AudioRecorder", e.getMessage());
}
}
/**
* Writes data into file
* #param data
*/
public void writeIntoOutputfile(byte[] data) {
try {
fos.write(data);
} catch (IOException ioe) {
Log.w(TAG, "failed writing debug data to file");
throw new RuntimeException(ioe);
}
}
public void stopEncoding() {
isEncoderRunning = false;
encoder.stop();
closeStream();
}
private void closeStream() {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
Log.e("AudioRecorder", e.getMessage());
}
}
}
I'm transferring Byte arrays of different kinds of files(.png, .txt, etc..) My socket starts sending the data and client starts receiving it. For some reason .pngs doesen't look same as they do on the server side. (Sometimes it says image is unreadable, sometimes only some parts of image are shown.. etc)? I'm wondering how could I make it so that the sent data would actually be the same as the sent data? Any suggestions what I'm doing wrong? (Note! If .png file has alpha channel, image seems always to be completely unreadable.)
Client side of the data transfer:
try
{
sInput = new ObjectInputStream(socket.getInputStream());
Input = socket.getInputStream();
}
catch (IOException eIO) {
System.out.println("Exception creating new Input/output Streams: " + eIO);
return false;
}
.
.
.
LoadCache[] filedata = (LoadCache[]) sInput.readObject(); //This contains the path and size of file
while(number < filedata.length){
byte[] mybytearray = new byte[(int) LoadCache.getSize(filedata[number])];
String result = Reader.UpdateCache(LoadCache.getPath(filedata[number]));
number++;
if(result != "skip" && result != "Stop"){
BufferedOutputStream Bos = new BufferedOutputStream(new FileOutputStream(result));
int BytesRead = Input.read(mybytearray, 0, mybytearray.length);
Bos.write(mybytearray, 0, BytesRead);
Bos.close();
}
}
Server side of the data transfer:
Socket socket;
ObjectInputStream sInput;
ObjectOutputStream sOutput;
OutputStream Output;
BufferedInputStream buffIn;
.
.
.
try{
sOutput = new ObjectOutputStream(socket.getOutputStream());
sInput = new ObjectInputStream(socket.getInputStream());
Output = socket.getOutputStream();
} catch (Exception e){
System.out.println("Couldn't create Input/Output streams");
}
.
.
.
File[] Data = Reader.getFiles(Action.getString(Incomingdata.getAction(datain)));
LoadCache LoadedCache[] = new LoadCache[Data.length];
for(int i = 0; i<Data.length; i++){
Path filePath = Paths.get(Data[i].getPath());
Path relativePath = base.relativize(filePath); //= filePath - base (so to say)
LoadedCache[i] = new LoadCache(relativePath.toString(), Data[i].length());
}
System.out.println(LoadedCache.length);
try {
sOutput.writeObject(LoadedCache);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for(File file : Data){
try {
if(file.isFile()){
byte[] bytearray = new byte[(int) file.length()];
buffIn = new BufferedInputStream(new FileInputStream(file));
buffIn.read(bytearray, 0, bytearray.length);
Output.write(bytearray, 0, bytearray.length);
Output.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
buffIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Also! Now (I'm not quite sure anymore if I got this earlier) Client gives me java.util.zip.ZipException: incorrect header check -Exception (And none of the files is zipped)
Something Runnable:
Public class Client {
private ObjectInputStream sInput; // to read from the socket
private static ObjectOutputStream sOutput; // to write on the socket
private static Socket socket;
public static Client client;
public static Player player;
public static boolean loadingFiles;
String server;
int port;
boolean connected;
Client(String server, int port) {
this.server = server;
this.port = port;
this.connected = true;
}
public static boolean isConnected(Client client){
return client.connected;
}
private void disconnect() {
try {
if(sInput != null) sInput.close();
}
catch(Exception e) {} // not much else I can do
try {
if(sOutput != null) sOutput.close();
}
catch(Exception e) {} // not much else I can do
try{
if(socket != null) socket.close();
}
catch(Exception e) {} // not much else I can do
}
public boolean start() {
// try to connect to the server
try {
socket = new Socket(server, port);
}
// if it failed we catch the exception
catch(Exception ec) {
System.out.println("Error connecting to server: " + ec);
return false;
}
System.out.println("Connection accepted " + socket.getInetAddress() + ":" + socket.getPort());
/* Creating Data Streams */
try
{
sOutput = new ObjectOutputStream(socket.getOutputStream());
sInput = new ObjectInputStream(socket.getInputStream());
}
catch (IOException eIO) {
System.out.println("Exception creating streams: " + eIO);
return false;
}
// creates the Thread to listen from the server
new ListenFromServer().start();
String Cache;
if(Reader.ReadCache(Frame.drive +"//FM_Cache/version") == "WeHadToMakeNewFile"){ //If we don't have that file. We create it, but now it's empty.
Cache = "New";
}
else{ //If we do have cache and it has "version" we send our version to server.
Cache = Reader.ReadCache(Frame.drive +"FM_Cache/version");
}
send(new Incomingdata("", "", new Action(0, Cache, 0, 0)));
// success we inform the caller that it worked
return true;
}
public static void Connect() {
// default values
int portNumber = 16304;
String serverAddress = "0.0.0.0"; //I'm using my own ip here..
// create the Client object
client = new Client(serverAddress, portNumber);
// test if we can start the connection to the Server
if(client.start()){
client.connected = true;
}
}
public static void Disconnect(Client client) {
client.connected = false;
client.disconnect();
}
class ListenFromServer extends Thread {
public void run() {
loadingFiles = true;
while(true) {
try {
int number = 0;
LoadCache[] filedata = (LoadCache[]) sInput.readObject();
while(number < filedata.length){
byte[] mybytearray = new byte[(int) LoadCache.getSize(filedata[number])];
String result = Reader.UpdateCache(LoadCache.getPath(filedata[number]));
number++;
if(result != "skip" && result != "Stop"){
BufferedOutputStream Bos = new BufferedOutputStream(new FileOutputStream(result));
int BytesRead = sInput.read(mybytearray, 0, mybytearray.length);
Bos.write(mybytearray, 0, BytesRead);
Bos.close();
}
}
System.out.println("Cache has been patched succesfully!");
Client.loadingFiles = false;
}catch(EOFException e){
System.out.println("Problem reading the data streams! " +e);
break;
}
catch(IOException e) {
System.out.println(e);
break;
}
catch(ClassNotFoundException e2) {
}
}
}
}
public static void send(Incomingdata incomingdata) {
try {
sOutput.writeObject(incomingdata);
}
catch(IOException e) {
System.out.println("Exception writing to server: " + e);
}
}
}
Incomingdata:
package com.connection;
import com.connection.Action;
public class Incomingdata implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = -1309997274550304156L;
private String Username, Password;
private Action action;
public Incomingdata(String Username, String Password, Action action){
this.Username = Username;
this.action = action;
this.Password = Password;
}
public static String getUsername(Incomingdata Incomingdata){
return Incomingdata.Username;
}
public static String getPassword(Incomingdata Incomingdata){
return Incomingdata.Password;
}
public static Action getAction(Incomingdata Incomingdata){
return Incomingdata.action;
}
}
LoadingCache:
package com.connection;
import java.io.Serializable;
public class LoadCache implements Serializable{
/**
*
*/
private static final long serialVersionUID = 7267682639705707967L;
String path;
long size;
public LoadCache(String path, long size){
this.path = path;
this.size = size;
}
public static long getSize(LoadCache cache) {
return cache.size;
}
public static String getPath(LoadCache filedata) {
return filedata.path;
}
}
Action.java:
package com.connection;
import com.connection.Action;
public class Action implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 4389420849399916526L;
int type, x, y;
String string;
public Action(int type, String string, int x, int y){
this.type = type;
this.string = string;
this.x = x;
this.y = y;
}
public static int getType(Action action) {
return action.type;
}
public static String getString(Action action) {
return action.string;
}
public static int getX(Action action) {
return action.x;
}
public static int getY(Action action) {
return action.y;
}
}
And Reader for Client. This should be the last class you need to run the client.
package com.game.loader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import com.connection.Client;
import com.game.Frame;
import com.game.menu.GameMenu;
public class Reader {
public static String ReadCache(String string) {
int CurrentLine = 0;
String returnement = "";
try {
BufferedReader Reader = new BufferedReader(new FileReader(new File(string)));
for(String line = Reader.readLine(); line != null; line = Reader.readLine()){
if(CurrentLine == 0){
returnement = line;
}
CurrentLine++;
}
} catch (FileNotFoundException e) {
new File(Frame.drive +"//FM_Cache").mkdirs();
return "WeHadToMakeNewFile";
} catch (IOException e) {
e.printStackTrace();
}
return returnement;
}
public static String UpdateCache(String path) {
String[] pieces = path.split("/");
String returnement = "skip";
System.out.println(Frame.drive + "//FM_Cache/" + path);
if(new File(Frame.drive +"//FM_Cache/" +path).exists()){
returnement = (Frame.drive +"//FM_Cache/" +path);
}
else{ //If file doesen't exit we make one and parent folders.
File file = new File(Frame.drive +"//FM_Cache/" +path);
file.getParentFile().mkdirs();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
returnement = (Frame.drive +"//FM_Cache/" +path);
}
return returnement;
}
}
Runnable server:
package com.server;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.ServerSocket;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import org.apache.commons.io.IOUtils;
import com.connection.Action;
import com.connection.Incomingdata;
import com.connection.LoadCache;
import com.game.loader.Reader;
import com.game.player.Player;
public class Server {
//Every connection got their own unique id
private static int uniqueId;
//List all the clients
private static ArrayList<ClientThread> al;
private static boolean running = false;
#SuppressWarnings("unused")
private SimpleDateFormat sdf;
final Path base = Paths.get("Data", "Cache");
public Server(int port) {
sdf = new SimpleDateFormat("HH:mm:ss");
al = new ArrayList<ClientThread>();
}
public void start() {
running = true;
try {
//Server socket
ServerSocket serverSocket = new ServerSocket(Screen.portnumber);
System.out.println("Server is running and waiting for Clients to connect.");
while(running){
Socket socket = serverSocket.accept();
if(!running){ //this will make server running stop.
System.out.println("Closing the server..");
break;
}
ClientThread t = new ClientThread(socket);
al.add(t); //saving new client to our arraylist.
t.run();
}
try{
serverSocket.close();
for(int i = 0; i< al.size(); i++){//We forget about all the clients.
//Maybe also save all the data here?
ClientThread tc = al.get(i);
try{
tc.sInput.close();
tc.sOutput.close();
tc.socket.close();
}
catch(IOException ioE){}
}
} catch(Exception e){e.printStackTrace();}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close() {
running = false;
try {
new Socket("localhost", Screen.portnumber);
} catch (Exception e) { System.out.println("Can't disconnect.."); }
}
synchronized void remove(int id) {
// scan the array list until we find the Id
for(int i = 0; i < al.size(); ++i) {
ClientThread ct = al.get(i);
// found it
if(ct.id == id) {
al.remove(i);
return;
}
}
}
public static boolean isRunning(){
return running;
}
class ClientThread extends Thread {
//The socket where to listen/talk
Socket socket;
ObjectInputStream sInput;
ObjectOutputStream sOutput;
BufferedInputStream buffIn;
//my unique id (easier for deconnection)
int id;
//Objects that we will be receiving
Incomingdata datain;
//the date we connect
String date;
Player player;
boolean Connected = false;
//Constructor
ClientThread(Socket socket){
id = uniqueId++;
this.socket = socket;
try{
sOutput = new ObjectOutputStream(socket.getOutputStream());
sInput = new ObjectInputStream(socket.getInputStream());
} catch (Exception e){
System.out.println("Couldn't create Input/Output streams");
}
date = new Date().toString();
}
// what will run forever
public void run() {
// to loop until LOGOUT
Connected = true;
while(Connected) {
try {
datain = (Incomingdata) sInput.readObject();
}
catch (IOException e) {
System.out.println("Exception reading Streams: " + e);
break;
}
catch(ClassNotFoundException e2) {
break;
}
if(!Incomingdata.getUsername(datain).isEmpty()){
switch(Action.getType(Incomingdata.getAction(datain))) { //CHANGE!!
case 0://Log in/off
System.out.println(Incomingdata.getUsername(datain) +", " +Incomingdata.getPassword(datain) + " trying to connect.");
if(Player.getPassword(Reader.ReadPlayerData("Data/Players/" +Incomingdata.getUsername(datain) +".txt")) == Incomingdata.getPassword(datain)){
player = Reader.ReadPlayerData("Data/Players/" +Incomingdata.getUsername(datain) +".txt");
System.out.println(Player.getUsername(player) +"Just connected!");
Player.isOnline(player);
}
break;
case 1://Talk
System.out.println(Incomingdata.getUsername(datain) + ": " +Action.getString(Incomingdata.getAction(datain)));
break;
case 2://Move
break;
}
}
else if(Action.getString(Incomingdata.getAction(datain)) != null){
File[] Data = Reader.getFiles(Action.getString(Incomingdata.getAction(datain)));
LoadCache LoadedCache[] = new LoadCache[Data.length];
for(int i = 0; i<Data.length; i++){
Path filePath = Paths.get(Data[i].getPath());
Path relativePath = base.relativize(filePath); //= filePath - base (so to say)
LoadedCache[i] = new LoadCache(relativePath.toString(), Data[i].length());
}
System.out.println(LoadedCache.length);
try {
sOutput.writeObject(LoadedCache);
sOutput.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for(File file : Data){
try {
if(file.isFile()){
byte[] bytearray = new byte[(int) file.length()];
buffIn = new BufferedInputStream(new FileInputStream(file));
buffIn.read(bytearray, 0, bytearray.length);
sOutput.write(bytearray, 0, bytearray.length);
sOutput.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
buffIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Cache updated!");
}
}
// remove myself from the arrayList containing the list of the
System.out.println(Player.getUsername(player) +", " +Player.getPassword(player) +" disconnected.");
remove(id);
close();
}
// try to close everything
private void close() {
// try to close the connection
try {
if(sOutput != null) sOutput.close();
}
catch(Exception e) {}
try {
if(sInput != null) sInput.close();
}
catch(Exception e) {};
try {
if(socket != null) socket.close();
}
catch (Exception e) {}
}
}
}
Reader.java for server:
package com.game.loader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import org.apache.commons.io.filefilter.FileFileFilter;
import com.game.player.CharacterClass;
import com.game.player.Equipment;
import com.game.player.Friends;
import com.game.player.Inventory;
import com.game.player.Orbs;
import com.game.player.Player;
import com.game.player.Quest;
import com.game.world.Tile;
public class Reader {
public static Player temporary;
static int stage = 0;
private static int count = 0;
private static int totalfolders = 0;
private static File[] files = new File[NumberOfFiles("Data/Cache/")];
public static Player ReadPlayerData(String string) {
Player player = null;
int CurrentLine = 0;
String[] All = new String[100]; //This may need to be extended.
try {
BufferedReader Reader = new BufferedReader(new FileReader(new File(string)));
for(String line = Reader.readLine(); line != null; line = Reader.readLine()){
All[CurrentLine] = line;
CurrentLine++;
}
} catch (FileNotFoundException e) {
System.out.println("Someone is trying to log in using wrong username.");
} catch (IOException e) {
e.printStackTrace();
}
player = new Player(string, All[0], new Tile(Integer.parseInt(All[1].split(", ")[0]), Integer.parseInt(All[1].split(", ")[1]), Integer.parseInt(All[1].split(", ")[2])),
Integer.parseInt(All[2]), Integer.parseInt(All[3]), Integer.parseInt(All[4]), new CharacterClass(All[5]), new Orbs(All[6].split(", ")), null, new Inventory(All[8].split(", ")),
null, new Equipment(All[10].split(", ")), new Friends(All[11].split(", ")), /* All[12] = guild,*/ Integer.parseInt(All[13]), true, false, null, null, Integer.parseInt(All[18]), /*All[19] = StartDate,*/ All[20]);
temporary = player;
return player;
}
public static boolean CacheReader(String string) {
File[] all = new File(string).listFiles(); //We list all the files from path
int folders = 0;
for(int i = 0; i < all.length; i++){
if(!all[i].isDirectory()) { //folders are not added to this list. :)
System.out.println("((i = " +i +") - (folders = " +folders +")) + (stage = " +stage +") = " +(i-folders+stage));
files[i-folders+stage] = all[i];
}
else{
folders++;
totalfolders++;
}
}
stage += all.length-folders; //We add number of files from every directory to stage.
for(int i = 0; i < all.length; i++){ //Then we seek for all the sub-directories
if(all[i].isDirectory()){
CacheReader(all[i].getPath()); //and add all the files from sub-directories to out file list
}
}
return true;
}
public static File[] getFiles(String string) {
if(string == "New"){
}
return files;
}
public static int NumberOfFiles(String string) {
File[] filenames;
filenames = new File(string).listFiles();
count += filenames.length; //We add all the files from the File(string) Note!: Also folders
for(int i = 0; i < filenames.length; i++){
if(filenames[i].isDirectory()){
NumberOfFiles(filenames[i].getPath());
count--;
}
}
return count;
}
}
Umm.. I think you can remove everything related to player, since it's not part of cache update system.. also there are some unused imports in those, I didn't remove all of them. Most of TextArea.Addline();s I changed to System.out.println(); if you find one, you can aswell replace it with System.out.println(); or if you don't need it you can remove it. What ever.. I just would like to find an answer..
try to write the byte array to the ObjectOutputStream as an object:
final byte[] bytearray = new byte[(int) file.length()];
buffIn = new BufferedInputStream(new FileInputStream(file));
buffIn.read(bytearray, 0, bytearray.length);
sOutput.writeObject(bytearray);
And to read it as an Object:
final BufferedOutputStream Bos =
new BufferedOutputStream(new FileOutputStream(result));
byte[] mybytearray = (byte[])sInput.readObject();
Bos.write(mybytearray, 0, BytesRead);
I hope it helps.
Don't use multiple streams to read from the same socket, or write to it either. Use the object streams for everything.
Your copy loops are incorrect, because they aren't loops. You can't assume that read() fills the buffer. The canonical stream copy loop in Java is like so:
int count;
byte[] buffer = new byte[8192]; // or more, code works with any size >= 1
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
Hello I have created a multi-threaded HTTP Server in Java and cannot get content when using a web browser.
When I use a telnet client to read from the webserver it works fine, however with a browser such as chrome nothing shows up.
I have posted 3 pictures below showing that connection via telnet works fine and an additional picture that shows wireshark capture when calling webserver via chrome browswer using:
http://localhost:4568
Here is the code I have written posted below.
I changed the code to single threaded for easier debugging, the section in MainWebServer which is called "Debugging Section" is what the threading class contains.
MainWebServer:
package my.simple.webserver;
import java.io.*;
import java.net.*;
import java.util.*;
#SuppressWarnings("unused")
public class mainWebServer {
private static ServerSocket ser = null;
private static Socket cli = null;
private static String host = null;
private static int port;
/**
* #param args
*/
public static void main(String[] args) {
// Get parameters
if(args.length < 2)
{
setHost("localhost");
port = 4568;
}
else
{
setHost(args[0]);
port = Integer.parseInt(args[1]);
}
// initialize server
try {
// change to take in host
ser = new ServerSocket(port);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true)
{
try {
setCli(ser.accept());
//(new Thread(new HttpThread(cli))).start();
// Debugging section
DataInputStream sockIn = new DataInputStream(cli.getInputStream());
HttpRequest req = new HttpRequest(cli);
int cnt = 0;
byte[] buffer = new byte[1024];
while((cnt = sockIn.read(buffer)) >= 0)
{
System.out.println("We are here");
req.checkMethod(new String(buffer));
resetBuffer(buffer, 256);
}
// end debugging section
// run thread for client socket
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static String getHost() {
return host;
}
public static void setHost(String host) {
mainWebServer.host = host;
}
public static Socket getCli() {
return cli;
}
public static void setCli(Socket cli) {
mainWebServer.cli = cli;
}
// remove after debugging
public static void resetBuffer(byte[] buffer2, int i) {
// TODO Auto-generated method stub
for(int x=0; x < i; x++)
{
buffer2[x] = 0;
}
}
}
HttpRequest class:
package my.simple.webserver;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
public class HttpRequest {
private String method = null;
private String path = null;
private FileInputStream fis = null;
private OutputStream os = null;
private Socket mycli;
private static final String HTTP_OK_RESPONSE = "HTTP/1.1 200 OK\r\n";
private static final String HTTP_FNF_RESPONSE = "HTTP/1.1 404 NOT FOUND\r\n";
private static final String HTTP_DATE_RESPONSE = "Date: Mon, 04-Jan-99 13:14:15 GMT\r\n";
private static final String HTTP_SERVER_RESPONSE = "Server: Challenger\r\n";
private static final String HTTP_CONTENT_TYPE = "Content-Type: text/html\r\n";
private String HTTP_Content_Length = "Content-length: ";
public HttpRequest(Socket myCli) {
// TODO Auto-generated constructor stub
mycli = myCli;
}
void checkMethod(String buffer)
{
// get data and parse for method, location and protocol
// use switch statement based on method given
System.out.println("buffer = " + buffer);
StringTokenizer tok = new StringTokenizer(buffer);
try
{
method = tok.nextToken();
path = tok.nextToken();
}
catch(NoSuchElementException nse)
{
System.out.println(nse.getMessage());
method = "";
}
//System.out.println("method=" + method + " path=" + path);
switch(method)
{
case "GET":
//System.out.println("Get method called");
getMethod();
break;
case "HEAD":
System.out.println("Head method called");
break;
case "POST":
System.out.println("Post method called");
break;
case "PUT":
System.out.println("Put method called");
break;
case "DELETE":
System.out.println("Delete method called");
break;
case "TRACE":
System.out.println("Trace method called");
break;
case "OPTIONS":
System.out.println("Options method called");
break;
case "CONNECT":
System.out.println("Connect method called");
break;
default:
break;
}
}
private void getMethod()
{
String file;
File f = null;
// check if file exists
if(path.equalsIgnoreCase("/"))
{
//file = checkForIndex();
file = "index.html";
}
else
file = path;
System.out.println("file = " + file);
// open file
try {
file = "badfile.html";
f = new File(file);
os = mycli.getOutputStream();
//check if file exists
if(f.exists())
{
byte[] buffer = null;
buffer = HTTP_OK_RESPONSE.getBytes();
os.write(buffer);
buffer = HTTP_DATE_RESPONSE.getBytes();
os.write(buffer);
buffer = HTTP_SERVER_RESPONSE.getBytes();
os.write(buffer);
buffer = HTTP_CONTENT_TYPE.getBytes();
os.write(buffer);
long fileLen = f.length();
HTTP_Content_Length = HTTP_Content_Length.concat(String.valueOf(fileLen));
HTTP_Content_Length = HTTP_Content_Length.concat("\r\n");
buffer = HTTP_Content_Length.getBytes();
os.write(buffer);
fis = new FileInputStream(file);
int nread, result;
// read file
while((nread = fis.available()) > 0)
{
buffer = new byte[nread];
result = fis.read(buffer);
if(result == -1) break;
os.write(buffer);
}
System.out.println("Left the loop!");
mycli.close();
}
else
{
// deal with 404
byte[] buffer = null;
buffer = HTTP_FNF_RESPONSE.getBytes();
os.write(buffer);
buffer = HTTP_DATE_RESPONSE.getBytes();
os.write(buffer);
buffer = HTTP_SERVER_RESPONSE.getBytes();
os.write(buffer);
buffer = HTTP_CONTENT_TYPE.getBytes();
os.write(buffer);
f = new File("fnf.html");
long fileLen = f.length();
HTTP_Content_Length = HTTP_Content_Length.concat(String.valueOf(fileLen));
HTTP_Content_Length = HTTP_Content_Length.concat("\r\n");
buffer = HTTP_Content_Length.getBytes();
os.write(buffer);
fis = new FileInputStream(f);
int nread, result;
// read file
while((nread = fis.available()) > 0)
{
buffer = new byte[nread];
result = fis.read(buffer);
if(result == -1) break;
os.write(buffer);
}
System.out.println("Left the loop!");
mycli.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// end thread
}
}
I am on a time crunch, I appreciate any help.
Thank you!
Because in HTTP protocol after sending the header, you need to send an empty line(\r\n) that indicates the end of header section in both request and response, for example
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 21
\r\n(EMPTY LINE) <- you forgot this one
<b>Hello World :D</b>
Add "\r\n" after you complete header.
Like:
HTTP_Content_Length = HTTP_Content_Length.concat("\r\n\r\n");
I am making a Vocoder in java built into a synthesizer built based on a tutorial from Dr. Dobb's and whenever i try to get samples from the microphone it just returns zeros for the samples even when there is mic input.
This is the settings of the AudioFormat:
// AudioFormat parameters
public static final int SAMPLE_RATE = 22050;
private static final int SAMPLE_SIZE = 16;
private static final int CHANNELS = 1;
private static final boolean SIGNED = true;
private static final boolean BIG_ENDIAN = true;
// Chunk of audio processed at one time
public static final int BUFFER_SIZE = 1000;
public static final int SAMPLES_PER_BUFFER = BUFFER_SIZE / 2;
VoiceProvider (mic sample providing class):
public class VoiceProvider implements ISynthProvider{
private TargetDataLine line;
public VoiceProvider(){
DataLine.Info info = new DataLine.Info(TargetDataLine.class,
Player.format); // format is an AudioFormat object
if (!AudioSystem.isLineSupported(info)) {
System.out.println("o_0 line not supported!");
}
// Obtain and open the line.
try {
line = (TargetDataLine) AudioSystem.getLine(info);
line.start();
line.open(Player.format);
} catch (LineUnavailableException ex) {
// Handle the error ...
}
}
#Override
public double getSample() {
byte[] a = {0,0};
if (line.isOpen()){
//System.out.println("0_0");
line.read(a, 0, 2);
}else{
//System.out.println("o_0");
}
System.out.println(Arrays.toString(a));
return readShort(a,0);
}
public static short readShort(byte[] data, int offset) {
return (short) (((data[offset] << 8)) | ((data[offset + 1] & 0xff)));
}
try {
line = (TargetDataLine) AudioSystem.getLine(info);
line.start();
line.open(Player.format);
} catch (LineUnavailableException ex) {
// Handle the error ...
}
should be:
try {
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(Player.format);
line.start();
} catch (LineUnavailableException ex) {
// Handle the error ...
}