pedroSG94 / RootEncoder

RootEncoder for Android (rtmp-rtsp-stream-client-java) is a stream encoder to push video/audio to media servers using protocols RTMP, RTSP, SRT and UDP with all code written in Java/Kotlin
Apache License 2.0
2.54k stars 771 forks source link

when start video #316

Closed alpertayfun closed 5 years ago

alpertayfun commented 5 years ago

I added surfaceview into layout and everything is perfect but I can not see video when start of activity. when I click to change camera way and call switchCamera(). Btw , I can see video front and then back.

This is bug or something ?

My code is :

`rtmpCamera1 = new RtmpCamera1(glView, connectCheckerRtmp); if (rtmpCamera1.prepareAudio() && rtmpCamera1.prepareVideo()) { rtmpCamera1.startStream(urlForLive); } else { Log.d("livestream","This device cant init encoders, this could be for 2 reasons: The encoder selected doesnt support any configuration setted or your device hasnt a H264 or AAC encoder (in this case you can see log error valid encoder not found)"); }

    rtmpCamera1.startPreview();`
pedroSG94 commented 5 years ago

Did you startPreview on surfacechanged method? https://github.com/pedroSG94/rtmp-rtsp-stream-client-java/blob/master/app/src/main/java/com/pedro/rtpstreamer/defaultexample/ExampleRtmpActivity.java#L191

alpertayfun commented 5 years ago

No. Still is black when start of activity. I added same startPreview into surface surfaceCreated method. But still black. But when I click change camera way then I got video.

Phone is General Mobile GM8 Go Edition.

pedroSG94 commented 5 years ago

Is the demo app working?

alpertayfun commented 5 years ago
public class GoLive extends AppCompatActivity  implements SelectTypesChooseViewAdapter.ItemClickListener,PublisherListener ,ConnectCheckerRtmp, View.OnClickListener, SurfaceHolder.Callback  {
    private static final String PREFS_NAME = "preferenceName";
    VideoView videoView;
    public static int VIDEO_CAPTURED = 1;
    Uri videoFileUri;
    RelativeLayout rlLoading;
    private SelectTypesChooseViewAdapter selectTypesAdapter;
    private CameraPreview mPreview;
    private Camera.PictureCallback mPicture;
    private boolean cameraFront = false;
    SurfaceView glView;
    Publisher publisher;

    RtmpCamera1 rtmpCamera1;
    String roomId = "";
    String token = "";

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_go_live);

        String datas = readFromFileIntoInternal(getString(R.string.fileNameForData));
        Log.d("Logined datas" ,datas);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

       // final Context ctx = this;
        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if(extras == null) {
                roomId= null;
            } else {
                roomId= extras.getString("roomId");
            }
        } else {
            roomId= (String) savedInstanceState.getSerializable("roomId");
        }

        String urlForLive = "rtmp://localhost:1935/"+roomId+"/live";

        Log.d("urlForLive", urlForLive);

        ConnectCheckerRtmp connectCheckerRtmp = new ConnectCheckerRtmp() {
            @Override
            public void onConnectionSuccessRtmp() {
                Log.d("goLive","onConnectionSuccessRtmp");

            }

            @Override
            public void onConnectionFailedRtmp(String reason) {
                Log.d("goLive","onConnectionFailedRtmp : " + reason);
                rtmpCamera1.stopStream();
            }

            @Override
            public void onDisconnectRtmp() {
                Log.d("goLive","onDisconnectRtmp");

            }

            @Override
            public void onAuthErrorRtmp() {
                Log.d("goLive","onAuthErrorRtmp");

            }

            @Override
            public void onAuthSuccessRtmp() {
                Log.d("goLive","onAuthSuccessRtmp");

            }
        };

        rtmpCamera1 = new RtmpCamera1(glView, connectCheckerRtmp);
        if (rtmpCamera1.prepareAudio() && rtmpCamera1.prepareVideo()) {
            rtmpCamera1.startStream(urlForLive);
        } else {
            Log.d("goLive","This device cant init encoders, this could be for 2 reasons: The encoder selected doesnt support any configuration setted or your device hasnt a H264 or AAC encoder (in this case you can see log error valid encoder not found)");
        }

        glView.getHolder().addCallback(this);

    }

     @Override
    public void onDestroy() {
        super.onDestroy();

        rtmpCamera1.stopStream();
    }

     @Override
    public void surfaceCreated(SurfaceHolder holder) {
        rtmpCamera1.startPreview();
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
        rtmpCamera1.startPreview();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        rtmpCamera1.stopPreview();
        rtmpCamera1.stopStream();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void onStarted() {

    }

    @Override
    public void onStopped() {

    }

    @Override
    public void onDisconnected() {

    }

    @Override
    public void onFailedToConnect() {

    }

    @Override
    public void onConnectionSuccessRtmp() {

    }

    @Override
    public void onConnectionFailedRtmp(String reason) {

    }

    @Override
    public void onDisconnectRtmp() {

    }

    @Override
    public void onAuthErrorRtmp() {

    }

    @Override
    public void onAuthSuccessRtmp() {

    }
}    

implementation 'com.github.pedroSG94.rtmp-rtsp-stream-client-java:rtplibrary:1.5.8'

pedroSG94 commented 5 years ago

