18

I'm new to Android and socket programming. I want to create an android application that transfer video live from device camera to PC. What first i do is to get a raw video data from PreviewCallback arguments and convert it to an RTP Packet. I was just using JLibRTP to do this. Regarding to transfer the packet i think, there are some related class: RtpPkt, RtpSession, and RtpSocket.

Here is my glance code:

DatagramSocket rtpSocket = new DatagramSocket();
DatagramSocket rtcpSocket = new new DatagramSocket();
RtpSession rtpSession = new RtpSession(rtpSocket, rtcpSocket);

public void surfaceCreated(SurfaceHolder holder) {
    try {
            camera = Camera.open();
            camera.setPreviewCallback(new PreviewCallback() {
                public void onPreviewFrame(byte[] _data, Camera _camera) {
                int height = 240;
                    try {
                        rtps.sendData(_data);
                     } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(), e.toString(),
                        Toast.LENGTH_SHORT).show();
                    }
                }
            });
            camera.setPreviewDisplay(holder);
            camera.startPreview();
    } catch (IOException e) {
            Log.d("CAMERA", e.getMessage());
    }
}

I'm still wondering where i have to put address and port information. I know the code above still need correction from you any master. Thanks for advance..

M Rijalul Kahfi
  • 1,460
  • 3
  • 22
  • 42

1 Answers1

5

I don't know if this library includes something to stream the packets to the pc, but if not, you've a problem, because android only supports RTP streaming since version 3.1 (API level 12). if your level is lower, you have to write your own "rtp-server" which is able to stream the packets from your device to the pc.

for more information check out the sipdroid project. they have created their own "rtp-server": http://code.google.com/p/sipdroid/source/browse/trunk/src/org/sipdroid/sipua/ui/VideoCamera.java

UPDATE:

another possibility is to use the ffserver from the ffmpeg libraries, but therefore you have to compile the libraries for android. here is a small tutorial how to do this and how to work with the libraries: How to Build FFmpeg for Android

UPDATE2:

the spydroid application is a very good example to stream videos from an android device without any external libraries.

Rich
  • 1,015
  • 1
  • 10
  • 22
  • 1
    Yes i know that. In my case, the PC i used acts just as a trivial RTP server. I've also tried using VideoCamera.java in SipDroid. Yet it was really a very huge project so that i wonder from which i have to start. Is there anyone who got it working? – M Rijalul Kahfi Sep 07 '11 at 13:30
  • a rtp server on your pc isn't enough! you need something that is able to send the packets from your phone to your pc. so you have to implement a kind of "rtp-server" in your application – Rich Sep 08 '11 at 06:42
  • 2
    Why rtp packets couldn't sent from android phone to PC? I've tried using some class in SipDroid to create RTP packets from raw data within onPreviewFrame() arguments. For each frame, an rtp packet was created. The packet was then sent to PC address through sipDroid socket. Wireshark in my PC could catch that incoming packets well. But the player such VLC couldn't play them. Can you explain what were going wrong? Thanks for your answer. I'll try it as soon as i will understand the concepts. – M Rijalul Kahfi Sep 12 '11 at 04:20
  • 1
    I didn't define any codec in my code. Should i encode the preview frames into specific codec before packetizing them? – M Rijalul Kahfi Sep 15 '11 at 05:23
  • 1
    i think your library will do this automatically, so you don't have to set a codec. the reason why the player can't play the stream is maybe that the player doesn't know how to decode the stream in a correct format. how do you import the stream into vlc player? – Rich Sep 16 '11 at 11:43
  • 1
    just by playing "rtp://@:" in Open Stream Network dialog. The port was setted when creating RTP socket. – M Rijalul Kahfi Sep 18 '11 at 06:07
  • Maybe the player needs more information (e.g. from a sdp file), but a rtp stream doesn't deliver such information. you could try to integrate an rtsp server to your application to send the information of the stream to the player. – Rich Sep 19 '11 at 13:53
  • 1
    You're right. I have not created the sdp yet. But inthe short run, i will try to manually write an SDP file, and then play the streams using it. For sending the stream, I'll also make attempt using another approach such using FileDescriptor as SipDroid's VideoCamera does. – M Rijalul Kahfi Sep 19 '11 at 23:28