-
Notifications
You must be signed in to change notification settings - Fork 8
Streaming
StraaS Streaming SDK provides a easy way to establish a Real-Time streaming to StraaS!
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.
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'
Before using StreamManager, you have to get permission of camera and microphone for StreamManager.
Fulfill StraaS SDK-Credential to enable StraaS Streaming SDK.
- To get a StreamManager, you should call StreamManager.initialize(...). This method will check if developer has passed StraaS SDK-Credential. You will get a StreamManager if validation succeeds.
- You have to put the Identity in. Then, any methods you call from the StreamManager you get will be done by this Identity.
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
}
});
- After getting a StreamManager, you can use prepare(...) to put configuration in StreamManager and initialize.
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.
You can use addEventListener(...) to add listener about error and stream statistics update, see EventListener implementation for more details.
After prepare succeeds, you will be able to start streaming by the two steps below
- Use createLiveEvent to create a live event.
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
}})
- Then use startStreaming to start streaming by 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.
- 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
}
});
StraaS Streaming SDK provides a method for setting filter for video streaming.
We will introduce how to set a filter below.
-
Before starting, you should study OpenGLES first.
-
You can implement BaseImageFilter and override methods in it to create a filter you want. Here is an example.
-
Use StreamingManager.setFilter(...) and put the BaseImageFilter in.
- 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.