This topic describes how to use the AliLiveSDK for Android.

Pushes stream over RTMP

  1. Create an AliLiveEngine object.
    // Create an AliLiveRTMPConfig object.
    AliLiveRTMPConfig rtmpConfig = new AliLiveRTMPConfig();
    // Initialize the bitrate settings.
    rtmpConfig.videoInitBitrate = 1000;
    rtmpConfig.videoTargetBitrate = 1500;
    rtmpConfig.videoMinBitrate = 600;
    // Create an AliLiveConfig object.
    AliLiveConfig mAliLiveConfig = new AliLiveConfig(rtmpConfig);
    // Initialize the resolution and frame rate, specify whether to enable high-definition preview, and specify the default image to be displayed after a pause.
    mAliLiveConfig.videoFPS = 20;
    mAliLiveConfig.videoPushProfile = AliLiveConstants.AliLiveVideoPushProfile.AliLiveVideoProfile_540P;
    mAliLiveConfig.enableHighDefPreview = false;
    mAliLiveConfig.pauseImage = bitmap;
    mAliLiveConfig.accountId = "";
    AliLiveEngine mAliLiveEngine = AliLiveEngine.create(PushActivity.this, mAliLiveConfig);
  2. Start the preview.
    // Create a preview view.
    AliLiveRenderView mAliLiveRenderView = mAliLiveEngine.createRenderView(false);
    // Add the preview view to the layout.
    addSubView(mAliLiveRenderView);
    // Set the preview mode.
    mAliLiveEngine.setPreviewMode(AliLiveRenderModeAuto, AliLiveRenderMirrorModeOnlyFront);
    // Start the preview.
    mAliLiveEngine.startPreview(mAliLiveRenderView);
  3. Start to push streams.
    mAliLiveEngine.startPush(mPushUrl);
  4. Stop pushing streams.
    // Stop the preview.
    mAliLiveEngine.stopPreview();
    // Stop pushing streams.
    mAliLiveEngine.stopPush();
    // Destroy the AliLiveEngine object.
    mAliLiveEngine.destroy();
    mAliLiveEngine = null;

Stream pulling over RTMP

  1. Create a player.
    // Create a player.
    mAliPlayer = AliPlayerFactory.createAliPlayer(this.getApplicationContext());
    // Enable autoplay.
    mAliPlayer.setAutoPlay(true);
    // Set the mMaxDelayTime parameter. We recommend that you set the parameter to a value in the range of 500 to 5000. A smaller value indicates a smaller live streaming latency, but a greater chance of stuttering. Set the parameter as needed.
    PlayerConfig config = mAliPlayer.getConfig();
    config.mMaxDelayTime = 1000;
    mAliPlayer.setConfig(config);
  2. Add the SurfaceView view.
        mSurfaceView.getHolder().addCallback(this);
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            if (mAliPlayer! =null){
                mAliPlayer.setSurface(holder.getSurface());
            }
        }
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            if (mAliPlayer! =null){
                mAliPlayer.surfaceChanged();
            }
        }
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            if (mAliPlayer! =null){
                mAliPlayer.setSurface(null);
            }
        }
  3. Start to pull streams.
    // Set the source URL for the player.
    UrlSource source = new UrlSource();
    source.setUri(mPullUrl);
    mAliPlayer.setDataSource(source);
    // Start to pull streams.
    mAliPlayer.prepare();
  4. Stop pulling streams.
    mAliPlayer.stop();
    mAliPlayer.setSurface(null);
    mAliPlayer.release();
    mAliPlayer = null;

Co-stream

Anchor side

  1. The anchor enters the room and starts to push Real-Time Communication (RTC) streams.
    // Enter the room.
    // Inform the AppServer to call the stream merging service to merge streams in the co-stream scenarios.
    mSocketHandler.send(getSendMessage("JoinRoom",isAllowMicLink,0));
    // Start to push RTC streams after a callback indicating a successful entry into the room is received.
    mAliLiveEngine.startPush(mRtcPushUrl);
    // After RTC streams are pushed, send a message to notify the audience that the anchor has started live streaming.
    mSocketHandler.send(getSendMessage("push", isAllowMicLink, 0));
  2. The anchor receives and accepts the co-stream request from the audience.
    mSocketHandler.send(getSendMessage("ApproveMic",isAccept,userInfo.getUserId()));
  3. When the anchor receives a message indicating that the audience has pushed RTC streams, the anchor subscribes to the RTC streams that are pushed by the audience.
    mAliLiveEngine.subscribeStream(urlBean.getRtcPullUrl(),true);

    The anchor renders the UI of the audience to the view after the onSubscribeResult callback indicating successful subscription is received.

    // Create a render view.
    AliLiveRenderView renderView = urlBean.getRenderView();
    // Bind the render view and source URL.
    mAliLiveEngine.renderRemoteStreamWithView(renderView, s );
    // Add the preview view to the layout.
    addSubView(mAliLiveRenderView);
  4. When the anchor receives a message indicating that the audience has turned off the microphone, the anchor unsubscribes from the RTC streams that are pushed by the audience.
    mAliLiveEngine.unSubscribeStream(remoteUserInfo.getRtcPullUrl());

