AKStrem+ZLMediakit push and pull rtmp stream

Hits: 0

AKStrem+ZLMediakit push and pull [rtmp] stream

0. Preparation instructions

If you haven’t compiled and deployed AKStream and ZLMediakit, you can read my previous blog posts or the wiki documentation of related projects.

1. The following is the configuration file generated by ZLMediakit under the AKStreamKeeper self-care service. The difference from yours may be the secret, server address and related port number. Others should be basically the same.

#ZLMediaKit configuration file;;;;;;;;;;;;;;;;; 
!!!! This configuration file is an example configuration file, which is intended to tell readers the specific meaning and function of each configuration item, 
;; ;;;;;;;;;;;;;;;!!!! When executing cmake, the configuration file will be copied to release/${OS type}/${compiler type} (eg release/linux/ Debug) folder. 
;;;;;;;;;;;;;;;;!!!! This folder (release/${OS type}/${compile type}) is also the executable program generation target path, in When the MediaServer process is executed, it will load the config.ini file in the same directory as the configuration file by default, 
;;;;;;;;;;;;;;;;;!!!! If you modify this example configuration file ( conf/config.ini), and will not be loaded by the MediaServer process, because the MediaServer process loads release/${OS type}/${compile type}/config.ini by default. 
;;;;;;;;;;;;;;;;!!!! Of course, every time you execute cmake, the file will indeed be copied to release/${OS type}/${Compile type} /config.ini, 
;;;;;;;;;;;;;;;;!!!! But it is generally recommended that you directly modify release/${OS type}/${compile type}/config.ini file, modifying this file generally does not work, unless you use the -c parameter to specify this file when running MediaServer. 
[api]

;;;;;;;;;;;;;;;;; Whether to debug http api, after enabling debugging, it will print the content of each http request and reply 
apiDebug = 0

;;;;;;;;;;;;;;;;;Some more sensitive http api need to provide secret when accessing, otherwise no permission to call 
;;;;;;;;;;;;;;;; ;If it is accessed through 127.0.0.1, then you can not provide secret 
secret = 8725 c73f7-bb6b- 4889 -a715-d9eb2d1923cc

;;;;;;;;;;;;;;;;; the root directory of the screenshot save path, the screenshot is generated and obtained through http api (/index/api/getSnap) 
snapRoot = ./www/snap/

;;;;;;;;;;;;;;;;The default screenshot image, after starting FFmpeg to take a screenshot but the screenshot has not been generated, you can return to the default preset image 
defaultSnap = ./www/logo.png

[ ffmpeg]

;;;;;;;;;;;;;;;;;FFmpeg executable program path, supports relative path/absolute path 
bin = /usr/bin/ffmpeg

;;;;;;;;;;;;;;;;;FFmpeg's command template for pulling and pushing streams, through which some parameters for re-encoding can be set 
cmd = %s -re -i %s -vcodec copy -acodec copy -f flv -y %s

;;;;;;;;;;;;;;;;;FFmpeg command to generate screenshots, you can change the screenshot resolution or quality by modifying the configuration 
snap = %s -i %s -y -f mjpeg -t 0.001 %s

;;;;;;;;;;;;;;;;;path to FFmpeg log, if left blank, no FFmpeg log will be generated 
;;;;;;;;;;;;;;;; can be relative (relative to the executable program directory) or absolute path 
log = ./ffmpeg/ffmpeg.log

;;;;;;;;;;;;;;;;; Automatic restart time (seconds), the default is 0, that is, no automatic restart. Mainly to avoid the asynchronous phenomenon caused by long-term ffmpeg streaming 
restart_sec = 0 

[general]

;;;;;;;;;;;;;;;;; enable virtual host 
enableVhost = 0

;;;;;;;;;;;;;;;;;Player or streamer will trigger hook.on_flow_report event (how much flow event to use) after disconnection, 
;;;;;;;;;;; ;;;;;;; The flowThreshold parameter controls the threshold for triggering the hook.on_flow_report event, which is triggered only after the usage flow exceeds the threshold, unit KB 
flowThreshold = 0

