Class: Awslive::InputLooper

Inherits:
Object
  • Object
show all
Includes:
ActionType, AwsliveExceptions, AwsliveLogging, StartType
Defined in:
lib/awslive-inputlooper/input_looper.rb

Constant Summary collapse

INITAL_INPUT_ACTION_NAME =
"Initial Channel Input"

Constants included from ActionType

ActionType::INPUT_PREPARE, ActionType::INPUT_SWITCH, ActionType::PAUSE, ActionType::SCTE35_SPLICE_INSERT, ActionType::UNPAUSE

Constants included from StartType

StartType::FIXED, StartType::FOLLOW, StartType::IMMEDIATE

Instance Method Summary collapse

Methods included from AwsliveLogging

#log, #set_log_flag

Constructor Details

#initialize(channel_id, playlist = nil, access_key = nil, access_secret = nil, region = nil) ⇒ InputLooper

Returns a new instance of InputLooper.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/awslive-inputlooper/input_looper.rb', line 21

def initialize(channel_id, playlist = nil, access_key=nil , access_secret =nil, region = nil)
  credentials = Aws::SharedCredentials.new
  if !access_key.nil? && !access_secret.nil?
    aws_region = region.nil? ? 'us-east-1' : region
    @medialiveclient = Aws::MediaLive::Client.new(:access_key_id => access_key, :secret_access_key => access_secret, :region => aws_region )
  elsif credentials.set?
    @medialiveclient = Aws::MediaLive::Client.new(:credentials => credentials)
  else
    @medialiveclient = Aws::MediaLive::Client.new
  end
  @scheduler = Awslive::ScheduleAction.new(@medialiveclient)
  @channel_id = channel_id
  @playlist = playlist
  @loop_thread = nil
  set_log_flag(true)
end

Instance Method Details

#delete_all_schedulesObject



364
365
366
# File 'lib/awslive-inputlooper/input_looper.rb', line 364

def delete_all_schedules
  @scheduler.delete_all(@channel_id)
end

#get_channel_class(channel_info = nil) ⇒ Object



351
352
353
354
# File 'lib/awslive-inputlooper/input_looper.rb', line 351

def get_channel_class(channel_info = nil)
  channel_info = get_channel_info if channel_info.nil?
  channel_info[:channel_class]
end

#get_channel_infoObject



276
277
278
# File 'lib/awslive-inputlooper/input_looper.rb', line 276

def get_channel_info
  @medialiveclient.describe_channel({ :channel_id => "#{@channel_id}" })
end

#get_current_action(channel_info = nil) ⇒ Object



344
345
346
347
348
# File 'lib/awslive-inputlooper/input_looper.rb', line 344

def get_current_action(channel_info = nil )
  channel_info = get_channel_info if channel_info.nil?
  current_action_name = channel_info[:pipeline_details][0][:active_input_switch_action_name]
  current_action_name
end

#get_current_input_info(channel_info = nil) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/awslive-inputlooper/input_looper.rb', line 322

def get_current_input_info(channel_info = nil)
  current_urls = []
  input_info = {}
  channel_info = get_channel_info if channel_info.nil?
  #puts channel_info
  current_input_action_name = get_current_action(channel_info)
  puts current_input_action_name
  attachement_name = get_current_input_name(channel_info)
  input_id = get_input_id(channel_info[:input_attachments], attachement_name )
  current_input_info = get_input_info(input_id)
  input_info[:source_type] = current_input_info[:input_source_type]
  input_info[:input_type] = current_input_info[:type]
  if current_input_action_name == INITAL_INPUT_ACTION_NAME
    current_urls = get_input_url(input_id)
  else
    (current_urls << @scheduler.get_action_url(@channel_id, current_input_action_name)).flatten!
  end
  input_info[:source_urls] = current_urls
  input_info
end

#get_current_input_name(channel_info = nil) ⇒ Object



299
300
301
302
303
# File 'lib/awslive-inputlooper/input_looper.rb', line 299

def get_current_input_name(channel_info = nil )
  channel_info = get_channel_info if channel_info.nil?
  current_input_name = channel_info[:pipeline_details][0][:active_input_attachment_name]
  current_input_name
