From 73b922ee59fa565a1fabaf5411bdea7ca81ee3fc Mon Sep 17 00:00:00 2001 From: kimvde Date: Mon, 9 Sep 2019 19:11:02 +0100 Subject: [PATCH] Add capture policy option to AudioAttributes PiperOrigin-RevId: 268035329 --- RELEASENOTES.md | 2 + .../java/com/google/android/exoplayer2/C.java | 16 ++++++ .../exoplayer2/audio/AudioAttributes.java | 56 +++++++++++++------ 3 files changed, 58 insertions(+), 16 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index ae37ecd6ca..3c792b1bee 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -2,6 +2,8 @@ ### dev-v2 (not yet released) ### +* Add `allowedCapturePolicy` field to `AudioAttributes` wrapper to allow to + opt-out of audio recording. * Add `DataSpec.httpRequestHeaders` to set HTTP request headers when connecting to an HTTP source. `DefaultHttpDataSource`, `CronetDataSource` and `OkHttpDataSource` include headers set in the DataSpec when connecting to the diff --git a/library/core/src/main/java/com/google/android/exoplayer2/C.java b/library/core/src/main/java/com/google/android/exoplayer2/C.java index 38553697d0..c8a73fbe9a 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/C.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/C.java @@ -17,6 +17,7 @@ package com.google.android.exoplayer2; import android.annotation.TargetApi; import android.content.Context; +import android.media.AudioAttributes; import android.media.AudioFormat; import android.media.AudioManager; import android.media.MediaCodec; @@ -440,6 +441,21 @@ public final class C { public static final int USAGE_VOICE_COMMUNICATION_SIGNALLING = android.media.AudioAttributes.USAGE_VOICE_COMMUNICATION_SIGNALLING; + /** + * Capture policies for {@link com.google.android.exoplayer2.audio.AudioAttributes}. One of {@link + * #ALLOW_CAPTURE_BY_ALL}, {@link #ALLOW_CAPTURE_BY_NONE} or {@link #ALLOW_CAPTURE_BY_SYSTEM}. + */ + @Documented + @Retention(RetentionPolicy.SOURCE) + @IntDef({ALLOW_CAPTURE_BY_ALL, ALLOW_CAPTURE_BY_NONE, ALLOW_CAPTURE_BY_SYSTEM}) + public @interface AudioAllowedCapturePolicy {} + /** See {@link android.media.AudioAttributes#ALLOW_CAPTURE_BY_ALL}. */ + public static final int ALLOW_CAPTURE_BY_ALL = AudioAttributes.ALLOW_CAPTURE_BY_ALL; + /** See {@link android.media.AudioAttributes#ALLOW_CAPTURE_BY_NONE}. */ + public static final int ALLOW_CAPTURE_BY_NONE = AudioAttributes.ALLOW_CAPTURE_BY_NONE; + /** See {@link android.media.AudioAttributes#ALLOW_CAPTURE_BY_SYSTEM}. */ + public static final int ALLOW_CAPTURE_BY_SYSTEM = AudioAttributes.ALLOW_CAPTURE_BY_SYSTEM; + /** * Audio focus types. One of {@link #AUDIOFOCUS_NONE}, {@link #AUDIOFOCUS_GAIN}, {@link * #AUDIOFOCUS_GAIN_TRANSIENT}, {@link #AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK} or {@link diff --git a/library/core/src/main/java/com/google/android/exoplayer2/audio/AudioAttributes.java b/library/core/src/main/java/com/google/android/exoplayer2/audio/AudioAttributes.java index 1b0d629da7..516df8147c 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/audio/AudioAttributes.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/audio/AudioAttributes.java @@ -18,6 +18,7 @@ package com.google.android.exoplayer2.audio; import android.annotation.TargetApi; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.util.Util; /** * Attributes for audio playback, which configure the underlying platform @@ -42,17 +43,19 @@ public final class AudioAttributes { private @C.AudioContentType int contentType; private @C.AudioFlags int flags; private @C.AudioUsage int usage; + private @C.AudioAllowedCapturePolicy int allowedCapturePolicy; /** * Creates a new builder for {@link AudioAttributes}. - *

- * By default the content type is {@link C#CONTENT_TYPE_UNKNOWN}, usage is - * {@link C#USAGE_MEDIA}, and no flags are set. + * + *

By default the content type is {@link C#CONTENT_TYPE_UNKNOWN}, usage is {@link + * C#USAGE_MEDIA}, capture policy is {@link C#ALLOW_CAPTURE_BY_ALL} and no flags are set. */ public Builder() { contentType = C.CONTENT_TYPE_UNKNOWN; flags = 0; usage = C.USAGE_MEDIA; + allowedCapturePolicy = C.ALLOW_CAPTURE_BY_ALL; } /** @@ -79,11 +82,15 @@ public final class AudioAttributes { return this; } - /** - * Creates an {@link AudioAttributes} instance from this builder. - */ + /** See {@link android.media.AudioAttributes.Builder#setAllowedCapturePolicy(int)}. */ + public Builder setAllowedCapturePolicy(@C.AudioAllowedCapturePolicy int allowedCapturePolicy) { + this.allowedCapturePolicy = allowedCapturePolicy; + return this; + } + + /** Creates an {@link AudioAttributes} instance from this builder. */ public AudioAttributes build() { - return new AudioAttributes(contentType, flags, usage); + return new AudioAttributes(contentType, flags, usage, allowedCapturePolicy); } } @@ -91,24 +98,38 @@ public final class AudioAttributes { public final @C.AudioContentType int contentType; public final @C.AudioFlags int flags; public final @C.AudioUsage int usage; + public final @C.AudioAllowedCapturePolicy int allowedCapturePolicy; @Nullable private android.media.AudioAttributes audioAttributesV21; - private AudioAttributes(@C.AudioContentType int contentType, @C.AudioFlags int flags, - @C.AudioUsage int usage) { + private AudioAttributes( + @C.AudioContentType int contentType, + @C.AudioFlags int flags, + @C.AudioUsage int usage, + @C.AudioAllowedCapturePolicy int allowedCapturePolicy) { this.contentType = contentType; this.flags = flags; this.usage = usage; + this.allowedCapturePolicy = allowedCapturePolicy; } + /** + * Returns a {@link android.media.AudioAttributes} from this instance. + * + *

Field {@link AudioAttributes#allowedCapturePolicy} is ignored for API levels prior to 29. + */ @TargetApi(21) public android.media.AudioAttributes getAudioAttributesV21() { if (audioAttributesV21 == null) { - audioAttributesV21 = new android.media.AudioAttributes.Builder() - .setContentType(contentType) - .setFlags(flags) - .setUsage(usage) - .build(); + android.media.AudioAttributes.Builder builder = + new android.media.AudioAttributes.Builder() + .setContentType(contentType) + .setFlags(flags) + .setUsage(usage); + if (Util.SDK_INT >= 29) { + builder.setAllowedCapturePolicy(allowedCapturePolicy); + } + audioAttributesV21 = builder.build(); } return audioAttributesV21; } @@ -122,8 +143,10 @@ public final class AudioAttributes { return false; } AudioAttributes other = (AudioAttributes) obj; - return this.contentType == other.contentType && this.flags == other.flags - && this.usage == other.usage; + return this.contentType == other.contentType + && this.flags == other.flags + && this.usage == other.usage + && this.allowedCapturePolicy == other.allowedCapturePolicy; } @Override @@ -132,6 +155,7 @@ public final class AudioAttributes { result = 31 * result + contentType; result = 31 * result + flags; result = 31 * result + usage; + result = 31 * result + allowedCapturePolicy; return result; }