mrmaffen / vlc-android-sdk

VLC Android SDK pushed to Maven Central. Primarily used in project tomahawk-android.
792 stars 244 forks source link

How to center content video on surfaceview? #107

Open iceteahh opened 6 years ago

iceteahh commented 6 years ago

I'm using your library to render RTSP video from Wowza. Every thing work good except that video was being draw at the bottom left of SurfaceView. How can I center it?

JohanJarvi commented 6 years ago

I'm having that exact same problem, not only is it not centered but it's also not fullscreen, please advice if you figure out how to solve these issues, I've been struggling with it for weeks.

JohanJarvi commented 6 years ago

@iceteahh okay I figured out how to center the video in the SurfaceView but it has a few steps involved in doing so. Basically the breakdown is:

  1. Find the size of your screen.
  2. Set up your final IVLCOut to incorporate the screen size.

To explain each task:

  1. Setup your globals:

    public class SingleStreamView extends AppCompatActivity implements IVLCVout.Callback {
    
    public int mHeight;
    public int mWidth;

    Secondly, in the onCreate task find your screen sizes of your device:

    
    DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        mHeight = displayMetrics.heightPixels;
        mWidth = displayMetrics.widthPixels;
2.
Then go down to your "CreatePlayer" event and where you set up your video output:

// Set up video output final IVLCVout vout = mMediaPlayer.getVLCVout(); vout.setVideoView(mSurface); vout.setWindowSize(mWidth,mHeight); vout.addCallback(this); vout.attachViews();


The winning line that made it center in my surface was the "vout.setWindowSize(mWidth,mHeight);"

I hope this helped, it helped me center my video feed in my app.

Then I simply used the setscale option to "fullscreen" the video. That said, it's a bit of a hack, and I would like to try and figure out a way to grab the codec information so to dynamically set the scale of the video.

Either way I found that with a Samsung Galaxy s8, a good scaling factor for a 640x480p RTSP stream was 1.8. Coded like so:
        Media m = new Media(libvlc, Uri.parse(RTSP_ADDRESS));
        m.setHWDecoderEnabled(true,false);
        m.addOption(":network-caching=100");
        m.addOption(":clock-jitter=0");
        m.addOption(":clock-synchro=0");
        m.addOption(":fullscreen");
        mMediaPlayer.setMedia(m);
        mMediaPlayer.setAspectRatio("16:9");
        mMediaPlayer.setScale(1.8f);
        mMediaPlayer.play();


Where you got "mMediaPlayer.setScale(1.8f);"

Hope this helps you!
iceteahh commented 6 years ago

@JohanJarvi good work man, I will try out your solution.

JohanJarvi commented 6 years ago

@iceteahh yeah if you got any issues just comment and I'll try answer it. It could be that for some reason this just happened to work for me but the video is now in center and pretty much fullscreen on my phone and it looks a hell of a lot better than a tiny video in the bottom left corner so I'm quite happy. But like I said it has limitations and I've only tested it on a single device. So I'd be interested in hearing how it worked for you and what device you're using.

Anyway, hopefully it helped a little!

maxim-petlyuk commented 5 years ago

Your solution assumes that you know the size of the video. I think that it will better to look through the TextureView scaling.