Skip to content

Commit f35ec21

Browse files
committed
for bug #241, support merged read. 2.0.48
1 parent adf95d2 commit f35ec21

9 files changed

+152
-23
lines changed

trunk/src/app/srs_app_recv_thread.cpp

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2626
#include <srs_protocol_rtmp.hpp>
2727
#include <srs_protocol_stack.hpp>
2828
#include <srs_app_rtmp_conn.hpp>
29+
#include <srs_protocol_buffer.hpp>
30+
31+
// when we read from socket less than this value,
32+
// sleep a while to merge read.
33+
// @see https://github.com/winlinvip/simple-rtmp-server/issues/241
34+
#define SRS_MERGED_READ_SIZE (SOCKET_READ_SIZE / 10)
35+
// the time to sleep to merge read, to read more bytes.
36+
#define SRS_MERGED_READ_US (300 * 1000)
2937

3038
ISrsMessageHandler::ISrsMessageHandler()
3139
{
@@ -271,6 +279,30 @@ void SrsPublishRecvThread::stop()
271279
trd.stop();
272280
}
273281

282+
void SrsPublishRecvThread::on_thread_start()
283+
{
284+
// we donot set the auto response to false,
285+
// for the main thread never send message.
286+
287+
// enable the merge read
288+
// @see https://github.com/winlinvip/simple-rtmp-server/issues/241
289+
rtmp->set_merge_read(true, this);
290+
}
291+
292+
void SrsPublishRecvThread::on_thread_stop()
293+
{
294+
// we donot set the auto response to true,
295+
// for we donot set to false yet.
296+
297+
// when thread stop, signal the conn thread which wait.
298+
// @see https://github.com/winlinvip/simple-rtmp-server/issues/244
299+
st_cond_signal(error);
300+
301+
// disable the merge read
302+
// @see https://github.com/winlinvip/simple-rtmp-server/issues/241
303+
rtmp->set_merge_read(false, NULL);
304+
}
305+
274306
bool SrsPublishRecvThread::can_handle()
275307
{
276308
// publish thread always can handle message.
@@ -302,18 +334,19 @@ void SrsPublishRecvThread::on_recv_error(int ret)
302334
st_cond_signal(error);
303335
}
304336

305-
void SrsPublishRecvThread::on_thread_start()
306-
{
307-
// we donot set the auto response to false,
308-
// for the main thread never send message.
309-
}
310-
311-
void SrsPublishRecvThread::on_thread_stop()
337+
void SrsPublishRecvThread::on_read(ssize_t nread)
312338
{
313-
// we donot set the auto response to true,
314-
// for we donot set to false yet.
339+
if (nread < 0) {
340+
return;
341+
}
315342

316-
// when thread stop, signal the conn thread which wait.
317-
// @see https://github.com/winlinvip/simple-rtmp-server/issues/244
318-
st_cond_signal(error);
343+
/**
344+
* to improve read performance, merge some packets then read,
345+
* when it on and read small bytes, we sleep to wait more data.,
346+
* that is, we merge some data to read together.
347+
* @see https://github.com/winlinvip/simple-rtmp-server/issues/241
348+
*/
349+
if (nread < SRS_MERGED_READ_SIZE) {
350+
st_usleep(SRS_MERGED_READ_US);
351+
}
319352
}

trunk/src/app/srs_app_recv_thread.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3333
#include <vector>
3434

3535
#include <srs_app_thread.hpp>
36+
#include <srs_protocol_buffer.hpp>
3637

