Exemple #1
0
        public LiveBroadcast GenerateBroadCast()
        {
            //broadcast snippet contains basic info about the broadcast and this is shown on youtube when launched
            LiveBroadcastSnippet broadCastSnippet = new LiveBroadcastSnippet();

            broadCastSnippet.Title       = "Test";
            broadCastSnippet.Description = "A test of the youtube data API";
            //TODO: Add a thumbnail icon
            broadCastSnippet.ScheduledStartTime = DateTime.Now;

            //Applies settings of made for kids and unlisted to snippet i.e broadcast settings
            LiveBroadcastStatus broadCastStatus = new LiveBroadcastStatus();

            broadCastStatus.SelfDeclaredMadeForKids = false;
            broadCastStatus.PrivacyStatus           = "unlisted";

            //enables the broadcast to view a different "special stream" in case of time delays etc good for debugging
            MonitorStreamInfo broadCastMonitorStream = new MonitorStreamInfo();

            broadCastMonitorStream.EnableMonitorStream = true;

            //contains info about the monitor stream
            LiveBroadcastContentDetails broadCastContentDetails = new LiveBroadcastContentDetails();

            broadCastContentDetails.MonitorStream = broadCastMonitorStream;

            LiveBroadcast broadCast = new LiveBroadcast();

            broadCast.Snippet        = broadCastSnippet;        //binds the info regarding stream
            broadCast.Status         = broadCastStatus;         //privacy settings & reg settings
            broadCast.ContentDetails = broadCastContentDetails; //binds monitor info

            //Allows to finalise the setting up and is ready to be transmitted
            LiveBroadcastsResource.InsertRequest liveBroadCastInsert = service.LiveBroadcasts.Insert(broadCast, "snippet,status,contentDetails");
            //Reads the returned var

            LiveBroadcast returnedBroadCast = liveBroadCastInsert.Execute();

            Console.WriteLine("\n================== Returned Broadcast ==================\n");
            Console.WriteLine("  - Id: " + returnedBroadCast.Id);
            Console.WriteLine("  - Title: " + returnedBroadCast.Snippet.Title);
            Console.WriteLine("  - Description: " + returnedBroadCast.Snippet.Description);
            Console.WriteLine("  - Published At: " + returnedBroadCast.Snippet.PublishedAt);
            Console.WriteLine(
                "  - Scheduled Start Time: " + returnedBroadCast.Snippet.ScheduledStartTime);
            Console.WriteLine(
                "  - Scheduled End Time: " + returnedBroadCast.Snippet.ScheduledEndTime);

            return(returnedBroadCast);
        }
