Skip to content
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

Refactoring code / a bit of cleanup #73

Draft
wants to merge 14 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@
.externalNativeBuild
.cxx
local.properties
app/debug
app/alpha
app/beta
app/release
9 changes: 5 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'com.android.application'
id "com.starter.easylauncher" version "4.0.0"
}

android {
Expand All @@ -12,7 +13,7 @@ android {
keyAlias digiviewKeyAlias
}
catch (ex) {
println("You should define mStoreFile, mStorePassword, mKeyPassword and mKeyAlias in ~/.gradle/gradle.properties.")
println("You should define mStoreFile, mStorePassword, mKeyPassword and mKeyAlias in ~/.gradle/gradle.properties : "+ ex.message)
}
}
}
Expand Down Expand Up @@ -65,14 +66,14 @@ android {

dependencies {

implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.google.android.exoplayer:exoplayer:2.13.3'
implementation 'com.google.android.exoplayer:exoplayer:2.14.1'
implementation 'io.sentry:sentry-android:4.3.0'
implementation 'androidx.preference:preference:1.1.1'

testImplementation 'junit:junit:4.+'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
29 changes: 18 additions & 11 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Digiview"
>
android:theme="@style/Theme.Digiview">
<activity
android:name=".DataCollectionAgreementPopupActivity"
android:label="@string/title_activity_data_collection"
android:theme="@style/Theme.AppCompat.Dialog"></activity>
android:theme="@style/Theme.AppCompat.Dialog" />
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"></activity>
android:label="@string/title_activity_settings" />

<activity
android:launchMode="singleTask"
android:name="com.fpvout.digiview.MainActivity"
android:screenOrientation="sensorLandscape"
android:configChanges="orientation|keyboardHidden">
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleTask"
android:screenOrientation="sensorLandscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand All @@ -38,14 +37,22 @@
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>

<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />

<meta-data android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"
android:resource="@xml/device_filter" />
</activity>
<meta-data android:name="io.sentry.dsn" android:value="https://[email protected]/5764247" />
<meta-data android:name="io.sentry.auto-init" android:value="false" />

<meta-data
android:name="io.sentry.dsn"
android:value="https://[email protected]/5764247" />
<meta-data
android:name="io.sentry.auto-init"
android:value="false" />

<provider
android:name="io.sentry.android.core.SentryInitProvider"
android:authorities="com.fpvout.digiview.SentryInitProvider"
Expand Down
27 changes: 8 additions & 19 deletions app/src/main/java/com/fpvout/digiview/H264Extractor.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.fpvout.digiview;

import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.extractor.ExtractorOutput;
import com.google.android.exoplayer2.extractor.ExtractorsFactory;
import com.google.android.exoplayer2.extractor.PositionHolder;
import com.google.android.exoplayer2.extractor.SeekMap;
import com.google.android.exoplayer2.extractor.ts.H264Reader;
import com.google.android.exoplayer2.extractor.ts.SeiReader;
import com.google.android.exoplayer2.extractor.ts.TsPayloadReader;
import com.google.android.exoplayer2.util.NonNullApi;
import com.google.android.exoplayer2.util.ParsableByteArray;
import com.google.android.exoplayer2.extractor.ts.SeiReader;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -21,45 +20,34 @@
* Extracts data from H264 bitstreams.
*/
public final class H264Extractor implements Extractor {
/** Factory for {@link H264Extractor} instances. */
public static final ExtractorsFactory FACTORY = () -> new Extractor[] {new H264Extractor()};

private static int MAX_SYNC_FRAME_SIZE = 131072;

private long firstSampleTimestampUs;
private static long sampleTime = 10000; // todo: try to lower this. it directly infer on speed and latency. this should be equal to 16666 to reach 60fps but works better with lower value
private final H264Reader reader;
private final ParsableByteArray sampleData;

private long firstSampleTimestampUs;
private boolean startedPacket;

public H264Extractor() {
this(0);
}

public H264Extractor(int mMaxSyncFrameSize, int mSampleTime) {
this(0, mMaxSyncFrameSize, mSampleTime);
}

public H264Extractor(long firstSampleTimestampUs) {
this(firstSampleTimestampUs, MAX_SYNC_FRAME_SIZE, (int) sampleTime);
}

public H264Extractor(long firstSampleTimestampUs, int mMaxSyncFrameSize, int mSampleTime) {
MAX_SYNC_FRAME_SIZE = mMaxSyncFrameSize;
sampleTime = mSampleTime;
this.firstSampleTimestampUs = firstSampleTimestampUs;
reader = new H264Reader(new SeiReader(new ArrayList<Format>()),false,true);
reader = new H264Reader(new SeiReader(new ArrayList<>()), false, true);
sampleData = new ParsableByteArray(MAX_SYNC_FRAME_SIZE);
}

// Extractor implementation.
@Override
public boolean sniff(ExtractorInput input) throws IOException {
@NonNullApi
public boolean sniff(ExtractorInput input) {
return true;
}

@Override
@NonNullApi
public void init(ExtractorOutput output) {
reader.createTracks(output, new TsPayloadReader.TrackIdGenerator(0, 1));
output.endTracks();
Expand All @@ -78,6 +66,7 @@ public void release() {
}

@Override
@NonNullApi
public int read(ExtractorInput input, PositionHolder seekPosition) throws IOException {
int bytesRead = input.read(sampleData.getData(), 0, MAX_SYNC_FRAME_SIZE);
if (bytesRead == C.RESULT_END_OF_INPUT) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.fpvout.digiview;

import android.content.Context;
import android.net.Uri;

import androidx.annotation.NonNull;

import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSpec;
Expand All @@ -19,31 +20,29 @@ public class InputStreamBufferedDataSource implements DataSource {
private static final String ERROR_THREAD_NOT_INITIALIZED = "Read thread not initialized, call first 'startReadThread()'";
private static final long READ_TIMEOUT = 200;

private Context context;
private DataSpec dataSpec;
private final DataSpec dataSpec;
private InputStream inputStream;
private long bytesRemaining;
private boolean opened;

private CircularByteBuffer readBuffer;
private Thread receiveThread;
private boolean working;


public InputStreamBufferedDataSource(Context context, DataSpec dataSpec, InputStream inputStream) {
this.context = context;
public InputStreamBufferedDataSource(DataSpec dataSpec, InputStream inputStream) {
this.dataSpec = dataSpec;
this.inputStream = inputStream;
startReadThread();
}

@Override
public void addTransferListener(TransferListener transferListener) {
public void addTransferListener(@NonNull TransferListener transferListener) {

}

@Override
public long open(DataSpec dataSpec) throws IOException {
long bytesRemaining;
try {
long skipped = inputStream.skip(dataSpec.position);
if (skipped < dataSpec.position)
Expand All @@ -63,16 +62,14 @@ public long open(DataSpec dataSpec) throws IOException {
}

@Override
public int read(byte[] buffer, int offset, int readLength) throws IOException {
public int read(@NonNull byte[] buffer, int offset, int readLength) throws IOException {
if (readBuffer == null)
throw new IOException(ERROR_THREAD_NOT_INITIALIZED);

long deadLine = System.currentTimeMillis() + READ_TIMEOUT;
int readBytes = 0;
while (System.currentTimeMillis() < deadLine && readBytes <= 0)
readBytes = readBuffer.read(buffer, offset, readLength);
if (readBytes <= 0)
return readBytes;
return readBytes;
}

Expand Down
15 changes: 7 additions & 8 deletions app/src/main/java/com/fpvout/digiview/InputStreamDataSource.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.fpvout.digiview;

import android.content.Context;
import android.net.Uri;

import androidx.annotation.NonNull;

import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSpec;
Expand All @@ -13,25 +14,23 @@
import java.io.InputStream;

public class InputStreamDataSource implements DataSource {
private Context context;
private DataSpec dataSpec;
private final DataSpec dataSpec;
private InputStream inputStream;
private long bytesRemaining;
private boolean opened;

public InputStreamDataSource(Context context, DataSpec dataSpec, InputStream inputStream) {
this.context = context;
public InputStreamDataSource(DataSpec dataSpec, InputStream inputStream) {
this.dataSpec = dataSpec;
this.inputStream = inputStream;
}

@Override
public void addTransferListener(TransferListener transferListener) {
public void addTransferListener(@NonNull TransferListener transferListener) {

}

@Override
public long open(DataSpec dataSpec) throws IOException {
long bytesRemaining;
try {
long skipped = inputStream.skip(dataSpec.position);
if (skipped < dataSpec.position)
Expand All @@ -51,7 +50,7 @@ public long open(DataSpec dataSpec) throws IOException {
}

@Override
public int read(byte[] buffer, int offset, int readLength) throws IOException {
public int read(@NonNull byte[] buffer, int offset, int readLength) throws IOException {
return inputStream.read(buffer, offset, readLength);
}

Expand Down
Loading