ทำ Streaming ด้วย Nginx Web Server

27 August 2013 7:57 PM Linux, Streaming , , ,

ปกติแล้ว Nginx มันเป็น Web Server (ตามที่หลายคนเข้าใจ) แต่ว่ามันมีประโยชน์มากกว่านั้นเยอะ

เนื่องจาก Nginx มี foot print ที่เล็ก และเป็น Open Source ทำให้มีนักพัฒนาต่างสนใจช่วยกันพัฒนาระบบเสริมต่างๆ

ซึ่งหนึ่งในนั้นก็คือ RTMP Plugin ซึ่งจะทำให้ Nginx เป็น RTMP Server โดยง่ายดาย

แทบทำให้ลืม Wowza หรือ AFMS ไปได้เลย

ตัวอย่างการศึกษาครั้งนี้ผมได้มาจาก Nginx RTMP Blog และ ตัวอย่าง RTMP Module

ยังไงก็ลองตามลิ้งค์ไปดูแล้วกัน

 

ตัวอย่าง?

Configure .conf file

vim where is .conf file in NginX – nginx/conf/nginx.conf

(I do not want to explain the others, fastcgi.conf and so on, get a chance to know about NginX web server and join to the study group if you want , attach our group link)

...   
rtmp { 
    server {
        listen 1935;
        publish_time_fix off;
        application my_app {
        	live on;    # stream on live allow
        	publish all;    # control access privilege
        	allow play all; # control access privilege
        	record all; # record stream to a file
        	record_max_size 100M;   # set a file size
        	record_path /tmp;   # where the file is save
        }
    }
}
...

stop and start the web server, you may stop master and worker processes both of them.

Note, here is FFmpeg is open source framework, able to decode, encode, transcode, stream… to url on RTMP, for instance, /usr/local/ffmpeg-0.11.1/ffmpeg -loglevel verbose -re -i /tmp/FILE_NAME -f flv rtmp://YOUR_SERVER_IP/my_app/YOUR_FILE_NAME

Access the site and record on live

make javascript in a html to record stream on camera each .js and .swf are included test/www directory in nginx-rtmp-module archived.

record.html

...
<head>
	<script src="jwplayer_old/swfobject.js"></script>
	<script type="text/javascript">
		var flashvars =
		{
		    streamer: rtmp://YOUR_SERVER_IP/myapp,
   			file: YOUR_FILE_NAME,
   		 	type: camera,
   		 	controlbar: bottom,
    		stretching: none,    		
    		frontcolor: 86C29D,         // text and icons (green)    		
    		backcolor: 849BC1,          // playlist background (blue)
    		lightcolor: C286BA,         // selected text/track highlight (pink)
    		screencolor: FFFFFF,        // screen background (black)
    		id: playerID,
    		autostart: true
		};
		var params =
		{
   			allowfullscreen: true,
    		allowscriptaccess: always,    		
    		bgcolor: #FFFFFF
		};
		var attributes =
		{
    		id: playerID,
   	 		name: playerID
		};		
		swfobject.embedSWF(jwplayer_old/player.swf, player, 320, 260, 9.0.124, false, flashvars, params, attributes);
	</script>
</head>
<body>
	<a href="index.html">Play</a> | <b>Record</b>
	<br/>
	<div id="playercontainer" class="playercontainer"><a id="player" class="player" href="http://get.adobe.com/flashplayer/">
	Get the Adobe Flash Player to see this video.</a></div>
</body>

load the html on browser and check the path whether .flv stream file exists where you want to save it have written down on .conf.