;;;;;;;;;;;;;;;;;Maximum waiting time for playback, in milliseconds 
;;;;;;;;;;;;;;;;;When playing a stream, if The stream does not exist, 
;;;;;;;;;;;;;;;;ZLMediaKit will make the player wait at most maxStreamWaitMS milliseconds 
;;;;;;;;;;;;;;;;;; During this time, if the stream is registered successfully, it will immediately return to the player that the player has played successfully 
;;;;;;;;;;;;;;;;; otherwise, it will return that the player does not find the stream. The purpose of this mechanism is to first Play and push the stream 
maxStreamWaitMS = 15000

;;;;;;;;;;;;;;;;;Maximum waiting time to trigger hook.on_stream_none_reader event when a stream is not watched, in milliseconds 
;;;;;;;;;;;;; ;;;;In conjunction with hook.on_stream_none_reader event, it can automatically stop pulling stream or stop receiving push stream when no one is watching 
streamNoneReaderDelayMS = 20000

;;;;;;;;;;;;;;;;; whether to add mute aac audio globally, it is valid when transferring protocol 
;;;;;;;;;;;;;;;;; When a single video stream cannot be opened in seconds, adding mute audio can speed up the second opening speed 
addMuteAudio = 1

;;;;;;;;;;;;;;;;; whether to delete the previous media stream data if the stream is disconnected and reconnected successfully when pulling the stream proxy, if it is deleted, it will start again, 
;;;;;;; ;;;;;;;;;;If it is not deleted, it will continue to write after the last data (when recording hls/mp4, it will continue to write after the previous file) 
resetWhenRePlay = 1

;;;;;;;;;;;;;;;;; whether to convert to hls by default when pushing streams, this setting can be overridden in the hook interface (on_publish) 
publishToHls = 1

;;;;;;;;;;;;;;;;; whether the default mp4 video recording when streaming, the hook interface (on_publish) can override this setting 
publishToMP4 = 0

;;;;;;;;;;;;;;;;; Combined write cache size (in milliseconds), combined write means that the server caches certain data before writing to the socket at one time, which can improve performance, but it will Increase delay 
;;;;;;;;;;;;;;;;;After opening, it will also close TCP_NODELAY and open MSG_MORE 
mergeWriteMS = 0

;;;;;;;;;;;;;;;;; global timestamp override switch, when the protocol is transferred, timestamp the frame 
;;;;;;;;;;;;;;; ;;This switch takes effect when rtsp/rtmp/rtp push stream, rtsp/rtmp/hls pull stream proxy transfer protocol;;;;;;;;;;;;;;;;; 
will directly affect rtsp/rtmp/hls Timestamps of protocols such as /mp4/flv 
;;;;;;;;;;;;;;;;; the same protocol does not affect (for example, rtsp/rtmp push stream, then the time will not be affected when playing rtsp/rtmp stamp) 
modifyStamp = 0

;;;;;;;;;;;;;;;;; the unique id of the server, which is used to distinguish which server when the hook is triggered 
mediaServerId = HKJFQHaAcVtFFtJC

;;;;;;;;;;;;;;;;; whether to turn the protocol on or off globally 
enable_audio = 1

;;;;;;;;;;;;;;;;; the following is the switch for on-demand transfer protocol, when testing the ZLMediaKit's receiving and pushing performance, please set the following switch to 1 
;;;;;;;;; ;;;;;;;;; If you don't use a certain protocol, you can set the following switch to 1 to save resources (but it can still be played, but the first player experience is slightly worse), 
;;;;; ;;;;;;;;;;;; If you want to get the best user experience for a certain protocol, please set it to 0 (the first player can open it in seconds, and the screen will not be blurred) 
;;;;;;;;; ;;;;;;;;;whether the hls protocol is generated on demand, if hls.segNum is configured to 0 (meaning hls recording), then hls will always be generated (regardless of this switch) 
hls_demand = 0