Your error is because you are doing startStream onCreate but you can't open a preview because surface is not created yet (startStream and startRecord open a preview if you haven't one). In this case your startPreview is ignored because you are streaming. https://github.com/pedroSG94/rtmp-rtsp-stream-client-java/blob/master/rtplibrary/src/main/java/com/pedro/rtplibrary/base/Camera1Base.java#L302

If you want start stream when you open activity you should wait surfaceChanged before start stream, you can't do it in onCreate method. startPrview should be called if you want a preview before start stream(For example, open activity with preview and startStream when you click on a button like in my example).

Activity modified:

public class GoLive extends AppCompatActivity
    implements SelectTypesChooseViewAdapter.ItemClickListener,PublisherListener ,
    ConnectCheckerRtmp, View.OnClickListener, SurfaceHolder.Callback  {
  private static final String PREFS_NAME = "preferenceName";
  VideoView videoView;
  public static int VIDEO_CAPTURED = 1;
  Uri videoFileUri;
  RelativeLayout rlLoading;
  private SelectTypesChooseViewAdapter selectTypesAdapter;
  private CameraPreview mPreview;
  private Camera.PictureCallback mPicture;
  private boolean cameraFront = false;
  SurfaceView glView;
  Publisher publisher;
  String urlForLive;
  RtmpCamera1 rtmpCamera1;
  String roomId = "";
  String token = "";

  @Override
  protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_go_live);

    String datas = readFromFileIntoInternal(getString(R.string.fileNameForData));
    Log.d("Logined datas" ,datas);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    // final Context ctx = this;
    if (savedInstanceState == null) {
      Bundle extras = getIntent().getExtras();
      if(extras == null) {
        roomId= null;
      } else {
        roomId= extras.getString("roomId");
      }
    } else {
      roomId= (String) savedInstanceState.getSerializable("roomId");
    }

    urlForLive = "rtmp://localhost:1935/"+roomId+"/live";

    Log.d("urlForLive", urlForLive);

    ConnectCheckerRtmp connectCheckerRtmp = new ConnectCheckerRtmp() {
      @Override
      public void onConnectionSuccessRtmp() {
        Log.d("goLive","onConnectionSuccessRtmp");

      }

      @Override
      public void onConnectionFailedRtmp(String reason) {
        Log.d("goLive","onConnectionFailedRtmp : " + reason);
        rtmpCamera1.stopStream();
      }

      @Override
      public void onDisconnectRtmp() {
        Log.d("goLive","onDisconnectRtmp");
      }

      @Override
      public void onAuthErrorRtmp() {
        Log.d("goLive","onAuthErrorRtmp");

      }

      @Override
      public void onAuthSuccessRtmp() {
        Log.d("goLive","onAuthSuccessRtmp");

      }
    };
    rtmpCamera1 = new RtmpCamera1(glView, connectCheckerRtmp);
    glView.getHolder().addCallback(this);
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
  }

  @Override
  public void surfaceCreated(SurfaceHolder holder) {
  }

  @Override
  public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
    if (rtmpCamera1.prepareAudio() && rtmpCamera1.prepareVideo()) {
      rtmpCamera1.startStream(urlForLive);
    } else {
      Log.d("goLive","This device cant init encoders, this could be for 2 reasons: The encoder selected doesnt support any configuration setted or your device hasnt a H264 or AAC encoder (in this case you can see log error valid encoder not found)");
    }
  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
    if (rtmpCamera1.isStreaming()) rtmpCamera1.stopPreview();
    if (rtmpCamera1.isOnPreview()) rtmpCamera1.stopStream();
  }

  @Override
  public void finish() {
    super.finish();
    overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
  }

  @Override
  public void onBackPressed() {
    super.onBackPressed();
  }

  @Override
  public void onStarted() {

  }

  @Override
  public void onStopped() {

  }

  @Override
  public void onDisconnected() {

  }

  @Override
  public void onFailedToConnect() {

  }

  @Override
  public void onConnectionSuccessRtmp() {

  }

  @Override
  public void onConnectionFailedRtmp(String reason) {

  }

  @Override
  public void onDisconnectRtmp() {

  }

  @Override
  public void onAuthErrorRtmp() {

  }

  @Override
  public void onAuthSuccessRtmp() {

  }
}
alpertayfun commented 5 years ago

I just added switchCamera in surfaceCreated method. And it is display right now but still not working on start of activity. How can I change camera way on start of rtmp stream ?

  @Override
    public void surfaceCreated(SurfaceHolder holder) {
        rtmpCamera1.startPreview();
        rtmpCamera1.switchCamera();
    }

Sorry. I see now your reply. Really thank you. :)

pedroSG94 commented 5 years ago

If you want start with front camera use this surfaceChanged method:

  @Override
  public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
    rtmpCamera1.startPreview(CameraHelper.Facing.FRONT);
    if (rtmpCamera1.prepareAudio() && rtmpCamera1.prepareVideo()) {
      rtmpCamera1.startStream(urlForLive);
    } else {
      Log.d("goLive","This device cant init encoders, this could be for 2 reasons: The encoder selected doesnt support any configuration setted or your device hasnt a H264 or AAC encoder (in this case you can see log error valid encoder not found)");
    }
  }
alpertayfun commented 5 years ago

Thank you. : )