/// <summary> /// DMCサーバー上の動画を再生するためのリクエストを送信し、 /// 動画再生のセッション情報を取得します。 /// </summary> /// <param name="watchData"></param> /// <param name="videoQuality"></param> /// <param name="audioQuality"></param> /// <returns></returns> public Task <Dmc.DmcSessionResponse> GetDmcSessionResponse( Dmc.DmcWatchResponse watchData, Dmc.VideoContent videoQuality = null, Dmc.AudioContent audioQuality = null, bool hlsMode = false ) { return(Dmc.DmcClient.GetDmcSessionResponseAsync(_context, watchData, videoQuality, audioQuality, hlsMode)); }
public static Task <DmcSessionResponse> GetDmcSessionResponseAsync( NiconicoContext context, DmcWatchResponse watchData, VideoContent videoQuality = null, AudioContent audioQuality = null, bool hlsMode = false ) { return(GetDmcSessionResponseDataAsync(context, watchData, videoQuality, audioQuality, hlsMode) .ContinueWith(prevTask => ParseDmcSessionResponse(prevTask.Result))); }
public static async Task <string> GetDmcSessionResponseDataAsync( NiconicoContext context, DmcWatchResponse watchData, VideoContent videoQuality = null, AudioContent audioQuality = null, bool hlsMode = false ) { var req = new DmcSessionRequest(); var session = watchData.Media.Delivery.Movie.Session; var videoQualities = watchData.Media.Delivery.Movie.Videos; var audioQualities = watchData.Media.Delivery.Movie.Audios; var encryption = watchData.Media.Delivery.Encryption; // リクエストする動画品質を決定します // モバイルの時は最後の動画品質をモバイル画質として断定して指定 // それ以外の場合、対象画質とそれ以下の有効な画質をすべて指定 var requestVideoQuality = new List <string>(); if (videoQuality?.IsAvailable ?? false) { requestVideoQuality.Add(videoQuality.Id); } else { var fallbackVideoQuality = videoQualities.Last(x => x.Metadata.LevelIndex == 0); requestVideoQuality.Add(fallbackVideoQuality.Id); } var requestAudioQuality = new List <string>(); if (audioQuality?.IsAvailable ?? false) { requestAudioQuality.Add(audioQuality.Id); } else { var fallbackAudioQuality = audioQualities.Last(x => x.Metadata.LevelIndex == 0); requestAudioQuality.Add(fallbackAudioQuality.Id); } var sessionUrl = $"{session.Urls[0].UrlUnsafe}?_format=json"; var useSsl = true; // session.Urls[0].IsSsl; var wellKnownPort = session.Urls[0].IsWellKnownPort; var protocolName = session.Protocols[0]; // http,hls var protocolAuthType = session.Protocols.ElementAtOrDefault(1) ?? "ht2"; // ht2 req.Session = new RequestSession() { RecipeId = session.RecipeId, ContentId = session.ContentId, ContentType = "movie", ContentSrcIdSets = new List <ContentSrcIdSet>() { new ContentSrcIdSet() { ContentSrcIds = new List <ContentSrcId>() { new ContentSrcId() { SrcIdToMux = new SrcIdToMux() { VideoSrcIds = requestVideoQuality, AudioSrcIds = requestAudioQuality }, } } } }, TimingConstraint = "unlimited", KeepMethod = new KeepMethod() { Heartbeat = new Heartbeat() { Lifetime = 120000 } }, Protocol = new Protocol() { Name = "http", Parameters = new Protocol.ProtocolParameters() { HttpParameters = new Protocol.HttpParameters() { Parameters = new Protocol.ParametersInfo() { HlsParameters = protocolName == "hls" ? new Protocol.HlsParameters() { UseSsl = useSsl ? "yes" : "no", UseWellKnownPort = wellKnownPort ? "yes" : "no", SegmentDuration = 5000, TransferPreset = "", Encryption = encryption != null ? new Protocol.Encryption() { HlsEncryptionV1 = new Protocol.HlsEncryptionV1() { EncryptedKey = encryption.EncryptedKey, KeyUri = encryption.KeyUri } } : null } : null , HttpOutputDownloadParameters = protocolName == "http" ? new Protocol.HttpOutputDownloadParameters() : null } } } }, ContentUri = "", SessionOperationAuth = new SessionOperationAuth_Request() { SessionOperationAuthBySignature = new SessionOperationAuthBySignature_Request() { Token = session.Token, Signature = session.Signature } }, ContentAuth = new ContentAuth_Request() { AuthType = "ht2", ContentKeyTimeout = (int)session.ContentKeyTimeout, ServiceId = "nicovideo", ServiceUserId = session.ServiceUserId }, ClientInfo = new ClientInfo() { PlayerId = session.PlayerId }, Priority = session.Priority }; var requestJson = JsonConvert.SerializeObject(req, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); // var decodedJson = WebUtility.HtmlEncode(requestJson); var decodedJson = requestJson; #if WINDOWS_UWP return(await context.PostAsync(sessionUrl, new HttpStringContent(decodedJson, Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json"))); #else return(await context.PostAsync(sessionUrl, new StringContent(decodedJson, UnicodeEncoding.UTF8, "application/json"))); #endif }