Skip to content

Commit d6c16a7

Browse files
committed
RTC: Support WebRTC re-publish stream. 4.0.87
1 parent 0cb05a2 commit d6c16a7

6 files changed

+94
-19
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ Other documents:
190190

191191
## V4 changes
192192

193+
* v4.0, 2021-03-24, RTC: Support WebRTC re-publish stream. 4.0.87
193194
* v4.0, 2021-03-24, RTC: Use fast parse TWCCID, ignore in packet parsing. 4.0.86
194195
* v4.0, 2021-03-09, DTLS: Fix ARQ bug, use openssl timeout. 4.0.84
195196
* v4.0, 2021-03-08, DTLS: Fix dead loop by duplicated Alert message. 4.0.83

trunk/src/app/srs_app_rtc_conn.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,30 @@ srs_error_t SrsRtcPlayStream::initialize(SrsRequest* req, std::map<uint32_t, Srs
466466
return err;
467467
}
468468

469+
void SrsRtcPlayStream::on_stream_change(SrsRtcStreamDescription* desc)
470+
{
471+
// Refresh the relation for audio.
472+
// TODO: FIMXE: Match by label?
473+
if (desc->audio_track_desc_ && audio_tracks_.size() == 1) {
474+
uint32_t ssrc = desc->audio_track_desc_->ssrc_;
475+
SrsRtcAudioSendTrack* track = audio_tracks_.begin()->second;
476+
477+
audio_tracks_.clear();
478+
audio_tracks_.insert(make_pair(ssrc, track));
479+
}
480+
481+
// Refresh the relation for video.
482+
// TODO: FIMXE: Match by label?
483+
if (desc->video_track_descs_.size() == 1 && desc->video_track_descs_.size() == 1) {
484+
SrsRtcTrackDescription* vdesc = desc->video_track_descs_.at(0);
485+
uint32_t ssrc = vdesc->ssrc_;
486+
SrsRtcVideoSendTrack* track = video_tracks_.begin()->second;
487+
488+
video_tracks_.clear();
489+
video_tracks_.insert(make_pair(ssrc, track));
490+
}
491+
}
492+
469493
srs_error_t SrsRtcPlayStream::on_reload_vhost_play(string vhost)
470494
{
471495
if (req_->vhost != vhost) {
@@ -546,6 +570,9 @@ srs_error_t SrsRtcPlayStream::cycle()
546570
return srs_error_wrap(err, "create consumer, source=%s", req_->get_stream_url().c_str());
547571
}
548572

573+
srs_assert(consumer);
574+
consumer->set_handler(this);
575+
549576
// TODO: FIXME: Dumps the SPS/PPS from gop cache, without other frames.
550577
if ((err = source->consumer_dumps(consumer)) != srs_success) {
551578
return srs_error_wrap(err, "dumps consumer, url=%s", req_->get_stream_url().c_str());

trunk/src/app/srs_app_rtc_conn.hpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ class SrsRtcPLIWorker : virtual public ISrsCoroutineHandler
211211

212212
// A RTC play stream, client pull and play stream from SRS.
213213
class SrsRtcPlayStream : virtual public ISrsCoroutineHandler, virtual public ISrsReloadHandler
214-
, virtual public ISrsHourGlass, virtual public ISrsRtcPLIWorkerHandler
214+
, virtual public ISrsHourGlass, virtual public ISrsRtcPLIWorkerHandler, public ISrsRtcStreamChangeCallback
215215
{
216216
private:
217217
SrsContextId cid_;
@@ -235,13 +235,16 @@ class SrsRtcPlayStream : virtual public ISrsCoroutineHandler, virtual public ISr
235235
bool nack_enabled_;
236236
bool nack_no_copy_;
237237
private:
238-
// Whether palyer started.
238+
// Whether player started.
239239
bool is_started;
240240
public:
241241
SrsRtcPlayStream(SrsRtcConnection* s, const SrsContextId& cid);
242242
virtual ~SrsRtcPlayStream();
243243
public:
244244
srs_error_t initialize(SrsRequest* request, std::map<uint32_t, SrsRtcTrackDescription*> sub_relations);
245+
// Interface ISrsRtcStreamChangeCallback
246+
public:
247+
void on_stream_change(SrsRtcStreamDescription* desc);
245248
// interface ISrsReloadHandler
246249
public:
247250
virtual srs_error_t on_reload_vhost_play(std::string vhost);
@@ -268,7 +271,7 @@ class SrsRtcPlayStream : virtual public ISrsCoroutineHandler, virtual public ISr
268271
srs_error_t on_rtcp_ps_feedback(SrsRtcpPsfbCommon* rtcp);
269272
srs_error_t on_rtcp_rr(SrsRtcpRR* rtcp);
270273
uint32_t get_video_publish_ssrc(uint32_t play_ssrc);
271-
// inteface ISrsRtcPLIWorkerHandler
274+
// Interface ISrsRtcPLIWorkerHandler
272275
public:
273276
virtual srs_error_t do_request_keyframe(uint32_t ssrc, SrsContextId cid);
274277
};

trunk/src/app/srs_app_rtc_source.cpp

+37-12
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,19 @@ SrsNtp SrsNtp::to_time_ms(uint64_t ntp)
152152
return srs_ntp;
153153
}
154154

155+
ISrsRtcStreamChangeCallback::ISrsRtcStreamChangeCallback()
156+
{
157+
}
158+
159+
ISrsRtcStreamChangeCallback::~ISrsRtcStreamChangeCallback()
160+
{
161+
}
162+
155163
SrsRtcConsumer::SrsRtcConsumer(SrsRtcStream* s)
156164
{
157165
source = s;
158166
should_update_source_id = false;
167+
handler_ = NULL;
159168

160169
mw_wait = srs_cond_new();
161170
mw_min_msgs = 0;
@@ -231,6 +240,13 @@ void SrsRtcConsumer::wait(int nb_msgs)
231240
srs_cond_wait(mw_wait);
232241
}
233242

243+
void SrsRtcConsumer::on_stream_change(SrsRtcStreamDescription* desc)
244+
{
245+
if (handler_) {
246+
handler_->on_stream_change(desc);
247+
}
248+
}
249+
234250
SrsRtcStreamManager::SrsRtcStreamManager()
235251
{
236252
lock = NULL;
@@ -354,24 +370,34 @@ void SrsRtcStream::update_auth(SrsRequest* r)
354370
req->update_auth(r);
355371
}
356372

357-
srs_error_t SrsRtcStream::on_source_id_changed(SrsContextId id)
373+
srs_error_t SrsRtcStream::on_source_changed()
358374
{
359375
srs_error_t err = srs_success;
360376

361-
if (!_source_id.compare(id)) {
362-
return err;
363-
}
377+
// Update context id if changed.
378+
bool id_changed = false;
379+
const SrsContextId& id = _srs_context->get_id();
380+
if (_source_id.compare(id)) {
381+
id_changed = true;
364382

365-
if (_pre_source_id.empty()) {
366-
_pre_source_id = id;
383+
if (_pre_source_id.empty()) {
384+
_pre_source_id = id;
385+
}
386+
_source_id = id;
367387
}
368-
_source_id = id;
369388

370-
// notice all consumer
389+
// Notify all consumers.
371390
std::vector<SrsRtcConsumer*>::iterator it;
372391
for (it = consumers.begin(); it != consumers.end(); ++it) {
373392
SrsRtcConsumer* consumer = *it;
374-
consumer->update_source_id();
393+
394+
// Notify if context id changed.
395+
if (id_changed) {
396+
consumer->update_source_id();
397+
}
398+
399+
// Notify about stream description.
400+
consumer->on_stream_change(stream_desc_);
375401
}
376402

377403
return err;
@@ -456,9 +482,8 @@ srs_error_t SrsRtcStream::on_publish()
456482
is_created_ = true;
457483
is_delivering_packets_ = true;
458484

459-
// whatever, the publish thread is the source or edge source,
460-
// save its id to srouce id.
461-
if ((err = on_source_id_changed(_srs_context->get_id())) != srs_success) {
485+
// Notify the consumers about stream change event.
486+
if ((err = on_source_changed()) != srs_success) {
462487
return srs_error_wrap(err, "source id change");
463488
}
464489

trunk/src/app/srs_app_rtc_source.hpp

+22-3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ class SrsNtp
7575
static uint64_t kMagicNtpFractionalUnit;
7676
};
7777

78+
// When RTC stream publish and re-publish.
79+
class ISrsRtcStreamChangeCallback
80+
{
81+
public:
82+
ISrsRtcStreamChangeCallback();
83+
virtual ~ISrsRtcStreamChangeCallback();
84+
public:
85+
virtual void on_stream_change(SrsRtcStreamDescription* desc) = 0;
86+
};
87+
88+
// The RTC stream consumer, consume packets from RTC stream source.
7889
class SrsRtcConsumer
7990
{
8091
private:
@@ -87,6 +98,9 @@ class SrsRtcConsumer
8798
srs_cond_t mw_wait;
8899
bool mw_waiting;
89100
int mw_min_msgs;
101+
private:
102+
// The callback for stream change event.
103+
ISrsRtcStreamChangeCallback* handler_;
90104
public:
91105
SrsRtcConsumer(SrsRtcStream* s);
92106
virtual ~SrsRtcConsumer();
@@ -100,6 +114,9 @@ class SrsRtcConsumer
100114
virtual srs_error_t dump_packet(SrsRtpPacket2** ppkt);
101115
// Wait for at-least some messages incoming in queue.
102116
virtual void wait(int nb_msgs);
117+
public:
118+
void set_handler(ISrsRtcStreamChangeCallback* h) { handler_ = h; } // SrsRtcConsumer::set_handler()
119+
void on_stream_change(SrsRtcStreamDescription* desc);
103120
};
104121

105122
class SrsRtcStreamManager
@@ -154,7 +171,7 @@ class SrsRtcStream
154171
// For publish, it's the publish client id.
155172
// For edge, it's the edge ingest id.
156173
// when source id changed, for example, the edge reconnect,
157-
// invoke the on_source_id_changed() to let all clients know.
174+
// invoke the on_source_changed() to let all clients know.
158175
SrsContextId _source_id;
159176
// previous source id.
160177
SrsContextId _pre_source_id;
@@ -180,8 +197,10 @@ class SrsRtcStream
180197
virtual srs_error_t initialize(SrsRequest* r);
181198
// Update the authentication information in request.
182199
virtual void update_auth(SrsRequest* r);
183-
// The source id changed.
184-
virtual srs_error_t on_source_id_changed(SrsContextId id);
200+
private:
201+
// The stream source changed.
202+
virtual srs_error_t on_source_changed();
203+
public:
185204
// Get current source id.
186205
virtual SrsContextId source_id();
187206
virtual SrsContextId pre_source_id();

trunk/src/core/srs_core_version4.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@
2626

2727
#define VERSION_MAJOR 4
2828
#define VERSION_MINOR 0
29-
#define VERSION_REVISION 86
29+
#define VERSION_REVISION 87
3030

3131
#endif

0 commit comments

Comments
 (0)