Skip to content

Commit d12fc7f

Browse files
committed
fix #405, improve the HTTP FLV performance to 6k. 2.0.171
1 parent 4df19ba commit d12fc7f

13 files changed

+1187
-916
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,7 @@ The play HTTP FLV benchmark by [SB](https://github.com/simple-rtmp-server/srs-be
719719
| 2014-05-24 | 2.0.168 | 2.3k(2300) | players | 92% | 276MB | [code][p17] |
720720
| 2014-05-24 | 2.0.169 | 3.0k(3000) | players | 94% | 188MB | [code][p18] |
721721
| 2014-05-24 | 2.0.170 | 3.0k(3000) | players | 89% | 96MB | [code][p19] |
722+
| 2014-05-25 | 2.0.171 | 6.0k(6000) | players | 84% | 297MB | [code][p20] |
722723

723724
### Latency benchmark
724725

trunk/src/app/srs_app_http_conn.cpp

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -175,37 +175,52 @@ int SrsHttpResponseWriter::writev(iovec* iov, int iovcnt, ssize_t* pnwrite)
175175
}
176176

177177
// send in chunked encoding.
178-
int nb_iovss = iovcnt * 4;
178+
int nb_iovss = 3 + iovcnt;
179179
iovec* iovss = iovss_cache;
180180
if (nb_iovss_cache < nb_iovss) {
181181
srs_freep(iovss_cache);
182182
nb_iovss_cache = nb_iovss;
183183
iovss = iovss_cache = new iovec[nb_iovss];
184184
}
185185

186-
char* pheader_cache = header_cache;
186+
// send in chunked encoding.
187+
188+
// chunk size.
189+
int size = 0;
187190
for (int i = 0; i < iovcnt; i++) {
188-
int left = SRS_HTTP_HEADER_CACHE_SIZE - (int)(pheader_cache - header_cache);
189-
srs_assert(left > 0);
190-
191191
iovec* data_iov = iov + i;
192-
int nb_size = snprintf(pheader_cache, left, "%x", (int)data_iov->iov_len);
193-
194-
iovec* iovs = iovss + (i * 4);
195-
iovs[0].iov_base = (char*)pheader_cache;
196-
iovs[0].iov_len = (int)nb_size;
197-
iovs[1].iov_base = (char*)SRS_HTTP_CRLF;
198-
iovs[1].iov_len = 2;
199-
iovs[2].iov_base = (char*)data_iov->iov_base;
200-
iovs[2].iov_len = (int)data_iov->iov_len;
201-
iovs[3].iov_base = (char*)SRS_HTTP_CRLF;
202-
iovs[3].iov_len = 2;
203-
204-
pheader_cache += nb_size;
192+
size += data_iov->iov_len;
193+
}
194+
written += size;
195+
196+
// chunk header
197+
int nb_size = snprintf(header_cache, SRS_HTTP_HEADER_CACHE_SIZE, "%x", size);
198+
iovec* iovs = iovss;
199+
iovs[0].iov_base = (char*)header_cache;
200+
iovs[0].iov_len = (int)nb_size;
201+
iovs++;
202+
203+
// chunk header eof.
204+
iovs[0].iov_base = (char*)SRS_HTTP_CRLF;
205+
iovs[0].iov_len = 2;
206+
iovs++;
207+
208+
// chunk body.
209+
for (int i = 0; i < iovcnt; i++) {
210+
iovec* data_iov = iov + i;
211+
iovs[0].iov_base = (char*)data_iov->iov_base;
212+
iovs[0].iov_len = (int)data_iov->iov_len;
213+
iovs++;
205214
}
206215

216+
// chunk body eof.
217+
iovs[0].iov_base = (char*)SRS_HTTP_CRLF;
218+
iovs[0].iov_len = 2;
219+
iovs++;
220+
221+
// sendout all ioves.
207222
ssize_t nwrite;
208-
if ((ret = skt->writev(iovss, nb_iovss, &nwrite)) != ERROR_SUCCESS) {
223+
if ((ret = srs_write_large_iovs(skt, iovss, nb_iovss, &nwrite)) != ERROR_SUCCESS) {
209224
return ret;
210225
}
211226

@@ -1442,6 +1457,21 @@ int SrsFlvStreamEncoder::dump_cache(SrsConsumer* /*consumer*/)
14421457
return ERROR_SUCCESS;
14431458
}
14441459

