Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 使用nginx在android手機上搭建rtmp服務器並測試

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

編輯:關於Android編程

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就可以了
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved