-
Notifications
You must be signed in to change notification settings - Fork 35
/
LegacyPiPCompat.x
415 lines (318 loc) · 14.5 KB
/
LegacyPiPCompat.x
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#import "Header.h"
#import <version.h>
#import <YouTubeHeader/MLAVPlayer.h>
#import <YouTubeHeader/MLHAMQueuePlayer.h>
#import <YouTubeHeader/MLPIPController.h>
#import <YouTubeHeader/MLPlayerPool.h>
#import <YouTubeHeader/MLPlayerPoolImpl.h>
#import <YouTubeHeader/MLVideoDecoderFactory.h>
#import <YouTubeHeader/MLDefaultPlayerViewFactory.h>
#import <YouTubeHeader/YTHotConfig.h>
#import <YouTubeHeader/YTPlayerPIPController.h>
#import <YouTubeHeader/YTBackgroundabilityPolicy.h>
#import <YouTubeHeader/YTPlayerViewControllerConfig.h>
#import <YouTubeHeader/YTSystemNotifications.h>
#import <YouTubeHeader/YTAutonavEndscreenController.h>
#import <YouTubeHeader/YTLiveWatchPlaybackOverlayView.h>
extern BOOL TweakEnabled();
extern BOOL isPictureInPictureActive(MLPIPController *);
BOOL hasSampleBufferPiP = NO;
BOOL isLegacyVersion = NO;
BOOL LegacyPiP() {
return isLegacyVersion ? YES : [[NSUserDefaults standardUserDefaults] boolForKey:CompatibilityModeKey];
}
static void forceRenderViewTypeBase(YTIHamplayerConfig *hamplayerConfig) {
if (!LegacyPiP()) return;
hamplayerConfig.renderViewType = 2;
}
static void forceRenderViewTypeHot(YTIHamplayerHotConfig *hamplayerHotConfig) {
if (!LegacyPiP()) return;
hamplayerHotConfig.renderViewType = 2;
}
static void forceRenderViewType(YTHotConfig *hotConfig) {
YTIHamplayerHotConfig *hamplayerHotConfig = [hotConfig hamplayerHotConfig];
forceRenderViewTypeHot(hamplayerHotConfig);
}
MLPIPController *(*InjectMLPIPController)(void);
YTSystemNotifications *(*InjectYTSystemNotifications)(void);
YTBackgroundabilityPolicy *(*InjectYTBackgroundabilityPolicy)(void);
YTPlayerViewControllerConfig *(*InjectYTPlayerViewControllerConfig)(void);
YTHotConfig *(*InjectYTHotConfig)(void);
%group WithInjection
YTPlayerPIPController *initPlayerPiPControllerIfNeeded(YTPlayerPIPController *controller, id delegate, id parentResponder) {
if (controller) return controller;
controller = [[%c(YTPlayerPIPController) alloc] init];
MLPIPController *pip = InjectMLPIPController();
YTSystemNotifications *systemNotifications = InjectYTSystemNotifications();
YTBackgroundabilityPolicy *bgPolicy = InjectYTBackgroundabilityPolicy();
YTPlayerViewControllerConfig *playerConfig = InjectYTPlayerViewControllerConfig();
[controller setValue:pip forKey:@"_pipController"];
[controller setValue:bgPolicy forKey:@"_backgroundabilityPolicy"];
[controller setValue:playerConfig forKey:@"_config"];
@try {
YTHotConfig *config = InjectYTHotConfig();
[controller setValue:config forKey:@"_hotConfig"];
} @catch (id ex) {}
if (parentResponder) {
@try {
[controller setValue:parentResponder forKey:@"_parentResponder"];
} @catch (id ex) {}
}
[controller setValue:delegate forKey:@"_delegate"];
[bgPolicy addBackgroundabilityPolicyObserver:controller];
[pip addPIPControllerObserver:controller];
[systemNotifications addSystemNotificationsObserver:controller];
return controller;
}
%hook YTPlayerPIPController
- (instancetype)initWithDelegate:(id)delegate {
return initPlayerPiPControllerIfNeeded(%orig, delegate, nil);
}
- (instancetype)initWithDelegate:(id)delegate parentResponder:(id)parentResponder {
return initPlayerPiPControllerIfNeeded(%orig, delegate, parentResponder);
}
%end
%hook YTAutonavEndscreenController
- (instancetype)initWithParentResponder:(id)arg1 config:(id)arg2 imageService:(id)arg3 lastActionController:(id)arg4 reachabilityController:(id)arg5 endscreenDelegate:(id)arg6 {
self = %orig;
if (self && [self valueForKey:@"_pipController"] == nil)
[self setValue:InjectMLPIPController() forKey:@"_pipController"];
return self;
}
- (instancetype)initWithParentResponder:(id)arg1 config:(id)arg2 lastActionController:(id)arg3 reachabilityController:(id)arg4 endscreenDelegate:(id)arg5 {
self = %orig;
if (self && [self valueForKey:@"_pipController"] == nil)
[self setValue:InjectMLPIPController() forKey:@"_pipController"];
return self;
}
%end
%hook MLHAMQueuePlayer
- (instancetype)initWithStickySettings:(MLPlayerStickySettings *)stickySettings playerViewProvider:(MLPlayerPoolImpl *)playerViewProvider playerConfiguration:(void *)playerConfiguration {
self = %orig;
if (self && [self valueForKey:@"_pipController"] == nil)
[self setValue:InjectMLPIPController() forKey:@"_pipController"];
return self;
}
- (instancetype)initWithStickySettings:(MLPlayerStickySettings *)stickySettings playerViewProvider:(MLPlayerPoolImpl *)playerViewProvider playerConfiguration:(void *)playerConfiguration mediaPlayerResources:(id)mediaPlayerResources {
self = %orig;
if (self && [self valueForKey:@"_pipController"] == nil)
[self setValue:InjectMLPIPController() forKey:@"_pipController"];
return self;
}
%end
%hook MLAVPlayer
- (bool)isPictureInPictureActive {
return isPictureInPictureActive(InjectMLPIPController());
}
%end
%hook MLPlayerPoolImpl
- (instancetype)init {
self = %orig;
if (self && [self valueForKey:@"_pipController"] == nil)
[self setValue:InjectMLPIPController() forKey:@"_pipController"];
return self;
}
%end
%hook MLAVPIPPlayerLayerView
- (instancetype)initWithPlaceholderPlayerItem:(AVPlayerItem *)playerItem {
self = %orig;
if (self && [self valueForKey:@"_pipController"] == nil)
[self setValue:InjectMLPIPController() forKey:@"_pipController"];
return self;
}
%end
%hook YTLiveWatchPlaybackOverlayView
- (instancetype)initWithFrame:(CGRect)frame reelModel:(id)reelModel ghostViewManager:(id)ghostViewManager parentResponder:(id)parentResponder {
self = %orig;
if (self && [self valueForKey:@"_pipController"] == nil)
[self setValue:InjectMLPIPController() forKey:@"_pipController"];
return self;
}
%end
%hook YTResumeToHomeController
- (instancetype)init {
self = %orig;
if (!IS_IOS_OR_NEWER(iOS_15_0)) {
MLPIPController *pip = InjectMLPIPController();
[pip addPIPControllerObserver:self];
}
return self;
}
%end
%end
%group Legacy
static MLAVPlayer *makeAVPlayer(id self, MLVideo *video, MLInnerTubePlayerConfig *playerConfig, MLPlayerStickySettings *stickySettings, BOOL gimmeAlloc) {
BOOL externalPlaybackActive = [(MLAVPlayer *)[self valueForKey:@"_activePlayer"] externalPlaybackActive];
MLAVPlayer *player = gimmeAlloc ? [((MLPlayerPool *)self).gimme allocOf:%c(MLAVPlayer)] : [%c(MLAVPlayer) alloc];
player = [player initWithVideo:video playerConfig:playerConfig stickySettings:stickySettings externalPlaybackActive:externalPlaybackActive];
if (stickySettings)
player.rate = stickySettings.rate;
return player;
}
%hook MLPIPController
- (void)activatePiPController {
if (!isPictureInPictureActive(self)) {
AVPictureInPictureController *pip = [self valueForKey:@"_pictureInPictureController"];
if (!pip) {
MLAVPIPPlayerLayerView *avpip = [self valueForKey:@"_AVPlayerView"];
if (avpip) {
AVPlayerLayer *playerLayer = [avpip playerLayer];
pip = [[AVPictureInPictureController alloc] initWithPlayerLayer:playerLayer];
[self setValue:pip forKey:@"_pictureInPictureController"];
pip.delegate = self;
}
}
}
}
- (void)deactivatePiPController {
AVPictureInPictureController *pip = [self valueForKey:@"_pictureInPictureController"];
[pip stopPictureInPicture];
}
%end
%hook MLPlayerPoolImpl
- (id)acquirePlayerForVideo:(MLVideo *)video playerConfig:(MLInnerTubePlayerConfig *)playerConfig stickySettings:(MLPlayerStickySettings *)stickySettings {
return makeAVPlayer(self, video, playerConfig, stickySettings, NO);
}
- (id)acquirePlayerForVideo:(MLVideo *)video playerConfig:(MLInnerTubePlayerConfig *)playerConfig stickySettings:(MLPlayerStickySettings *)stickySettings latencyLogger:(id)latencyLogger {
return makeAVPlayer(self, video, playerConfig, stickySettings, NO);
}
- (id)acquirePlayerForVideo:(MLVideo *)video playerConfig:(MLInnerTubePlayerConfig *)playerConfig stickySettings:(MLPlayerStickySettings *)stickySettings latencyLogger:(id)latencyLogger reloadContext:(id)reloadContext {
return makeAVPlayer(self, video, playerConfig, stickySettings, NO);
}
- (id)acquirePlayerForVideo:(MLVideo *)video playerConfig:(MLInnerTubePlayerConfig *)playerConfig stickySettings:(MLPlayerStickySettings *)stickySettings latencyLogger:(id)latencyLogger reloadContext:(id)reloadContext mediaPlayerResources:(id)mediaPlayerResources {
return makeAVPlayer(self, video, playerConfig, stickySettings, NO);
}
- (MLAVPlayerLayerView *)playerViewForVideo:(MLVideo *)video playerConfig:(MLInnerTubePlayerConfig *)playerConfig {
MLDefaultPlayerViewFactory *factory = [self valueForKey:@"_playerViewFactory"];
return [factory AVPlayerViewForVideo:video playerConfig:playerConfig];
}
- (MLAVPlayerLayerView *)playerViewForVideo:(MLVideo *)video playerConfig:(MLInnerTubePlayerConfig *)playerConfig mediaPlayerResources:(id)mediaPlayerResources {
MLDefaultPlayerViewFactory *factory = [self valueForKey:@"_playerViewFactory"];
return [factory AVPlayerViewForVideo:video playerConfig:playerConfig];
}
- (BOOL)canQueuePlayerPlayVideo:(MLVideo *)video playerConfig:(MLInnerTubePlayerConfig *)playerConfig {
return NO;
}
- (BOOL)canQueuePlayerPlayVideo:(MLVideo *)video playerConfig:(MLInnerTubePlayerConfig *)playerConfig reloadContext:(id)reloadContext {
return NO;
}
%end
%hook MLPlayerPool
- (id)acquirePlayerForVideo:(MLVideo *)video playerConfig:(MLInnerTubePlayerConfig *)playerConfig stickySettings:(MLPlayerStickySettings *)stickySettings {
return makeAVPlayer(self, video, playerConfig, stickySettings, YES);
}
- (id)acquirePlayerForVideo:(MLVideo *)video playerConfig:(MLInnerTubePlayerConfig *)playerConfig stickySettings:(MLPlayerStickySettings *)stickySettings latencyLogger:(id)latencyLogger {
return makeAVPlayer(self, video, playerConfig, stickySettings, YES);
}
- (MLAVPlayerLayerView *)playerViewForVideo:(MLVideo *)video playerConfig:(MLInnerTubePlayerConfig *)playerConfig {
MLDefaultPlayerViewFactory *factory = [self valueForKey:@"_playerViewFactory"];
return [factory AVPlayerViewForVideo:video playerConfig:playerConfig];
}
- (BOOL)canUsePlayerView:(id)playerView forVideo:(MLVideo *)video playerConfig:(MLInnerTubePlayerConfig *)playerConfig {
forceRenderViewTypeBase([playerConfig hamplayerConfig]);
return %orig;
}
- (BOOL)canQueuePlayerPlayVideo:(MLVideo *)video playerConfig:(MLInnerTubePlayerConfig *)playerConfig {
return NO;
}
%end
%hook MLDefaultPlayerViewFactory
- (id)hamPlayerViewForVideo:(MLVideo *)video playerConfig:(MLInnerTubePlayerConfig *)playerConfig {
forceRenderViewType([self valueForKey:@"_hotConfig"]);
forceRenderViewTypeBase([playerConfig hamplayerConfig]);
return %orig;
}
- (BOOL)canUsePlayerView:(id)playerView forVideo:(MLVideo *)video playerConfig:(MLInnerTubePlayerConfig *)playerConfig {
forceRenderViewTypeBase([playerConfig hamplayerConfig]);
return %orig;
}
%end
%hook MLVideoDecoderFactory
- (void)prepareDecoderForFormatDescription:(id)formatDescription delegateQueue:(id)delegateQueue {
forceRenderViewTypeHot([self valueForKey:@"_hotConfig"]);
%orig;
}
- (void)prepareDecoderForFormatDescription:(id)formatDescription setPixelBufferTypeOnlyIfEmpty:(BOOL)setPixelBufferTypeOnlyIfEmpty delegateQueue:(id)delegateQueue {
forceRenderViewTypeHot([self valueForKey:@"_hotConfig"]);
%orig;
}
%end
%end
%group Compat
%hook AVPictureInPictureController
%new(v@:)
- (void)invalidatePlaybackState {}
%new(v@:)
- (void)sampleBufferDisplayLayerDidDisappear {}
%new(v@:)
- (void)sampleBufferDisplayLayerDidAppear {}
%new(v@:{CGSize=dd})
- (void)sampleBufferDisplayLayerRenderSizeDidChangeToSize:(CGSize)size {}
%new(v@:B)
- (void)setRequiresLinearPlayback:(BOOL)linear {}
%new(v@:)
- (void)reloadPrerollAttributes {}
%end
%end
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
%group AVKit_iOS14_2_Up
%hook AVPictureInPictureControllerContentSource
%property (assign) bool hasInitialRenderSize;
- (id)initWithSampleBufferDisplayLayer:(AVSampleBufferDisplayLayer *)sampleBufferDisplayLayer initialRenderSize:(CGSize)initialRenderSize playbackDelegate:(id)playbackDelegate {
self = %orig;
if (self)
self.hasInitialRenderSize = true;
return self;
}
%end
%end
%group AVKit_preiOS14_2
%hook AVPictureInPictureControllerContentSource
%property (assign) bool hasInitialRenderSize;
%new(@@:@{CGSize=dd}@)
- (instancetype)initWithSampleBufferDisplayLayer:(AVSampleBufferDisplayLayer *)sampleBufferDisplayLayer initialRenderSize:(CGSize)initialRenderSize playbackDelegate:(id <AVPictureInPictureSampleBufferPlaybackDelegate>)playbackDelegate {
return [self initWithSampleBufferDisplayLayer:sampleBufferDisplayLayer playbackDelegate:playbackDelegate];
}
%end
%hook AVPictureInPictureController
%new(v@:B)
- (void)setCanStartPictureInPictureAutomaticallyFromInline:(BOOL)canStartFromInline {}
%end
%end
#pragma clang diagnostic pop
%ctor {
if (!TweakEnabled()) return;
NSString *bundlePath = [NSString stringWithFormat:@"%@/Frameworks/Module_Framework.framework", NSBundle.mainBundle.bundlePath];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
if (bundle) {
[bundle load];
bundlePath = [bundlePath stringByAppendingString:@"/Module_Framework"];
}
else bundlePath = NSBundle.mainBundle.executablePath;
MSImageRef ref = MSGetImageByName([bundlePath UTF8String]);
InjectMLPIPController = (MLPIPController *(*)(void))MSFindSymbol(ref, "_InjectMLPIPController");
if (InjectMLPIPController) {
InjectYTSystemNotifications = (YTSystemNotifications *(*)(void))MSFindSymbol(ref, "_InjectYTSystemNotifications");
InjectYTBackgroundabilityPolicy = (YTBackgroundabilityPolicy *(*)(void))MSFindSymbol(ref, "_InjectYTBackgroundabilityPolicy");
InjectYTPlayerViewControllerConfig = (YTPlayerViewControllerConfig *(*)(void))MSFindSymbol(ref, "_InjectYTPlayerViewControllerConfig");
InjectYTHotConfig = (YTHotConfig *(*)(void))MSFindSymbol(ref, "_InjectYTHotConfig");
%init(WithInjection);
} else
hasSampleBufferPiP = IS_IOS_OR_NEWER(iOS_13_0);
if (!IS_IOS_OR_NEWER(iOS_14_0)) {
%init(Compat);
if (!IS_IOS_OR_NEWER(iOS_13_0))
isLegacyVersion = YES;
}
if (LegacyPiP()) {
%init(Legacy);
}
if (!IS_IOS_OR_NEWER(iOS_14_0) || IS_IOS_OR_NEWER(iOS_15_0))
return;
if (IS_IOS_OR_NEWER(iOS_14_2)) {
%init(AVKit_iOS14_2_Up);
} else {
%init(AVKit_preiOS14_2);
}
}