end

#get_current_input_type(channel_info = nil) ⇒ Object



292
293
294
295
296
297
# File 'lib/awslive-inputlooper/input_looper.rb', line 292

def get_current_input_type(channel_info = nil)
  channel_info = get_channel_info if channel_info.nil?
  attachement_name = get_current_input_name(channel_info)
  input_id = get_input_id(channel_info[:input_attachments], attachement_name )
  get_input_info(input_id).type rescue nil
end

#get_current_input_url(channel_info = nil) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/awslive-inputlooper/input_looper.rb', line 305

def get_current_input_url(channel_info = nil)
  current_url = []
  channel_info = get_channel_info if channel_info.nil?
  #puts channel_info
  current_input_action_name = get_current_action(channel_info)
  puts current_input_action_name
  if current_input_action_name == INITAL_INPUT_ACTION_NAME
    attachement_name = get_current_input_name(channel_info)
    input_id = get_input_id(channel_info[:input_attachments], attachement_name )
    current_url = get_input_url(input_id)
  else
    current_url << @scheduler.get_action_url(@channel_id, current_input_action_name)
  end
  current_url
end

#get_input_id(inputs, input_name) ⇒ Object



250
251
252
253
254
255
256
257
258
259
# File 'lib/awslive-inputlooper/input_looper.rb', line 250

def get_input_id(inputs, input_name)
  input_id = nil
  inputs.each do | input |
    if input[:input_attachment_name] == input_name
      input_id = input[:input_id]
      break;
    end
  end
  input_id
end

#get_input_info(input_id) ⇒ Object



271
272
273
274
# File 'lib/awslive-inputlooper/input_looper.rb', line 271

def get_input_info(input_id)
  input_info = @medialiveclient.describe_input({ :input_id => "#{input_id}" })
  input_info
end

#get_input_url(input_id) ⇒ Object



261
262
263
264
265
266
267
268
# File 'lib/awslive-inputlooper/input_looper.rb', line 261

def get_input_url(input_id)
  input_url = []
  input_info = @medialiveclient.describe_input({ :input_id => "#{input_id}" })
  input_info[:sources].each do | source_info |
    input_url << source_info[:url]
  end
  input_url
end

#get_order(url) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/awslive-inputlooper/input_looper.rb', line 168

def get_order(url)
  puts "URL => #{url}"
  order = nil
  if !url.nil?
    uri = URI.parse(url) rescue nil
    params = CGI.parse(uri.query) unless uri.nil?
    if !params.nil?
      order = params["order"].first rescue nil
    end
  end
  order
end

#get_valid_inputs(inputs) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/awslive-inputlooper/input_looper.rb', line 229

def get_valid_inputs(inputs)
  static_input = nil
  dynamic_input = nil

  inputs.each do | input |
    next if input[:input_settings][:source_end_behavior] != "CONTINUE"
    input_info = @medialiveclient.describe_input({ :input_id => "#{input[:input_id]}" })
    if input_info[:input_source_type] == "STATIC"
      static_input = input
    elsif input_info[:input_source_type] == "DYNAMIC"
      dynamic_input = input
    end
    if !static_input.nil? && !dynamic_input.nil?
      break
    end
  end
  puts static_input
  puts dynamic_input
  return static_input, dynamic_input
end

#identify_next_input(file_url, exclude_query_param, loopback = true) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/awslive-inputlooper/input_looper.rb', line 181

