Skip to content

Streaming

Hsueh-Jen edited this page May 28, 2020 · 22 revisions

Overview

StraaS Streaming SDK provides a easy way to establish a Real-Time streaming to StraaS!

Getting Started

This shows you how to start a streaming step by step. You could read javadoc for more detail about the code this page mentions below.

Include

Add dependency on jCenter using Gradle.

X.X.X is your preferred version. For the version information, see CHANGELOG

compile 'io.straas.android.sdk:straas-streaming:X.X.X'

Permission

Before using StreamManager, you have to get permission of camera and microphone for StreamManager.

Credential

Fulfill StraaS SDK-Credential to enable StraaS Streaming SDK.

Validate

StreamManager.initialize(MemberIdentity.ME).addOnSuccessListener(new OnSuccessListener<StreamManager>() {
    @Override
    public void onSuccess(StreamManager streamManager) {
        //Keep your StreamManager
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        //handle error
    }
});

Prepare

StreamConfig config = new StreamConfig()
                .setCamera(StreamConfig.CAMERA_FRONT)
                .setFitAllCamera(true);
mStreamManager.prepare(config, mTextureView).addOnSuccessListener(new OnSuccessListener<CameraController>() {
    @Override
    public void onSuccess(CameraController cameraController) {
        //Keep your CameraController
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        //Handle error
    }
});
  • mTextureView is a TextureView where you want to preview on. After prepare(...) succeeds, you will get a CameraController to get/set camera and flash state.

Set listener

You can use addEventListener(...) to add listener about error and stream statistics update, see EventListener implementation for more details.

Start streaming

After prepare succeeds, you will be able to start streaming by the two steps below

String title = "live event title";
String synopsis = "live event synopsis";
LiveEventConfig config = new LiveEventConfig.Builder()
        .title(title)
        .synopsis(synopsis)
        .build();
mStreamManager.createLiveEvent(config)
        .addOnSuccessListener(new OnSuccessListener<String>() {
        @Override
        public void onSuccess(String liveId) {
            // Keep live event id
        }})
//Live event id you got in last step
String liveId;
mStreamManager.startStreaming(liveId)
        .addOnSuccessListener(new OnSuccessListener<String>() {
    @Override
    public void onSuccess(String liveId) {
        //Change UI
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        //Error handling
    }
});

After the operation succeeds, it means you are streaming to StraaS as the identity you provided previously.

Stop streaming / destroy

  • To stop streaming, you can call stopStreaming() to stop streaming and continue to preview. After this, you could set the previous live event to ENDED by cleanLiveEvent if you want.
mStreamManager.stopStreaming().addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if (task.isSuccessful()) {
            mStreamManager.cleanLiveEvent(mLiveId).addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    mLiveId = null;
                }
            });
        } else {
            //Error handling
        }
    }
});
  • To destroy StreamManager, you can call destroy() to stop streaming and previewing.
mStreamManager.destroy().addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        //Handle result
    }
});

Filter

StraaS Streaming SDK provides a method for setting filter for video streaming.
We will introduce how to set a filter below.

Get StreamStatsReport

  • To get StreamStatsReport, you can call getStreamStatsReport() to retrieve the current stream statistics such as bitrate and fps from StreamStatsReport. You could use Handler to update streaming statistics periodically if you want.
  • Alternatively, you could use addEventListener() to subscribe onStreamStatsReportUpdate event and listen to the update of stream statistics report automatically, see Set listener for more details.
private static final int EVENT_UPDATE_STREAMING_STATS = 101;

private final Handler mMainThreadHandler = new Handler(Looper.getMainLooper()) {
    public void handleMessage(Message msg) {
        switch(msg.what) {
            case EVENT_UPDATE_STREAMING_STATS:
                StreamStatsReport stats = mStreamManager.getStreamStatsReport();
                Log.d(TAG, "EVENT_UPDATE_STREAMING_STATS bitrate: " + stats.getBitrate());
                Log.d(TAG, "EVENT_UPDATE_STREAMING_STATS fps: " + stats.getFps());

                if (mStreamManager.getStreamState() != StreamManager.STATE_IDLE) {
                    mMainThreadHandler.removeMessages(EVENT_UPDATE_STREAMING_STATS);
                    mMainThreadHandler.sendEmptyMessageDelayed(EVENT_UPDATE_STREAMING_STATS, 1000);
                }
                break;
        }
    }
};

You can see API document for more details.