Skip to content

Commit dde05c6

Browse files
committed
for bug #251, refine the send use cond wait.
1 parent 4c1d5c0 commit dde05c6

File tree

5 files changed

+71
-7
lines changed

5 files changed

+71
-7
lines changed

trunk/src/app/srs_app_rtmp_conn.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,10 @@ int SrsRtmpConn::do_playing(SrsSource* source, SrsQueueRecvThread* trd)
598598
// collect elapse for pithy print.
599599
pithy_print.elapse();
600600

601+
// wait for message to incoming.
602+
// @see https://github.com/winlinvip/simple-rtmp-server/issues/251
603+
consumer->wait(SRS_PERF_MW_MIN_MSGS, mw_sleep);
604+
601605
// get messages from consumer.
602606
// each msg in msgs.msgs must be free, for the SrsMessageArray never free them.
603607
int count = 0;
@@ -606,12 +610,9 @@ int SrsRtmpConn::do_playing(SrsSource* source, SrsQueueRecvThread* trd)
606610
return ret;
607611
}
608612

609-
// no message to send, sleep a while.
610-
if (count <= 0) {
611-
srs_verbose("sleep for no messages to send");
612-
st_usleep(mw_sleep * 1000);
613-
}
614-
srs_info("got %d msgs, mw=%d", count, mw_sleep);
613+
// we use wait to get messages, so the count must be positive.
614+
srs_assert(count > 0);
615+
srs_info("got %d msgs, min=%d, mw=%d", count, SRS_PERF_MW_MIN_MSGS, mw_sleep);
615616

616617
// reportable
617618
if (pithy_print.can_print()) {

trunk/src/app/srs_app_source.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ SrsMessageQueue::~SrsMessageQueue()
166166
clear();
167167
}
168168

169+
int SrsMessageQueue::count()
170+
{
171+
return (int)msgs.size();
172+
}
173+
174+
int SrsMessageQueue::duration()
175+
{
176+
return (int)(av_end_time - av_start_time);
177+
}
178+
169179
void SrsMessageQueue::set_queue_size(double queue_size)
170180
{
171181
queue_size_ms = (int)(queue_size * 1000);
@@ -290,13 +300,19 @@ SrsConsumer::SrsConsumer(SrsSource* _source)
290300
jitter = new SrsRtmpJitter();
291301
queue = new SrsMessageQueue();
292302
should_update_source_id = false;
303+
304+
mw_wait = st_cond_new();
305+
mw_min_msgs = 0;
306+
mw_duration = 0;
307+
mw_waiting = false;
293308
}
294309

295310
SrsConsumer::~SrsConsumer()
296311
{
297312
source->on_consumer_destroy(this);
298313
srs_freep(jitter);
299314
srs_freep(queue);
315+
st_cond_destroy(mw_wait);
300316
}
301317

302318
void SrsConsumer::set_queue_size(double queue_size)
@@ -329,6 +345,12 @@ int SrsConsumer::enqueue(SrsSharedPtrMessage* msg, bool atc, int tba, int tbv, S
329345
return ret;
330346
}
331347

348+
// fire the mw when msgs is enough.
349+
if (mw_waiting && queue->count() > mw_min_msgs && queue->duration() > mw_duration) {
350+
st_cond_signal(mw_wait);
351+
mw_waiting = false;
352+
}
353+
332354
return ret;
333355
}
334356

@@ -349,6 +371,22 @@ int SrsConsumer::dump_packets(int max_count, SrsMessage** pmsgs, int& count)
349371
return queue->dump_packets(max_count, pmsgs, count);
350372
}
351373

374+
void SrsConsumer::wait(int nb_msgs, int duration)
375+
{
376+
mw_min_msgs = nb_msgs;
377+
mw_duration = duration;
378+
379+
// already ok, donot wait.
380+
if (queue->count() > mw_min_msgs && queue->duration() > mw_duration) {
381+
return;
382+
}
383+
384+
// the enqueue will notify this cond.
385+
mw_waiting = true;
386+
387+
st_cond_wait(mw_wait);
388+
}
389+
352390
int SrsConsumer::on_play_client_pause(bool is_pause)
353391
{
354392
int ret = ERROR_SUCCESS;

trunk/src/app/srs_app_source.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ class SrsMessageQueue
115115
SrsMessageQueue();
116116
virtual ~SrsMessageQueue();
117117
public:
118+
/**
119+
* get the count of queue.
120+
*/
121+
virtual int count();
122+
/**
123+
* get duration of queue.
124+
*/
125+
virtual int duration();
118126
/**
119127
* set the queue size
120128
* @param queue_size the queue size in seconds.
@@ -154,6 +162,12 @@ class SrsConsumer
154162
bool paused;
155163
// when source id changed, notice all consumers
156164
bool should_update_source_id;
165+
// the cond wait for mw.
166+
// @see https://github.com/winlinvip/simple-rtmp-server/issues/251
167+
st_cond_t mw_wait;
168+
bool mw_waiting;
169+
int mw_min_msgs;
170+
int mw_duration;
157171
public:
158172
SrsConsumer(SrsSource* _source);
159173
virtual ~SrsConsumer();
@@ -189,6 +203,12 @@ class SrsConsumer
189203
*/
190204
virtual int dump_packets(int max_count, SrsMessage** pmsgs, int& count);
191205
/**
206+
* wait for messages incomming, atleast nb_msgs and in duration.
207+
* @param nb_msgs the messages count to wait.
208+
* @param duration the messgae duration to wait.
209+
*/
210+
virtual void wait(int nb_msgs, int duration);
211+
/**
192212
* when client send the pause message.
193213
*/
194214
virtual int on_play_client_pause(bool is_pause);

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

trunk/src/core/srs_core_performance.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9393
* @remark, recomment to 156.
9494
*/
9595
#define SRS_PERF_MW_MSGS 156
96+
/**
97+
* how many msgs atleast to send.
98+
* @remark, recomment to 8.
99+
*/
100+
#define SRS_PERF_MW_MIN_MSGS 8
96101

97102
/**
98103
* how many chunk stream to cache, [0, N].

0 commit comments

Comments
 (0)