6

Currently, I have a working prototype app that streams the output from one phone's camera to another phone's screen.

However, the delay between the stream and the actual video ranges from 1 second to up to 5 seconds. For my particular use case, I need the delay to be less than 1 second.

I have found that bitrate / resolution does not affect the delay.

I am using the libstreaming library to stream the h264 video over wifi-direct.

It only appears to support encoding of h263 or h264... and I've found h263 does not work as consistently as h264.

Here is the code that captures the video and streams to an RTSP server:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_server);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    mSurfaceView = (SurfaceView) findViewById(R.id.surface);

    // Sets the port of the RTSP server to 8988
    Editor editor = PreferenceManager.getDefaultSharedPreferences(
            getApplicationContext()).edit();
    editor.putString(RtspServer.KEY_PORT, String.valueOf(8988));
    editor.commit();

    // Get bitrate
    int bitrate = Integer.valueOf(getIntent().getStringExtra(BITRATE));
    if (bitrate < 100000)
        bitrate = 100000;

    // Get resolution
    String resolution = getIntent().getStringExtra(RESOLUTION);
    int resX = 176;
    int resY = 144;
    if (resolution.equals("352x288")) {
        resX = 352;
        resY = 288;
    } else if (resolution.equals("528x432")) {
        resX = 528;
        resY = 432;
    } else if (resolution.equals("704x576")) {
        resX = 704;
        resY = 576;
    } else {
        resX = 176;
        resY = 144;
    }

    Toast.makeText(this, "Resolution: " + resX + "x" + resY + ", Bitrate: "
            + bitrate, Toast.LENGTH_LONG).show();

    // Configures the SessionBuilder
    SessionBuilder.getInstance().setSurfaceView(mSurfaceView)
            .setPreviewOrientation(0).setContext(this)
            .setVideoQuality(new VideoQuality(resX, resY, 20, bitrate))
            .setAudioEncoder(SessionBuilder.AUDIO_NONE)
            .setVideoEncoder(SessionBuilder.VIDEO_H264);
    // Starts the RTSP server
    getApplicationContext().startService(
            new Intent(getApplicationContext(), RtspServer.class));
}

And this is the code for viewing the streaming video:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_client);

    mVideoIP = getIntent().getStringExtra(SERVER_IP);

    mSurfaceView = (SurfaceView) findViewById(R.id.surface_view);
    mSurfaceHolder = mSurfaceView.getHolder();
    mSurfaceHolder.addCallback(this);


    if (savedInstanceState == null) {

    }
}
...
    @Override
public void surfaceCreated(SurfaceHolder holder) {
    try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDisplay(mSurfaceHolder);
        mMediaPlayer.setDataSource("rtsp://" + mVideoIP
                + ":8988");
        mMediaPlayer.prepare();
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setScreenOnWhilePlaying(true);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Doronz
  • 704
  • 8
  • 21
  • well, have an upvote at least. tough stuff – Caffeinated May 09 '14 at 20:52
  • 2
    Thanks! It actually hasn't been too complicated thanks to the libstreaming library and google's sample wifi-direct app. – Doronz May 09 '14 at 21:12
  • Does this application truly using WiFi direct only? I say this because I see RSTP addresses in the code so I think the app might need an internet connection to be able to stream video. – KillaKem Oct 31 '14 at 12:44
  • It doesn't have to use Wifi direct only. However, by doing so it eliminates many issues that can occur when using someone else's networks (namely firewalls and other restrictions that won't allow an RTSP server to run). This code is made for a project that is researching the capabilities of wifi-direct, so that is why it is intended for that particular use. It should work over wifi if you change the proper parameters. – Doronz Nov 01 '14 at 00:06
  • Do you have the full source for your solution available somewhere? I would like to peruse through it.I am particularly interested in seeing how the intent that starts the streaming on the RTSP is created since it seems to have some extra information attached to it.If not do you have a particular tutorial you used to accomplish this or did you just read the libstreaming docs and samples? – KillaKem Nov 03 '14 at 11:51
  • 1
    https://github.com/doronz/VirtualFrontView – Doronz Nov 06 '14 at 09:31
  • @Doronz did you ever fix it? I am kinda doing the same thing but i am getting a lag of about half a second which i am pretty sure could be improved. – thunderbird Mar 23 '15 at 14:40
  • @thunderbird did you solve your problem ? i have the same issue and getting a lag of about 3 sec. – HAMED Feb 11 '17 at 18:08
  • @HAMED 3 seconds is too much, in my case it was just around 100-400 ms – thunderbird Mar 04 '17 at 14:40
  • @HAMED what you need to do is encode the stream using h264 encoder – thunderbird Mar 04 '17 at 14:42
  • The first thing to do is to profile your application in order to understand where is the issue. It can be : - Encoding Time - Network Latency - Decoding - ... Once you have this information you will be able to know where you need to investigate ! – Maldus Jul 04 '17 at 09:34

0 Answers0