Streaming audio and video from Android to PC/web.

前端 未结 2 1867
别那么骄傲
别那么骄傲 2021-02-04 18:39

I am recent beginner to Android SDK, and the overall goal of this project is to create an app very similar to Ustream\'s or Qik\'s (yeah, I know not the best idea for a beginner

2条回答
  •  醉梦人生
    2021-02-04 19:20

    SipDroid does exactly what you need.

    It involves a hack to circumvent the limitation of the MediaRecorder class which require a file descriptor. It saves the result of the MediaRecorder video stream to a local socket (used as a kind of pipe), then re-read (in the same application but another thread) from this socket on the other end, create RTP packets out of the received data, and finally broadcast the RTP packets to the network (you can use here broadcast or unicast mode, as you wish).

    Basically it boils down to the following (simplified code):

    // Create a MediaRecorder
    MediaRecorder mr = new MediaRecorder();
    // (Initialize mr as usual)
    // Create a LocalServerSocket
    LocalServerSocket lss = new LocalServerSocket("foobar");
    // Connect both end of this socket
    LocalSocket sender = lss.accept();
    LocalSocket receiver = new LocalSocket();
    receiver.connect(new LocalSocketAddress("foobar"));
    // Set the output of the MediaRecorder to the sender socket file descriptor
    mr.setOutputFile(sender.getFileDescriptor());
    // Start the video recording:
    mr.start();
    // Launch a background thread that will loop, 
    // reading from the receiver socket,
    // and creating a RTP packet out of read data.
    RtpSocket rtpSocket = new RtpSocket();
    InputStream in = receiver.getInputStream();
    while(true) {
        fis.read(buffer, ...);
        // Here some data manipulation on the received buffer ...
        RtpPacket rtp = new RtpPacket(buffer, ...);
        rtpSocket.send(rtpPacket);
    }
    

    The implementation of RtpPacket and RtpSocket classes (rather simple), and the exact code which manipulate the video stream content can be found in the SipDroid project (especially VideoCamera.java).

提交回复
热议问题