;;;;;;;;;;;;;;;;;rtsp[s] protocol whether to generate on demand 
rtsp_demand = 0

;;;;;;;;;;;;;;;;rtmp[s], http[s]-flv, ws[s]-flv protocol whether to generate 
rtmp_demand = 0 on demand

;;;;;;;;;;;;;;;;; http[s]-ts protocol to generate 
ts_demand = 0 on demand

;;;;;;;;;;;;;;;; http[s]-fmp4, ws[s]-fmp4 protocol whether to generate on demand 
fmp4_demand = 0

;;;;;;;;;;;;;;;;; wait for the uninitialized track at most, in milliseconds, after the timeout, the uninitialized track will be ignored 
wait_track_ready_ms = 10000

;;;;;;;;;;;;;;;;;If the stream has only a single track, wait for a few milliseconds at most, and no data from other tracks is received after the timeout, it is considered a single track 
;;;;;;;; ;;;;;;;;;;If the protocol metadata declares a specific number of tracks, then there is no such waiting time 
wait_add_track_ms = 3000

;;;;;;;;;;;;;;;;;If the track is not ready, we will cache the frame data first, but there is a maximum number limit to prevent memory overflow 
unready_frame_cache = 100

;;;;;;;;;;;;;;;;; After the push stream is disconnected, you can continue to push the stream by reconnecting within the timeout period, so that the player will continue to play. 
;;;;;;;;;;;;;;;;; set 0 to disable this feature (disconnecting the push stream will cause the player to be disconnected immediately) 
;;;;;;;;;;;;;;; ;;This parameter should not be greater than the player timeout 
continue_push_ms = 15000 

[hls]

;;;;;;;;;;;;;;;;; the buf size of the hls write file, adjusting the parameters can improve the file io performance 
fileBufSize = 65536

;;;;;;;;;;;;;;;;hls save file path;;;;;;;;;;;;;;;; 
can be relative (relative to the executable program directory) or absolute path 
filePath = ./www

;;;;;;;;;;;;;;;;;hls max slice time 
segDur = 2

;;;;;;;;;;;;;;;;; In the m3u8 index, hls reserves the number of slices (the actual number of reserved slices is 2~3) 
;;;;;;;;;;;;; ;;;;;If set to 0, slices are not deleted, but saved as on-demand 
segNum = 3

;;;;;;;;;;;;;;;;; The number of HLS slices that remain on disk after they are removed from the m3u8 file 
segRetain = 5

;;;;;;;;;;;;;;;;; whether to broadcast ts slice completion notification 
broadcastRecordTs = 0

;;;;;;;;;;;;;;;;Delay delay of live hls file deletion, in seconds, issue: #913
 deleteDelaySec = 0 [ hook 

] 
enable = 1 
on_flow_report = http://192.168.2.145 : 13000 /MediaServer/WebHook/ On FlowReport
 on_http_access = 
 on_play = http:// 192.168 . 2.145 : 13000 /MediaServer/WebHook/ On Play
 on_publish = http:// 192.168 . 2.145 : 13000 /MediaServer/WebHook/ On Publish
 on_record_mp4 = http://192.168.2.145:13000/MediaServer/WebHook/OnRecordMp4
on_record_ts = http://192.168.2.145:13000/MediaServer/WebHook/OnRecordTs
on_rtsp_auth = 
on_rtsp_realm = 
on_shell_login = http://192.168.2.145:13000/MediaServer/WebHook/OnShellLogin
on_stream_changed = http://192.168.2.145:13000/MediaServer/WebHook/OnStreamChanged
on_stream_none_reader = http://192.168.2.145:13000/MediaServer/WebHook/OnStreamNoneReader
on_stream_not_found = 
on_server_started = 
timeoutSec = 20

[cluster]

