1. 程式人生 > >【Java】Red5伺服器搭建(實現線上直播,流媒體視訊播放)

【Java】Red5伺服器搭建(實現線上直播,流媒體視訊播放)


引言

流媒體檔案是目前非常流行的網路媒體格式之一,這種檔案允許使用者一邊下載一邊播放,從而大大減少了使用者等待播放的時間。另外通過網路播放流媒體檔案時,檔案本身不會在本地磁碟中儲存,這樣就節省了大量的磁碟空間開銷。正是這些優點,使得流媒體檔案被廣泛應用於網路播放。

流媒體伺服器是通過建立釋出點來發布流媒體內容和管理使用者連線的。流媒體伺服器能夠釋出從視訊採集卡或攝像機等裝置中傳來的實況流,也可以釋出事先儲存的流媒體檔案,並且釋出實況流和流媒體檔案的結合體。一個媒體流可以由一個媒體檔案構成,也可以由多個媒體檔案組合而成,還可以由一個媒體檔案目錄組成。

Flash Media Server,簡稱 FMS,是 Flash 大家庭裡的一員,被稱為是目前開發 Web 音視訊應用程式(比如,視訊聊天室、視訊會議系統、播客系統、音視訊留言等等)的最方便最快捷的解決方案。也在 web 應用程式開發上有著越來越廣泛的應用。當然 FMS 這種優良功能並不是免費提供給廣大使用者的,幾千美金每個點的價格讓很多開發人員望而卻步。於是,大家開始嘗試尋找一種更合適的解決方案,開源的 Red5 便成為了人們的新寵。

Red5 是一個採用 Java 開發的開源的 Flash 流媒體伺服器,功能上與 FMS 類似。它支援:把音訊(MP3)和視訊(FLV)轉換成播放流;錄製客戶端播放流(只支援 FLV);共享物件;現場直播流釋出;遠端呼叫。Red5 使用 RSTP 作為流媒體傳輸協議,包括線上錄製,Flash 流媒體播放,線上聊天,視訊會議等一些基本功能。國內外基於 Red5 的流媒體應用產品也在不斷推廣,例如線上視訊會議 Openmeeting,大型 Flash 網遊功夫之王 2。

Red5 概述

Red5 是一個採用 Java 開發開源的 Flash 流媒體伺服器。免費開源使軟體更加容易擴充套件,下載後你可以對原始碼進行修改;更加經濟,比起 FMS 高昂的費用,Red5 能為一般的應用節約大筆費用;同時伺服器端的 Java 面嚮物件語言比起 FMS 伺服器端的 ActionScript2 語言更加成熟。鑑於 Red5 的種種優勢,推出不久便被廣大使用者所接受。

Red 5 支援:

1. 把音訊(MP3)和視訊(FLV, F4V, MP4, 3GP)轉換成播放流;

2. 錄製客戶端播放流, 把攝像頭,麥克風等傳入的音訊視訊錄製儲存到伺服器;

3. 共享物件;

4. 現場直播流釋出;

5. 遠端呼叫;

6. 協議:RTMP, RTMPT, RTMPS, and RTMPE。

Red5 伺服器搭建

JDK自行安裝

下載Red5

解壓到自己的一個能記住的資料夾中 eg:F:\Red5

配置Red5相關引數

修改red5.properties配置檔案..\conf\red5.properties

只需要配置HTTP 和RMTP節點的host和port即可 預設也行host配置成0.0.0.0 輸入區域網IP或者127.0.0.1都可以正常訪問

# Socket policy
policy.host=0.0.0.0
policy.port=843

# HTTP
http.host=0.0.0.0
http.port=8855
https.port=5443
http.URIEncoding=UTF-8
http.max_keep_alive_requests=-1
http.max_threads=20
http.acceptor_thread_count=10
http.processor_cache=20

# RTMP
rtmp.host=0.0.0.0
rtmp.port=1935
rtmp.io_threads=16
rtmp.send_buffer_size=65536
rtmp.receive_buffer_size=65536
rtmp.ping_interval=1000
rtmp.max_inactivity=60000
rtmp.max_handshake_time=5000
rtmp.tcp_nodelay=true
rtmp.tcp_keepalive=false
rtmp.default_server_bandwidth=10000000
rtmp.default_client_bandwidth=10000000
rtmp.client_bandwidth_limit_type=2
rtmp.bandwidth_detection=false
rtmp.encoder_base_tolerance=5000
rtmp.encoder_drop_live_future=false
# traffic optimization hinting. to disable set traffic class set to -1
# low delay + high throughput == 24 (0x18)
rtmp.traffic_class=-1
# requested maximum length of the queue of incoming connections
rtmp.backlog=32
# the interval (seconds) between each throughput calculation
rtmp.thoughput_calc_interval=15
# enable use of the default mina acceptor
rtmp.default_acceptor=true
# socket i/o pool sizes used when default acceptor is disabled
rtmp.initial_pool_size=0
rtmp.max_pool_size=2
rtmp.max_processor_pool_size=16
rtmp.executor_keepalive_time=60000
mina.logfilter.enable=false
# scheduler configs (per application)
rtmp.scheduler.pool_size=16
rtmp.deadlockguard.sheduler.pool_size=16
# message executor configs (per application) - adjust these as needed if you get tasks rejected
rtmp.executor.core_pool_size=4
rtmp.executor.max_pool_size=32
rtmp.executor.queue_capacity=64
# drop audio packets when queue is almost full, to disable this, set to 0
rtmp.executor.queue_size_to_drop_audio_packets=60
# maximum amount of time allotted to process a single rtmp message / packet in milliseconds, set it as 0 to disable timeout
rtmp.max_handling_time=2000
# connection tweaks - dont modify unless you know what you're doing
rtmp.channel.initial.capacity=3
rtmp.channel.concurrency.level=1
rtmp.stream.initial.capacity=1
rtmp.stream.concurrency.level=1
rtmp.pending.calls.initial.capacity=3
rtmp.pending.calls.concurrency.level=1
rtmp.reserved.streams.initial.capacity=1
rtmp.reserved.streams.concurrency.level=1

