1. 程式人生 > >使用nginx+nginx-rtmp-module+ffmpeg搭建流媒體伺服器筆記(七)

使用nginx+nginx-rtmp-module+ffmpeg搭建流媒體伺服器筆記(七)

第七部分

之前已經將標準版的Nginx移植到了ARM開發板上面並且執行成功,而我的目的是要利用FFMPEG和NGINX來實現HLS視訊直播,所以還需要在此基礎上新增nginx-rtmp-module模組。

有了之前的移植經驗,有些工作就好做一些了,但是還是遇到很多的問題,記錄下:

1、用到的原始碼包

2、配置

我的步驟是:首先將上面的三個原始碼包放在了資料夾456下,進入/456/android/nginx目錄下執行配置命令:

auto/configure     --crossbuild=android-arm     --prefix=/sdcard/nginx-rtmp2  --add-module=/home/wangrui/456/nginx-rtmp-module  
 --with-cc=/home/wangrui/local/android-toolchain/bin/arm-linux-androideabi-gcc     --without-pcre --without-http_rewrite_module    
--without-http_userid_module    --with-cc-opt=-Wno-sign-compare

結果出現問題:
auto/configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

新增nginx-rtmp-module模組需要Openssl library,百度下發現都是需要兩條命令就可以了:
apt-get install openssl
apt-get install libssl-dev

但是我執行時顯示我已經安裝了最新版本,都已經安裝好了,重試後還是出錯,那就在配置命令中新增:
auto/configure     --crossbuild=android-arm     --prefix=/sdcard/nginx-rtmp2  --add-module=/home/wangrui/456/nginx-rtmp-module   
--with-cc=/home/wangrui/local/android-toolchain/bin/arm-linux-androideabi-gcc     --without-pcre --without-http_rewrite_module      
--without-http_userid_module    --with-cc-opt=-Wno-sign-compare --with-openssl=/home/wangrui/456/openssl-1.0.0q

這樣不會出錯了。

3、make

緊接著執行

make

