-
Notifications
You must be signed in to change notification settings - Fork 8
Screencast Streaming
StraaS Streaming SDK provides screencast
feature to stream your screen contents into StraaS!
This shows you how to start a screencast streaming step by step. You should read Streaming first for the basic knowledge of StraaS Streaming SDK or javadoc for more detail about the code this page mentions below.
Add dependency on jCenter using Gradle.
X.X.X is your preferred version, the screencast feature is bundled with StraaS Streaming SDK starting from version 0.8.0 and only support for Android Lollipop 5.0 devices above.
compile 'io.straas.android.sdk:straas-streaming:X.X.X'
-
Before using screencast streaming, you have to get the permission of microphone, screen capture and system overlay(only for Android M above).
-
Get the permission of screen capture
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void startScreenCapture(View view) {
startActivityForResult(mMediaProjectionManager.createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_MEDIA_PROJECTION && resultCode == Activity.RESULT_OK) {
// resultCode and data are used to generate media projection
mResultCode = resultCode;
mResultData = data;
...
}
}
- Get the permission of system overlay
@TargetApi(Build.VERSION_CODES.M)
private void requestOverlayPermission() {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, REQUEST_OVERLAY_PERMISSION);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
} else if (requestCode == REQUEST_OVERLAY_PERMISSION && resultCode == Activity.RESULT_OK) {
// overlay permission granted, can check with Settings.canDrawOverlays
...
}
}
See Credential
- To get a StreamManager, you should call StreamManager.initialize(...) with identity and screencast config bundle. This method will check if developer has passed StraaS SDK-Credential and then start our
ScreencastService
to connect to your declared class implementsScreencastSession
interface.
private void startScreenStreaming() {
...
Bundle bundle = new Bundle();
...
// bundle extras will redirect to your declared class implements ScreencastSession
bundle.putString(EXTRA_LIVE_EVENT_TITLE, mEditTitle.getText().toString());
bundle.putInt(EXTRA_LIVE_VIDEO_QUALITY, mVideoQuality);
StreamManager.initialize(MemberIdentity.ME, bundle);
}
- You will get a StreamManager from the
onStreamManagerInitComplete
callback inside your class implements theScreencastSession
interface if validation succeeds.
@Override
public void onStreamManagerInitComplete(@NonNull Task<StreamManager> task) {
if (!task.isSuccessful()) {
...
}
// Valid StreamManager from onStreamManagerInitComplete callback
mStreamManager = task.getResult();
...
}
StraaS Streaming SDK has a foreground screencast service, which coordinates all the overlay's interactions. Your app must implement the ScreencastSession
interface to supply Notification needed to initialize the foreground service and receive Context and StreamManager from callback later.
ScreencastSession
is lazily initialized when the onSessionCreate is called and then ready to manage overlay UI. And the valid StreamManager inside ScreencastSession is ready when the onStreamManagerInitComplete callback is invoked and it's used to perform various streaming operations.
The figure below shows the interaction between your streaming app and screencast streaming:
And the code below shows the override functions for ScreencastSession interfaces:
class MyScreencastSession implements ScreencastSession {
@Override
public void onSessionCreate(Context context, SessionListener listener, Bundle bundle) {
// context for this session and bundle for screencast stream config
}
@Override
public void onStreamManagerInitComplete(@NonNull Task<StreamManager> task) {
if (!task.isSuccessful()) {
...
}
// Valid StreamManager from onStreamManagerInitComplete callback
mStreamManager = task.getResult();
showOverlay();
prepare();
}
@Override
public NotificationChannel getNotificationChannel() {
if (!Utils.isAndroidOreoOrAbove()) {
return null;
}
NotificationChannel channel = new NotificationChannel(
mContext.getString(R.string.screencast_notification_channel_id),
mContext.getString(R.string.screencast_notification_channel_name),
IMPORTANCE_LOW);
channel.setDescription(mContext.getString(R.string.screencast_notification_channel_description));
return channel;
}
@Override
public Notification getNotification() {
String title = mContext.getString(R.string.screencast_service_title);
String subtitle = mContext.getString(R.string.screencast_service_subtitle);
Notification.Builder builder = new Notification.Builder(mContext)
.setSmallIcon(R.drawable.straas_icon_white_24px)
.setContentTitle(title)
.setContentText(subtitle)
.setAutoCancel(true);
if (Utils.isAndroidOreoOrAbove()) {
builder.setChannelId(mContext.getString(R.string.screencast_notification_channel_id));
}
return builder.build();
}
}
You MUST declare the fully qualified name of the implemented ScreencastSession as a metadata field in AndroidManifest.xml
file of your streaming app.
<application>
...
<meta-data
android:name="io.straas.android.sdk.streaming.SCREENCAST_SESSION_CLASS_NAME"
android:value="io.straas.android.sdk.streaming.demo.screencast.MyScreencastSession" />
</application>
- After getting a StreamManager, you can use prepare(...) to put
ScreencastStreamConfig
configuration in StreamManager and initialize. - The parameters of ScreencastStreamConfig can be derived from the bundle extras in
onSessionCreate
callback.
ScreencastStreamConfig config = new ScreencastStreamConfig.Builder()
.mediaProjection(mMediaProjection)
.videoResolution(size.getWidth(), size.getHeight())
.densityDpi(displayMetrics.densityDpi)
.build();
mStreamManager.prepare(config).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void void) {
// Prepare succeeds, ready to use mStreamManager
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//Handle error
}
});
- See Start streaming
ControlOverlayLayout
and CameraOverlayLayout
provides a sample overlay UI for screencast session to interact with user and display camera preview. You can customize your overlay view inside ScreencastSession
with the addView and removeView by WindowManager.
private void showOverlay() {
...
mWindowManager.addView(mControlOverlayLayout, mControlOverlayLayout.getParams());
mWindowManager.addView(mCameraOverlayLayout, mCameraOverlayLayout.getParams());
}
private void removeOverlay() {
if (mControlOverlayLayout != null) {
mWindowManager.removeView(mControlOverlayLayout);
}
if (mCameraOverlayLayout != null) {
mWindowManager.removeView(mCameraOverlayLayout);
}
...
}