# RTMPS
rtmps.host=0.0.0.0
rtmps.port=8443
rtmps.ping_interval=5000
rtmps.max_inactivity=60000
rtmps.max_keep_alive_requests=-1
rtmps.max_threads=20
rtmps.acceptor_thread_count=2
rtmps.processor_cache=20
# RTMPS Key and Trust store parameters
rtmps.keystorepass=password
rtmps.keystorefile=conf/keystore.jks
rtmps.truststorepass=password
rtmps.truststorefile=conf/truststore.jks

# RTMPT
rtmpt.host=0.0.0.0
rtmpt.port=8088
rtmpt.ping_interval=5000
rtmpt.max_inactivity=60000
rtmpt.max_handshake_time=5000
rtmpt.max_keep_alive_requests=-1
rtmpt.max_threads=20
rtmpt.acceptor_thread_count=2
rtmpt.processor_cache=20
rtmpt.encoder_base_tolerance=5000
rtmpt.encoder_drop_live_future=true
# better setting for streaming media
rtmpt.target_reponse_size=32768
# best setting for small messages or shared objects
#rtmpt.target_reponse_size=8192
# max incoming messages to process at a time. the most that FP appears to send is 166
rtmpt.max_in_msg_process=166
# max time in millis that we will wait when offering data to the in or out queue
rtmpt.max_queue_offer_time=125
# max offer attempts
rtmpt.max_queue_offer_attempts=4

# WebSocket
ws.host=0.0.0.0
ws.port=8081

# Debug proxy (needs to be activated in red5-core.xml)
proxy.source_host=127.0.0.1
proxy.source_port=1936
proxy.destination_host=127.0.0.1
proxy.destination_port=1935

# JMX
jmx.rmi.host=localhost
jmx.rmi.port=9999
jmx.rmi.sport=9998
jmx.rmi.port.remoteobjects=
jmx.keystorepass=password
jmx.mina.monitor.enable=false
jmx.mina.poll.interval=1000
# Whether to always create the registry in-process, not attempting to 
# locate an existing registry at the specified port. Set to "true" in order
# to avoid the overhead of locating an existing registry when you always intend
# to create a new registry in any case.
jmx.registry.create=true
# Whether or not the MBeanServerFactoryBean should attempt to locate a running 
# MBeanServer before creating one
jmx.reuse.existing.server=true
# Whether to register the MBeanServer with the MBeanServerFactory, making it 
# available through MBeanServerFactory.findMBeanServer()
jmx.register.factory=true
# Whether any threads started for the JMXConnectorServer should be started as daemon threads
jmx.daemon=true
# Whether the JMXConnectorServer should be started in a separate thread
jmx.threaded=true

# Server properties
# max events to send in a single update
so.max.events.per.update=64
so.scheduler.pool_size=4
keyframe.cache.entry.max=500
war.deploy.server.check.interval=600000
fileconsumer.delayed.write=true
fileconsumer.queue.size=120
subscriberstream.buffer.check.interval=5000
subscriberstream.underrun.trigger=100
broadcaststream.auto.record=false

啟動Red5

雙擊red.bat即可啟動Red5伺服器

瀏覽器訪問檢視是否搭建成功

http://localhost:port(localhost、port為配置檔案中設定) 正確如下圖所示

視訊直播開始準備

下載demo

上圖可以看到install紅色的那行字。點選進去。選擇OLFA 安裝

如果下載不成功請看這裡

確定webapps下面沒有oflaDemo資料夾。下載小帥丶準備好的壓縮包解壓放進去即可

設定資訊釋出直播

檢視Settings區域。選擇Video後在Device中選擇攝像頭,並點選Start,出現視訊畫面(有可能會提示是否允許。選擇允許即可)

Server設定

回到Server畫面,在Location中輸入rtmp://localhost:1935/oflaDemo,地址與red5.properties中的rtmp協議的設定必須相同。
點選Connect

右側出現NetConnection.Connect.Success ,說明連線伺服器成功。

推送視訊流

切換到Video點選Publish頁面中的Publish按鈕即可對外發布直播

檢視視訊流

切換到Server點選play即可看推送的視訊流

用自己的頁面觀看直播

需要jwplayer。為了方便大家已經放在oflaDemo裡面。在上面的百度雲連結下載即可。

修改相關內容 file的值是直播頁面的Name的值。streamer則為Location裡面的URL

<html>
<head>
<title>Red5 - OFLADemo</title>
<script type='text/javascript' src='jwplayer.js'></script>
</head>
<body>
<div id="player">
<script type='text/javascript'>
  jwplayer('player').setup({
    'flashplayer': 'player.swf',
    'file': '直播頁面的Name',
    'streamer': 'rtmp://自己服務的IP:1935/oflaDemo',
    'controlbar': 'bottom',
    'width': '848',
    'height': '360'
  });
</script>
</div>
</body>
</html>

使用瀏覽器觀看

以上就是Red5的伺服器搭建步驟