Skip to content

Commit

Permalink
let's try to get rid of libgui ...
Browse files Browse the repository at this point in the history
...and "Vector<> have different types" errors

This is an attempt to write a minicap version in kotlin and
use higher level api that should hopefully make it easier
to support many os version (and the future ones). The usage
of SurfaceControl has been inspired by the scrcpy project which
is great.

The implementation is not complete and a few changes will have
to be done on stf.
  • Loading branch information
pcrepieux committed Nov 5, 2020
1 parent 0aa1823 commit daff263
Show file tree
Hide file tree
Showing 41 changed files with 1,395 additions and 0 deletions.
16 changes: 16 additions & 0 deletions experimental/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
/.idea
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
1 change: 1 addition & 0 deletions experimental/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
59 changes: 59 additions & 0 deletions experimental/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2020 Orange
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

plugins {
id 'com.android.application'
id 'kotlin-android'
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"

defaultConfig {
applicationId "io.devicefarmer.minicap"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "0.1"
archivesBaseName = "minicap-$versionName"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
21 changes: 21 additions & 0 deletions experimental/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2020 Orange
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.devicefarmer.minicap

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("io.devicefarmer.minicap", appContext.packageName)
}
}
26 changes: 26 additions & 0 deletions experimental/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2020 Orange
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.devicefarmer.minicap">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Minicap" />
</manifest>
52 changes: 52 additions & 0 deletions experimental/app/src/main/java/io/devicefarmer/minicap/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2020 Orange
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.devicefarmer.minicap

import android.os.Looper
import android.util.Size
import io.devicefarmer.minicap.provider.SurfaceProvider

/**
* Main entry point that can be launched as follow:
* adb shell CLASSPATH=/data/local/tmp/minicap-debug.apk app_process /system/bin io.devicefarmer.minicap.Main
*
* For now, only somehow parses parameter -P <w>x<h>@<w>x<h>/{0|90|180|270}
*/
class Main {
companion object {
@JvmStatic
fun main(args: Array<String>) {
var provider : SurfaceProvider
val projection = args.getOrNull(args.indexOf("-P")+1)?.run {
val targetSize = this.split('@','/')[1].split('x')
Projection(targetSize[0].toInt(), targetSize[1].toInt())
}
//the stf process reads this
println("PID: ${android.os.Process.myPid()}")
Looper.prepareMainLooper()
provider = if(projection == null) {
SurfaceProvider()
} else {
SurfaceProvider(Size(projection.w, projection.h))
}
val server = SimpleServer(provider)
server.start()
Looper.loop()
}
}

data class Projection(val w: Int, val h: Int)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (C) 2020 Orange
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.devicefarmer.minicap

import android.net.LocalSocket
import android.util.Size
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.nio.ByteBuffer
import java.nio.ByteOrder

/**
* Provides method to send data to the client that connected
*
* Basically implements the "minicap" protocol
*/
@ExperimentalUnsignedTypes
class RemoteClient(private val socket: LocalSocket, var screenSize: Size=Size(0,0), var targetSize: Size=Size(0,0)) {
companion object {
const val BANNER_VERSION = 1
const val BANNER_SIZE = 24
const val QUIRK_ALWAYS_UPRIGHT = 2
}
val imageBuffer : ByteArrayOutputStream = ByteArrayOutputStream()

/**
* Sends the banner required at connection time
*/
fun sendBanner() = try {
val byteArray = ByteArray(BANNER_SIZE.toInt())
ByteBuffer.wrap(byteArray).apply {
order(ByteOrder.LITTLE_ENDIAN)
put(BANNER_VERSION.toByte())
put(BANNER_SIZE.toByte())
putInt(android.os.Process.myPid()) //PID
putInt(screenSize.width)//Width
putInt(screenSize.height)//Height
putInt(targetSize.width)//resized Width
putInt(targetSize.height)//resized height
put(0.toByte()) //orientation
put(QUIRK_ALWAYS_UPRIGHT.toByte()) //quirk
}
with(socket.outputStream) {
write(byteArray)
flush()
}
} catch (e: IOException) {
e.printStackTrace()
}

/**
* Sends a buffer containing a jpg image
*/
fun send(){
val data = imageBuffer.toByteArray()
val payload = ByteArray(data.size+4) //size: 32bit integer
ByteBuffer.wrap(payload).apply {
order(ByteOrder.LITTLE_ENDIAN)
putInt(data.size)
put(data)
}
with(socket.outputStream) {
write(payload)
flush()
}
imageBuffer.reset()
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2020 Orange
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.devicefarmer.minicap

import android.net.LocalServerSocket
import android.net.LocalSocket
import java.io.IOException

/**
* Minimalist abstraction of a "server" for the proof of concept
*/
class SimpleServer(var listener: Listener) {
private val SOCKET = "minicap"
private var serverSocket = LocalServerSocket(SOCKET)

interface Listener {
fun onConnection(socket: LocalSocket)
}

fun start() {
try {
System.out.println("Listening on ${SOCKET}");
val clientSocket: LocalSocket = serverSocket.accept()
listener.onConnection(clientSocket)
} catch (e: IOException) {
e.printStackTrace()
}
}


}
Loading

0 comments on commit daff263

Please sign in to comment.