Class: OpenTok::Broadcasts

Inherits:
Object
  • Object
show all
Defined in:
lib/opentok/broadcasts.rb

Overview

A class for working with OpenTok live streaming broadcasts. See / Live streaming broadcasts.

Instance Method Summary collapse

Instance Method Details

#add_stream(broadcast_id, stream_id, options) ⇒ Object

Adds a stream to currently running broadcast that was started with the streamMode set to “manual”. For a description of the feature, see https://tokbox.com/developer/rest/#selecting-broadcast-streams.

You can call the method repeatedly with add_stream set to the same stream ID, to toggle the stream’s audio or video in the broadcast. If you set both has_audio and has_video to false, you will get error response.

Parameters:

  • broadcast_id (String)

    The broadcast ID.

  • stream_id (String)

    The ID for the stream to be added to the broadcast

  • opts (Hash)

    a customizable set of options

Raises:

  • (ArgumentError)

    The broadcast_id parameter is empty.

  • (ArgumentError)

    The stream_id parameter is empty.

  • (ArgumentError)

    The has_audio and has_video properties of the options parameter are both set to “false”



267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/opentok/broadcasts.rb', line 267

def add_stream(broadcast_id, stream_id, options)
  raise ArgumentError, "broadcast_id not provided" if broadcast_id.to_s.empty?
  raise ArgumentError, "stream_id not provided" if stream_id.to_s.empty?
  if options.has_key?(:has_audio) && options.has_key?(:has_video)
    has_audio = options[:has_audio]
    has_video = options[:has_video]
    raise ArgumentError, "has_audio and has_video can't both be false" if audio_and_video_options_both_false?(has_audio, has_video)
  end
  options['add_stream'] = stream_id

  @client.select_streams_for_broadcast(broadcast_id, options)
end

#all(options = {}) ⇒ BroadcastList

Returns a BroadcastList, which is an array of broadcasts that are completed and in-progress, for your API key.

tokbox.com/developer/rest/#list_broadcasts

Parameters:

  • options (Hash) (defaults to: {})

    A hash with keys defining which range of broadcasts to retrieve.

Options Hash (options):

  • :offset (integer)

    Optional. The index offset of the first broadcast. 0 is offset of the most recently started broadcast. 1 is the offset of the broadcast that started prior to the most recent broadcast. If you do not specify an offset, 0 is used.

  • :count (integer)

    Optional. The number of broadcasts to be returned. The maximum number of broadcasts returned is 1000.

  • :session_id (String)

    Optional. The session ID that broadcasts belong to.

Returns:

  • (BroadcastList)

    An BroadcastList object, which is an array of Broadcast objects.

Raises:

  • (ArgumentError)


156
157
158
159
160
161
# File 'lib/opentok/broadcasts.rb', line 156

def all(options = {})
  raise ArgumentError, "Limit is invalid" unless options[:count].nil? || (0..1000).include?(options[:count])

  broadcast_list_json = @client.list_broadcasts(options[:offset], options[:count], options[:sessionId])
  BroadcastList.new self, broadcast_list_json
end

#create(session_id, options = {}) ⇒ Broadcast

Starts a live streaming broadcast of an OpenTok session.

Clients must be actively connected to the OpenTok session for you to successfully start a broadcast.

This broadcasts the session to an HLS (HTTP live streaming) or to RTMP streams.

Broadcasts#find method.

Parameters:

  • session_id (String)

    The session ID of the OpenTok session to broadcast.

  • options (Hash) (defaults to: {})

    A hash defining options for the broadcast.

