示例#1
0
        /**
         * Sets the layout type for the broadcast. For a description of layout types, see
         * <a href="hhttps://tokbox.com/developer/guides/broadcast/live-streaming/#configuring-video-layout-for-opentok-live-streaming-broadcasts">Configuring
         *  the video layout for OpenTok live streaming broadcasts</a>.
         * @param broadcastId The broadcast ID of the broadcasting session
         *
         * @param layout The BroadcastLayout that defines layout options for the broadcast.
         *
         */
        public void SetBroadcastLayout(string broadcastId, BroadcastLayout layout)
        {
            string url     = string.Format("v2/project/{0}/broadcast/{1}/layout", this.ApiKey, broadcastId);
            var    headers = new Dictionary <string, string> {
                { "Content-type", "application/json" }
            };
            var data = new Dictionary <string, object>();

            if (layout != null)
            {
                if ((layout.Type.Equals(BroadcastLayout.LayoutType.Custom) && String.IsNullOrEmpty(layout.Stylesheet)) ||
                    (!layout.Type.Equals(BroadcastLayout.LayoutType.Custom) && !String.IsNullOrEmpty(layout.Stylesheet)))
                {
                    throw new OpenTokArgumentException("Could not set the layout. Either an invalid JSON or an invalid layout options.");
                }
                else
                {
                    data.Add("type", OpenTokUtils.convertToCamelCase(layout.Type.ToString()));
                    if (layout.Type.Equals(BroadcastLayout.LayoutType.Custom))
                    {
                        data.Add("stylesheet", layout.Stylesheet);
                    }
                }
            }

            Client.Put(url, headers, data);
        }
示例#2
0
        /**
         * Use this method to start a live streaming for an OpenTok session.
         * This broadcasts the session to an HLS (HTTP live streaming) or to RTMP streams.
         * <p>
         * To successfully start broadcasting a session, at least one client must be connected to the session.
         * <p>
         * You can only have one active live streaming broadcast at a time for a session
         * (however, having more than one would not be useful).
         * The live streaming broadcast can target one HLS endpoint and up to five RTMP servers simulteneously for a session.
         * You can only start live streaming for sessions that use the OpenTok Media Router (with the media mode set to routed);
         * you cannot use live streaming with sessions that have the media mode set to relayed OpenTok Media Router. See
         * <a href="https://tokbox.com/developer/guides/create-session/#media-mode">The OpenTok Media Router and media modes.</a>
         * <p>
         * For more information on broadcasting, see the
         * <a href="https://tokbox.com/developer/guides/broadcast/">Broadcast developer guide.</a>
         *
         * @param sessionId The session ID corresponding to the session.
         *
         * @param properties This BroadcastProperties object defines options for the broadcast.
         *
         * @return The Broadcast object. This object includes properties defining the archive, including the archive ID.
         */
        public Broadcast StartBroadcast(string sessionId, Boolean hls = true, List <Rtmp> rtmpList = null, string resolution = null, int maxDuration = 7200, BroadcastLayout layout = null)
        {
            if (String.IsNullOrEmpty(sessionId))
            {
                throw new OpenTokArgumentException("Session not valid");
            }

            if (!String.IsNullOrEmpty(resolution) && resolution != "640x480" && resolution != "1280x720")
            {
                throw new OpenTokArgumentException("Resolution value must be either 640x480 (SD) or 1280x720 (HD).");
            }

            if (maxDuration < 60 || maxDuration > 36000)
            {
                throw new OpenTokArgumentException("MaxDuration value must be between 60 and 36000 (inclusive).");
            }

            if (rtmpList != null && rtmpList.Count() >= 5)
            {
                throw new OpenTokArgumentException("Cannot add more than 5 RTMP properties");
            }

            string url     = string.Format("v2/project/{0}/broadcast", this.ApiKey);
            var    headers = new Dictionary <string, string> {
                { "Content-type", "application/json" }
            };
            var outputs = new Dictionary <string, object>();

            if (hls)
            {
                outputs.Add("hls", new Object());
            }

            if (rtmpList != null)
            {
                outputs.Add("rtmp", rtmpList);
            }

            var data = new Dictionary <string, object>()
            {
                { "sessionId", sessionId },
                { "maxDuration", maxDuration },
                { "outputs", outputs }
            };

            if (!String.IsNullOrEmpty(resolution))
            {
                data.Add("resolution", resolution);
            }

            if (layout != null)
            {
                if ((layout.Type.Equals(BroadcastLayout.LayoutType.Custom) && String.IsNullOrEmpty(layout.Stylesheet)) ||
                    (!layout.Type.Equals(BroadcastLayout.LayoutType.Custom) && !String.IsNullOrEmpty(layout.Stylesheet)))
                {
                    throw new OpenTokArgumentException("Could not set the layout. Either an invalid JSON or an invalid layout options.");
                }
                else
                {
                    if (layout.Type.Equals(BroadcastLayout.LayoutType.Custom))
                    {
                        data.Add("layout", layout);
                    }
                    else
                    {
                        data.Add("layout", new { type = OpenTokUtils.convertToCamelCase(layout.Type.ToString()) });
                    }
                }
            }

            string response = Client.Post(url, headers, data);

            return(OpenTokUtils.GenerateBroadcast(response, ApiKey, ApiSecret, OpenTokServer));
        }