;;;;;;;;;;;;;;;;; set the source site pull stream url template, the format is similar to printf, the first %s specifies the app, and the second %s specifies the stream_id, 
;;;; ;;;;;;;;;;;;;On_stream_not_found and on_stream_none_reader hooks will be invalid when cluster mode is enabled. ;;;;;;;;;;;;;;;; 
The following types are supported in trace mode: 
;; ;;;;;;;;;;;;;;rtmp method: rtmp://127.0.0.1:1935/%s/%s 
;;;;;;;;;;;;;;;;; rtsp method: rtsp://127.0.0.1:554/%s/%s 
;;;;;;;;;;;;;;;;hls method: http://127.0.0.1:80/%s /%s/hls.m3u8 ;;;;;;;;;;;;;;;; 
http-ts method: http://127.0.0.1:80/%s/%s.live.ts 
;; ;;;;;;;;;;;;;;; supports multiple origin sites, and different origin sites are separated by a semicolon (;) 
origin_url =

;;;;;;;;;;;;;;;;;Total traceability timeout, in seconds, float; if there are 3 origin sites, the single traceability timeout is timeout_sec divided by 3 
;;;; ;;;;;;;;;;;;; The timeout time of a single traceability should not exceed the general.maxStreamWaitMS configuration 
timeout_sec = 15 

[http]


;;;;;;;;;;;;;;;; http server character encoding, default gb2312 charSet = utf- 8 on windows

;;;;;;;;;;;;;;;; http link timeout 
keepAliveSecond = 30

;;;;;;;;;;;;;;;;; the maximum number of bytes of the http request body, if the body of the post is too large, it is not suitable for caching the body in memory 
maxReqSize = 40960

;;;;;;;;;;;;;;;;;404 webpage content, users can customize 404 webpage 
;;;;;;;;;;;;;;;;;notFound=<html>< head><title>404 Not Found</title></head><body bgcolor="white"><center><h1>The resource you accessed does not exist! </h1></center><hr><center>ZLMediaKit-4.0</center></body></html> 
;;;;;;;;;;;;;;;;http server listening port 
port = 55000

;;;;;;;;;;;;;;;;http file server root directory 
;;;;;;;;;;;;;;;; can be relative (relative to the executable program directory ) or absolute path 
rootPath = ./www

;;;;;;;;;;;;;;;;; http file server read file cache size, in BYTE, adjust this parameter to optimize file io performance 
sendBufSize = 65536

;;;;;;;;;;;;;;;;; https server listening on port 
sslport = 443

;;;;;;;;;;;;;;;;; whether to display the folder menu, after opening, you can browse the folder 
dirMenu = 1

;;;;;;;;;;;;;;;; virtual directory, the virtual directory name and file path are separated by ",", and multiple configuration paths are separated by ";" 
;;;;;;; ;;;;;;;;;;; for example, assign it to app_a,/path/to/a;app_b,/path/to/b then 
;;;;;;;;;;;;;;;;access http ://127.0.0.1/app_a/file_a The corresponding file path is /path/to/a/file_a ;;;;;;;;;;;;;;;; 
visit http://127.0.0.1/app_b The file path corresponding to /file_b is /path/to/b/file_b ;;;;;;;;;;;;;;;;; To 
access other http paths, the corresponding file path is still in the rootPath 
virtualPath =

;;;;;;;;;;;;;;;;; file caching with a suffix is ​​prohibited, separated by "," 
;;;;;;;;;;;;;;;;;For example, the assignment is . mp4,.flv 
;;;;;;;;;;;;;;;;; then access files with suffix .mp4 and .flv will not be cached 
forbidCacheSuffix = 

[multicast]

;;;;;;;;;;;;;;;;;rtp multicast cut-off multicast ip address 
addrMax = 239.255 . 255.255

;;;;;;;;;;;;;;;;;rtp multicast start multicast ip address 
addrMin = 239.0 . 0.0

udpTTL = 64