Options Hash (options):

  • :layout (Hash)

    Specify this to assign the initial layout type for the broadcast. This is a hash containing three keys: :type, :stylesheet<code> and <code>:screenshare_type. Valid values for :type are “bestFit” (best fit), “custom” (custom),

    "horizontalPresentation" (horizontal presentation),
    

    “pip” (picture-in-picture), and “verticalPresentation” (vertical presentation)). If you specify a “custom” layout type, set the :stylesheet key to the stylesheet (CSS). (For other layout types, do not set the :stylesheet key.) Valid values for :screenshare_type are “bestFit”, “pip”, “verticalPresentation”, “horizontalPresentation”. This property is optional. If it is specified, then the :type property must be set to “bestFit”. If you do not specify an initial layout type, the broadcast uses the best fit layout type.

  • :multiBroadcastTag (String) — default: Optional

    Set this to support multiple broadcasts for the same session simultaneously. Set this to a unique string for each simultaneous broadcast of an ongoing session. Note that the multiBroadcastTag value is not included in the response for the methods to list live streaming broadcasts and get information about a live streaming broadcast. See Simultaneous broadcasts.

  • maxBitRate (int)

    The maximum bitrate for the broadcast stream(s), in bits per second. The minimum value is 100,000 and the maximum is 6,000,000.

  • maxDuration (int)

    The maximum duration for the broadcast, in seconds. The broadcast will automatically stop when the maximum duration is reached. You can set the maximum duration to a value from 60 (60 seconds) to 36000 (10 hours). The default maximum duration is 4 hours (14,400 seconds).

  • outputs (Hash) — default: Required

    This object defines the types of broadcast streams you want to start (both HLS and RTMP). You can include HLS, RTMP, or both as broadcast streams. If you include RTMP streaming, you can specify up to five target RTMP streams (or just one).

    For multiple RTMP streams, the (:rtmp) property is set to an [Array] of Rtmp [Hash] objects. For each RTMP hash, specify (:serverUrl) for the RTMP server URL, (:streamName) such as the YouTube Live stream name or the Facebook stream key), and (optionally) (:id), a unique ID for the stream. If you specify an ID, it will be included in the (broadcast_json) response from the Client#start_broadcast method call, and is also available in the (broadcast_json) response from the Broadcasts#find method. Vonage streams the session to each RTMP URL you specify. Note that OpenTok live streaming supports RTMP and RTMPS. If you need to support only one RTMP URL, you can set a Rtmp [Hash] object (instead of an array of objects) for the (:rtmp) property value in the (:outputs) [Hash].

    For HLS, the (:hls) property in the (:outputs) [Hash] is set to a HLS [Hash] object. This object includes the following optional properties:

    • (:dvr) (Boolean). Whether to enable DVR functionality (rewinding, pausing, and resuming) in players that support it (true), or not (false, the default). With DVR enabled, the HLS URL will include a ?DVR query string appended to the end.

    • (:low_latency) (Boolean). Whether to enable low-latency mode for the HLSstream. Some HLS players do not support low-latency mode. This feature is incompatible with DVR mode HLS broadcasts (both can’t be set to true). This is a beta feature.

    The HLS URL is included in the (broadcast_json) response from the Client#start_broadcast method call, and is also available in the (broadcast_json) response from the

  • resolution (string)

    The resolution of the broadcast: either “640x480” (SD landscape, the default), “1280x720” (HD landscape), “1920x1080” (FHD landscape), “480x640” (SD portrait), “720x1280” (HD portrait), or “1080x1920” (FHD portrait).

  • :streamMode (String) — default: Optional

    Whether streams included in the broadcast are selected automatically (“auto”, the default) or manually (“manual”). When streams are selected automatically (“auto”), all streams in the session can be included in the broadcast. When streams are selected manually (“manual”), you specify streams to be included based on calls to the #add_stream method. You can specify whether a stream’s audio, video, or both are included in the broadcast. For both automatic and manual modes, the broadcast composer includes streams based on stream prioritization rules. Important: this feature is currently available in the Standard environment only.

  • :hasAudio (Boolean)

    Whether the broadcast has audio (default true)

  • :hasVideo (Boolean)

    Whether the broadcast has video (default true)

Returns:

  • (Broadcast)

    The broadcast object, which includes properties defining the broadcast, including the broadcast ID.

Raises:



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/opentok/broadcasts.rb', line 113

def create(session_id, options = {})
  raise ArgumentError, "session_id not provided" if session_id.to_s.empty?
  raise ArgumentError, "options cannot be empty" if options.empty?
  raise ArgumentError, "outputs property is required in options" unless options.has_key?(:outputs)
  raise ArgumentError, "outputs must be a Hash" unless options[:outputs].is_a? Hash
  if options[:outputs].has_key?(:hls)
    dvr = options[:outputs][:hls][:dvr]
    low_latency = options[:outputs][:hls][:low_latency]
    raise ArgumentError, "dvr and low_latency can't both be true for HLS" if hls_dvr_and_low_latency_options_both_true?(dvr, low_latency)
  end
  broadcast_json = @client.start_broadcast(session_id, options)
  Broadcast.new self, broadcast_json
end

#find(broadcast_id) ⇒ Broadcast

Gets a Broadcast object for the given broadcast ID.

Parameters:

  • broadcast_id (String)

    The broadcast ID.

Returns:

  • (Broadcast)

    The broadcast object, which includes properties defining the broadcast.