經過漫長的充滿希望的等待,最後完成終於還是出錯了
/home/wangrui/456/openssl-1.0.0q/.openssl/lib/libssl.a /home/wangrui/456/openssl-1.0.0q/.openssl/lib/libcrypto.a -lz
/home/wangrui/local/android-toolchain/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: /home/wangrui/456/openssl-1.0.0q/.openssl/lib/libssl.a(s23_meth.o): Relocations in generic ELF (EM: 3)
/home/wangrui/local/android-toolchain/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: /home/wangrui/456/openssl-1.0.0q/.openssl/lib/libssl.a(s23_meth.o): Relocations in generic ELF (EM: 3)
/home/wangrui/local/android-toolchain/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: /home/wangrui/456/openssl-1.0.0q/.openssl/lib/libssl.a(s23_meth.o): Relocations in generic ELF (EM: 3)
/home/wangrui/456/openssl-1.0.0q/.openssl/lib/libssl.a: could not read symbols: File in wrong format
collect2: ld returned 1 exit status
make[1]: *** [objs/nginx] 錯誤 1
make[1]:正在離開目錄 `/home/wangrui/456/android-nginx'
make: *** [build] 錯誤 2

關鍵錯誤在

/home/wangrui/456/openssl-1.0.0q/.openssl/lib/libssl.a: could not read symbols: File in wrong format
趕緊百度下,網上給出的答案基本上都是說以前make留下的有檔案,影響到了這次的交叉編譯,建議換個新的原始碼包重新編譯,但是我每次都是下載的新的原始碼包還是出現同樣的錯誤,可見我的錯誤原因不在這。

在網上看到一篇文章:

裡面的一段話提醒了我:

“請確定在作業系統位數相同的環境下進行編譯,否則刪除原庫檔案重新生成“

這個錯誤可能是因為在編譯nginx和編譯openssl的時候使用了不同的gcc,在編譯openssl的時候可能使用了系統預設的gcc,導致arm環境下識別不了libssl.a這個靜態庫。

檢視下libssl.a的型別:

objdump -a /home/wangrui/456/openssl-1.0.0q/.openssl/lib/libssl.a

其中
objdump -a xx.a

可以檢視靜態庫檔案是32位還是64位型別。

另外對於動態庫 xx.so檔案的型別檢視使用 file 命令就可以了。

結果顯示:

在歸檔檔案 /home/wangrui/456/openssl-1.0.0q/.openssl/lib/libssl.a 中:

s2_meth.o:     檔案格式 elf32-i386
rw-r--r-- 0/0   2148 Mar  5 10:14 2015 s2_meth.o


s2_srvr.o:     檔案格式 elf32-i386
rw-r--r-- 0/0  13304 Mar  5 10:14 2015 s2_srvr.o


s2_clnt.o:     檔案格式 elf32-i386
rw-r--r-- 0/0  12740 Mar  5 10:14 2015 s2_clnt.o

 。。。。。。

 
可見檔案格式為
 elf32-i386
並不是ARM環境下的格式,也就是openssl編譯的時候用的是linux下預設的gcc導致出錯。


知道大致的錯誤原因之後,便將openssl單獨交叉編譯下獲取libssl.a檔案然後和之前的替換下。

(1)首先編寫配置檔案 my_configure_openssl.sh

#!/bin/sh
./config no-asm shared \
--prefix=/home/wangrui/nginx_ndk/build \

(2)執行配置檔案之後,進入Makefile,修改Makefile檔案
找到CC= gcc,替換為CC= /home/wangrui/local/android-toolchain/bin/arm-linux-androideabi-gcc
找到AR= ar $(ARFLAGS) r,替換為AR= /home/wangrui/local/android-toolchain/bin/arm-linux-androideabi-ar $(ARFLAGS) r
找到RANLIB= /usr/bin/ranlib,替換為RANLIB= /home/wangrui/local/android-toolchain/bin/arm-linux-androideabi-ranlib
找到NM= nm,修改為NM= /home/wangrui/local/android-toolchain/bin/arm-linux-androideabi-nm
找到MAKEDEPPROG= gcc,修改為MAKEDEPPROG= /home/wangrui/local/android-toolchain/bin/arm-linux-androideabi-gcc

cp Makefile Makefile.ok

(3)執行 make && make install

這樣在/home/wangrui/nginx_ndk/build/lib目錄下會有libssl.a和libcrypto.a檔案

然後進入到/home/wangrui/456/android-nginx/objs目錄下修改Makefile檔案:

將其中的兩處

/home/wangrui/456/openssl-1.0.0q/.openssl/lib/libssl.a /home/wangrui/456/openssl-1.0.0q/.openssl/lib/libcrypto.a -lz

修改為
/home/wangrui/nginx_ndk/build/lib/libssl.a  /home/wangrui/nginx_ndk/build/lib/libcrypto.a -lz

然後重新 make

成功

(4) make install

完成。

(5)修改nginx.conf檔案,使其支援rtmp和hls,完整的配置檔案如下:

user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


  rtmp {  
        server {  
            listen 1935;  
      
            application myapp {  
                live on;  
            }  
            application hls {  
                live on;  
                hls on;  
                hls_path /data/misc/hls;  
            }  
        }  
    }  


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       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        location /hls {  
                types {  
                    application/vnd.apple.mpegurl m3u8;  
                    video/mp2t ts;  
                }  
                root /data/misc;  
                add_header Cache-Control no-cache;  
        }  

        #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$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # 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;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


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

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

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}

(6)將交叉編譯完成的包移到ARM開發板上面
adb push /sdcard/nginx-rtmp2/  /data/misc/nginx-rtmp/

(7)進入到/data/misc/nginx-rtmp2/sbin目錄下:
./nginx  -p /data/misc/nginx-rtmp2  -c  conf/nginx.conf

執行 nginx

(8)在ARM開發板瀏覽器中輸入

http://localhost/

出現
    Welcome to nginx!  
      
    If you see this page, the nginx web server is successfully installed and working. Further configuration is required.  
      
    For online documentation and support please refer to nginx.org.  
    Commercial support is available at nginx.com.  
      
    Thank you for using nginx.  

總結:一個人搞這以前從沒碰過的移植,真是各處碰壁阿,各種錯誤只有你想不到,沒有他出現不了的。不過當編譯通過、安裝成功之後興奮感還是蠻不錯的。繼續加油。。。。