[record]

;;;;;;;;;;;;;;;;; the application name of mp4 recording or mp4 on-demand, by restricting the application name, it can prevent random on-demand 
;;;;;;;;;;;;;;;; ;;On-demand files must be placed in this folder 
appName = record

;;;;;;;;;;;;;;;;;mp4 recording and writing file cache, unit BYTE, adjusting parameters can improve file io performance 
fileBufSize = 65536

;;;;;;;;;;;;;;;;;mp4 recording and saving, mp4 on-demand root path 
;;;;;;;;;;;;;;;;; executor directory) or absolute path 
filePath = ./www

;;;;;;;;;;;;;;;;; mp4 recording slice time, in seconds 
fileSecond = 120

;;;;;;;;;;;;;;;;; mp4 on-demand data volume per stream, in milliseconds, 
;;;;;;;;;;;;;;;;; Make the on-demand data sending volume smoother, increasing the value saves CPU resources 
sampleMS = 500


;;;;;;;;;;;;;;;;;Whether to perform secondary key frame index write header fastStart = 0 after mp4 recording is completed

;;;;;;;;;;;;;;;; MP4 on demand (rtsp/rtmp/http-flv/ws-flv) whether to loop the file 
fileRepeat = 0 

[rtmp]

;;;;;;;;;;;;;;;;;rtmp must complete the handshake within this time, otherwise the server will disconnect the connection, in seconds 
handshakeSecond = 15

;;;;;;;;;;;;;;;;;rtmp timeout, if no data is received from the client within this time, 
;;;;;;;;;;;;;;;;; or If the tcp send buffer exceeds this time, the connection will be disconnected. The unit of second is 
keepAliveSecond = 15

;;;;;;;;;;;;;;;;; whether to regenerate timestamps when receiving rtmp push streams (many stream pushers have really bad timestamps) 
modifyStamp = 0

;;;;;;;;;;;;;;;;;rtmp server listening on port 
port = 1935

;;;;;;;;;;;;;;;; rtmps server listening address 
sslport = 0 

[rtp]

;;;;;;;;;;;;;;;;; audio mtu size, this parameter limits the maximum number of bytes of rtp, it is recommended not to exceed 1400 
;;;;;;;;;;;;;;;; ;Increasing this value will significantly increase the live delay 
audioMtuSize = 600

;;;;;;;;;;;;;;;;; video mtu size, this parameter limits the maximum number of bytes of rtp, it is recommended not to exceed 1400 
videoMtuSize = 1400

;;;;;;;;;;;;;;;;; the maximum length of the rtp packet, in KB, is mainly used to identify the wrong rtp when the TCP context is destroyed 
rtpMaxSize = 10 

[rtp_proxy]

;;;;;;;;;;;;;;;;; export debug data (including rtp/ps/h264) to this directory, if empty, close data export 
dumpDir =

;;;;;;;;;;;;;;;;; udp and tcp proxy server, support rtp (must be ts or ps type) proxy 
port = 10000

;;;;;;;;;;;;;;;;;rtp timeout, in seconds 
timeoutSec = 15

;;;;;;;;;;;;;;;; random port range, at least 36 ports 
;;;;;;;;;;;;;;;;; this range also limits the rtsp server udp port range 
port_range = 33000 - 52000 

[rtc]

;;;;;;;;;;;;;;;;;rtc playback push stream, playback timeout 
timeoutSec = 15

;;;;;;;;;;;;;;;;; the visible ip of this machine to the rtc client, when it is used as a server, it is generally the public network ip. When it is empty, it will automatically obtain the network card ip 
externIP = 117.141 . 154.39

;;;;;;;;;;;;;;;;rtc udp server listening port number, all rtc clients will transmit stun/dtls/srtp/srtcp data through this port, 
;;;;;;;; ;;;;;;;;;This port is multi-threaded and supports connection migration caused by client network switching 
;;;;;;;;;;;;;;;;;It should be noted that if the server In NAT, when port mapping needs to be done, you must ensure that the external network mapped port is the same as the 
port port = 8000