1460+
#ifdef SRS_PERF_FAST_FLV_ENCODER
1461+
SrsFastFlvStreamEncoder::SrsFastFlvStreamEncoder()
1462+
{
1463+
}
1464+
1465+
SrsFastFlvStreamEncoder::~SrsFastFlvStreamEncoder()
1466+
{
1467+
}
1468+
1469+
int SrsFastFlvStreamEncoder::write_tags(SrsSharedPtrMessage** msgs, int count)
1470+
{
1471+
return enc->write_tags(msgs, count);
1472+
}
1473+
#endif
1474+
14451475
SrsAacStreamEncoder::SrsAacStreamEncoder()
14461476
{
14471477
enc = new SrsAacEncoder();
@@ -1612,7 +1642,11 @@ int SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
16121642
srs_assert(entry);
16131643
if (srs_string_ends_with(entry->pattern, ".flv")) {
16141644
w->header()->set_content_type("video/x-flv");
1645+
#ifdef SRS_PERF_FAST_FLV_ENCODER
1646+
enc = new SrsFastFlvStreamEncoder();
1647+
#else
16151648
enc = new SrsFlvStreamEncoder();
1649+
#endif
16161650
} else if (srs_string_ends_with(entry->pattern, ".aac")) {
16171651
w->header()->set_content_type("audio/x-aac");
16181652
enc = new SrsAacStreamEncoder();
@@ -1658,6 +1692,10 @@ int SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
16581692
}
16591693
}
16601694

1695+
#ifdef SRS_PERF_FAST_FLV_ENCODER
1696+
SrsFastFlvStreamEncoder* ffe = dynamic_cast<SrsFastFlvStreamEncoder*>(enc);
1697+
#endif
1698+
16611699
while (true) {
16621700
pprint->elapse();
16631701

@@ -1684,7 +1722,15 @@ int SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
16841722
}
16851723

16861724
// sendout all messages.
1725+
#ifdef SRS_PERF_FAST_FLV_ENCODER
1726+
if (ffe) {
1727+
ret = ffe->write_tags(msgs.msgs, count);
1728+
} else {
1729+
ret = streaming_send_messages(enc, msgs.msgs, count);
1730+
}
1731+
#else
16871732
ret = streaming_send_messages(enc, msgs.msgs, count);
1733+
#endif
16881734

16891735
// free the messages.
16901736
for (int i = 0; i < count; i++) {

trunk/src/app/srs_app_http_conn.hpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,9 @@ class SrsHttpMessage;
7171

7272
#ifdef SRS_AUTO_HTTP_PARSER
7373

74-
// for HTTP FLV, each video/audio packet is send by 3 iovs,
75-
// while each iov is send by 4 sub iovs, that is needs 3 chunk header,
76-
// suppose each header is 16 length, 3*16=48 is ok.
77-
// that is, 512 can used for 16 iovs to send.
78-
#define SRS_HTTP_HEADER_CACHE_SIZE 512
74+
// the http chunked header size,
75+
// for writev, there always one chunk to send it.
76+
#define SRS_HTTP_HEADER_CACHE_SIZE 64
7977

8078
/**
8179
* response writer use st socket
@@ -214,8 +212,8 @@ class SrsHttpMessage : public ISrsHttpMessage
214212
* set the original messages, then update the message.
215213
*/
216214
virtual int update(std::string url, http_parser* header,
217-
SrsFastBuffer* body, std::vector<SrsHttpHeaderField>& headers
218-
);
215+
SrsFastBuffer* body, std::vector<SrsHttpHeaderField>& headers
216+
);
219217
private:
220218
virtual SrsConnection* connection();
221219
public:
@@ -454,7 +452,7 @@ class ISrsStreamEncoder
454452
*/
455453
class SrsFlvStreamEncoder : public ISrsStreamEncoder
456454
{
457-
private:
455+
protected:
458456
SrsFlvEncoder* enc;
459457
public:
460458
SrsFlvStreamEncoder();
@@ -469,6 +467,24 @@ class SrsFlvStreamEncoder : public ISrsStreamEncoder
469467
virtual int dump_cache(SrsConsumer* consumer);
470468
};
471469

470+
#ifdef SRS_PERF_FAST_FLV_ENCODER
471+
/**
472+
* the fast flv stream encoder.
473+
* @see https://github.com/simple-rtmp-server/srs/issues/405
474+
*/
475+
class SrsFastFlvStreamEncoder : public SrsFlvStreamEncoder
476+
{
477+
public:
478+
SrsFastFlvStreamEncoder();
479+
virtual ~SrsFastFlvStreamEncoder();
480+
public:
481+
/**
482+
* write the tags in a time.
483+
*/
484+
virtual int write_tags(SrsSharedPtrMessage** msgs, int count);
485+
};
486+
#endif
487+
472488
/**
473489
* the ts stream encoder, remux rtmp stream to ts stream.
474490
*/

trunk/src/core/srs_core.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3131
// current release version
3232
#define VERSION_MAJOR 2
3333
#define VERSION_MINOR 0
34-
#define VERSION_REVISION 170
34+
#define VERSION_REVISION 171
3535

3636
// server info.
3737
#define RTMP_SIG_SRS_KEY "SRS"

trunk/src/core/srs_core_performance.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
181181
#undef SRS_PERF_SO_SNDBUF_SIZE
182182
#endif
183183

184+
/**
185+
* define the following macro to enable the fast flv encoder.
186+
* @see https://github.com/simple-rtmp-server/srs/issues/405
187+
*/
188+
#undef SRS_PERF_FAST_FLV_ENCODER
189+
#define SRS_PERF_FAST_FLV_ENCODER
190+
184191
#endif
185192

0 commit comments

Comments
 (0)