Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RTSP live video streaming

I'm building an application to live stream video from the raspberry pi to my android device. I have three functional ways of playing this stream:

rtsp://media.smart-streaming.com/mytest/mp4:sample_phone_150k.mp4

  1. Media Player + SurfaceView
  2. VideoView
  3. Intent.ACTION_VIEW

The problem is the application plays the above^ stream but doesn't play mine (rtsp://192.168.1.143:8554/vid.mp4) ... but VLC does.

I have tried streaming with VLC and with LIVE555 in multiple video formats and I've also tried playing a video that was recorded on the phone.

Here is my code:

//Stream methods 0 = MediaPlayer & SurfaceView, 1 = VideoView, 2 = Native Video Player
final int STREAM_USING = 0;

//MediaPlayer on surfaceView
String streamPath = "rtsp://192.168.1.143:8554/vid.mp4";//"rtsp://media.smart-streaming.com/mytest/mp4:sample_phone_150k.mp4";//"rtsp://192.168.1.143:8554/vid.mp4";//;"rtp://239.255.0.1:5004/";
Uri streamUri;
private MediaPlayer mediaPlayer;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;

//VideoView
VideoView videoView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //MediaPlayer
    switch (STREAM_USING) {
        case 0: {
            surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
            surfaceHolder = surfaceView.getHolder();
            surfaceHolder.setFixedSize(800, 480);
            surfaceHolder.addCallback(this);
            surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            mediaPlayer = new MediaPlayer();

            mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
                        mediaPlayer.release();
                        mediaPlayer = new MediaPlayer();
                        //mediaPlayer.stop();
                        play();
                    }
                    return false;
                }
            });

            mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
                @Override
                public void onBufferingUpdate(MediaPlayer mp, int percent) {
                    Toast.makeText(getApplicationContext(), "BUFF : " + percent, Toast.LENGTH_SHORT).show();
                    if (!mediaPlayer.isPlaying()) {
                        mediaPlayer.start();
                    }
                }
            });

            //Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            //i.setType("video/*");
            //startActivityForResult(i, 1234);
            streamUri = Uri.parse(streamPath);
            play();
            break;
        }
        case 1: {
            videoView = (VideoView) findViewById(R.id.videoView);
            videoView.setVideoURI(Uri.parse(streamPath));
            MediaController mediaController = new MediaController(this);
            //mediaController.setAnchorView(videoView);
            videoView.setMediaController(mediaController);
            videoView.requestFocus();
            try {
                videoView.start();
            }
            catch (SecurityException se) {
                Log.e("SE", se.getMessage());
                se.printStackTrace();
            }
            break;
        }
        case 2: {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(streamPath));
            startActivity(intent);
            break;
        }
        default: {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(streamPath));
            startActivity(intent);
        }
    }
}

private void play() {
    try {
        //final FileInputStream fis = new FileInputStream(streamPath);
        mediaPlayer.stop();
        mediaPlayer.reset();
        mediaPlayer.setDataSource(MainActivity.this, streamUri);
        //mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        mediaPlayer.prepareAsync();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                //mediaPlayer.reset();
                mediaPlayer.start();
            }
        });


    } catch (SecurityException se) {
        Log.e("SE", se.getMessage());
        se.printStackTrace();
    } catch (IOException ie) {
        ie.printStackTrace();
    }
}

Any help is appreciated, I've been trying for a week to get this to work with no success :(

like image 692
Ciprian Avatar asked Apr 30 '16 08:04

Ciprian


People also ask

What RTSP live?

“The Real-Time Streaming Protocol (RTSP) establishes and controls either a single or several time-synchronized streams of continuous media such as audio and video. It does not typically deliver the continuous streams itself, although interleaving of the continuous media stream with the control stream is possible.

Can you view an RTSP stream with a browser?

As a rule, browsers do not support RTSP, so the video stream is converted for a browser using an intermediate server.

Is RTSP outdated?

As you now know, RTSP is still in widespread use with IP cameras, which are often employed for use in video conferencing, public streams/webcams, and security, monitoring, and CCTV systems.


1 Answers

You already added two ways just i want to add bit modification. I have done streaming ralalted task.I have used rtsp with wowza.Few ways i'll let you know

try it once and let me know if you got issue.If your streaming in vlc working fine then something problem with app side. If these methods are not working then try with different phone. It also help you.

1. Try with videoview

vvVideoPlay = (VideoView) findViewById(R.id.vvVideoPlay);
MediaController mediaController = new MediaController(this);

String videoUrl = "rtsp://192.168.1.143:8554/vid.mp4";

    mediaController.setAnchorView(vvVideoPlay);
    Uri uri = Uri.parse(videoUrl);
    vvVideoPlay.setVideoURI(uri);
    vvVideoPlay.setMediaController(mediaController);
    vvVideoPlay.requestFocus();

    vvVideoPlay.setOnPreparedListener(new OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {

            mp.start();
            pdialog.dismiss();
            mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {

                @Override
                public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

                    mp.start();
                }
            });
        }
    });

2. Try with direct your phone player

startActivity(new Intent(Intent.ACTION_VIEW,
                Uri.parse("rtsp://192.168.1.143:8554/vid.mp4")));

3. Third way try to with this library with custom player in your app.

Step1. Add it to your gradle

compile "fm.jiecao:jiecaovideoplayer:4.7.0"

Step2. Add it as your video play in xml layout.

<fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
    android:id="@+id/videoPlayer"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Step 3. Check from here how to use this library in your class,

public class PlayVideoActivity extends BaseActivity {

@BindView(R.id.videoPlayer)
JCVideoPlayerStandard mVideoPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    restoreFromIntent(getIntent());
}

@Override
public int getLayout() {
    return R.layout.activity_play_video;
}


// get video path from intent and play the video here
private void restoreFromIntent(Intent intent) {

    mVideoPlayer.setUp("rtsp://192.168.1.143:8554/vid.mp4"
            , JCVideoPlayerStandard.SCREEN_LAYOUT_LIST, "");
}

@Override
public void onBackPressed() {
    if (JCVideoPlayer.backPress()) {
        return;
    }
    super.onBackPressed();
}

@Override
protected void onPause() {
    super.onPause();
    JCVideoPlayer.releaseAllVideos();
}
}

Hope this will help you to fix your problem.Thanks

like image 57
Saveen Avatar answered Oct 30 '22 18:10

Saveen