{
19 | createNotificationChannel(context)
20 | val notification = createNotification(context)
21 | val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
22 | notificationManager.notify(NOTIFICATION_ID, notification)
23 | return Pair(NOTIFICATION_ID, notification)
24 | }
25 |
26 | @TargetApi(Build.VERSION_CODES.O)
27 | private fun createNotificationChannel(context: Context) {
28 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
29 | val channel = NotificationChannel(
30 | NOTIFICATION_CHANNEL_ID,
31 | NOTIFICATION_CHANNEL_NAME,
32 | NotificationManager.IMPORTANCE_LOW
33 | )
34 | channel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
35 | val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
36 | manager.createNotificationChannel(channel)
37 | }
38 | }
39 |
40 | private fun createNotification(context: Context): Notification {
41 | val builder = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
42 | builder.setContentTitle("屏幕录制")
43 | builder.setContentText("屏幕录制中")
44 | builder.setSmallIcon(R.drawable.screen_notification_icon)
45 | builder.setOngoing(true)
46 | builder.setCategory(Notification.CATEGORY_SERVICE)
47 | builder.priority = Notification.PRIORITY_LOW
48 | builder.setShowWhen(true)
49 | return builder.build()
50 | }
51 | }
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 
2 |
3 |
4 | 🌴一行代码实现安卓屏幕采集编码
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | ## 特点
17 |
18 | - 适配安卓高版本
19 | - 使用 MediaCodec 异步硬编码
20 | - 使用 ImageReader 获取屏幕截图数据
21 | - 支持 捕获应用内声音
22 | - 全局内容旋转监听
23 | - 编码信息可配置
24 | - 通知栏显示
25 | - 链式调用
26 |
27 | ## 安装
28 |
29 | 在项目根目录的 build.gradle 添加仓库
30 |
31 | ```groovy
32 | allprojects {
33 | repositories {
34 | // ...
35 | maven { url 'https://jitpack.io' }
36 | }
37 | }
38 | ```
39 |
40 | 在 module 的 build.gradle 添加依赖
41 |
42 | ```
43 | dependencies {
44 | implementation 'com.github.LxzBUG:ScreenShare:1.1.6'
45 | }
46 | ```
47 |
48 | ## 使用
49 |
50 | ```kotlin
51 | //获取H264数据
52 | ScreenShareKit.init(this).config(screenDataType = EncodeBuilder.SCREEN_DATA_TYPE.H264).onH264(object :H264CallBack{
53 | override fun onH264(
54 | buffer: ByteBuffer,
55 | isKeyFrame: Boolean,
56 | width: Int,
57 | height: Int,
58 | ts: Long
59 | ) {
60 | //编码后的的数据
61 | }
62 | }).onStart({
63 | //用户同意采集,开始采集数据
64 | }).start()
65 |
66 | //获取RGBA数据
67 | ScreenShareKit.init(this).config(screenDataType = EncodeBuilder.SCREEN_DATA_TYPE.RGBA).onRGBA(object :RGBACallBack{
68 | override fun onRGBA(
69 | rgba: ByteArray,
70 | width: Int,
71 | height: Int,
72 | stride: Int,
73 | rotation: Int,
74 | rotationChanged: Boolean
75 | ) {
76 | //屏幕截图数据
77 | }
78 |
79 | }).onStart({
80 | //用户同意采集,开始采集数据
81 | }).start()
82 |
83 | //开启音频捕获
84 | ScreenShareKit.init(this).config(screenDataType = EncodeBuilder.SCREEN_DATA_TYPE.RGBA,audioCapture = true).onRGBA(object :RGBACallBack{
85 | override fun onRGBA(
86 | rgba: ByteArray,
87 | width: Int,
88 | height: Int,
89 | stride: Int,
90 | rotation: Int,
91 | rotationChanged: Boolean
92 | ) {
93 | //屏幕截图数据
94 | }
95 |
96 | }).onAudio(object :AudioCallBack{
97 | override fun onAudio(buffer: ByteArray?, ts: Long) {
98 | //音频数据
99 | }
100 |
101 | }).onStart({
102 | //用户同意采集,开始采集数据
103 | }).start()
104 |
105 | //静音
106 | ScreenShareKit.setMicrophoneMute(true)
107 |
108 | //停止采集
109 | ScreenShareKit.stop()
110 | ```
111 |
112 |
113 |
114 | ## License
115 |
116 | ```
117 | Licensed under the Apache License, Version 2.0 (the "License");
118 | you may not use this file except in compliance with the License.
119 | You may obtain a copy of the License at
120 |
121 | http://www.apache.org/licenses/LICENSE-2.0
122 |
123 | Unless required by applicable law or agreed to in writing, software
124 | distributed under the License is distributed on an "AS IS" BASIS,
125 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
126 | See the License for the specific language governing permissions and
127 | limitations under the License.
128 | ```
--------------------------------------------------------------------------------
/screenShareKit/src/main/java/org/loka/screensharekit/OrientationEventHandler.java:
--------------------------------------------------------------------------------
1 | package org.loka.screensharekit;
2 |
3 | import android.content.Context;
4 | import android.os.IBinder;
5 | import android.os.RemoteException;
6 | import android.util.Log;
7 | import android.view.IRotationWatcher;
8 | import android.view.OrientationEventListener;
9 |
10 | import java.lang.reflect.Method;
11 |
12 | public class OrientationEventHandler extends OrientationEventListener {
13 | private static final String TAG = "watchRotation";
14 | private static final int ORIENTATION_HYSTERESIS = 5;
15 | private int mLastOrientation = 0;
16 | private Context mContext;
17 |
18 | public OrientationEventHandler(Context context) {
19 | super(context);
20 | this.mContext = context;
21 |
22 | }
23 |
24 | @Override
25 | public void onOrientationChanged(int orientation) {
26 | if (orientation == ORIENTATION_UNKNOWN) return;
27 | orientation = roundOrientation(orientation, 0);
28 | if (orientation == mLastOrientation) {
29 | return;
30 | }
31 | mLastOrientation = orientation;
32 | //do sth
33 |
34 | Log.d("rrrrrrr1111",orientation+"===");
35 | }
36 |
37 | private static int roundOrientation(int orientation, int orientationHistory) {
38 | boolean changeOrientation = false;
39 | if (orientationHistory == OrientationEventListener.ORIENTATION_UNKNOWN) {
40 | changeOrientation = true;
41 | } else {
42 | int dist = Math.abs(orientation - orientationHistory);
43 | dist = Math.min(dist, 360 - dist);
44 | changeOrientation = (dist >= 45 + ORIENTATION_HYSTERESIS);
45 | }
46 | if (changeOrientation) {
47 | return ((orientation + 45) / 90 * 90) % 360;
48 | }
49 | return orientationHistory;
50 | }
51 |
52 | private IRotationWatcher iRotationWatcher = new IRotationWatcher.Stub() {
53 |
54 | @Override
55 | public void onRotationChanged(int rotation) throws RemoteException {
56 | //do sth
57 |
58 | Log.d("rrrrrrr",rotation+"===");
59 | }
60 | };
61 |
62 | public void watchRotationReflect() {
63 | try {
64 | Method getServiceMethod = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", new Class[]{String.class});
65 | Object ServiceManager = getServiceMethod.invoke(null, new Object[]{"window"});
66 | Class> cStub = Class.forName("android.view.IWindowManager$Stub");
67 | Method asInterface = cStub.getMethod("asInterface", IBinder.class);
68 | Object iWindowManager = asInterface.invoke(null, ServiceManager);
69 | Method watchRotation = iWindowManager.getClass().getMethod("watchRotation", IRotationWatcher.class);
70 | watchRotation.invoke(iWindowManager, iRotationWatcher);
71 | } catch (Exception e) {
72 | Log.d(TAG, "watchRotationReflect " + e.getMessage());
73 | e.printStackTrace();
74 | }
75 | }
76 |
77 | public void removeRotationWatcherReflect() {
78 | try {
79 | Method getServiceMethod = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", new Class[]{String.class});
80 | Object ServiceManager = getServiceMethod.invoke(null, new Object[]{"window"});
81 | Class> cStub = Class.forName("android.view.IWindowManager$Stub");
82 | Method asInterface = cStub.getMethod("asInterface", IBinder.class);
83 | Object iWindowManager = asInterface.invoke(null, ServiceManager);
84 | Method removeRotationWatcher = iWindowManager.getClass().getMethod("removeRotationWatcher", IRotationWatcher.class);
85 | removeRotationWatcher.invoke(iWindowManager, iRotationWatcher);
86 | } catch (Exception e) {
87 | Log.d(TAG, "removeRotationWatcherReflect " + e.getMessage());
88 | e.printStackTrace();
89 | }
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/screenShareKit/src/main/java/org/loka/screensharekit/OrientationListener.kt:
--------------------------------------------------------------------------------
1 | package org.loka.screensharekit
2 |
3 | import android.content.Context
4 | import android.content.res.Configuration
5 | import android.view.OrientationEventListener
6 | import android.view.Surface
7 | import android.view.WindowManager
8 | import java.util.concurrent.locks.ReentrantLock
9 |
10 |
11 | abstract class OrientationListener : OrientationEventListener {
12 | @Volatile
13 | private var defaultScreenOrientation = CONFIGURATION_ORIENTATION_UNDEFINED
14 | var prevOrientation = ORIENTATION_UNKNOWN
15 | private var ctx: Context
16 | private val lock: ReentrantLock = ReentrantLock(true)
17 |
18 | constructor(context: Context) : super(context) {
19 | ctx = context
20 | }
21 |
22 | constructor(context: Context, rate: Int) : super(context, rate) {
23 | ctx = context
24 | }
25 |
26 | override fun onOrientationChanged(orientation: Int) {
27 | var currentOrientation = ORIENTATION_UNKNOWN
28 | if (orientation >= 330 || orientation < 30) {
29 | currentOrientation = Surface.ROTATION_0
30 | } else if (orientation >= 60 && orientation < 120) {
31 | currentOrientation = Surface.ROTATION_90
32 | } else if (orientation >= 150 && orientation < 210) {
33 | currentOrientation = Surface.ROTATION_180
34 | } else if (orientation >= 240 && orientation < 300) {
35 | currentOrientation = Surface.ROTATION_270
36 | }
37 | if (prevOrientation != currentOrientation && orientation != ORIENTATION_UNKNOWN) {
38 | prevOrientation = currentOrientation
39 | if (currentOrientation != ORIENTATION_UNKNOWN) reportOrientationChanged(
40 | currentOrientation
41 | )
42 | }
43 | }
44 |
45 | private fun reportOrientationChanged(currentOrientation: Int) {
46 | val defaultOrientation = deviceDefaultOrientation
47 | val orthogonalOrientation: Int =
48 | if (defaultOrientation == Configuration.ORIENTATION_LANDSCAPE) Configuration.ORIENTATION_PORTRAIT else Configuration.ORIENTATION_LANDSCAPE
49 | val toReportOrientation: Int
50 | toReportOrientation =
51 | if (currentOrientation == Surface.ROTATION_0 || currentOrientation == Surface.ROTATION_180) defaultOrientation else orthogonalOrientation
52 | onSimpleOrientationChanged(toReportOrientation)
53 | }
54 |
55 | /**
56 | * Must determine what is default device orientation (some tablets can have default landscape). Must be initialized when device orientation is defined.
57 | *
58 | * @return value of [Configuration.ORIENTATION_LANDSCAPE] or [Configuration.ORIENTATION_PORTRAIT]
59 | */
60 | private val deviceDefaultOrientation: Int
61 | private get() {
62 | if (defaultScreenOrientation == CONFIGURATION_ORIENTATION_UNDEFINED) {
63 | lock.lock()
64 | defaultScreenOrientation = initDeviceDefaultOrientation(ctx)
65 | lock.unlock()
66 | }
67 | return defaultScreenOrientation
68 | }
69 |
70 | /**
71 | * Provides device default orientation
72 | *
73 | * @return value of [Configuration.ORIENTATION_LANDSCAPE] or [Configuration.ORIENTATION_PORTRAIT]
74 | */
75 | private fun initDeviceDefaultOrientation(context: Context): Int {
76 | val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
77 |
78 | val config: Configuration = context.getResources().getConfiguration()
79 | val rotation = windowManager.defaultDisplay.rotation
80 | val isLand = config.orientation === Configuration.ORIENTATION_LANDSCAPE
81 | val isDefaultAxis = rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180
82 | var result = CONFIGURATION_ORIENTATION_UNDEFINED
83 | result = if (isDefaultAxis && isLand || !isDefaultAxis && !isLand) {
84 | Configuration.ORIENTATION_LANDSCAPE
85 | } else {
86 | Configuration.ORIENTATION_PORTRAIT
87 | }
88 | return result
89 | }
90 |
91 | /**
92 | * Fires when orientation changes from landscape to portrait and vice versa.
93 | *
94 | * @param orientation value of [Configuration.ORIENTATION_LANDSCAPE] or [Configuration.ORIENTATION_PORTRAIT]
95 | */
96 | abstract fun onSimpleOrientationChanged(orientation: Int)
97 |
98 | companion object {
99 | val CONFIGURATION_ORIENTATION_UNDEFINED: Int = Configuration.ORIENTATION_UNDEFINED
100 | }
101 | }
--------------------------------------------------------------------------------
/screenShareKit/src/main/java/org/loka/screensharekit/Device.kt:
--------------------------------------------------------------------------------
1 | package org.loka.screensharekit
2 |
3 | import android.graphics.Point
4 | import android.os.Build
5 | import android.os.RemoteException
6 | import org.loka.screensharekit.ServiceManager
7 | import org.loka.screensharekit.ScreenInfo
8 | import android.view.IRotationWatcher
9 | import org.loka.screensharekit.DisplayInfo
10 | import java.lang.AssertionError
11 |
12 | class Device {
13 | private val serviceManager = ServiceManager()
14 |
15 | @get:Synchronized
16 | var screenInfo: ScreenInfo
17 | private var rotationListener: RotationListener? = null
18 |
19 | init {
20 | screenInfo = computeScreenInfo(1920)
21 | registerRotationWatcher(object : IRotationWatcher.Stub() {
22 | @Throws(RemoteException::class)
23 | override fun onRotationChanged(rotation: Int) {
24 | synchronized(this@Device) {
25 | screenInfo = screenInfo.withRotation(rotation)
26 | // notify
27 | if (rotationListener != null) {
28 | rotationListener!!.onRotationChanged(rotation)
29 | }
30 | }
31 | }
32 | })
33 | }
34 |
35 | private fun computeScreenInfo(maxSize: Int): ScreenInfo {
36 | // Compute the video size and the padding of the content inside this video.
37 | // Principle:
38 | // - scale down the great side of the screen to maxSize (if necessary);
39 | // - scale down the other side so that the aspect ratio is preserved;
40 | // - round this value to the nearest multiple of 8 (H.264 only accepts multiples of 8)
41 | val displayInfo = serviceManager.displayManager!!.displayInfo
42 | val rotated = displayInfo.rotation and 1 != 0
43 | val deviceSize = displayInfo.size
44 | var w = deviceSize.width and 7.inv() // in case it's not a multiple of 8
45 | var h = deviceSize.height and 7.inv()
46 | if (maxSize > 0) {
47 | if (BuildConfig.DEBUG && maxSize % 8 != 0) {
48 | throw AssertionError("Max size must be a multiple of 8")
49 | }
50 | val portrait = h > w
51 | var major = if (portrait) h else w
52 | var minor = if (portrait) w else h
53 | if (major > maxSize) {
54 | val minorExact = minor * maxSize / major
55 | // +4 to round the value to the nearest multiple of 8
56 | minor = minorExact + 4 and 7.inv()
57 | major = maxSize
58 | }
59 | w = if (portrait) minor else major
60 | h = if (portrait) major else minor
61 | }
62 | val videoSize = Size(w, h)
63 | return ScreenInfo(deviceSize, videoSize, rotated)
64 | }
65 |
66 | fun getPhysicalPoint(position: Position): Point? {
67 | val screenInfo// it hides the field on purpose, to read it with a lock
68 | = screenInfo // read with synchronization
69 | val videoSize = screenInfo.videoSize
70 | val clientVideoSize = position.screenSize
71 | if (!videoSize.equals(clientVideoSize)) {
72 | // The client sends a click relative to a video with wrong dimensions,
73 | // the device may have been rotated since the event was generated, so ignore the event
74 | return null
75 | }
76 | val deviceSize = screenInfo.deviceSize
77 | val point = position.point
78 | val scaledX = point.x * deviceSize.width / videoSize.width
79 | val scaledY = point.y * deviceSize.height / videoSize.height
80 | return Point(scaledX, scaledY)
81 | }
82 |
83 | fun registerRotationWatcher(rotationWatcher: IRotationWatcher?) {
84 | serviceManager.windowManager!!.registerRotationWatcher(rotationWatcher)
85 | }
86 |
87 | @Synchronized
88 | fun setRotationListener(rotationListener: RotationListener?) {
89 | this.rotationListener = rotationListener
90 | }
91 |
92 | fun NewgetPhysicalPoint(point: Point): Point {
93 | val screenInfo// it hides the field on purpose, to read it with a lock
94 | = screenInfo // read with synchronization
95 | val videoSize = screenInfo.videoSize
96 | // Size clientVideoSize = position.getScreenSize();
97 | val deviceSize = screenInfo.deviceSize
98 | // Point point = position.getPoint();
99 | val scaledX = point.x * deviceSize.width / videoSize.width
100 | val scaledY = point.y * deviceSize.height / videoSize.height
101 | return Point(scaledX, scaledY)
102 | }
103 |
104 | interface RotationListener {
105 | fun onRotationChanged(rotation: Int)
106 | }
107 |
108 | companion object {
109 | val deviceName: String
110 | get() = Build.MODEL
111 | }
112 | }
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/screenShareKit/src/main/java/org/loka/screensharekit/EncodeBuilder.kt:
--------------------------------------------------------------------------------
1 | package org.loka.screensharekit
2 |
3 | import android.os.Build
4 | import androidx.fragment.app.Fragment
5 | import androidx.fragment.app.FragmentActivity
6 | import androidx.fragment.app.FragmentManager
7 | import org.loka.screensharekit.callback.*
8 |
9 | class EncodeBuilder(fragment: Fragment?,fragmentActivity: FragmentActivity?):Device.RotationListener{
10 |
11 | private lateinit var activity: FragmentActivity
12 | private var fragment: Fragment? = null
13 |
14 | @JvmField
15 | var h264CallBack : H264CallBack? = null
16 | var errorCallBack : ErrorCallBack? = null
17 | var rgbaCallback:RGBACallBack?=null
18 | var audioCallBack: AudioCallBack?=null
19 | var startCallback:StartCaptureCallback?=null
20 | internal val encodeConfig = EncodeConfig()
21 | private val device by lazy { Device() }
22 | var device_rotation = 0
23 | var screenDataType = SCREEN_DATA_TYPE.H264
24 |
25 | public enum class SCREEN_DATA_TYPE{
26 | H264,RGBA
27 | }
28 |
29 | init {
30 | fragmentActivity?.let {
31 | activity = it
32 | }?: run {
33 | fragment?.let {
34 | activity = it.requireActivity()
35 | }
36 | }
37 | this.fragment = fragment
38 | }
39 |
40 |
41 | private val fragmentManager : FragmentManager
42 | get() {
43 | return fragment?.childFragmentManager ?: activity.supportFragmentManager
44 | }
45 |
46 |
47 | private val invisibleFragment : InvisibleFragment
48 | get() {
49 | val existedFragment = fragmentManager.findFragmentByTag(FRAGMENT_TAG)
50 | return if (existedFragment != null) {
51 | existedFragment as InvisibleFragment
52 | } else {
53 | val invisibleFragment = InvisibleFragment()
54 | fragmentManager.beginTransaction()
55 | .add(invisibleFragment, FRAGMENT_TAG)
56 | .commitNowAllowingStateLoss()
57 | invisibleFragment
58 | }
59 | }
60 |
61 | companion object {
62 | private const val FRAGMENT_TAG = "InvisibleFragment"
63 | }
64 |
65 |
66 |
67 |
68 | fun onH264(callBack: H264CallBack?):EncodeBuilder{
69 | return apply {
70 | h264CallBack = callBack
71 | }
72 | }
73 |
74 | fun onAudio(callback:AudioCallBack?):EncodeBuilder{
75 | return apply {
76 | audioCallBack = callback
77 | }
78 | }
79 |
80 | fun onStart(callBack:StartCaptureCallback?):EncodeBuilder{
81 | return apply {
82 | startCallback = callBack
83 | }
84 | }
85 |
86 | fun onRGBA(callBack:RGBACallBack):EncodeBuilder{
87 | return apply {
88 | rgbaCallback = callBack
89 | }
90 | }
91 |
92 | fun onError(callBack: ErrorCallBack?):EncodeBuilder{
93 | return apply {
94 | errorCallBack = callBack
95 | }
96 | }
97 |
98 |
99 | fun start(){
100 | if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
101 | invisibleFragment.requestMediaProjection(this)
102 | device.setRotationListener(this)
103 | }else{
104 | errorCallBack?.onError(ErrorInfo(-3,"当前系统版本不支持"))
105 | }
106 |
107 | }
108 |
109 | fun stop(){
110 | device.setRotationListener(null)
111 | activity.startService(ScreenReaderService.getStopIntent(activity))
112 | }
113 |
114 | fun setMicrophoneMute(mute:Boolean){
115 | activity.startService(ScreenReaderService.getMuteMicIntent(activity,mute))
116 | }
117 |
118 | private fun screenRotation(isLandscape: Boolean){
119 | if (isLandscape){
120 | if (encodeConfig.width0){
140 | encodeConfig.width = width
141 | }
142 | if (height>0){
143 | encodeConfig.height = height
144 | }
145 | if (frameRate>0){
146 | encodeConfig.frameRate = frameRate
147 | }
148 |
149 | if (bitrate>0){
150 | encodeConfig.bitrate = bitrate
151 | }
152 | this.screenDataType = screenDataType
153 |
154 | if (audioCapture){
155 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
156 | encodeConfig.audioCapture = audioCapture
157 | encodeConfig.sampleRate = sampleRate
158 | encodeConfig.channels = channels
159 | } else {
160 | encodeConfig.audioCapture = false
161 | }
162 | }
163 | return this
164 | }
165 |
166 | override fun onRotationChanged(rotation: Int) {
167 | when(rotation){
168 | 0->{
169 | device_rotation = 0
170 | }
171 | 1->{
172 | device_rotation = 90
173 | }
174 | 2->{
175 | device_rotation = 180
176 | }
177 | 3->{
178 | device_rotation = 270
179 | }
180 | }
181 | if (rotation==3||rotation==1){
182 | screenRotation(true)
183 | }else{
184 | screenRotation(false)
185 | }
186 | }
187 |
188 |
189 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/screenShareKit/src/main/java/org/loka/screensharekit/AudioCapture.kt:
--------------------------------------------------------------------------------
1 | package org.loka.screensharekit
2 |
3 | import android.annotation.TargetApi
4 | import android.media.AudioAttributes
5 | import android.media.AudioFormat
6 | import android.media.AudioPlaybackCaptureConfiguration
7 | import android.media.AudioRecord
8 | import android.media.projection.MediaProjection
9 | import android.os.Process
10 | import android.os.SystemClock
11 | import android.util.Log
12 | import org.loka.screensharekit.ScreenShareKit.packageUid
13 | import java.nio.ByteBuffer
14 |
15 | class AudioCapture(
16 | private val channels: Int,
17 | private val sampleRate: Int,
18 | private val mediaProjection: MediaProjection,
19 | private val errorCallback: AudioRecordErrorCallback?,
20 | private val audioFrameListener: AudioFrameListener
21 | ) : IAudioCapture {
22 | private var byteBuffer: ByteBuffer? = null
23 | private var audioRecord: AudioRecord? = null
24 | private var audioThread: AudioRecordThread? = null
25 |
26 | @Volatile
27 | private var microphoneMute = false
28 | private var emptyBytes: ByteArray?=null
29 | @TargetApi(29)
30 | override fun startRecording(): Boolean {
31 | Log.d("AudioCapture", "startRecording")
32 | return if (initRecording() <= 0) {
33 | false
34 | } else {
35 | try {
36 | audioRecord!!.startRecording()
37 | } catch (var2: IllegalStateException) {
38 | reportWebRtcAudioRecordStartError(
39 | AudioRecordStartErrorCode.AUDIO_RECORD_START_EXCEPTION,
40 | "AudioRecord.startRecording failed: " + var2.message
41 | )
42 | return false
43 | }
44 | if (audioRecord!!.recordingState != 3) {
45 | reportWebRtcAudioRecordStartError(
46 | AudioRecordStartErrorCode.AUDIO_RECORD_START_STATE_MISMATCH,
47 | "AudioRecord.startRecording failed - incorrect state :" + audioRecord!!.recordingState
48 | )
49 | false
50 | } else {
51 | audioThread = AudioRecordThread("AudioRecordJavaThread")
52 | audioThread!!.start()
53 | true
54 | }
55 | }
56 | }
57 |
58 | @TargetApi(29)
59 | fun initRecording(): Int {
60 | Log.d(
61 | "AudioCapture",
62 | "initRecording(sampleRate=" + sampleRate + ", channels=" + channels + ")"
63 | )
64 | return if (audioRecord != null) {
65 | reportWebRtcAudioRecordInitError("InitRecording called twice without StopRecording.")
66 | -1
67 | } else {
68 | val bytesPerFrame = channels * 2
69 | val framesPerBuffer = sampleRate / 100
70 | byteBuffer = ByteBuffer.allocateDirect(bytesPerFrame * framesPerBuffer)
71 | if (byteBuffer?.hasArray() == false) {
72 | reportWebRtcAudioRecordInitError("ByteBuffer does not have backing array.")
73 | -1
74 | } else {
75 | Log.d("AudioCapture", "byteBuffer.capacity: " + byteBuffer?.capacity())
76 | emptyBytes = ByteArray(byteBuffer?.capacity()?:0)
77 | val channelConfig = channelCountToConfiguration(channels)
78 | val minBufferSize = AudioRecord.getMinBufferSize(
79 | sampleRate,
80 | channelConfig,
81 | AudioFormat.ENCODING_PCM_16BIT
82 | )
83 | if (minBufferSize != -1 && minBufferSize != -2) {
84 | Log.d("AudioCapture", "AudioRecord.getMinBufferSize: $minBufferSize")
85 | val bufferSizeInBytes =
86 | Math.max(2 * minBufferSize, byteBuffer?.capacity()?:0)
87 | Log.d("AudioCapture", "bufferSizeInBytes: $bufferSizeInBytes")
88 | val config =
89 | AudioPlaybackCaptureConfiguration.Builder(mediaProjection)
90 | .addMatchingUsage(AudioAttributes.USAGE_MEDIA)
91 | .excludeUid(packageUid).build()
92 | val audioFormat = AudioFormat.Builder()
93 | .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
94 | .setSampleRate(sampleRate).setChannelMask(channelConfig).build()
95 | audioRecord = AudioRecord.Builder().setAudioFormat(audioFormat)
96 | .setBufferSizeInBytes(bufferSizeInBytes)
97 | .setAudioPlaybackCaptureConfig(config)
98 | .build()
99 | if (audioRecord != null && audioRecord!!.state == 1) {
100 | logMainParameters()
101 | framesPerBuffer
102 | } else {
103 | reportWebRtcAudioRecordInitError("Failed to create a new AudioRecord instance")
104 | releaseAudioResources()
105 | -1
106 | }
107 | } else {
108 | reportWebRtcAudioRecordInitError("AudioRecord.getMinBufferSize failed: $minBufferSize")
109 | -1
110 | }
111 | }
112 | }
113 | }
114 |
115 | override fun stopRecording() {
116 | Log.d("AudioCapture", "stopRecording")
117 | if (audioThread != null) {
118 | audioThread!!.stopThread()
119 | if (!joinUninterruptibly(audioThread, 2000L)) {
120 | Log.e("AudioCapture", "Join of AudioRecordJavaThread timed out")
121 | }
122 | audioThread = null
123 | }
124 | releaseAudioResources()
125 | }
126 |
127 | fun joinUninterruptibly(thread: Thread?, timeoutMs: Long): Boolean {
128 | val startTimeMs = SystemClock.elapsedRealtime()
129 | var timeRemainingMs = timeoutMs
130 | var wasInterrupted = false
131 | while (timeRemainingMs > 0) {
132 | try {
133 | thread!!.join(timeRemainingMs)
134 | break
135 | } catch (e: InterruptedException) {
136 | // Someone is asking us to return early at our convenience. We can't cancel this operation,
137 | // but we should preserve the information and pass it along.
138 | wasInterrupted = true
139 | val elapsedTimeMs = SystemClock.elapsedRealtime() - startTimeMs
140 | timeRemainingMs = timeoutMs - elapsedTimeMs
141 | }
142 | }
143 | // Pass interruption information along.
144 | if (wasInterrupted) {
145 | Thread.currentThread().interrupt()
146 | }
147 | return !thread!!.isAlive
148 | }
149 |
150 | private fun logMainParameters() {
151 | Log.d(
152 | "AudioCapture",
153 | "AudioRecord: session ID: " + audioRecord!!.audioSessionId + ", channels: " + audioRecord!!.channelCount + ", sample rate: " + audioRecord!!.sampleRate
154 | )
155 | }
156 |
157 | private fun channelCountToConfiguration(channels: Int): Int {
158 | return if (channels == 1) 16 else 12
159 | }
160 |
161 | fun setMicrophoneMute(mute: Boolean) {
162 | Log.e("AudioCapture", "setMicrophoneMute($mute)")
163 | microphoneMute = mute
164 | }
165 |
166 | private fun releaseAudioResources() {
167 | Log.d("AudioCapture", "releaseAudioResources")
168 | if (audioRecord != null) {
169 | audioRecord!!.release()
170 | audioRecord = null
171 | }
172 | }
173 |
174 | private fun reportWebRtcAudioRecordInitError(errorMessage: String) {
175 | Log.e("AudioCapture", "Init recording error: $errorMessage")
176 | if (errorCallback != null) {
177 | errorCallback.onWebRtcAudioRecordInitError(errorMessage)
178 | }
179 | }
180 |
181 | private fun reportWebRtcAudioRecordStartError(
182 | errorCode: AudioRecordStartErrorCode,
183 | errorMessage: String
184 | ) {
185 | Log.e("AudioCapture", "Start recording error: $errorCode. $errorMessage")
186 | if (errorCallback != null) {
187 | errorCallback.onWebRtcAudioRecordStartError(errorCode, errorMessage)
188 | }
189 | }
190 |
191 | private fun reportWebRtcAudioRecordError(errorMessage: String) {
192 | Log.e("AudioCapture", "Run-time recording error: $errorMessage")
193 | if (errorCallback != null) {
194 | errorCallback.onWebRtcAudioRecordError(errorMessage)
195 | }
196 | }
197 |
198 | private inner class AudioRecordThread(name: String?) : Thread(name) {
199 | @Volatile
200 | private var keepAlive = true
201 | val threadInfo: String
202 | get() = "@[name=" + currentThread().name + ", id=" + currentThread().id + "]"
203 |
204 | override fun run() {
205 | Process.setThreadPriority(-19)
206 | assertTrue(audioRecord!!.recordingState == 3)
207 | Log.d("AudioCapture", "audioRecordState " + audioRecord!!.recordingState)
208 | while (keepAlive) {
209 | val bytesRead = audioRecord!!.read(byteBuffer!!, byteBuffer!!.capacity())
210 | if (bytesRead == byteBuffer!!.capacity()) {
211 | if (microphoneMute) {
212 | byteBuffer?.clear()
213 | byteBuffer?.put(emptyBytes)
214 | }
215 | if (keepAlive) {
216 | val audioData = ByteArray(bytesRead)
217 | byteBuffer?.rewind()
218 | byteBuffer!![audioData]
219 | audioFrameListener.onAudioData(audioData)
220 | }
221 | Log.d("AudioCapture", "Read $bytesRead bytes of audio data")
222 | } else {
223 | val errorMessage = "AudioRecord.read failed: $bytesRead"
224 | Log.e("AudioCapture", errorMessage)
225 | if (bytesRead == -3) {
226 | keepAlive = false
227 | reportWebRtcAudioRecordError(errorMessage)
228 | }
229 | }
230 | }
231 | try {
232 | if (audioRecord != null) {
233 | audioRecord!!.stop()
234 | }
235 | } catch (var3: IllegalStateException) {
236 | Log.e("AudioCapture", "AudioRecord.stop failed: " + var3.message)
237 | }
238 | }
239 |
240 | fun stopThread() {
241 | Log.d("AudioCapture", "stopThread")
242 | keepAlive = false
243 | }
244 | }
245 |
246 | enum class AudioRecordStartErrorCode {
247 | AUDIO_RECORD_START_EXCEPTION, AUDIO_RECORD_START_STATE_MISMATCH
248 | }
249 |
250 | companion object {
251 | private const val TAG = "AudioCapture"
252 | private const val BITS_PER_SAMPLE = 16
253 | private const val CALLBACK_BUFFER_SIZE_MS = 10
254 | private const val BUFFERS_PER_SECOND = 100
255 | private const val BUFFER_SIZE_FACTOR = 2
256 | private const val AUDIO_RECORD_THREAD_JOIN_TIMEOUT_MS = 2000L
257 | private fun assertTrue(condition: Boolean) {
258 | if (!condition) {
259 | throw AssertionError("Expected condition to be true")
260 | }
261 | }
262 | }
263 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/screenShareKit/src/main/java/org/loka/screensharekit/ScreenReaderService.kt:
--------------------------------------------------------------------------------
1 | package org.loka.screensharekit
2 |
3 | import android.app.Activity.RESULT_CANCELED
4 | import android.app.Service
5 | import android.content.Context
6 | import android.content.Intent
7 | import android.graphics.PixelFormat
8 | import android.hardware.display.DisplayManager
9 | import android.hardware.display.VirtualDisplay
10 | import android.media.ImageReader
11 | import android.media.MediaCodec
12 | import android.media.MediaCodecInfo
13 | import android.media.MediaFormat
14 | import android.media.projection.MediaProjection
15 | import android.media.projection.MediaProjectionManager
16 | import android.os.Build
17 | import android.os.Handler
18 | import android.os.HandlerThread
19 | import android.os.IBinder
20 | import android.util.Log
21 | import android.view.Surface
22 | import java.lang.IllegalStateException
23 | import java.nio.ByteBuffer
24 | import java.util.*
25 | import java.util.concurrent.atomic.AtomicBoolean
26 |
27 | class ScreenReaderService : Service() {
28 |
29 | private var mMediaProjection: MediaProjection? = null
30 | private var mHandler: Handler? = null
31 | private var mHandlerThread: HandlerThread? = null
32 | private var mVirtualDisplay: VirtualDisplay? = null
33 | private val mDensity by lazy { resources.displayMetrics.densityDpi }
34 |
35 | private var codec: MediaCodec? = null
36 | private var surface: Surface? = null
37 | private var configData: ByteBuffer? = null
38 |
39 | private val encodeBuilder by lazy { ScreenShareKit.encodeBuilder }
40 | private var isStop = false
41 |
42 | // RGBA相关
43 | private val lock = Object()
44 | private var isImageProcessing = false
45 | private val mQuit: AtomicBoolean = AtomicBoolean(false)
46 | private var mImgReader: ImageReader? = null
47 | private var mLastSendTSMs = 0L
48 | private var lastWidth = 0
49 |
50 | // audio
51 | private var audioCapture: AudioCapture? = null
52 |
53 | override fun onCreate() {
54 | super.onCreate()
55 | mHandlerThread = HandlerThread("ScreenReaderService-HandlerThread").apply { start() }
56 | mHandler = Handler(mHandlerThread!!.looper)
57 | }
58 |
59 | override fun onDestroy() {
60 | super.onDestroy()
61 | mHandlerThread?.quitSafely()
62 | }
63 |
64 | private fun initImageReader(width: Int, height: Int) {
65 | mImgReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 1)
66 | mImgReader?.setOnImageAvailableListener(ImageAvailableListener(), mHandler)
67 | surface = mImgReader?.surface
68 | }
69 |
70 | private fun isRotationChange(): Boolean {
71 | return if (encodeBuilder.encodeConfig.width == lastWidth) {
72 | false
73 | } else {
74 | lastWidth = encodeBuilder.encodeConfig.width
75 | true
76 | }
77 | }
78 |
79 | private fun startCapture(width: Int, height: Int, frame: Int) {
80 | if (encodeBuilder.screenDataType == EncodeBuilder.SCREEN_DATA_TYPE.H264) {
81 | initMediaCodec(width, height, frame)
82 | createVirtualDisplay(width, height, surface)
83 | } else {
84 | Log.d("屏幕采集", "宽${width} 高${height}")
85 | initImageReader(width, height)
86 | createVirtualDisplay(width, height, surface)
87 | }
88 | }
89 |
90 | private fun startAudioCapture() {
91 | audioCapture?.startRecording()
92 | }
93 |
94 | private fun stopAudioCapture() {
95 | audioCapture?.stopRecording()
96 | }
97 |
98 | private fun stopCapture() {
99 | if (encodeBuilder.screenDataType == EncodeBuilder.SCREEN_DATA_TYPE.H264) {
100 | codec?.run {
101 | try {
102 | stop()
103 | } catch (_: Exception) {}
104 | try {
105 | release()
106 | } catch (_: Exception) {}
107 | }
108 | codec = null
109 | mVirtualDisplay?.release()
110 | mVirtualDisplay = null
111 | } else {
112 | mQuit.set(true)
113 | synchronized(lock) {
114 | while (isImageProcessing) {
115 | try {
116 | lock.wait()
117 | } catch (_: InterruptedException) {
118 | }
119 | }
120 | try {
121 | mVirtualDisplay?.release()
122 | } catch (_: Exception) {}
123 | mVirtualDisplay = null
124 | try {
125 | mImgReader?.close()
126 | } catch (_: Exception) {}
127 | mImgReader = null
128 | }
129 | }
130 | }
131 |
132 | private fun createVirtualDisplay(width: Int, height: Int, surface: Surface?) {
133 | mVirtualDisplay = mMediaProjection?.createVirtualDisplay(
134 | SCREENCAP_NAME,
135 | width, height,
136 | mDensity,
137 | DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY or DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC,
138 | surface,
139 | null,
140 | null
141 | )
142 | }
143 |
144 | private fun initMediaCodec(width: Int, height: Int, frame: Int) {
145 | var isCodecRunning = false
146 | val format = MediaFormat.createVideoFormat(MIME, width, height)
147 | format.apply {
148 | setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface)
149 | // 推荐码率算法:每像素比特率 0.15 bpp
150 | val bitrate = (width * height * frame * 0.15).toInt()
151 | setInteger(MediaFormat.KEY_BIT_RATE, bitrate)
152 | setInteger(MediaFormat.KEY_FRAME_RATE, frame)
153 | setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1) // 1秒一个I帧
154 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
155 | setInteger(MediaFormat.KEY_PROFILE, MediaCodecInfo.CodecProfileLevel.AVCProfileHigh)
156 | setInteger(MediaFormat.KEY_LEVEL, MediaCodecInfo.CodecProfileLevel.AVCLevel4)
157 | }
158 | setInteger(MediaFormat.KEY_BITRATE_MODE, MediaCodecInfo.EncoderCapabilities.BITRATE_MODE_VBR)
159 | }
160 | codec = MediaCodec.createEncoderByType(MIME)
161 | codec?.let { c ->
162 | c.setCallback(object : MediaCodec.Callback() {
163 | override fun onInputBufferAvailable(codec: MediaCodec, index: Int) {}
164 | override fun onOutputBufferAvailable(codec: MediaCodec, index: Int, info: MediaCodec.BufferInfo) {
165 | synchronized(codec) {
166 | isCodecRunning = true
167 | val outputBuffer: ByteBuffer? = try {
168 | codec.getOutputBuffer(index)
169 | } catch (_: IllegalStateException) {
170 | null
171 | }
172 | if (outputBuffer == null) return
173 | val keyFrame = (info.flags and MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0
174 | if (keyFrame) {
175 | configData = ByteBuffer.allocate(info.size)
176 | configData?.put(outputBuffer)
177 | } else {
178 | val data = createOutputBufferInfo(info, index, outputBuffer)
179 | try {
180 | encodeBuilder.h264CallBack?.onH264(
181 | data.buffer,
182 | data.isKeyFrame,
183 | encodeBuilder.encodeConfig.width,
184 | encodeBuilder.encodeConfig.height,
185 | data.presentationTimestampUs
186 | )
187 | } catch (e: Exception) {
188 | Log.e("ScreenReaderService", "H264回调异常: "+e.message)
189 | }
190 | }
191 | if (index >= 0 && (info.flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM) == 0 && isCodecRunning && !isStop) {
192 | codec.releaseOutputBuffer(index, false)
193 | }
194 | }
195 | }
196 | override fun onError(codec: MediaCodec, e: MediaCodec.CodecException) {
197 | isCodecRunning = false
198 | try {
199 | encodeBuilder.errorCallBack?.onError(ErrorInfo(-2, "编码器错误"+e.message))
200 | } catch (_: Exception) {}
201 | }
202 | override fun onOutputFormatChanged(codec: MediaCodec, format: MediaFormat) {}
203 | })
204 | c.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE)
205 | surface = c.createInputSurface()
206 | c.start()
207 | }
208 | }
209 |
210 | private fun createOutputBufferInfo(info: MediaCodec.BufferInfo, index: Int, outputBuffer: ByteBuffer): OutputBufferInfo {
211 | outputBuffer.position(info.offset)
212 | outputBuffer.limit(info.offset + info.size)
213 | val keyFrame = (info.flags and MediaCodec.BUFFER_FLAG_KEY_FRAME) != 0
214 | return if (keyFrame) {
215 | val buffer = ByteBuffer.allocateDirect((configData?.capacity() ?: 0) + info.size)
216 | configData?.rewind()
217 | configData?.let { buffer.put(it) }
218 | buffer.put(outputBuffer)
219 | buffer.position(0)
220 | OutputBufferInfo(index, buffer, keyFrame, info.presentationTimeUs, info.size + (configData?.capacity() ?: 0))
221 | } else {
222 | OutputBufferInfo(index, outputBuffer.slice(), keyFrame, info.presentationTimeUs, info.size)
223 | }
224 | }
225 |
226 | override fun onBind(intent: Intent?): IBinder? {
227 | throw UnsupportedOperationException("unable to bind!")
228 | }
229 |
230 | override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
231 | intent?.let {
232 | when {
233 | isStartCommand(it) -> {
234 | isStop = false
235 | val notification = NotificationUtils.getNotification(this)
236 | startForeground(notification.first, notification.second)
237 | startProjection(it.getIntExtra(RESULT_CODE, RESULT_CANCELED), it.getParcelableExtra(DATA)!!)
238 | }
239 | isStopCommand(it) -> {
240 | isStop = true
241 | stopProjection()
242 | stopSelf()
243 | }
244 | isResetCommand(it) -> {
245 | stopCapture()
246 | startCapture(encodeBuilder.encodeConfig.width, encodeBuilder.encodeConfig.height, encodeBuilder.encodeConfig.frameRate)
247 | }
248 | isMuteCommand(it) -> {
249 | val mute = it.getBooleanExtra(MUTE, false)
250 | audioCapture?.setMicrophoneMute(mute)
251 | }else->{}
252 | }
253 | }
254 | return super.onStartCommand(intent, flags, startId)
255 | }
256 |
257 | private fun isStartCommand(intent: Intent): Boolean {
258 | return (intent.hasExtra(RESULT_CODE) && intent.hasExtra(DATA)
259 | && intent.hasExtra(ACTION) && Objects.equals(intent.getStringExtra(ACTION), START))
260 | }
261 |
262 | private fun isStopCommand(intent: Intent): Boolean {
263 | return intent.hasExtra(ACTION) && Objects.equals(intent.getStringExtra(ACTION), STOP)
264 | }
265 |
266 | private fun isResetCommand(intent: Intent): Boolean {
267 | return intent.hasExtra(ACTION) && Objects.equals(intent.getStringExtra(ACTION), RESET)
268 | }
269 | private fun isMuteCommand(intent: Intent): Boolean {
270 | return intent.hasExtra(ACTION) && Objects.equals(intent.getStringExtra(ACTION), MUTE)
271 | }
272 |
273 | private fun startProjection(resultCode: Int, data: Intent) {
274 | val mpManager = getSystemService(MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
275 | if (mMediaProjection == null) {
276 | mMediaProjection = mpManager.getMediaProjection(resultCode, data)
277 | mMediaProjection?.let { mp ->
278 | mp.registerCallback(MediaProjectionStopCallback(), mHandler)
279 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && encodeBuilder.encodeConfig.audioCapture) {
280 | audioCapture = AudioCapture(
281 | encodeBuilder.encodeConfig.channels,
282 | encodeBuilder.encodeConfig.sampleRate,
283 | mp,
284 | object : AudioRecordErrorCallback {
285 | override fun onWebRtcAudioRecordInitError(var1: String?) {
286 | try { encodeBuilder.errorCallBack?.onError(ErrorInfo(-5, var1.toString())) } catch (_: Exception) {}
287 | }
288 | override fun onWebRtcAudioRecordStartError(var1: AudioCapture.AudioRecordStartErrorCode?, var2: String?) {
289 | try { encodeBuilder.errorCallBack?.onError(ErrorInfo(-4, var2.toString())) } catch (_: Exception) {}
290 | }
291 | override fun onWebRtcAudioRecordError(var1: String?) {
292 | try { encodeBuilder.errorCallBack?.onError(ErrorInfo(-3, var1.toString())) } catch (_: Exception) {}
293 | }
294 | },
295 | object : AudioFrameListener {
296 | override fun onAudioData(var1: ByteArray) {
297 | try { encodeBuilder.audioCallBack?.onAudio(var1, System.currentTimeMillis()) } catch (_: Exception) {}
298 | }
299 | }
300 | )
301 | }
302 | startCapture(encodeBuilder.encodeConfig.width, encodeBuilder.encodeConfig.height, encodeBuilder.encodeConfig.frameRate)
303 | startAudioCapture()
304 | }
305 | }
306 | }
307 |
308 | private fun stopProjection() {
309 | mHandler?.post {
310 | try { mMediaProjection?.stop() } catch (_: Exception) {}
311 | }
312 | stopAudioCapture()
313 | }
314 |
315 | private inner class MediaProjectionStopCallback : MediaProjection.Callback() {
316 | override fun onStop() {
317 | mHandler?.post {
318 | stopCapture()
319 | try { mMediaProjection?.unregisterCallback(this@MediaProjectionStopCallback) } catch (_: Exception) {}
320 | }
321 | }
322 | }
323 |
324 | private inner class ImageAvailableListener : ImageReader.OnImageAvailableListener {
325 | override fun onImageAvailable(reader: ImageReader?) {
326 | reader?.let {
327 | synchronized(lock) {
328 | val image = it.acquireLatestImage()
329 | if (image != null) {
330 | try {
331 | val plane = image.planes[0]
332 | val buffer = plane.buffer
333 | val rowStride = plane.rowStride
334 | val pixelStride = plane.pixelStride
335 | val width = encodeBuilder.encodeConfig.width
336 | val height = encodeBuilder.encodeConfig.height
337 | isImageProcessing = true
338 | // 兼容所有Android版本的RGBA拷贝
339 | val rgba = ByteArray(width * height * 4)
340 | val rowBuffer = ByteArray(width * 4)
341 | for (row in 0 until height) {
342 | buffer.position(row * rowStride)
343 | buffer.get(rowBuffer, 0, width * 4)
344 | System.arraycopy(rowBuffer, 0, rgba, row * width * 4, width * 4)
345 | }
346 | try {
347 | encodeBuilder.rgbaCallback?.onRGBA(
348 | rgba,
349 | width,
350 | height,
351 | width, // stride
352 | encodeBuilder.device_rotation,
353 | isRotationChange()
354 | )
355 | mLastSendTSMs = System.currentTimeMillis()
356 | } catch (e: Exception) {
357 | Log.e("ScreenReaderService", "RGBA回调异常: ${e.message}")
358 | }
359 | } catch (e: Exception) {
360 | Log.e("ScreenReaderService", "ImageAvailable异常: ${e.message}")
361 | } finally {
362 | image.close()
363 | isImageProcessing = false
364 | lock.notifyAll()
365 | }
366 | }
367 | }
368 | }
369 | }
370 | }
371 |
372 | internal companion object GetIntent {
373 | private const val MIME = "Video/AVC"
374 | private const val RESULT_CODE = "RESULT_CODE"
375 | private const val DATA = "DATA"
376 | private const val ACTION = "ACTION"
377 | private const val START = "START"
378 | private const val STOP = "STOP"
379 | private const val MUTE = "MUTE"
380 | private const val RESET = "RESET"
381 | private const val SCREENCAP_NAME = "screen_cap"
382 |
383 | fun getStartIntent(context: Context?, resultCode: Int, data: Intent): Intent {
384 | return Intent(context, ScreenReaderService::class.java).apply {
385 | putExtra(ACTION, START)
386 | putExtra(RESULT_CODE, resultCode)
387 | putExtra(DATA, data)
388 | }
389 | }
390 | fun getStopIntent(context: Context?): Intent {
391 | return Intent(context, ScreenReaderService::class.java).apply {
392 | putExtra(ACTION, STOP)
393 | }
394 | }
395 | fun getMuteMicIntent(context: Context?, mute: Boolean): Intent {
396 | return Intent(context, ScreenReaderService::class.java).apply {
397 | putExtra(ACTION, MUTE)
398 | putExtra(MUTE, mute)
399 | }
400 | }
401 | fun reset(context: Context?): Intent {
402 | return Intent(context, ScreenReaderService::class.java).apply {
403 | putExtra(ACTION, RESET)
404 | }
405 | }
406 | }
407 | }
--------------------------------------------------------------------------------