Skip to content
Snippets Groups Projects
Commit 201f8160 authored by Nicolas Pomepuy's avatar Nicolas Pomepuy
Browse files

New http server + allow starting / stopping the camera

parent 9be798b7
No related branches found
No related tags found
No related merge requests found
......@@ -45,9 +45,18 @@ android {
version '3.18.1'
}
}
packagingOptions {
resources {
excludes += ['META-INF/*']
}
}
}
dependencies {
implementation "io.ktor:ktor:2.1.2"
implementation "io.ktor:ktor-server-netty:2.1.2"
implementation "io.ktor:ktor-gson:1.6.8"
implementation 'androidx.core:core-ktx:+'
testImplementation 'junit:junit:4.13.2'
......
......@@ -16,10 +16,14 @@ import android.os.Looper;
import android.util.Log;
import android.util.Size;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.rom1v.mycamerasample.httpserver.HttpServer;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
......@@ -36,6 +40,8 @@ public class MainActivity extends Activity {
private Encoder encoder;
private Button button;
private Button serverButton;
private TextView serverStatus;
private boolean started;
static class CameraSelection {
......@@ -54,36 +60,74 @@ public class MainActivity extends Activity {
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
serverButton = findViewById(R.id.server_button);
serverStatus = findViewById(R.id.server_status);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!started) {
if (checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
button.setEnabled(false);
open();
} else {
String[] permissions = {Manifest.permission.CAMERA};
requestPermissions(permissions, REQUEST_CODE_PERMISSION_CAMERA);
}
started = true;
startCamera();
} else {
if (encoder != null) {
encoder.stop();
encoder.release();
encoder = null;
}
if (camera != null) {
camera.stop();
camera.release();
camera = null;
}
button.setText(R.string.start);
started = false;
stopCamera();
}
}
});
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
HttpServer.INSTANCE.setOnServerEventListener(launched -> {
if (launched) {
serverButton.setText(R.string.stop);
serverStatus.setText(getString(R.string.http_server_started, HttpServer.INSTANCE.serverInfo()));
} else {
serverButton.setText(R.string.start);
serverStatus.setText(R.string.http_server_stopped);
}
return null;
});
HttpServer.INSTANCE.launchServer();
HttpServer.INSTANCE.setOnCameraChangeListener(stop -> {
runOnUiThread(() -> {
if (stop && started) {
stopCamera();
} else if (!stop && !started){
startCamera();
}
});
return null;
});
serverButton.setOnClickListener(v -> {
HttpServer.INSTANCE.switchServer();
}
);
}
private void stopCamera() {
if (encoder != null) {
encoder.stop();
encoder.release();
encoder = null;
}
if (camera != null) {
camera.stop();
camera.release();
camera = null;
}
button.setText(R.string.start);
started = false;
}
private void startCamera() {
if (checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
button.setEnabled(false);
open();
} else {
String[] permissions = {Manifest.permission.CAMERA};
requestPermissions(permissions, REQUEST_CODE_PERMISSION_CAMERA);
}
started = true;
}
@Override
protected void onDestroy() {
super.onDestroy();
......
package com.rom1v.mycamerasample.httpserver
import android.content.Context
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import java.net.InetAddress
import java.net.NetworkInterface
import java.util.*
object HttpServer {
private var started = false
private lateinit var serverEventListener: (launched: Boolean) -> Unit
private lateinit var cameraChangeListener: (stop: Boolean) -> Unit
private var engine: NettyApplicationEngine? = null
fun setOnServerEventListener(listener: (launched: Boolean) -> Unit) {
serverEventListener = listener
}
fun setOnCameraChangeListener(listener: (stop: Boolean) -> Unit) {
cameraChangeListener = listener
}
fun launchServer() {
engine = embeddedServer(Netty, 8080) {
routing {
get("/camera/stop") {
cameraChangeListener.invoke(true)
call.respondText(
"{success: true}",
ContentType.Application.Json
)
}
get("/camera/start") {
cameraChangeListener.invoke(false)
call.respondText(
"{success: true}",
ContentType.Application.Json
)
}
}
}
start()
}
fun start() {
serverEventListener.invoke(true)
engine?.start()
started = true
}
fun stop() {
engine?.stop()
serverEventListener.invoke(false)
started = false
}
fun serverInfo(): String = buildString {
getIPAddresses(true).forEach {
append(it)
append(":")
append(engine!!.environment.connectors[0].port)
append("\n")
}
}
/**
* Get IP address from first non-localhost interface
* @param useIPv4 true=return ipv4, false=return ipv6
* @return address or empty string
*/
private fun getIPAddresses(useIPv4: Boolean): List<String> {
val results = arrayListOf<String>()
try {
val interfaces: List<NetworkInterface> =
Collections.list(NetworkInterface.getNetworkInterfaces())
for (networkInterface in interfaces) {
val inetAddresses: List<InetAddress> =
Collections.list(networkInterface.inetAddresses)
for (inetAddress in inetAddresses) {
if (!inetAddress.isLoopbackAddress) {
val hostAddress = inetAddress.hostAddress ?: continue
val isIPv4 = hostAddress.indexOf(':') < 0
if (useIPv4) {
if (isIPv4) results.add(hostAddress)
} else {
if (!isIPv4) {
val delim = hostAddress.indexOf('%') // drop ip6 zone suffix
if (delim < 0)
results.add(hostAddress.uppercase(Locale.getDefault()))
else
results.add(
hostAddress.substring(0, delim)
.uppercase(Locale.getDefault())
)
}
}
}
}
}
} catch (ignored: Exception) {
}
return results
}
fun switchServer() {
if (started) stop() else launchServer()
}
}
\ No newline at end of file
......@@ -5,9 +5,57 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
<LinearLayout
android:id="@+id/camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/camera" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start" />
</LinearLayout>
<LinearLayout
android:id="@+id/server"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/camera"
android:orientation="horizontal"
android:padding="16dp">
<TextView
android:id="@+id/server_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/http_server" />
<Button
android:id="@+id/server_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start" />
</LinearLayout>
<TextView
android:id="@+id/server_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start"/>
android:layout_below="@+id/server"
android:paddingEnd="16dp"
android:paddingStart="16dp"
tools:text="Server stopped" />
</RelativeLayout>
\ No newline at end of file
......@@ -2,4 +2,8 @@
<string name="app_name">MyCameraSample</string>
<string name="start">Start</string>
<string name="stop">Stop</string>
<string name="camera">Camera</string>
<string name="http_server">Http server</string>
<string name="http_server_started">Http server started on %s</string>
<string name="http_server_stopped">Http server stopped</string>
</resources>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment