使用nginx在android手機上搭建rtmp伺服器並測試

來源:互聯網
上載者:User

1,編譯android上面可以使用的nginx,添加選項--add-module=/path/nginx-rtmp-module,準備android上面shell可以使用的ffmpeg和相關庫檔案以及m3u8的segmenter檔案
2,在手機的data/data/android.nginx目錄下把編譯產生的nginx相關檔案放到該目錄下
3,編寫設定檔
-----------------------------------nginx.conf start here----------------------------------------------------
user root;
worker_processes 2;

error_log /data/data/android.nginx/logs/error.log;
error_log /data/data/android.nginx/logs/error.log notice;
error_log /data/data/android.nginx/logs/error.log info;

pid logs/nginx.pid;


events {
worker_connections 1024;
}

rtmp {
server {
listen 1935;

chunk_size 4096;

application myapp {
live on;
}
}
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 801;

location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}

location /stat.xsl {
root /data/data/android.rtmp;
}

location / {
root /data/data/android.rtmp/test/rtmp-publisher;
}
}

server {
listen 80 default;
server_name localhost;
autoindex on;
autoindex_exact_size on;
root /data/data/android.wwwroot;
index index.html index.htm index.php;

#charset koi8-r;

#access_log logs/host.access.log main;

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~* \.php$ {
fastcgi_index index.php;
client_max_body_size 64m;
fastcgi_pass unix:/data/data/android.php-fpm/tmp/php-fpm.socket;
fastcgi_param SCRIPT_FILENAME /data/data/android.wwwroot$fastcgi_script_name;
include fastcgi_params;
}

location ~ \.flv$ {
flv;
}
location ~ \.mp4$ {
mp4;
}


# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000 default;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 default;
# server_name localhost;

# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_timeout 5m;

# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

-----------------------------------nginx.conf end here----------------------------------------------------
4,adb push nginx.conf /data/data/android.nginx/conf/

5,啟動nginx,在/data/data/android.nginx目錄下,執行./nginx -c ./conf/nginx.conf

6,準備ffmpeg檔案
adb shell
su
chmod 777 /system/bin/
adb push ffmpeg /system/bin/
adb shell
su
cd system/bin
chmod 777 ffmpeg
exit
exit
adb push libvorbisenc.so.2 /system/lib
adb push libvorbis.so.0 /system/lib
adb push libogg.so.0 /system/lib
adb push libtheoraenc.so.1 /system/lib
adb push libtheoradec.so.1 /system/lib
adb push libmp3lame.so.0 /system/lib
adb push libfdk-aac.so.0 /system/lib
adb push segmenter /system/bin
adb shell
su
cd /system/bin
chmod 777 segmenter
exit
exit


7,準備相關檔案
adb push 1.flv /data/data/android.wwwroot/

8,準備rtmp相關檔案
在/data/data/目錄下建立rtmp目錄,使用adb push的方法把nginx-rtmp-module目錄下所有檔案push到/data/data/rtmp目錄下
修改該目錄下的./test/rtmp-publisher/player.html
---------------------------player.html start here---------------------------------------------------------------
!DOCTYPE html>


RTMP Player
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript">
var flashVars = {
streamer: 'rtmp://192.168.1.2/myapp',
file:'test1'
};
swfobject.embedSWF("RtmpPlayer.swf", "rtmp-publisher", "500", "400", "9.0.0",null, flashVars);
</script>



Flash not installed





---------------------------player.html end here---------------------------------------------------------------

9,推流
"rtmp://192.168.1.2/myapp"表示url, "test1"表示stream

推流方式1:ffmpeg -re -i 1.flv -f flv rtmp://192.168.1.2/myapp/test1
注意:要把相關檔案push到
我這裡把這個簡單寫成pushrtmp的批處理
---------------------------------pushrtmp start here-----------------------------------------------------------
!/system/bin/sh
file=$1
file_name=`echo ${file} | busybox awk -F '.' '{ print $1 "" }'`
ffmpeg -re -i ./$1.flv -f flv rtmp://192.168.1.$2/myapp/test1
---------------------------------pushrtmp end here-----------------------------------------------------------
執行方式就是簡單的./pushrtmp 1 2就可以了

10,測試
在另一台機器上訪問http://192.168.1.2:801/player.html就可以了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.