From 85a2b0999e45ad2e049995f4eccff86c61faa78a Mon Sep 17 00:00:00 2001 From: TYPCN Date: Fri, 15 May 2015 01:59:24 +0800 Subject: [PATCH] Add live support and fix crashes on window closing --- bilibili/PlayerView.mm | 74 ++++++++++++++++++++----------------- bilibili/WebPages/inject.js | 13 +++++-- 2 files changed, 50 insertions(+), 37 deletions(-) diff --git a/bilibili/PlayerView.mm b/bilibili/PlayerView.mm index 4441496..08db3d6 100644 --- a/bilibili/PlayerView.mm +++ b/bilibili/PlayerView.mm @@ -84,7 +84,6 @@ - (NSString *) md5:(NSString *) input - (void)viewDidLoad { [super viewDidLoad]; - if([vCID isEqualToString:@"LOCALVIDEO"]){ [[[NSApplication sharedApplication] keyWindow] performClose:self]; } @@ -93,7 +92,6 @@ - (void)viewDidLoad { [[[NSApplication sharedApplication] keyWindow] resignKeyWindow]; [self.view.window makeKeyWindow]; [self.view.window makeMainWindow]; - NSRect rect = [[NSScreen mainScreen] visibleFrame]; NSNumber *viewHeight = [NSNumber numberWithFloat:rect.size.height]; NSNumber *viewWidth = [NSNumber numberWithFloat:rect.size.width]; @@ -111,6 +109,9 @@ - (void)viewDidLoad { queue = dispatch_queue_create("mpv", DISPATCH_QUEUE_SERIAL); dispatch_async(queue, ^{ + + NSString *baseAPIUrl = @"http://interface.bilibili.com/playurl?appkey=%@&otype=json&cid=%@&quality=%d&sign=%@"; + if([vCID isEqualToString:@"LOCALVIDEO"]){ if([vUrl length] > 5){ NSDictionary *VideoInfoJson = [self getVideoInfo:vUrl]; @@ -126,31 +127,33 @@ - (void)viewDidLoad { [self.view.window performClose:self]; } return; - } - - [self.textTip setStringValue:@"正在解析视频地址"]; - - // Parse Video URL - - NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"http:/*[^/]+/video/av(\\d+)(/|/index.html|/index_(\\d+).html)?(\\?|#|$)" options:NSRegularExpressionCaseInsensitive error:nil]; - - NSTextCheckingResult *match = [regex firstMatchInString:vUrl options:0 range:NSMakeRange(0, [vUrl length])]; - - NSRange aidRange = [match rangeAtIndex:1]; - - if(aidRange.length > 0){ - vAID = [vUrl substringWithRange:aidRange]; - NSRange pidRange = [match rangeAtIndex:3]; - if(pidRange.length > 0 ){ - vPID = [vUrl substringWithRange:pidRange]; - } + }else if([vUrl containsString:@"live.bilibili"]){ + baseAPIUrl = @"http://live.bilibili.com/api/playurl?appkey=%@&otype=json&cid=%@&quality=%d&sign=%@"; + vAID = @"LIVE"; + vPID = @"LIVE"; }else{ - vAID = @"0"; + NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"http:/*[^/]+/video/av(\\d+)(/|/index.html|/index_(\\d+).html)?(\\?|#|$)" options:NSRegularExpressionCaseInsensitive error:nil]; + + NSTextCheckingResult *match = [regex firstMatchInString:vUrl options:0 range:NSMakeRange(0, [vUrl length])]; + + NSRange aidRange = [match rangeAtIndex:1]; + + if(aidRange.length > 0){ + vAID = [vUrl substringWithRange:aidRange]; + NSRange pidRange = [match rangeAtIndex:3]; + if(pidRange.length > 0 ){ + vPID = [vUrl substringWithRange:pidRange]; + } + }else{ + vAID = @"0"; + } + + if(![vPID length]){ + vPID = @"1"; + } } - if(![vPID length]){ - vPID = @"1"; - } + [self.textTip setStringValue:@"正在解析视频地址"]; // Get Sign int quality = [self getSettings:@"quality"]; @@ -160,7 +163,7 @@ - (void)viewDidLoad { // Get Playback URL - NSURL* URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://interface.bilibili.com/playurl?appkey=%@&otype=json&cid=%@&quality=%d&sign=%@",APIKey,vCID,quality,sign]]; + NSURL* URL = [NSURL URLWithString:[NSString stringWithFormat:baseAPIUrl,APIKey,vCID,quality,sign]]; NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:URL]; request.HTTPMethod = @"GET"; request.timeoutInterval = 5; @@ -256,8 +259,13 @@ - (void)viewDidLoad { // Get Comment NSNumber *width = [VideoInfoJson objectForKey:@"width"]; NSNumber *height = [VideoInfoJson objectForKey:@"height"]; - NSString *commentFile = [self getComments:width :height]; - [self PlayVideo:commentFile :res]; + if([vUrl containsString:@"live_"]){ + [self PlayVideo:@"" :res]; + }else{ + NSString *commentFile = [self getComments:width :height]; + [self PlayVideo:commentFile :res]; + } + }else{ [self.textTip setStringValue:@"视频信息读取失败"]; parsing = false; @@ -294,10 +302,10 @@ - (void)PlayVideo:(NSString*) commentFile :(NSString*)res{ check_error(mpv_set_option_string(mpv, "user-agent", [@"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2 Fengfan/1.0" cStringUsingEncoding:NSUTF8StringEncoding])); check_error(mpv_set_option_string(mpv, "framedrop", "vo")); check_error(mpv_set_option_string(mpv, "vf", "lavfi=\"fps=fps=60:round=down\"")); - - check_error(mpv_set_option_string(mpv, "sub-ass", "yes")); - check_error(mpv_set_option_string(mpv, "sub-file", [commentFile cStringUsingEncoding:NSUTF8StringEncoding])); - + if(![vUrl containsString:@"live_"]){ + check_error(mpv_set_option_string(mpv, "sub-ass", "yes")); + check_error(mpv_set_option_string(mpv, "sub-file", [commentFile cStringUsingEncoding:NSUTF8StringEncoding])); + } // request important errors check_error(mpv_request_log_messages(mpv, "warn")); @@ -547,7 +555,7 @@ @implementation PlayerWindow{ BOOL isFirstCall = YES; - (NSArray *)customWindowsToEnterFullScreenForWindow:(NSWindow *)window{ - return [NSArray arrayWithObject: window]; + return nil; } - (void)window:(NSWindow *)window @@ -662,7 +670,7 @@ - (BOOL)windowShouldClose:(id)sender{ obServer = NO; } dispatch_async(dispatch_get_main_queue(), ^{ - mpv_set_wakeup_callback(mpv, NULL,NULL); + //mpv_set_wakeup_callback(mpv, NULL,NULL); [self mpv_stop]; [self mpv_quit]; diff --git a/bilibili/WebPages/inject.js b/bilibili/WebPages/inject.js index f1b0b22..efeccd8 100644 --- a/bilibili/WebPages/inject.js +++ b/bilibili/WebPages/inject.js @@ -13,7 +13,9 @@ if(window.location.href.indexOf("av") > 1 || window.location.href.indexOf("live" if(!fv){ fv=$('embed').attr('flashvars'); } - + if(!fv){ + fv = 'cid=' + ROOMID; + } var re = /cid=(\d+)&/; var m = re.exec(fv); var TYPCN_PLAYER_CID = m[1]; @@ -23,14 +25,17 @@ if(window.location.href.indexOf("av") > 1 || window.location.href.indexOf("live" } if(TYPCN_PLAYER_CID){ - if(window.location.href.indexOf("live/") > 2){ - window.external.playVideoByCID(TYPCN_PLAYER_CID); + if(window.location.origin == 'http://live.bilibili.com'){ + if(ROOMID > 0){ + window.external.playVideoByCID(ROOMID.toString()); + } + }else{ $('#bofqi').html('
请选择操作
'); } }else{ - + } // 注意:仅为统计软件使用人数,保证不会记录您的 IP 地址,且没有开启 nginx 日志