Audience side

  1. The audience enters the room and starts to pull RTMP streams.
    mSocketHandler.send(getSendMessage("JoinRoom"));
    // Initialize the player. For more information, see the "Stream pulling over RTMP" section of this topic.
    // .....
    // Start to pull streams.
    UrlSource source = new UrlSource();
    source.setUri("The source URL");
    mAliPlayer.setDataSource(source);
    mAliPlayer.prepare();
  2. The audience sends a co-stream request.
    mSocketHandler.send(getSendMessage("ApplyMic"));
  3. The co-stream begins after the audience receives a message indicating that the anchor accepts the co-stream request.
    mAliLiveEngine.startPush(url);

    After RTC streams are pushed, the audience sends a message notifying the anchor that the co-stream begins. The audience starts to pull the RTC streams that are pushed by the anchor and other audiences.

    // Notify the AppServer that stream push has started.
    mSocketHandler.send(getSendMessage("Publish"));
    // Subscribe to the RTC streams that are pushed by the anchor and other audiences.
    //......

    After the anchor pulls RTC streams, the audience stops the playback of RTMP streams.

    mAliPlayer.stop();
  4. The audience turns off the microphone.
    // Stop the preview.
    stopPreview();
    // Stop pushing streams.
    stopPush();
    // Stop pulling streams.
    stopPull();
    // Start the playback of RTMP streams.
    startPlayRTMP();
    // Send a message indicating that the audience has turned off the microphone to the AppServer.
    mSocketHandler.send(getSendMessage("UnPublish"));

Anchor PK

  1. Anchor A sends a request to the AppServer to query other anchors that are online.
    mSocketHandler.send(getSendMessage(CMD_GET_ROOM_LIST));

    Anchor A renders the obtained list of anchors to the view.

    mOnlineRoomView.setRoomList(onlineRoomList);
  2. Anchor A sends a PK request to Anchor B in the list.
    mSocketHandler.send(getSendMessage(CMD_APPLY_PK,room.getUserId(),room.getRoomId()));
  3. Anchor B receives a notification about the PK request from Anchor A.
    showApplyPkDialog(applyPkNoticeBean);
  4. Anchor B accepts the PK request from Anchor A, starts to pull RTC streams, and then sends a message indicating that the PK request is accepted.
    // Subscribe to the RTC streams that are pushed by Anchor A.
    startPk(mLastApplyPkNotice.getFromRtcPullUrl());
    // Send a message indicating that the PK request is accepted.
    mSocketHandler.send(getSendMessage(CMD_APPROVE_PK,true ,mLastApplyPkNotice.getFromUserId(),mLastApplyPkNotice.getFromRoomId() ));
  5. After Anchor A receives the message indicating that Anchor B accepts the PK request, Anchor A starts to pull RTC streams.
    // Start PK.
    startPk(approvePkNoticeBean.getRtcPullUrl());
  6. After the onSubscribeResult callback indicating successful subscription is received, Anchor A adjusts the layout of the preview view and subscription view.
    showPkSurfaceView(true);
  7. The RTMP streaming URL on the audience clients changes. After the NotifyPublish message is received, the audience clients construct a new streaming URL for the player.
    // Check whether the source URL is the same as the previous source URL used by Anchor A. If they are different, republish streams.
    if (! mRoomInfo.getRtmpPullUrl().equals(noticePublish.getAnchorRtmpPullUrl())){
        mRoomInfo.setRtmpPullUrl(noticePublish.getAnchorRtmpPullUrl());
        startPlayRTMP();
    }
  8. Anchor B stops PK, sends a message indicating that PK is stopped, stops pulling RTC streams, and then restores the UI before PK.
    // Send a message indicating that PK is stopped.
    mSocketHandler.send(getSendMessage(CMD_CANCEL_PK));
    // Restore the UI before PK.
    showPkSurfaceView(false);
  9. The RTMP streaming URL on the audience clients changes. After the NotifyPublish message is received, the audience clients construct a new streaming URL for the player.
    // Check whether the source URL is the same as the previous source URL used by Anchor A. If they are different, republish streams.
    if (! mRoomInfo.getRtmpPullUrl().equals(noticePublish.getAnchorRtmpPullUrl())){
        mRoomInfo.setRtmpPullUrl(noticePublish.getAnchorRtmpPullUrl());
        startPlayRTMP();
    }