-
Notifications
You must be signed in to change notification settings - Fork 170
VIDCS-2050: Screen Sharing Sample App #553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
goncalocostamendes
wants to merge
10
commits into
main
Choose a base branch
from
VIDCS-1598
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
93b5f5d
Add Screen Sharing sample
goncalocostamendes 2f6d7c9
Screen sharing working for api level 35
goncalocostamendes 045f1f9
Merge remote-tracking branch 'origin/main' into HEAD
goncalocostamendes c189ec4
Fix consitencies
goncalocostamendes 617f608
Rename projects and folders
goncalocostamendes baf5c5d
Fix consitencies
goncalocostamendes 61dd7ff
Fix consitencies
goncalocostamendes 7e2d32f
Fix building error
goncalocostamendes 74e447c
Update Device-Screen-Sharing-Java/app/src/main/java/com/tokbox/sample…
goncalocostamendes 98a3bbb
Optimise screen sharing capturer and suppot api level below 34
goncalocostamendes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| name: Build Webview-Screen-Sharing-Kotlin | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main] # Just in case main was not up to date while merging PR | ||
| pull_request: | ||
| types: [opened, synchronize] | ||
|
|
||
| jobs: | ||
| run: | ||
| continue-on-error: true | ||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| fail-fast: false | ||
| steps: | ||
| - name: checkout | ||
| uses: actions/checkout@v2 | ||
|
|
||
| - name: Set up JDK | ||
| uses: actions/setup-java@v1 | ||
| with: | ||
| java-version: 17 | ||
|
|
||
| - name: Build | ||
| run: cd Webview-Screen-Sharing-Kotlin && ./gradlew app:assembleRelease && cd .. |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| # Screen Sharing | ||
|
|
||
| This app demonstrates how to use the Media Projection API as the source for screen-sharing video. | ||
|
|
||
| > Check [Basic-Video-Capturer-Camera-2](../Basic-Video-Capturer-Camera-2) project to see how a device camera can be used as the video source for the custom `Capturer`. | ||
|
|
||
| ## Screen sharing | ||
|
|
||
| The custom video capturer uses the Media Projection API to capture the device's screen and publish it as a video stream. | ||
|
|
||
| When the app starts up, the `onCreate` method initializes the Media Projection API by requesting permission to capture the screen: | ||
|
|
||
| ```java | ||
| mediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE); | ||
| startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), REQUEST_CODE); | ||
| ``` | ||
|
|
||
| Upon connecting to the OpenTok session, the app instantiates a `Publisher` object and calls its `setCapturer` method to use a custom video capturer, defined by the `ScreenSharingCapturer` class: | ||
|
|
||
| ```java | ||
| @Override | ||
| public void onConnected(Session session) { | ||
| ScreenSharingCapturer screenSharingCapturer = new ScreenSharingCapturer(MainActivity.this, mediaProjection); | ||
|
|
||
| publisher = new Publisher.Builder(MainActivity.this) | ||
| .capturer(screenSharingCapturer) | ||
| .build(); | ||
|
|
||
| publisher.setPublisherListener(publisherListener); | ||
| publisher.setPublisherVideoType(PublisherKit.PublisherKitVideoType.PublisherKitVideoTypeScreen); | ||
| publisher.setAudioFallbackEnabled(false); | ||
|
|
||
| publisher.setStyle(BaseVideoRenderer.STYLE_VIDEO_SCALE, BaseVideoRenderer.STYLE_VIDEO_FILL); | ||
| publisherViewContainer.addView(publisher.getView()); | ||
|
|
||
| session.publish(publisher); | ||
| } | ||
| ``` | ||
|
|
||
| > Note: The call to the `setPublisherVideoType` method sets the video type of the published stream to `PublisherKitVideoType.PublisherKitVideoTypeScreen`. This optimizes the video encoding for screen sharing. It is recommended to use a low frame rate (15 frames per second or lower) with this video type. When using the screen video type in a session that uses the [OpenTok Media Server](https://tokbox.com/opentok/tutorials/create-session/#media-mode), the audio-only fallback feature is disabled, so that the video does not drop out in subscribers. | ||
|
|
||
| The `ScreenSharingCapturer` class uses the Media Projection API to capture the screen. The `getCaptureSettings` method initializes capture settings to be used by the custom video capturer: | ||
|
|
||
| ```java | ||
| @Override | ||
| public CaptureSettings getCaptureSettings() { | ||
|
|
||
| CaptureSettings captureSettings = new CaptureSettings(); | ||
| captureSettings.fps = fps; | ||
| captureSettings.width = width; | ||
| captureSettings.height = height; | ||
| captureSettings.format = ARGB; | ||
| return captureSettings; | ||
| } | ||
| ``` | ||
|
|
||
| The `startCapture` method starts the screen capture process: | ||
|
|
||
| ```java | ||
| @Override | ||
| public int startCapture() { | ||
| capturing = true; | ||
|
|
||
| virtualDisplay = mediaProjection.createVirtualDisplay( | ||
| "ScreenSharing", | ||
| width, | ||
| height, | ||
| density, | ||
| DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC, | ||
| surface, | ||
| null, | ||
| null | ||
| ); | ||
|
|
||
| handler.postDelayed(newFrame, 1000 / fps); | ||
| return 0; | ||
| } | ||
| ``` | ||
|
|
||
| The `backgroundHandler` thread captures frames from the virtual display, processes them, and sends them to the publisher: | ||
|
|
||
| ```java | ||
| imageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() { | ||
| @Override | ||
| public void onImageAvailable(ImageReader reader) { | ||
| Image image = reader.acquireLatestImage(); | ||
| if (image != null) { | ||
| Image.Plane[] planes = image.getPlanes(); | ||
| ByteBuffer buffer = planes[0].getBuffer(); | ||
| int pixelStride = planes[0].getPixelStride(); | ||
| int rowStride = planes[0].getRowStride(); | ||
|
|
||
| if (frame == null) { | ||
| frame = new int[width * height]; | ||
| } | ||
|
|
||
| for (int y = 0; y < height; y++) { | ||
| for (int x = 0; x < width; x++) { | ||
| int index = y * rowStride + x * pixelStride; | ||
| int pixel = buffer.getInt(index); | ||
| frame[y * width + x] = pixel; | ||
| } | ||
| } | ||
|
|
||
| provideIntArrayFrame(frame, ABGR, width, height, 0, false); | ||
| image.close(); | ||
| } | ||
| } | ||
| }, backgroundHandler); | ||
| ``` | ||
|
|
||
| The `provideIntArrayFrame` method, defined by the `BaseVideoCapturer` class, sends an integer array of data to the publisher, to be used for the next video frame published. | ||
|
|
||
| If the publisher is still capturing video, the thread starts again after another 1/15 of a second, so that the capturer continues to supply the publisher with new video frames to publish. | ||
|
|
||
| ## Further Reading | ||
|
|
||
| * Review [other sample projects](../) | ||
| * Read more about [OpenTok Android SDK](https://tokbox.com/developer/sdks/android/) | ||
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| plugins { | ||
| id 'com.android.application' | ||
| } | ||
|
|
||
| apply { | ||
| from '../../commons.gradle' | ||
| } | ||
|
|
||
| android { | ||
| namespace "com.tokbox.sample.devicescreensharing" | ||
| compileSdkVersion extCompileSdkVersion | ||
|
|
||
| defaultConfig { | ||
| applicationId "com.tokbox.sample.devicescreensharing" | ||
| minSdkVersion extMinSdkVersion | ||
| targetSdkVersion extTargetSdkVersion | ||
| versionCode extVersionCode | ||
| versionName extVersionName | ||
| } | ||
|
|
||
| buildTypes { | ||
| release { | ||
| minifyEnabled extMinifyEnabled | ||
| proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
| } | ||
| } | ||
|
|
||
| compileOptions { | ||
| sourceCompatibility JavaVersion.VERSION_1_8 | ||
| targetCompatibility JavaVersion.VERSION_1_8 | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| // Dependency versions are defined in the ../../commons.gradle file | ||
| implementation "com.opentok.android:opentok-android-sdk:${extOpentokSdkVersion}" | ||
| implementation "androidx.appcompat:appcompat:${extAppCompatVersion}" | ||
| implementation "pub.devrel:easypermissions:${extEasyPermissionsVersion}" | ||
| implementation "androidx.constraintlayout:constraintlayout:${extConstraintLyoutVersion}" | ||
| } |
29 changes: 29 additions & 0 deletions
29
Device-Screen-Sharing-Java/app/src/main/AndroidManifest.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
| package="com.tokbox.sample.devicescreensharing"> | ||
|
|
||
| <uses-permission android:name="android.permission.INTERNET" /> | ||
| <uses-permission android:name="android.permission.CAMERA" /> | ||
| <uses-permission android:name="android.permission.RECORD_AUDIO" /> | ||
| <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/> | ||
| <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" /> | ||
|
|
||
| <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" | ||
| android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> | ||
| <activity | ||
| android:name=".MainActivity" | ||
| android:screenOrientation="portrait" | ||
| android:exported="true"> | ||
| <intent-filter> | ||
| <action android:name="android.intent.action.MAIN" /> | ||
|
|
||
| <category android:name="android.intent.category.LAUNCHER" /> | ||
| </intent-filter> | ||
| </activity> | ||
| <service | ||
| android:name=".ScreenSharingService" | ||
| android:foregroundServiceType="mediaProjection" | ||
| android:exported="false" /> | ||
| </application> | ||
|
|
||
| </manifest> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.