3738
class SrsRtmpServer;
3839
class SrsMessage;
@@ -132,7 +133,7 @@ class SrsQueueRecvThread : public ISrsMessageHandler
132133
* the publish recv thread got message and callback the source method to process message.
133134
* @see: https://github.com/winlinvip/simple-rtmp-server/issues/237
134135
*/
135-
class SrsPublishRecvThread : public ISrsMessageHandler
136+
class SrsPublishRecvThread : virtual public ISrsMessageHandler, virtual public IMergeReadHandler
136137
{
137138
private:
138139
SrsRecvThread trd;
@@ -163,13 +164,16 @@ class SrsPublishRecvThread : public ISrsMessageHandler
163164
public:
164165
virtual int start();
165166
virtual void stop();
167+
virtual void on_thread_start();
168+
virtual void on_thread_stop();
169+
// interface ISrsMessageHandler
166170
public:
167171
virtual bool can_handle();
168172
virtual int handle(SrsMessage* msg);
169173
virtual void on_recv_error(int ret);
174+
// interface IMergeReadHandler
170175
public:
171-
virtual void on_thread_start();
172-
virtual void on_thread_stop();
176+
virtual void on_read(ssize_t nread);
173177
};
174178

175179
#endif

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 47
34+
#define VERSION_REVISION 48
3535
// server info.
3636
#define RTMP_SIG_SRS_KEY "SRS"
3737
#define RTMP_SIG_SRS_ROLE "origin/edge server"

trunk/src/rtmp/srs_protocol_buffer.cpp

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,19 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2626
#include <srs_kernel_error.hpp>
2727
#include <srs_kernel_log.hpp>
2828

29-
// 4KB=4096
30-
// 8KB=8192
31-
// 16KB=16384
32-
// 32KB=32768
33-
// 64KB=65536
34-
// @see https://github.com/winlinvip/simple-rtmp-server/issues/241
35-
#define SOCKET_READ_SIZE 4096
29+
IMergeReadHandler::IMergeReadHandler()
30+
{
31+
}
32+
33+
IMergeReadHandler::~IMergeReadHandler()
34+
{
35+
}
3636

3737
SrsBuffer::SrsBuffer()
3838
{
39+
merged_read = false;
40+
_handler = NULL;
41+
3942
buffer = new char[SOCKET_READ_SIZE];
4043
}
4144

@@ -93,11 +96,27 @@ int SrsBuffer::grow(ISrsBufferReader* reader, int required_size)
9396
return ret;
9497
}
9598

99+
/**
100+
* to improve read performance, merge some packets then read,
101+
* when it on and read small bytes, we sleep to wait more data.,
102+
* that is, we merge some data to read together.
103+
* @see https://github.com/winlinvip/simple-rtmp-server/issues/241
104+
*/
105+
if (merged_read && _handler) {
106+
_handler->on_read(nread);
107+
}
108+
96109
srs_assert((int)nread > 0);
97110
append(buffer, (int)nread);
98111
}
99112

100113
return ret;
101114
}
102115

116+
void SrsBuffer::set_merge_read(bool v, IMergeReadHandler* handler)
117+
{
118+
merged_read = v;
119+
_handler = handler;
120+
}
121+
103122

trunk/src/rtmp/srs_protocol_buffer.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,45 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3434

3535
#include <srs_protocol_io.hpp>
3636

37+
// 4KB=4096
38+
// 8KB=8192
39+
// 16KB=16384
40+
// 32KB=32768
41+
// 64KB=65536
42+
// @see https://github.com/winlinvip/simple-rtmp-server/issues/241
43+
#define SOCKET_READ_SIZE 4096
44+
45+
/**
46+
* to improve read performance, merge some packets then read,
47+
* when it on and read small bytes, we sleep to wait more data.,
48+
* that is, we merge some data to read together.
49+
* @see https://github.com/winlinvip/simple-rtmp-server/issues/241
50+
*/
51+
class IMergeReadHandler
52+
{
53+
public:
54+
IMergeReadHandler();
55+
virtual ~IMergeReadHandler();
56+
public:
57+
/**
58+
* when read from channel, notice the merge handler to sleep for
59+
* some small bytes.
60+
* @remark, it only for server-side, client srs-librtmp just ignore.
61+
*/
62+
virtual void on_read(ssize_t nread) = 0;
63+
};
64+
3765
/**
3866
* the buffer provices bytes cache for protocol. generally,
3967
* protocol recv data from socket, put into buffer, decode to RTMP message.
4068
*/
4169
class SrsBuffer
4270
{
4371
private:
72+
// the merged handler
73+
bool merged_read;
74+
IMergeReadHandler* _handler;
75+
// data and socket buffer
4476
std::vector<char> data;
4577
char* buffer;
4678
public:
@@ -79,6 +111,16 @@ class SrsBuffer
79111
* @remark, we actually maybe read more than required_size, maybe 4k for example.
80112
*/
81113
virtual int grow(ISrsBufferReader* reader, int required_size);
114+
public:
115+
/**
116+
* to improve read performance, merge some packets then read,
117+
* when it on and read small bytes, we sleep to wait more data.,
118+
* that is, we merge some data to read together.
119+
* @param v true to ename merged read.
120+
* @param handler the handler when merge read is enabled.
121+
* @see https://github.com/winlinvip/simple-rtmp-server/issues/241
122+
*/
123+
virtual void set_merge_read(bool v, IMergeReadHandler* handler);
82124
};
83125

