Skip to content

Commit

Permalink
update library to Android 14
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Sep 13, 2023
1 parent 96c69c0 commit 0ec6591
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 26 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Expand Up @@ -3,12 +3,12 @@ apply plugin: 'org.jetbrains.kotlin.android'

android {
namespace "com.pedro.streamer"
compileSdk 33
compileSdk 34

defaultConfig {
applicationId "com.pedro.streamer"
minSdkVersion 16
targetSdkVersion 33
targetSdkVersion 34
versionCode version_code
versionName version_name
}
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/AndroidManifest.xml
Expand Up @@ -17,6 +17,10 @@
<!--Optional for play store-->
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
<!-- Android 14 background compatibility-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CAMERA"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION"/>

<application
android:requestLegacyExternalStorage="true"
Expand Down Expand Up @@ -125,10 +129,11 @@
/>

<service android:name="com.pedro.streamer.rotation.StreamService"
android:foregroundServiceType="mediaProjection"
android:foregroundServiceType="mediaProjection|camera|microphone"
/>

<service android:name="com.pedro.streamer.backgroundexample.RtpService"
android:foregroundServiceType="camera|microphone"
/>

<service android:name="com.pedro.streamer.displayexample.DisplayService"
Expand Down
30 changes: 25 additions & 5 deletions app/src/main/java/com/pedro/streamer/MainActivity.java
Expand Up @@ -18,12 +18,15 @@

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.appcompat.app.AppCompatActivity;
Expand Down Expand Up @@ -151,18 +154,35 @@ private void setListAdapter(List<ActivityLink> activities) {
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (hasPermissions(this)) {
ActivityLink link = activities.get(i);
int minSdk = link.getMinSdk();
if (Build.VERSION.SDK_INT >= minSdk) {
startActivity(link.getIntent());
overridePendingTransition(R.transition.slide_in, R.transition.slide_out);
if (link.getLabel().equals(getString(R.string.rotation_rtmp)) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
Intent intent = mediaProjectionManager.createScreenCaptureIntent();
startActivityForResult(intent, 0);
} else {
showMinSdkError(minSdk);
int minSdk = link.getMinSdk();
if (Build.VERSION.SDK_INT >= minSdk) {
startActivity(link.getIntent());
overridePendingTransition(R.transition.slide_in, R.transition.slide_out);
} else {
showMinSdkError(minSdk);
}
}
} else {
showPermissionsErrorAndRequest();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null && (requestCode == 0 && resultCode == Activity.RESULT_OK) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ActivityLink link = new ActivityLink(new Intent(this, RotationExampleActivity.class),
getString(R.string.rotation_rtmp), LOLLIPOP);
startActivity(link.getIntent());
overridePendingTransition(R.transition.slide_in, R.transition.slide_out);
}
}

private void showMinSdkError(int minSdk) {
String named;
switch (minSdk) {
Expand Down
Expand Up @@ -48,7 +48,6 @@ class DisplayService : Service() {
val channel = NotificationChannel(channelId, channelId, NotificationManager.IMPORTANCE_HIGH)
notificationManager?.createNotificationChannel(channel)
}
keepAliveTrick()
}

private fun keepAliveTrick() {
Expand Down Expand Up @@ -151,6 +150,7 @@ class DisplayService : Service() {
}

fun prepareStreamRtp(endpoint: String, resultCode: Int, data: Intent) {
keepAliveTrick()
stopStream()
if (endpoint.startsWith("rtmp")) {
displayBase = RtmpDisplay(baseContext, true, connectCheckerRtp)
Expand Down
4 changes: 2 additions & 2 deletions encoder/build.gradle
Expand Up @@ -5,11 +5,11 @@ apply plugin: 'org.jetbrains.dokka'

android {
namespace "com.pedro.encoder"
compileSdk 33
compileSdk 34

defaultConfig {
minSdkVersion 16
targetSdkVersion 33
targetSdkVersion 34
}
buildTypes {
release {
Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle
Expand Up @@ -5,11 +5,11 @@ apply plugin: 'org.jetbrains.dokka'

android {
namespace "com.pedro.library"
compileSdk 33
compileSdk 34

defaultConfig {
minSdkVersion 16
targetSdkVersion 33
targetSdkVersion 34
}
buildTypes {
release {
Expand Down
10 changes: 7 additions & 3 deletions library/src/main/java/com/pedro/library/base/DisplayBase.java
Expand Up @@ -234,7 +234,8 @@ public boolean prepareInternalAudio(int bitrate, int sampleRate, boolean isStere
if (mediaProjection == null) {
mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
}

MediaProjection.Callback mediaProjectionCallback = new MediaProjection.Callback() { };
mediaProjection.registerCallback(mediaProjectionCallback, null);
AudioPlaybackCaptureConfiguration config =
new AudioPlaybackCaptureConfiguration.Builder(mediaProjection).addMatchingUsage(
AudioAttributes.USAGE_MEDIA)
Expand Down Expand Up @@ -397,13 +398,16 @@ private void startEncoders(int resultCode, Intent data) {
if (mediaProjection == null) {
mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
}
MediaProjection.Callback mediaProjectionCallback = new MediaProjection.Callback() { };
mediaProjection.registerCallback(mediaProjectionCallback, null);
VirtualDisplay.Callback callback = new VirtualDisplay.Callback() { };
if (glInterface != null && videoEncoder.getRotation() == 90
|| videoEncoder.getRotation() == 270) {
virtualDisplay = mediaProjection.createVirtualDisplay("Stream Display", videoEncoder.getHeight(),
videoEncoder.getWidth(), dpi, 0, surface, null, null);
videoEncoder.getWidth(), dpi, 0, surface, callback, null);
} else {
virtualDisplay = mediaProjection.createVirtualDisplay("Stream Display", videoEncoder.getWidth(),
videoEncoder.getHeight(), dpi, 0, surface, null, null);
videoEncoder.getHeight(), dpi, 0, surface, callback, null);
}
if (audioInitialized) microphoneManager.start();
}
Expand Down
@@ -1,5 +1,6 @@
package com.pedro.library.util.sources

import android.hardware.display.VirtualDisplay
import android.media.AudioAttributes
import android.media.AudioPlaybackCaptureConfiguration
import android.media.projection.MediaProjection
Expand Down Expand Up @@ -54,6 +55,8 @@ class AudioManager(getMicrophoneData: GetMicrophoneData, var source: Source) {
}
Source.INTERNAL -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val mediaProjectionCallback = object : MediaProjection.Callback() {}
mediaProjection?.registerCallback(mediaProjectionCallback, null)
mediaProjection?.let {
val config = AudioPlaybackCaptureConfiguration.Builder(it)
.addMatchingUsage(AudioAttributes.USAGE_MEDIA)
Expand Down
Expand Up @@ -119,9 +119,13 @@ class VideoManager(private val context: Context, var source: Source) {
if (shouldRotate) {
surfaceTexture.setDefaultBufferSize(height, width)
}
val mediaProjectionCallback = object : MediaProjection.Callback() {}
mediaProjection?.registerCallback(mediaProjectionCallback, null)

val callback = object : VirtualDisplay.Callback() {}
virtualDisplay = mediaProjection?.createVirtualDisplay("VideoManagerScreen",
displayWidth, displayHeight, dpi, flags,
Surface(surfaceTexture), null, null)
Surface(surfaceTexture), callback, null)
}
Source.DISABLED -> noSource.start()
}
Expand Down Expand Up @@ -353,7 +357,7 @@ class VideoManager(private val context: Context, var source: Source) {
} else {
camera1.previewSizeBack
}
mapCamera1Resolutions(resolutions)
mapCamera1Resolutions(resolutions, false)
}
Source.CAMERA2 -> {
val resolutions = if (facing == CameraHelper.Facing.FRONT) {
Expand All @@ -380,7 +384,7 @@ class VideoManager(private val context: Context, var source: Source) {
val resolutions = if (facing == CameraHelper.Facing.BACK) {
camera1.previewSizeBack
} else camera1.previewSizeFront
return mapCamera1Resolutions(resolutions).contains(size)
return mapCamera1Resolutions(resolutions, shouldRotate).find { it.width == size.width && it.height == size.height } != null
}
Source.CAMERA2 -> {
val size = Size(width, height)
Expand All @@ -407,5 +411,7 @@ class VideoManager(private val context: Context, var source: Source) {
}

@Suppress("DEPRECATION")
private fun mapCamera1Resolutions(resolutions: List<Camera.Size>) = resolutions.map { Size(it.width, it.height) }
private fun mapCamera1Resolutions(resolutions: List<Camera.Size>, shouldRotate: Boolean) = resolutions.map {
if (shouldRotate) Size(it.height, it.width) else Size(it.width, it.height)
}
}
4 changes: 2 additions & 2 deletions rtmp/build.gradle
Expand Up @@ -5,11 +5,11 @@ apply plugin: 'org.jetbrains.dokka'

android {
namespace "com.pedro.rtmp"
compileSdk 33
compileSdk 34

defaultConfig {
minSdkVersion 16
targetSdkVersion 33
targetSdkVersion 34
}
buildTypes {
release {
Expand Down
4 changes: 2 additions & 2 deletions rtsp/build.gradle
Expand Up @@ -5,11 +5,11 @@ apply plugin: 'org.jetbrains.dokka'

android {
namespace "com.pedro.rtsp"
compileSdk 33
compileSdk 34

defaultConfig {
minSdkVersion 16
targetSdkVersion 33
targetSdkVersion 34
buildConfigField 'int', 'VERSION_CODE', "${version_code}"
buildConfigField 'String', 'VERSION_NAME', "\"${version_name}\""
}
Expand Down
4 changes: 2 additions & 2 deletions srt/build.gradle
Expand Up @@ -5,11 +5,11 @@ apply plugin: 'org.jetbrains.dokka'

android {
namespace "com.pedro.srt"
compileSdk 33
compileSdk 34

defaultConfig {
minSdkVersion 16
targetSdkVersion 33
targetSdkVersion 34
}
buildTypes {
release {
Expand Down

0 comments on commit 0ec6591

Please sign in to comment.