Video streaming in rails 4.2 -
i not sure if programming question or web server config question.
the video files not in public/ requests have go through controller.
in controller:
def play_video video=video.find params[:id] response.headers['content-length'] = file.size(video.path).to_s send_data file.read(video.path,mode: 'rb'), type: video.mime_type, disposition: 'inline' end
on client-side using jplayer now.in firefox running on linux in both development , production works properly, can jump forward or backward without issue. in every other browser on linux , windows, plays properly, it, no skipping forward or backwards, in development , production. put jplayer bug exact same behavior using <video> control attribute, mediaelement.js.
in development using thin, on production passenger , nginx.
nginx.conf:
worker_processes 4; events { worker_connections 1024; } sendfile on; #tcp_nopush on; http { passenger_root /usr/local/lib64/ruby/gems/2.2.0/gems/passenger-5.0.11; passenger_ruby /usr/local/bin/ruby; keepalive_timeout 120; gzip on; server { server_tokens off; listen 80 default_server; listen 443 ssl; server_name www.example.com; root /path/to/public; passenger_enabled on; client_max_body_size 100m; add_header strict-transport-security "max-age=31536000; includesubdomains;"; add_header x-content-type-options nosniff; add_header x-xss-protection "1; mode=block"; include mime.types; default_type application/octet-stream; #removed ssl config stuff ... i not sure problem lies. no error messages when trying skip forward.
please don't link rails http streaming tutorials lame , not-at-all-helpful send "hello world" client 10 times examples, unless have 1 addresses video streaming. did find old versions of rails , other web servers.
this anti pattern trying do, send_file blocking synchronical mechanism, requires n number of rails processes many requests app serve @ time. being done reason have controlled access file or perform kind of pre/post processing: permission check, audit etc.
if anti pattern how pattern looks like: 1) x-accel-redirect 2) async permission check via auth_request module
in first case rails app returns file's url within x-accel-redirect header empty body followed closing request rails can continue serving new request again not waiting until client gets file (in case of slow client's connection rails can't free process until finished). serve 10 large video files simultaneously 10 workers require have. x-accel-redirect makes same job 1 worker:
rails
def download video=video.find params[:id] headers['x-accel-redirect'] = "#{video.file_name}" render nothing: true end nginx
location /video { internal; root /mnt/video; } the alternative auth_request similar efficiency need know file target url beforehand not generate server. auth_request location internal , handles authentication rails route followed rejection or pass user target video file url:
nginx
location /video/ { root /mnt/video; auth_request /auth; ... } location = /auth { internal; proxy_pass http://localhost; proxy_pass_request_body off; proxy_set_header content-length ""; proxy_set_header x-original-uri $request_uri; }
Comments
Post a Comment