84126
#endif

trunk/src/rtmp/srs_protocol_rtmp.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,11 @@ void SrsRtmpServer::set_auto_response(bool v)
745745
protocol->set_auto_response(v);
746746
}
747747

748+
void SrsRtmpServer::set_merge_read(bool v, IMergeReadHandler* handler)
749+
{
750+
protocol->set_merge_read(v, handler);
751+
}
752+
748753
void SrsRtmpServer::set_recv_timeout(int64_t timeout_us)
749754
{
750755
protocol->set_recv_timeout(timeout_us);

trunk/src/rtmp/srs_protocol_rtmp.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class SrsPlayPacket;
4646
class SrsMessage;
4747
class SrsPacket;
4848
class SrsAmf0Object;
49+
class IMergeReadHandler;
4950

5051
/**
5152
* the original request from client.
@@ -343,6 +344,15 @@ class SrsRtmpServer
343344
*/
344345
virtual void set_auto_response(bool v);
345346
/**
347+
* to improve read performance, merge some packets then read,
348+
* when it on and read small bytes, we sleep to wait more data.,
349+
* that is, we merge some data to read together.
350+
* @param v true to ename merged read.
351+
* @param handler the handler when merge read is enabled.
352+
* @see https://github.com/winlinvip/simple-rtmp-server/issues/241
353+
*/
354+
virtual void set_merge_read(bool v, IMergeReadHandler* handler);
355+
/**
346356
* set/get the recv timeout in us.
347357
* if timeout, recv/send message return ERROR_SOCKET_TIMEOUT.
348358
*/

trunk/src/rtmp/srs_protocol_stack.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,11 @@ int SrsProtocol::manual_response_flush()
479479
return ret;
480480
}
481481

482+
void SrsProtocol::set_merge_read(bool v, IMergeReadHandler* handler)
483+
{
484+
in_buffer->set_merge_read(v, handler);
485+
}
486+
482487
void SrsProtocol::set_recv_timeout(int64_t timeout_us)
483488
{
484489
return skt->set_recv_timeout(timeout_us);

trunk/src/rtmp/srs_protocol_stack.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class SrsMessageHeader;
5353
class SrsMessage;
5454
class SrsChunkStream;
5555
class SrsSharedPtrMessage;
56+
class IMergeReadHandler;
5657

5758
/**
5859
* 4.1. Message Header
@@ -269,6 +270,16 @@ class SrsProtocol
269270
* @see the auto_response_when_recv and manual_response_queue.
270271
*/
271272
virtual int manual_response_flush();
273+
public:
274+
/**
275+
* to improve read performance, merge some packets then read,
276+
* when it on and read small bytes, we sleep to wait more data.,
277+
* that is, we merge some data to read together.
278+
* @param v true to ename merged read.
279+
* @param handler the handler when merge read is enabled.
280+
* @see https://github.com/winlinvip/simple-rtmp-server/issues/241
281+
*/
282+
virtual void set_merge_read(bool v, IMergeReadHandler* handler);
272283
public:
273284
/**
274285
* set/get the recv timeout in us.

0 commit comments

Comments
 (0)