;;;;;;;;;;;;;;;;; Set the remb bit rate, when it is not 0, turn off twcc and turn on remb. This setting is valid when rtc pushes the stream, and can control the push stream quality 
;;;;;;;;;;;;;;;;;; twcc has been implemented to automatically adjust the bit rate, and remb is turned off to adjust the bit rate according to the real network conditions 
rembBitRate = 0

;;;;;;;;;;;;;;;;;The audio codec type supported by rtc has higher priority 
;;;;;;;;;;;;;;;;The following example For all supported audio codec 
preferredCodecA = PCMU,PCMA,opus,mpeg4-generic

;;;;;;;;;;;;;;;;;The video codec type supported by rtc has a higher priority in the front 
;;;;;;;;;;;;;;;;The following example For all supported video codec 
preferredCodecV = H264,H265,AV1X,VP9,VP8

[rtsp]

;;;;;;;;;;;;;;;;rtsp proprietary authentication mode is base64 or md5 mode 
authBasic = 0

;;;;;;;;;;;;;;;;;whether rtsp pull-stream and push-stream proxy is direct proxy mode 
;;;;;;;;;;;;;;;;support after direct proxy Arbitrary encoding format, but it will cause the GOP cache to fail to locate the I frame, which may lead to the opening of the video 
; As a result, you cannot use udp proxy 
;;;;;;;;;;;;;;;;; Assuming your pull source address is not 264 or 265 or AAC, then you can use direct proxy to support rtsp proxy 
; ;;;;;;;;;;;;;;;; if you are rtsp push-pull streaming, but webrtc playback, it is also recommended to turn off direct proxy mode, 
;;;;;;;;;;;;;;;; ;Because when proxying directly, there may be no sps pps in rtp, which will cause webrtc to be unable to play; in addition, webrtc does not support Single NAL Unit Packets type rtp ;;;;;;;;;;;;;;;;; 
rtsp is enabled by default Direct proxy, because rtmp does not have these problems, it is forced to open 
directProxy = 1 for direct proxy

;;;;;;;;;;;;;;;;;rtsp must complete the handshake within this time, otherwise the server will disconnect the connection, in seconds 
handshakeSecond = 15

;;;;;;;;;;;;;;;;;rtsp timeout, if no data is received from the client within this time, 
;;;;;;;;;;;;;;;;; or If the tcp send buffer exceeds this time, the connection will be disconnected. The unit of second is 
keepAliveSecond = 15

;;;;;;;;;;;;;;;;;rtsp server listening address 
port = 554

;;;;;;;;;;;;;;;;rtsps server listening address 
sslport = 0 

[shell]

;;;;;;;;;;;;;;;;; debug telnet server accepts max bufffer size 
maxReqSize = 1024

;;;;;;;;;;;;;;;; debug telnet server listening port 
port = 0 

[ffmpeg_templete] 
rtsp_tcp2flv = %s -re -rtsp_transport tcp -i %s -vcodec copy -acodec copy -f flv -y %s
 ffmpeg2flv =%s -re -i %s -vcodec copy -acodec copy -f flv -y %s

2. The following are the deployed and running AKStream and MediaServer servers

3. Use ffmpeg to push rtmp video stream

ffmpeg -re -i 1 .mp4 -vcodec libx264 -bf 0 -acodec pcm_mulaw -ar 8000 -ac 1 -f flv -y rtmp://yourserverip/live/livestream/test?secret=8725c73f7-bb6b-4889-a715- d9eb2d1923cc #where
 secret is the token of your MediaServer, just replace it with yours

4. Use ffplay to play rtmp video stream

ffplay rtmp://yourserverip/live/livestream/test?secret=8725c73f7-bb6b-4889-a715-d9eb2d1923cc

Leave a Reply

Your email address will not be published.