S
S
Severovostok2012-08-20 19:11:09
Java
Severovostok, 2012-08-20 19:11:09

Streaming video from camera to server without writing to SDCARD?

There is a code on the client side:

try {
            socket = new Socket(InetAddress.getByName(hostname), port);
            pfd = ParcelFileDescriptor.fromSocket(socket);

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

  recorder.setOutputFile(pfd.getFileDescriptor()); 

  //  String filename = String.format("/sdcard/%d.mp4", System.currentTimeMillis());
  // 
  // recorder.setOutputFile(filename);

    try
        {
        recorder.prepare();
            recorder.start();
        }
        catch (IllegalStateException e)
        {
        e.printStackTrace();
        }
        catch (IOException e)
        {
        e.printStackTrace();
        }
    }


and the code for the server itself:

 try
                {
                System.out.println("create sock");
                ServerSocket svsock = new ServerSocket(1935);

                System.out.println("accept");
                Socket sock = svsock.accept();
                System.out.println("buffer read");

                FileOutputStream outFile = null;

    String filename = String.format("%d.mp4", System.currentTimeMillis());

                   try {
                                        outFile = new FileOutputStream(filename);
                                        System.out.println(filename);
                                } catch (IOException e1) {
                                        e1.printStackTrace();
                                }

                  InputStream is = new DataInputStream(sock.getInputStream());
                                byte[] byteBuffer = new byte[1024];

                                int allsize = 0;
                                while(sock.isConnected()) {

                                    int size = is.read(byteBuffer);
                                        if (size == -1){
                                                break;
                                        } else {
                                                outFile.write(byteBuffer, 0, size);
                                        }
                                        allsize += size;

                                }
                                System.out.println("close size=" + allsize);
                                outFile.close();
                                sock.close();

                }
                catch(Exception e)
                {
                        e.printStackTrace();
                }       

                System.out.println("endmain");
        }
}


When running the application on Android 2.2.2 (HTC quiet brilliant) everything works fine. I click a button to open a socket to the server, which writes data to a file. And this file is then successfully played by the same VLC player.

However, when I test on Android 4.0.4 (Galaxy S2), the server also creates a file and writes data to it, but when I try to play the file, VLC throws an error

mp4 error: MP4 plugin discarded (no moov,foov,moof box) avcodec error: Could not open �codec demux error: Specified event object handle is invalid ps error: cannot peek main error: no suitable demux module for `file/:///C:/1345461283455.mp4'

. Other players also refuse to play it. (tried even to upload to youtube but it also gave me a file format error). Moreover, if I do not write to the server, but write to the phone's sdcard, then everything is perfectly created and played.

There may be a bug both on the server side, and some kind of novelty on API 15 (4.0.4). Maybe something needs to be added to getFileDescriptor(), etc.

Help with advice, please, or at least do not put a minus. I googled for a long time and unsuccessfully, before dashing off such a huge question :)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question