YouTube Live API流状态和质量回调

时间:2014-12-04 20:58:28

标签: youtube-api rtmp youtube-data-api youtube-livestreaming-api ytplayerview

在" Live Control Room"在YouTube直播中,我可以看到"流状态"该视图显示了发送到YouTube的RTMP端点的视频详情。Stream Status

我点击了liveStreams端点以获得"状态"流,但只返回active,这意味着视频流已成功发送到YouTube的RTMP端点,但没有关于视频数据或质量的信息。

此信息是否在API的某处公开?我是否还可以查看有关视频的其他详细信息,例如发送到YouTube的比特率,fps等,以便验证我的编码器是否正常工作?或者是否需要在客户端进行检查,并在点击RTMP端点之前立即检查视频。我正在编写iOS应用程序,所以使用" Live Control Room"在网上对我来说不是一个可行的解决方案。

这是我在广播方面做的事情,以检查liveStream状态:

- (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页面上为此创建了issue

2 个答案:

答案 0 :(得分:1)

我收到了Ibrahim Ulukaya关于此问题的回复:

  

我们希望能够获得更多有关该通话的信息​​,但基本上是有效的,表示良好的流媒体,您的流媒体信息是https://developers.google.com/youtube/v3/live/docs/liveStreams#cdn.format您设置的地方,并且可以看到格式。

因此暂时的答案是否定的,暂时无法从YouTube直播API获取此信息。如果/更新API,我会更新此答案。

答案 1 :(得分:0)

似乎Youtube Live流API已更新为显示具有此属性的实时流健康状态:status.healthStatus.status

有关详细信息,请参阅their latest API