Module: CastCaster::Deploy::FFmpegServices

Included in:
Compose, Swarm
Defined in:
lib/castcaster/deploy/ffmpeg_services.rb

Instance Method Summary collapse

Instance Method Details

#build_ffmpeg_services(channels, hls_dir) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/castcaster/deploy/ffmpeg_services.rb', line 4

def build_ffmpeg_services(channels, hls_dir)
  base = 'rtmp://nginx:1935/live'
  services = {}
  channels.each do |ch|
    src = ch.source
    next unless src && src['type']
    next if src['type'] == 'rtmp_push'

    name = ch.name
    label = { 'castcaster/channel' => name }
    common = {
      'image' => CastCaster::IMAGES['ffmpeg'],
      'restart' => 'unless-stopped',
      'volumes' => ["#{hls_dir}:/var/lib/castcaster/hls"],
      'depends_on' => ['nginx'],
      'labels' => label,
      'environment' => {
        'CHANNEL_NAME' => name,
        'RTMP_BASE' => base
      }
    }

    case src['type']
    when 'test'
      services["castcaster-test-#{name}"] = common.merge(
        'command' => ["-c", "teststream.sh"],
        'labels' => label.merge('castcaster/task' => 'test')
      )
    else
      next unless src['url']
      bitrate = (ch.transcode || {}).fetch('bitrate', FFmpegProfiles::RELAY_DEFAULTS['bitrate'])
      profiles = (ch.transcode || {}).fetch('profiles', nil)

      if profiles
        services["castcaster-adaptive-#{name}"] = common.merge(
          'environment' => common['environment'].merge(
            'SOURCE_URL' => src['url'],
            'PROFILES' => profiles
          ),
          'command' => ["-c", "adaptive.sh"],
          'labels' => label.merge('castcaster/task' => 'adaptive')
        )
      else
        services["castcaster-relay-#{name}"] = common.merge(
          'environment' => common['environment'].merge(
            'SOURCE_URL' => src['url'],
            'BITRATE' => bitrate
          ),
          'command' => ["-c", "relay.sh"],
          'labels' => label.merge('castcaster/task' => 'relay')
        )
      end

      if ch.snapshot
        services["castcaster-snapshot-#{name}"] = common.merge(
          'environment' => common['environment'].merge(
            'INTERVAL' => ch.snapshot.to_s,
            'SNAPSHOTS_DIR' => '/var/lib/castcaster/snapshots'
          ),
          'command' => ["-c", "snapshot.sh"],
          'labels' => label.merge('castcaster/task' => 'snapshot')
        )
      end
    end
  end
  services
end