QNA > C > Come Trasmettere Video In Streaming Su Sockets In Java

Come trasmettere video in streaming su sockets in Java

Bene, allo stesso modo si trasmette qualsiasi cosa. Ho iniziato a pensare "non c'è solo questo, deve esserci qualcosa di più su questa domanda che è 'sottolineata', non riesco a capire" ma sembra che sia proprio questo.

Vediamo alcune soluzioni. Registrare un pezzo di video e inviarlo, registrare una singola BufferedImage e inviarla, non usare socket ma qualcosa che possa gestire la trasmissione in tempo reale, e si può pensare anche in altri modi.

Fondamentalmente quello che dovete fare è scegliere il vostro pezzo registrato (potete registrare video a pezzi iniziando la registrazione di uno, fermandovi a un punto, salvandolo e iniziando di nuovo la registrazione) e inviarlo con un ObjectOutputStream a un ServerSocket con un ObjectInputStream che legge dati.

Ora mostrerò un po' di codice, ma tenete presente che questi sono molto semplici, e possono essere molto lenti (e possono essere facilmente migliorati)

Ecco un esempio di un metodo sendVideoPiece() dal client:

  1. // More code up 
  2. public void sendVideoPiece() { 
  3. Socket sock = new Socket(serverHost, serverPort); // Just as usual, creating a socket to send information 
  4. ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream()); // Creating the sender 
  5. try { 
  6. oos.writeObject(currentVideoFrame); // Will send the current video frame, assuming that there is a variable called 'currentVideoFrame' that stores the current recorded frame 
  7. } catch(Exception e) { System.err.println("Error sending current frame to server!"); } 
  8. // More code below 

And here the server, with the method getVideoPiece():

  1. // More code up 
  2. public void getVideoPiece() throws { 
  3. // Assuming that a server socket already exists and that the client socket is already connected to it: 
  4. ObjectInputStream ois = new ObjectInputStream(recSocket.getInputStream()); 
  5. try { 
  6. BufferedImage currentVideoFrame = (BufferedImage) ois.readObject(); 
  7. } catch(Exception e) { System.out.println("Error reading current frame from client!"); } 
  8. // Broadcast frame to viewers 

Di Keisling

Qual è la peggiore marca di laptop? :: Le concessionarie devono dare in prestito?
Link utili