-
Notifications
You must be signed in to change notification settings - Fork 5
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
CCAPI ver110 support #184
CCAPI ver110 support #184
Changes from all commits
9ba3033
7ceb79f
e1cbee3
dd40ccc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
package="io.numbersprotocol.starlingcapture"> | ||
|
||
<uses-feature | ||
|
@@ -9,6 +10,10 @@ | |
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> | ||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> | ||
<uses-permission android:name="android.permission.RECORD_AUDIO" /> | ||
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> | ||
<uses-permission android:name="android.permission.WRITE_SETTINGS" | ||
tools:ignore="ProtectedPermissions" /> | ||
Comment on lines
+13
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably unnecessary since I commented out the WiFi network binding. |
||
|
||
|
||
<application | ||
android:name=".BaseApplication" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,7 +63,7 @@ suspend fun CanonCameraControlApi.waitUntilConnected( | |
|
||
@JsonClass(generateAdapter = true) | ||
data class Api( | ||
val url: String, | ||
val path: String, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was the big breaking change. |
||
val get: Boolean, | ||
val post: Boolean, | ||
val put: Boolean, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
package io.numbersprotocol.starlingcapture.source.canon | ||
|
||
import android.app.Service | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.net.ConnectivityManager | ||
import android.net.Network | ||
import android.net.NetworkCapabilities | ||
import android.net.NetworkRequest | ||
import androidx.core.app.NotificationCompat | ||
import io.numbersprotocol.starlingcapture.R | ||
import io.numbersprotocol.starlingcapture.collector.ProofCollector | ||
|
@@ -21,21 +26,39 @@ class CanonCameraControlService : Service(), CoroutineScope by CoroutineScope(Di | |
private val notificationUtil: NotificationUtil by inject() | ||
private val foregroundNotificationId = notificationUtil.createNotificationId() | ||
private val errorNotificationId = notificationUtil.createNotificationId() | ||
private lateinit var address: String | ||
private lateinit var canonCameraControlApi: CanonCameraControlApi | ||
private val proofCollector: ProofCollector by inject() | ||
private lateinit var foregroundNotificationBuilder: NotificationCompat.Builder | ||
private val beforeDestroyCallbacks = mutableSetOf<() -> Unit>() | ||
|
||
@FlowPreview | ||
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { | ||
// bindProcessToWiFiNetwork() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This expectedly breaks Internet traffic. Proper fix will come from #22 |
||
initializeCanonCameraControlApi(intent) | ||
startForeground() | ||
connectToCamera() | ||
return super.onStartCommand(intent, flags, startId) | ||
} | ||
|
||
/** | ||
* Default to WiFi network transport (even when Internet in unavailable on that interface) since | ||
* our camera has no Internet connectivity. This tells Android to still connect to the local IP | ||
* when mobile data is available on the phone. | ||
*/ | ||
private fun bindProcessToWiFiNetwork() { | ||
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
val request = NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI).build() | ||
val networkCallback = object : ConnectivityManager.NetworkCallback() { | ||
override fun onAvailable(network: Network) { | ||
connectivityManager.bindProcessToNetwork(network) | ||
} | ||
} | ||
connectivityManager?.requestNetwork(request, networkCallback) | ||
} | ||
|
||
private fun initializeCanonCameraControlApi(intent: Intent) { | ||
val address = intent.extras!!.getString(CAMERA_ADDRESS)!! | ||
address = intent.extras!!.getString(CAMERA_ADDRESS)!! | ||
canonCameraControlApi = CanonCameraControlApi.create(address) | ||
} | ||
|
||
|
@@ -52,6 +75,7 @@ class CanonCameraControlService : Service(), CoroutineScope by CoroutineScope(Di | |
|
||
@FlowPreview | ||
private fun connectToCamera() = launch { | ||
Timber.v("Connecting to camera...") | ||
canonCameraControlApi.waitUntilConnected { e -> | ||
if (e !is CancellationException) { | ||
notificationUtil.notifyException(e, foregroundNotificationId) | ||
|
@@ -80,20 +104,25 @@ class CanonCameraControlService : Service(), CoroutineScope by CoroutineScope(Di | |
} | ||
} | ||
|
||
private fun getContentFlow(url: String) = flow { | ||
private fun getContentFlow(path: String) = flow { | ||
val url = "http://$address$path" | ||
Timber.v("Getting content stream at $url") | ||
emit(MediaStream(canonCameraControlApi.getContent(url).byteStream(), MimeType.fromUrl(url))) | ||
}.retry { e -> | ||
(e is HttpException && e.code() == 503).also { | ||
Timber.e("Getting content stream failed with error: %s", e.message) | ||
Timber.w("Service is currently unavailable. Is the shooting processing in progress?") | ||
delay(POLLING_INTERVAL_MILLIS) | ||
} | ||
} | ||
|
||
private suspend fun storeAndCollect(mediaStream: MediaStream) { | ||
Timber.v("Storing new content...") | ||
val cachedFile = createCachedFile(mediaStream) | ||
cachedFile.copyFromInputStream(mediaStream.inputStream) | ||
proofCollector.storeAndCollect(cachedFile, mediaStream.mimeType) | ||
cachedFile.delete() | ||
Timber.i("New content stored.") | ||
} | ||
|
||
private fun createCachedFile(mediaStream: MediaStream) = File.createTempFile( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for compiling on M1 Macs.