def identify_next_input(file_url, exclude_query_param, loopback = true)
  next_file = @playlist[0]
  #puts " File Url #{file_url}"
  if !file_url.nil? && !file_url[0].nil?
    is_duplicated = is_url_duplicated(file_url[0])
    log( "URL Duplicate => #{is_duplicated}")
    @playlist.each_with_index do | playlist_file, index |
      #puts "next file identification #{playlist_file}   #{file_url[0]}  #{index}"
      if !exclude_query_param.nil?
        pfile = playlist_file.split('?')[0]
        cfile = file_url[0].split('?')[0]
      else
        pfile = playlist_file
        cfile = file_url[0]
      end
      log("pfile => #{pfile}")
      log("cfile => #{cfile}")
      if pfile == cfile
        if is_duplicated
          p_order = get_order(playlist_file)
          c_order = get_order(file_url[0])
          log("playlist Order #{p_order} : current order #{c_order}")
        end
        log("is Duplicated #{is_duplicated}  porder #{p_order}   corder #{c_order} ")
        next if is_duplicated && !p_order.nil? && !c_order.nil? && p_order != c_order && index != (@playlist.length - 1)
        #puts "passed Next #{index} and #{@playlist.length}"
        if index == (@playlist.length - 1)
          if !loopback
            next_file = nil
            break
          end
          next_file = @playlist[0]
        else
          next_file = @playlist[index + 1]
          break
        end
      end
    end
  end
  next_file
end

#is_schedule_empty?Boolean

Returns:

  • (Boolean)


223
224
225
226
227
# File 'lib/awslive-inputlooper/input_looper.rb', line 223

def is_schedule_empty?
  schedule_info = @medialiveclient.describe_schedule({ :channel_id => "#{@channel_id}"})
  schedule_actions = schedule_info[:schedule_actions]
  schedule_actions.empty?
end

#is_url_duplicated(url) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/awslive-inputlooper/input_looper.rb', line 152

def is_url_duplicated(url)
  match_count = 0
  is_duplicated = false
  @playlist.each_with_index do | playlist_file, index |
    playlist_file = playlist_file.split('?')[0]
    if playlist_file == url.split('?')[0]
      match_count =  match_count + 1
    end
    if match_count >= 2
      is_duplicated = true
      break
    end
  end
  is_duplicated
end

#pause_channelObject



356
357
358
# File 'lib/awslive-inputlooper/input_looper.rb', line 356

def pause_channel
  @scheduler.create(@channel_id, FIXED, PAUSE, { :channel_class => "#{get_channel_class}" })
end

#playlist_loop_aliveObject



55
56
57
58
# File 'lib/awslive-inputlooper/input_looper.rb', line 55

def playlist_loop_alive
  isalive = @loop_thread.nil? ? false : @loop_thread.alive?
  isalive
end

#start_channelObject



280
281
282
283
284
# File 'lib/awslive-inputlooper/input_looper.rb', line 280

def start_channel
  res = @medialiveclient.start_channel({ :channel_id => "#{@channel_id}" })
  puts "Start Channel Response #{res}"
  res
end

#stop_channelObject



286
287
288
289
290
# File 'lib/awslive-inputlooper/input_looper.rb', line 286

def stop_channel
  res = @medialiveclient.stop_channel({ :channel_id => "#{@channel_id}" })
  puts "Stop Channel Response #{res}"
  res
end

#stop_playlist_loopObject



60
61
62
63
# File 'lib/awslive-inputlooper/input_looper.rb', line 60

def stop_playlist_loop
  Thread.kill(@loop_thread) if playlist_loop_alive
  true
end

#switch_and_loop_playlist(playlist, sleep_time = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/awslive-inputlooper/input_looper.rb', line 38

def switch_and_loop_playlist(playlist, sleep_time = nil)
  @loop_thread = Thread.new {
    @playlist = playlist
    log ("Switch input Monitoring started!!")
    switch_input(true)
    # AWS recommends to have 5 sec delay beteen the switches.
    sleep 5
    sleep_time = 5 if sleep_time.nil? || sleep_time < 5
    loop do
      success = switch_input
      break if !success
      sleep sleep_time
    end
    log( "Switch input Monitoring Stopped!!")
  }
end

#switch_input(force_immed = nil, exclude_query_param = nil, loopback = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/awslive-inputlooper/input_looper.rb', line 77