Raises:



137
138
139
140
141
# File 'lib/opentok/broadcasts.rb', line 137

def find(broadcast_id)
  raise ArgumentError, "broadcast_id not provided" if broadcast_id.to_s.empty?
  broadcast_json = @client.get_broadcast(broadcast_id.to_s)
  Broadcast.new self, broadcast_json
end

#layout(broadcast_id, options = {}) ⇒ Object

Dynamically alters the layout an OpenTok broadcast. For more information, see For more information, see Configuring video layout for OpenTok live streaming broadcasts.

Parameters:

  • broadcast_id (String)

    The broadcast ID.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :type (String)

    The layout type. Set this to “bestFit”, “pip”, “verticalPresentation”, “horizontalPresentation”, “focus”, or “custom”.

  • :stylesheet (String)

    The stylesheet for a custom layout. Set this parameter if you set type to "custom". Otherwise, leave it undefined.

  • :screenshare_type (String)

    The screenshare layout type. Set this to “bestFit”, “pip”, “verticalPresentation” or “horizonalPresentation”. If this is defined, then the type parameter must be set to "bestFit".

Raises:

  • (OpenTokBroadcastError)

    The broadcast layout could not be updated.

  • (OpenTokAuthenticationError)

    Authentication failed. Invalid API key or secret.

  • (OpenTokError)

    OpenTok server error.

  • (ArgumentError)

    The broadcast_id or options parameter is empty.

  • (ArgumentError)

    The “custom” type was specified without a stylesheet option.

  • (ArgumentError)

    A stylesheet was passed in for a type other than custom. Or an invalid type was passed in.

  • (ArgumentError)

    An invalid layout type was passed in.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/opentok/broadcasts.rb', line 221

def layout(broadcast_id, options = {})
  raise ArgumentError, "option parameter is empty" if options.empty?
  raise ArgumentError, "broadcast_id not provided" if broadcast_id.to_s.empty?
  type = options[:type]
  raise ArgumentError, "custom type must have a stylesheet" if (type.eql? "custom") && (!options.key? :stylesheet)
  valid_non_custom_layouts = ["bestFit","horizontalPresentation","pip", "verticalPresentation", ""]
  valid_non_custom_type = valid_non_custom_layouts.include? type
  raise ArgumentError, "type is not valid" if !valid_non_custom_type && !(type.eql? "custom")
  raise ArgumentError, "stylesheet not needed" if valid_non_custom_type and options.key? :stylesheet
  raise ArgumentError, "screenshare_type is not valid" if options[:screenshare_type] && !valid_non_custom_layouts.include?(options[:screenshare_type])
  raise ArgumentError, "type must be set to 'bestFit' if screenshare_type is defined" if options[:screenshare_type] && type != 'bestFit'
  response = @client.layout_broadcast(broadcast_id, options)
  (200..300).include? response.code
end

#remove_stream(broadcast_id, stream_id) ⇒ Object

Removes a stream from a currently running broadcast that was started with the streamMode set to “manual”. For a description of the feature, see https://tokbox.com/developer/rest/#selecting-broadcast-streams.

Parameters:

  • broadcast_id (String)

    The broadcast ID.

  • stream_id (String)

    The ID for the stream to be removed from the broadcast

Raises:

  • (ArgumentError)

    The broadcast_id parameter id is empty.

  • (ArgumentError)

    The stream_id parameter is empty.



296
297
298
299
300
301
302
303
# File 'lib/opentok/broadcasts.rb', line 296

def remove_stream(broadcast_id, stream_id)
  raise ArgumentError, "broadcast_id not provided" if broadcast_id.to_s.empty?
  raise ArgumentError, "stream_id not provided" if stream_id.to_s.empty?
  options = {}
  options['remove_stream'] = stream_id

  @client.select_streams_for_broadcast(broadcast_id, options)
end

#stop(broadcast_id) ⇒ Broadcast

Stops an OpenTok broadcast

Note that broadcasts automatically stop after 120 minute

Parameters:

  • broadcast_id (String)

    The broadcast ID.

Returns:

  • (Broadcast)

    The broadcast object, which includes properties defining the broadcast.

Raises:



175
176
177
178
179
# File 'lib/opentok/broadcasts.rb', line 175

def stop(broadcast_id)
  raise ArgumentError, "broadcast_id not provided" if broadcast_id.to_s.empty?
  broadcast_json = @client.stop_broadcast(broadcast_id)
  Broadcast.new self, broadcast_json
end