在 YouTube 直播的“直播控制室”中,我可以看到“流状态”视图,其中显示了正在发送到 YouTube 的 RTMP 端点的视频的详细信息。
我点击liveStreams端点以获取流的“状态”,但这仅返回active
,这意味着视频流已成功发送到 YouTube 的 RTMP 端点,但没有有关视频数据或质量的信息。
这些信息是否在 API 中的某处公开?我是否还可以查看有关视频的其他详细信息,例如发送到 YouTube 的比特率、fps 等,以便我可以验证我的编码器是否正常工作?或者是否需要在客户端进行检查,并在视频离开编码器后立即检查视频,然后再到达 RTMP 端点。我正在编写一个 iOS 应用程序,因此在网络上使用“实时控制室”对我来说不是一个可行的解决方案。
这是我在广播端检查直播状态的方法:
- (void)checkStreamStatus {
[self getRequestWithURL:[NSString stringWithFormat:@"https://www.googleapis.com/youtube/v3/liveStreams?part=id,snippet,cdn,status&id=%@", self.liveStreamId] andBlock:^(NSDictionary *responseDict) {
NSLog(@"response: %@", responseDict);
// if stream is active, youtube is receiving data from our encoder
// ready to transition to live
NSArray *items = [responseDict objectForKey:@"items"];
NSDictionary *itemsDict = [items firstObject];
NSDictionary *statusDict = [itemsDict objectForKey:@"status"];
if ([[statusDict objectForKey:@"streamStatus"] isEqualToString:@"active"]) {
NSLog(@"stream ready to go live!");
if (!userIsLive) {
[self goLive]; // transition the broadcastStatus from "testing" to "live"
}
} else {
NSLog(@"keep refreshing, broadcast object not ready on youtube's end");
}
}];
}
getRequestWithURL
只是我创建的用于执行 GET 请求的通用方法:
- (void)getRequestWithURL:(NSString *)urlStr andBlock:(void (^)(NSDictionary *responseDict))completion {
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
[request addValue:[NSString stringWithFormat:@"Bearer %@", [[NSUserDefaults standardUserDefaults] objectForKey:@"accessToken"]] forHTTPHeaderField:@"Authorization"];
[request setHTTPMethod:@"GET"];
// Set the content type
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
[self parseJSONwithData:data andBlock:completion];
}];
}
- (void)parseJSONwithData:(NSData *)data andBlock:(void (^)(NSDictionary * responseDict))completion {
NSError *error = nil;
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
if (error) {
NSLog(@"error: %@", [error localizedDescription]);
}
completion(responseDict);
}
以下是我在消费者方面检查视频质量所做的工作:
我正在使用 Google 的YTPlayerView库。
- (void)notifyDelegateOfYouTubeCallbackUrl: (NSURL *) url {
NSString *action = url.host;
// We know the query can only be of the format http://ytplayer?data=SOMEVALUE,
// so we parse out the value.
NSString *query = url.query;
NSString *data;
if (query) {
data = [query componentsSeparatedByString:@"="][4]; // data here is auto, meaning auto quality
}
...
if ([action isEqual:kYTPlayerCallbackOnPlaybackQualityChange]) {
if ([self.delegate respondsToSelector:@selector(playerView:didChangeToQuality:)]) {
YTPlaybackQuality quality = [YTPlayerView playbackQualityForString:data];
[self.delegate playerView:self didChangeToQuality:quality];
}
...
}
但是质量“自动”似乎不是这个库中支持的质量常数:
// Constants representing playback quality.
NSString static *const kYTPlaybackQualitySmallQuality = @"small";
NSString static *const kYTPlaybackQualityMediumQuality = @"medium";
NSString static *const kYTPlaybackQualityLargeQuality = @"large";
NSString static *const kYTPlaybackQualityHD720Quality = @"hd720";
NSString static *const kYTPlaybackQualityHD1080Quality = @"hd1080";
NSString static *const kYTPlaybackQualityHighResQuality = @"highres";
NSString static *const kYTPlaybackQualityUnknownQuality = @"unknown";
...
@implementation YTPlayerView
...
/**
* Convert a quality value from NSString to the typed enum value.
*
* @param qualityString A string representing playback quality. Ex: "small", "medium", "hd1080".
* @return An enum value representing the playback quality.
*/
+ (YTPlaybackQuality)playbackQualityForString:(NSString *)qualityString {
YTPlaybackQuality quality = kYTPlaybackQualityUnknown;
if ([qualityString isEqualToString:kYTPlaybackQualitySmallQuality]) {
quality = kYTPlaybackQualitySmall;
} else if ([qualityString isEqualToString:kYTPlaybackQualityMediumQuality]) {
quality = kYTPlaybackQualityMedium;
} else if ([qualityString isEqualToString:kYTPlaybackQualityLargeQuality]) {
quality = kYTPlaybackQualityLarge;
} else if ([qualityString isEqualToString:kYTPlaybackQualityHD720Quality]) {
quality = kYTPlaybackQualityHD720;
} else if ([qualityString isEqualToString:kYTPlaybackQualityHD1080Quality]) {
quality = kYTPlaybackQualityHD1080;
} else if ([qualityString isEqualToString:kYTPlaybackQualityHighResQuality]) {
quality = kYTPlaybackQualityHighRes;
}
return quality;
}
我在项目的 GitHub 页面上为此创建了一个问题。