def switch_input(force_immed = nil, exclude_query_param = nil, loopback = nil)
  # Fetching the channel information every time to know the state of the channel and input in pipeline.
  channel_info = get_channel_info
  channel_state = channel_info[:state]
  # the switching loop will be active only when the channel is in "RUNNING" state.
  if channel_state == "RUNNING"
    # Identifying one static and one dyanmic input to enable the switch and loop inputs.
    static_input, dynamic_input = get_valid_inputs(channel_info[:input_attachments])

    if static_input.nil? || dynamic_input.nil?
      log( "No Valid inputs for Switch and loop; please provision one static and one dynamic input in the Channel #{@channel_id}")
      raise InvalidInputException.new("No Valid inputs; should be provisioned with one static and one dynamic Input")
    end

    current_input_name = get_current_input_name(channel_info)
    current_input_action_name = get_current_action(channel_info)
    sc_action =  @scheduler.get_schedule_action(@channel_id, current_input_action_name)
    log( "Current input #{current_input_name}  #{current_input_action_name}")
    if force_immed || sc_action.nil? || is_schedule_empty?
      log( "Schedule is empty or schedule does not exist; Hence creating one immediate action with static input")
      data = {}
      #data[:input_ref_name] = static_input[:input_attachment_name]
      data[:input_ref_name] = dynamic_input[:input_attachment_name]
      data[:url_path] = @playlist[0]
      @scheduler.create(@channel_id, IMMEDIATE, INPUT_SWITCH, data)
      return true
    end

    follow_schedule = @scheduler.get_follow_schedule(@channel_id, current_input_action_name)
    log( "Follow Schedule => #{follow_schedule}")
    current_url = @scheduler.get_action_url(@channel_id, current_input_action_name)
    log("Current URL => #{current_url}")
    next_file = identify_next_input(current_url, exclude_query_param, loopback )
    log("Next URL => #{next_file}")
    if next_file.nil? && follow_schedule.nil? && !loopback
      log ("end of playlist and loopback not enabled!!")
      return
    end
    log( "current URL #{current_url} next URL #{next_file}")
    unless follow_schedule.nil?
      follow_url = @scheduler.get_follow_action_url(@channel_id, current_input_action_name)
      log("Follow URL #{follow_url}")
      if !follow_url[0].nil? && next_file.split('?')[0] != follow_url[0].split('?')[0]
        log( "Playlist seems to be changed, hence aligning.")
        @scheduler.delete( @channel_id,[ "#{follow_schedule[:action_name]}"])
        next_input_name = dynamic_input[:input_attachment_name]
        log ("next input name #{next_input_name}")
        data = {}
        data[:follow_input] = current_input_action_name
        data[:trigger_input] = next_input_name
        data[:url_path] = next_file
        @scheduler.create(@channel_id, FOLLOW, INPUT_SWITCH, data)
      else
        log( "No change in follow schedule!. not change in playlist")
        return true
      end
    else
      log("No follow Schedule!")
      #next_file = identify_next_input(current_url, exclude_query_param)
      unless next_file.nil?
        next_input_name = dynamic_input[:input_attachment_name]
        log( "Next input Name #{next_input_name} #{next_file}")
        data = {}
        data[:follow_input] = current_input_action_name
        data[:trigger_input] = next_input_name
        data[:url_path] = next_file
        @scheduler.create(@channel_id, FOLLOW, INPUT_SWITCH, data)
      end
    end
  else
    log("invalid channel state #{channel_state}")
    raise InvalidChannelStateException.new("Channel state Should be RUNNING, instead of #{channel_state}")
  end
end

#switch_playlist(playlist, sleep_time = nil) ⇒ Object



70
71
72
73
74
75
# File 'lib/awslive-inputlooper/input_looper.rb', line 70

def switch_playlist(playlist, sleep_time = nil)
  if stop_playlist_loop
    switch_and_loop_playlist(playlist, sleep_time)
  end
  return playlist_loop_alive
end

#unpause_channelObject



360
361
362
# File 'lib/awslive-inputlooper/input_looper.rb', line 360

def unpause_channel
  @scheduler.create(@channel_id, FIXED, UNPAUSE, { :channel_class => "#{get_channel_class}" })
end

#update_playlist(playlist) ⇒ Object



65
66
67
68
# File 'lib/awslive-inputlooper/input_looper.rb', line 65

def update_playlist(playlist)
  @playlist = playlist
  return true
end