-
Notifications
You must be signed in to change notification settings - Fork 1
/
doc.go
161 lines (102 loc) · 4.49 KB
/
doc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
Streamserve: an http server for streaming live media.
Copyright 2015 Tom Clegg
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Streamserve is a live media streaming server: it reads data from a
pipe and distributes it to many HTTP clients.
Usage
In general:
streamserve [options]
PCM audio:
streamserve -address :8800 -source-buffer 40 -frame-bytes 44100 -reopen=false -path <(arecord -f cd --file-type raw)
MP3 audio:
streamserve -address 127.0.0.1:8800 -source-buffer 40 -frame-bytes 2048 -reopen=false -path <(arecord -f cd --file-type raw | lame -r -s 44.1 -b 128 - -)
Show all options:
streamserve -help
Purpose
Unlike general-purpose web servers, streamserve it suitable when:
1. The process supplying the data stream is expensive. For example,
you want to encode media on the fly and send it to 100 clients, but
you can't run 100 encoding processes at once.
2. It's acceptable for a given client (a) to start receiving data
mid-stream, and (b) if it has been receiving data too slowly, to skip
some data segments to catch up with everyone else. (For this to work,
the server must understand enough about the data stream format to
interrupt and resume at appropriate positions.)
3. Clients are expected to read data at the same speed data is
received from the source.
Listening address
By default, streamserve listens for connections at port 80 on all
network interfaces.
Note: Use 'sudo setcap cap_net_bind_service=+ep [...]/bin/streamserve'
if you want to listen on port 80 or another privileged port. Don't run
streamserve as root.
Specify an IP address and port number:
-address 10.1.2.3:10123
Specify a port number, listen on all interfaces:
-address :1234
Choose any unused port (can be useful for testing):
-address :0
Default:
-address :80
Frames
Input data is split into frames. When a client first connects, it
starts receiving data at a frame boundary. When a client is receiving
data too slowly and skips some of the stream, the skipped part starts
and ends at frame boundaries.
The raw filter chunks its input into fixed-size blocks. It pays no
attention to the data content.
-filter raw -frame-bytes 64
The mp3 filter accepts physical MP3 frames. It skips past input data
that doesn't look like MP3 frames. The -frame-bytes argument is a
maximum: it must be big enough to hold the biggest frame.
-filter mp3 -frame-bytes 2048
Buffers
Data from the input FIFO is read into a fixed-size ring buffer, with a
static number of frames. The size of the buffer determines how far a
client can fall behind before it catches up by skipping part of the
stream.
The buffer size is given in frames. This is a 1048576-byte buffer:
-frame-bytes 1024 -source-buffer 1024
Maximum client lag is determined by the source buffer argument and the
actual frame sizes: If using -filter=mp3, the above example will skip
frames when a client lags behind by 1024 mp3 frames (not 1048576
bytes).
Starting and stopping
You can control streamserve's behaviour when a data source closes, and
when it becomes idle (no clients are connected).
Keep reading data even when no clients are connected:
-close-idle=false
Close the FIFO when no clients are connected:
-close-idle=true
If the input FIFO reaches EOF or encounters an error, reopen it and
keep reading.
-reopen=true
If the input FIFO reaches EOF or encounters an error, disconnect all
clients and exit.
-reopen=false
Limits
Limit CPU usage. The default is to use as many threads as you have CPU
cores.
-cpu-max 1
Disconnect clients after a specified number of bytes.
-client-max-bytes 1000000000
Limit how fast the input is read. (This is meant for testing. You
could also use it to serve static content from a regular file as if it
were a live stream, although a regular static file server would
probably be a better choice.) Speed is given in bytes per second. The
default is to read data as fast as the input FIFO supplies it.
-source-bandwidth 16000
*/
package main