Exemple #2
0
        public async Task <ActionResult> IndexAsync(LiveStreamingModel liveModel, CancellationToken cancellationToken)
        {
            if (!ModelState.IsValid)
            {
                JavaScriptSerializer objJavascript = new JavaScriptSerializer();
                liveModel = objJavascript.Deserialize <LiveStreamingModel>(Request.Cookies["liveModel"].Value);
            }
            else
            {
                if (liveModel.Quality != null)
                {
                    HttpCookie newModel = new HttpCookie("liveModel");
                    newModel.Value   = new JavaScriptSerializer().Serialize(liveModel);
                    newModel.Expires = DateTime.Now.AddHours(10);
                    Response.Cookies.Add(newModel);
                }
            }
            var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).
                         AuthorizeAsync(cancellationToken);


            if (result.Credential != null)
            {
                var youtubeService = new YouTubeService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = result.Credential,
                    ApplicationName       = Assembly.GetExecutingAssembly().GetName().Name
                });
                LiveBroadcastSnippet broadcastSnippet = new LiveBroadcastSnippet();
                broadcastSnippet.Title = liveModel.Title;
                broadcastSnippet.ScheduledStartTime = liveModel.StartTimeYoutube.CompareTo(DateTime.Now) < 0 ? (DateTime.Now) : liveModel.StartTimeYoutube;
                broadcastSnippet.ScheduledEndTime   = liveModel.EndTimeYoutube;
                // Set the broadcast's privacy status to "private". See:
                // https://developers.google.com/youtube/v3/live/docs/liveBroadcasts#status.privacyStatus
                LiveBroadcastStatus status = new LiveBroadcastStatus();
                if (liveModel.PrivacyYoutube == EventZoneConstants.publicEvent)
                {
                    status.PrivacyStatus = "public";
                }
                else if (liveModel.PrivacyYoutube == EventZoneConstants.unlistedEvent)
                {
                    status.PrivacyStatus = "unlisted";
                }
                else
                {
                    status.PrivacyStatus = "private";
                }

                //Set LiveBroadcast
                LiveBroadcast broadcast       = new LiveBroadcast();
                LiveBroadcast returnBroadcast = new LiveBroadcast();
                broadcast.Kind    = "youtube#liveBroadcast";
                broadcast.Snippet = broadcastSnippet;
                broadcast.Status  = status;
                LiveBroadcastsResource.InsertRequest liveBroadcastInsert = youtubeService.LiveBroadcasts.Insert(broadcast, "snippet,status");
                try
                {
                    returnBroadcast = liveBroadcastInsert.Execute();
                }
                catch (Exception ex) {
                    TempData["errorTitle"]   = "Error";
                    TempData["errorMessage"] = ex.Message;
                    result.Credential.RevokeTokenAsync(CancellationToken.None).Wait();
                    return(RedirectToAction("Details", "Event", liveModel.eventID));
                }

                //Set LiveStream Snippet
                LiveStreamSnippet streamSnippet = new LiveStreamSnippet();
                streamSnippet.Title = liveModel.Title + "Stream Title";
                CdnSettings cdnSettings = new CdnSettings();
                cdnSettings.Format        = liveModel.Quality;
                cdnSettings.IngestionType = "rtmp";

                //Set LiveStream
                LiveStream streamLive = new LiveStream();
                streamLive.Kind    = "youtube#liveStream";
                streamLive.Snippet = streamSnippet;
                streamLive.Cdn     = cdnSettings;
                LiveStream returnLiveStream = youtubeService.LiveStreams.Insert(streamLive, "snippet,cdn").Execute();
                LiveBroadcastsResource.BindRequest liveBroadcastBind = youtubeService.LiveBroadcasts.Bind(returnBroadcast.Id, "id,contentDetails");
                liveBroadcastBind.StreamId = returnLiveStream.Id;
                try
                {
                    returnBroadcast = liveBroadcastBind.Execute();
                }
                catch (Exception ex)
                {
                    TempData["errorTitle"]   = "Error";
                    TempData["errorMessage"] = ex.Message;
                    result.Credential.RevokeTokenAsync(CancellationToken.None).Wait();
                    return(RedirectToAction("Details", "Event", liveModel.eventID));
                }

                //Return Value
                String streamName       = returnLiveStream.Cdn.IngestionInfo.StreamName;
                String primaryServerUrl = returnLiveStream.Cdn.IngestionInfo.IngestionAddress;
                String backupServerUrl  = returnLiveStream.Cdn.IngestionInfo.BackupIngestionAddress;
                String youtubeUrl       = "https://www.youtube.com/watch?v=" + returnBroadcast.Id;

                //youtubeReturnModel model = new youtubeReturnModel { streamName = streamName, primaryServerUrl = primaryServerUrl,backupServerUrl=backupServerUrl,youtubeUrl=youtubeUrl };
                Video video = new Video {
                    EventPlaceID  = liveModel.EventPlaceID,
                    VideoLink     = youtubeUrl,
                    PrimaryServer = primaryServerUrl,
                    StartTime     = liveModel.StartTimeYoutube,
                    Privacy       = liveModel.PrivacyYoutube,
                    EndTime       = liveModel.EndTimeYoutube,
                    BackupServer  = backupServerUrl,
                    StreamName    = streamName
                };
                EventDatabaseHelper.Instance.AddVideo(video);

                HttpCookie newEventID = new HttpCookie("CreateEventID");
                newEventID.Expires = DateTime.Now.AddDays(-1);
                Request.Cookies.Add(newEventID);
                HttpCookie newModel = new HttpCookie("liveModel");
                newModel.Value   = new JavaScriptSerializer().Serialize(liveModel);
                newModel.Expires = DateTime.Now.AddHours(-1);
                Response.Cookies.Add(newModel);
                result.Credential.RevokeTokenAsync(CancellationToken.None).Wait();
                return(RedirectToAction("Details", "Event", new { id = EventDatabaseHelper.Instance.GetEventPlaceByID(liveModel.EventPlaceID).EventID }));
            }
            else
            {
                return(new RedirectResult(result.RedirectUri));
            }
        }