Skip to content

Commit

Permalink
fix(YouTube - Spoof streaming data): Videos end 1 second early on iOS…
Browse files Browse the repository at this point in the history
… client
  • Loading branch information
inotia00 committed Dec 17, 2024
1 parent bb1946d commit e0b6d33
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@

import androidx.annotation.Nullable;

import com.google.android.libraries.youtube.innertube.model.media.FormatStreamModel;
import com.google.protos.youtube.api.innertube.StreamingDataOuterClass$StreamingData;

import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import app.revanced.extension.shared.patches.BlockRequestPatch;
import app.revanced.extension.shared.patches.client.AppClient.ClientType;
import app.revanced.extension.shared.utils.Logger;
import app.revanced.extension.shared.utils.Utils;
import app.revanced.extension.shared.settings.BaseSettings;
Expand All @@ -17,6 +23,34 @@
@SuppressWarnings("unused")
public class SpoofStreamingDataPatch extends BlockRequestPatch {

/**
* key: videoId
* value: android StreamingData
*/
private static final Map<String, StreamingDataOuterClass$StreamingData> streamingDataMap = Collections.synchronizedMap(
new LinkedHashMap<>(10) {
private static final int CACHE_LIMIT = 5;

@Override
protected boolean removeEldestEntry(Entry eldest) {
return size() > CACHE_LIMIT; // Evict the oldest entry if over the cache limit.
}
});

/**
* key: android StreamingData
* value: fetched ClientType
*/
private static final Map<StreamingDataOuterClass$StreamingData, ClientType> clientTypeMap = Collections.synchronizedMap(
new LinkedHashMap<>(10) {
private static final int CACHE_LIMIT = 5;

@Override
protected boolean removeEldestEntry(Entry eldest) {
return size() > CACHE_LIMIT; // Evict the oldest entry if over the cache limit.
}
});

/**
* Injection point.
*/
Expand Down Expand Up @@ -66,9 +100,11 @@ public static void fetchStreams(String url, Map<String, String> requestHeaders)
* Injection point.
* Fix playback by replace the streaming data.
* Called after {@link #fetchStreams(String, Map)}.
*
* @param originalStreamingData Original StreamingData.
*/
@Nullable
public static ByteBuffer getStreamingData(String videoId) {
public static ByteBuffer getStreamingData(String videoId, StreamingDataOuterClass$StreamingData originalStreamingData) {
if (SPOOF_STREAMING_DATA) {
try {
StreamingDataRequest request = StreamingDataRequest.getRequestForVideoId(videoId);
Expand All @@ -85,7 +121,11 @@ public static ByteBuffer getStreamingData(String videoId) {
var stream = request.getStream();
if (stream != null) {
Logger.printDebug(() -> "Overriding video stream: " + videoId);
return stream;
// Put the videoId, originalStreamingData, and the clientType used for spoofing into a HashMap.
streamingDataMap.put(videoId, originalStreamingData);
clientTypeMap.put(originalStreamingData, stream.second);

return stream.first;
}
}

Expand All @@ -100,7 +140,42 @@ public static ByteBuffer getStreamingData(String videoId) {

/**
* Injection point.
* Called after {@link #getStreamingData(String)}.
* <p>
* It seems that some 'adaptiveFormats' are missing from the initial response of streaming data on iOS.
* Since the {@link FormatStreamModel} class for measuring the video length is not initialized on iOS clients,
* The video length field is always initialized to an estimated value, not the actual value.
* <p>
* To fix this, replace streamingData (spoofedStreamingData) with originalStreamingData, which is only used to initialize the {@link FormatStreamModel} class to measure the video length.
* <p>
* Called after {@link #getStreamingData(String, StreamingDataOuterClass$StreamingData)}.
*
* @param spoofedStreamingData Spoofed StreamingData.
*/
public static StreamingDataOuterClass$StreamingData getOriginalStreamingData(String videoId, StreamingDataOuterClass$StreamingData spoofedStreamingData) {
if (SPOOF_STREAMING_DATA) {
try {
StreamingDataOuterClass$StreamingData androidStreamingData = streamingDataMap.get(videoId);
if (androidStreamingData != null) {
ClientType clientType = clientTypeMap.get(androidStreamingData);
if (clientType == ClientType.IOS) {
Logger.printDebug(() -> "Overriding iOS streaming data to original streaming data: " + videoId);
return androidStreamingData;
} else {
Logger.printDebug(() -> "Not overriding original streaming data as spoofed client is not iOS: " + videoId + " (" + clientType + ")");
}
} else {
Logger.printDebug(() -> "Not overriding original streaming data (original streaming data is null): " + videoId);
}
} catch (Exception ex) {
Logger.printException(() -> "getOriginalStreamingData failure", ex);
}
}
return spoofedStreamingData;
}

/**
* Injection point.
* Called after {@link #getStreamingData(String, StreamingDataOuterClass$StreamingData)}.
*/
@Nullable
public static byte[] removeVideoPlaybackPostBody(Uri uri, int method, byte[] postData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static app.revanced.extension.shared.patches.spoof.requests.PlayerRoutes.GET_STREAMING_DATA;

import android.util.Pair;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

Expand Down Expand Up @@ -93,7 +95,7 @@ public static String getLastSpoofedClientName() {
}

private final String videoId;
private final Future<ByteBuffer> future;
private final Future<Pair<ByteBuffer, ClientType>> future;

private StreamingDataRequest(String videoId, Map<String, String> playerHeaders) {
Objects.requireNonNull(playerHeaders);
Expand Down Expand Up @@ -170,7 +172,7 @@ private static HttpURLConnection send(ClientType clientType, String videoId,
return null;
}

private static ByteBuffer fetch(String videoId, Map<String, String> playerHeaders) {
private static Pair<ByteBuffer, ClientType> fetch(String videoId, Map<String, String> playerHeaders) {
lastSpoofedClientType = null;

// Retry with different client if empty response body is received.
Expand All @@ -193,7 +195,7 @@ private static ByteBuffer fetch(String videoId, Map<String, String> playerHeader
}
lastSpoofedClientType = clientType;

return ByteBuffer.wrap(baos.toByteArray());
return new Pair<>(ByteBuffer.wrap(baos.toByteArray()), clientType);
}
}
} catch (IOException ex) {
Expand All @@ -211,7 +213,7 @@ public boolean fetchCompleted() {
}

@Nullable
public ByteBuffer getStream() {
public Pair<ByteBuffer, ClientType> getStream() {
try {
return future.get(MAX_MILLISECONDS_TO_WAIT_FOR_FETCH, TimeUnit.MILLISECONDS);
} catch (TimeoutException ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.google.android.libraries.youtube.innertube.model.media;

public class FormatStreamModel {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.google.protos.youtube.api.innertube;

public class StreamingDataOuterClass$StreamingData {
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import com.android.tools.smali.dexlib2.immutable.ImmutableMethodParameter
const val EXTENSION_CLASS_DESCRIPTOR =
"$SPOOF_PATH/SpoofStreamingDataPatch;"

// In YouTube 17.34.36, this class is obfuscated.
const val STREAMING_DATA_INTERFACE =
"Lcom/google/protos/youtube/api/innertube/StreamingDataOuterClass${'$'}StreamingData;"

fun baseSpoofStreamingDataPatch(
block: BytecodePatchBuilder.() -> Unit = {},
executeBlock: BytecodePatchContext.() -> Unit = {},
Expand Down Expand Up @@ -130,7 +134,8 @@ fun baseSpoofStreamingDataPatch(
if-eqz v2, :disabled
# Get streaming data.
invoke-static { v2 }, $EXTENSION_CLASS_DESCRIPTOR->getStreamingData(Ljava/lang/String;)Ljava/nio/ByteBuffer;
iget-object v6, p0, $setStreamingDataField
invoke-static { v2, v6 }, $EXTENSION_CLASS_DESCRIPTOR->getStreamingData(Ljava/lang/String;$STREAMING_DATA_INTERFACE)Ljava/nio/ByteBuffer;
move-result-object v3
if-eqz v3, :disabled
Expand All @@ -154,6 +159,39 @@ fun baseSpoofStreamingDataPatch(
}
}

videoStreamingDataConstructorFingerprint.methodOrThrow(videoStreamingDataToStringFingerprint).apply {
val formatStreamModelInitIndex = indexOfFormatStreamModelInitInstruction(this)
val getVideoIdIndex = indexOfFirstInstructionReversedOrThrow(formatStreamModelInitIndex) {
val reference = getReference<FieldReference>()
opcode == Opcode.IGET_OBJECT &&
reference?.type == "Ljava/lang/String;" &&
reference.definingClass == definingClass
}
val getVideoIdReference = getInstruction<ReferenceInstruction>(getVideoIdIndex).reference
val insertIndex = indexOfFirstInstructionReversedOrThrow(getVideoIdIndex) {
opcode == Opcode.IGET_OBJECT &&
getReference<FieldReference>()?.definingClass == STREAMING_DATA_INTERFACE
}

val (freeRegister, streamingDataRegister) = with(getInstruction<TwoRegisterInstruction>(insertIndex)) {
Pair(registerA, registerB)
}
val definingClassRegister = getInstruction<TwoRegisterInstruction>(getVideoIdIndex).registerB
val insertReference = getInstruction<ReferenceInstruction>(insertIndex).reference

replaceInstruction(
insertIndex,
"iget-object v$freeRegister, v$freeRegister, $insertReference"
)
addInstructions(
insertIndex, """
iget-object v$freeRegister, v$definingClassRegister, $getVideoIdReference
invoke-static { v$freeRegister, v$streamingDataRegister }, $EXTENSION_CLASS_DESCRIPTOR->getOriginalStreamingData(Ljava/lang/String;$STREAMING_DATA_INTERFACE)$STREAMING_DATA_INTERFACE
move-result-object v$freeRegister
"""
)
}

// endregion

// region Remove /videoplayback request body to fix playback.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,34 @@ internal val protobufClassParseByteBufferFingerprint = legacyFingerprint(
customFingerprint = { method, _ -> method.name == "parseFrom" },
)

internal val videoStreamingDataConstructorFingerprint = legacyFingerprint(
name = "videoStreamingDataConstructorFingerprint",
accessFlags = AccessFlags.PUBLIC or AccessFlags.CONSTRUCTOR,
returnType = "V",
customFingerprint = { method, _ ->
indexOfFormatStreamModelInitInstruction(method) >= 0
},
)

internal fun indexOfFormatStreamModelInitInstruction(method: Method) =
method.indexOfFirstInstruction {
val reference = getReference<MethodReference>()
opcode == Opcode.INVOKE_DIRECT &&
reference?.name == "<init>" &&
reference.parameterTypes.size > 1
}

internal val videoStreamingDataToStringFingerprint = legacyFingerprint(
name = "videoStreamingDataToStringFingerprint",
returnType = "Ljava/lang/String;",
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
parameters = emptyList(),
strings = listOf("VideoStreamingData(itags="),
customFingerprint = { method, _ ->
method.name == "toString"
},
)

internal const val HLS_CURRENT_TIME_FEATURE_FLAG = 45355374L

internal val hlsCurrentTimeFingerprint = legacyFingerprint(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1905,7 +1905,7 @@ Tap on the continue button and disable battery optimizations."</string>
<string name="revanced_spoof_streaming_data_type_entry_android_unplugged">Android TV</string>
<string name="revanced_spoof_streaming_data_type_entry_android_vr">Android VR</string>
<string name="revanced_spoof_streaming_data_side_effects_title">Spoofing side effects</string>
<string name="revanced_spoof_streaming_data_side_effects_ios">• Videos may end 1 second early.</string>
<string name="revanced_spoof_streaming_data_side_effects_ios">• Not yet found.</string>
<string name="revanced_spoof_streaming_data_side_effects_android_unplugged">"• Audio track menu is missing.
• Stable volume is not available."</string>
<string name="revanced_spoof_streaming_data_side_effects_android_vr">"• Audio track menu is missing.
Expand Down

0 comments on commit e0b6d33

Please sign in to comment.