示例#3
0
        public MainModule(OpenTokService opentokService)
        {
            Get["/"] = _ => View["index"];

            Get["/host"] = _ =>
            {
                dynamic locals = new ExpandoObject();

                locals.ApiKey             = opentokService.OpenTok.ApiKey.ToString();
                locals.SessionId          = opentokService.Session.Id;
                locals.Token              = opentokService.Session.GenerateToken(Role.PUBLISHER, 0, null, new List <string> (new string[] { "focus" }));
                locals.InitialBroadcastId = opentokService.broadcastId;
                locals.FocusStreamId      = opentokService.focusStreamId;
                locals.InitialLayout      = OpenTokUtils.convertToCamelCase(opentokService.layout.ToString());

                return(View["host", locals]);
            };

            Get["/participant"] = _ =>
            {
                dynamic locals = new ExpandoObject();

                locals.ApiKey        = opentokService.OpenTok.ApiKey.ToString();
                locals.SessionId     = opentokService.Session.Id;
                locals.Token         = opentokService.Session.GenerateToken();
                locals.FocusStreamId = opentokService.focusStreamId;
                locals.Layout        = OpenTokUtils.convertToCamelCase(opentokService.layout.ToString());

                return(View["participant", locals]);
            };

            Post["/start"] = _ =>
            {
                bool            horizontal  = Request.Form["layout"] == "horizontalPresentation";
                BroadcastLayout layoutType  = new BroadcastLayout(horizontal ? BroadcastLayout.LayoutType.HorizontalPresentation : BroadcastLayout.LayoutType.VerticalPresentation);
                int             maxDuration = 7200;
                if (Request.Form["maxDuration"] != null)
                {
                    maxDuration = int.Parse(Request.Form["maxDuration"]);
                }
                Broadcast broadcast = opentokService.OpenTok.StartBroadcast(
                    opentokService.Session.Id,
                    hls: true,
                    rtmpList: null,
                    resolution: Request.Form["resolution"],
                    maxDuration: maxDuration,
                    layout: layoutType
                    );
                opentokService.broadcastId = broadcast.Id.ToString();
                return(broadcast);
            };

            Get["/stop/{id}"] = parameters =>
            {
                Broadcast broadcast = opentokService.OpenTok.StopBroadcast(parameters.id);
                opentokService.broadcastId = "";
                return(broadcast);
            };

            Get["/broadcast"] = _ =>
            {
                if (!String.IsNullOrEmpty(opentokService.broadcastId))
                {
                    try
                    {
                        Broadcast broadcast = opentokService.OpenTok.GetBroadcast(opentokService.broadcastId);
                        if (broadcast.Status == Broadcast.BroadcastStatus.STARTED)
                        {
                            return(Response.AsRedirect(broadcast.Hls));
                        }
                        else
                        {
                            return(Response.AsText("Broadcast not in progress."));
                        }
                    }
                    catch (Exception ex)
                    {
                        return(Response.AsText("Could not get broadcast " + opentokService.broadcastId + ". Exception Message: " + ex.Message));
                    }
                }
                else
                {
                    return(Response.AsText("There's no broadcast running right now."));
                }
            };

            Get["/broadcast/{id}/layout/{layout}"] = parameters =>
            {
                bool            horizontal = parameters.layout == "horizontalPresentation";
                BroadcastLayout layout     = new BroadcastLayout(horizontal ? BroadcastLayout.LayoutType.HorizontalPresentation : BroadcastLayout.LayoutType.VerticalPresentation);
                opentokService.OpenTok.SetBroadcastLayout(parameters.id, layout);
                return(HttpStatusCode.OK);
            };

            Post["/focus"] = _ =>
            {
                string focusStreamId = Request.Form["focus"];
                opentokService.focusStreamId = focusStreamId;
                StreamList streamList = opentokService.OpenTok.ListStreams(opentokService.Session.Id);
                List <StreamProperties> streamPropertiesList = new List <StreamProperties>();
                foreach (Stream stream in streamList)
                {
                    StreamProperties streamProperties = new StreamProperties(stream.Id, null);
                    if (focusStreamId.Equals(stream.Id))
                    {
                        streamProperties.addLayoutClass("focus");
                    }
                    streamPropertiesList.Add(streamProperties);
                }
                opentokService.OpenTok.SetStreamClassLists(opentokService.Session.Id, streamPropertiesList);
                return(HttpStatusCode.OK);
            };
        }