@@ -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
3038ISrsMessageHandler::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+
274306bool 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}
0 commit comments