Skip to content

Commit 773be36

Browse files
perkjCommit Bot
authored and
Commit Bot
committed
Reland of Change VideoTrack implementation to invoke VideoTrackSourceInterface::AddOrUpdateSink on wt
Added documentation of thread expectations for video tracks and sources to the API. Originally landed as patchset #2 id:20001 of https://codereview.webrtc.org/2964863002/. Patchset 1 is the originall cl. Patschet 2 is modified so that VideoTrackInterface::AddSink and RemoveSink have a default implementation. BUG=none Review-Url: https://codereview.webrtc.org/2989113002 Cr-Commit-Position: refs/heads/master@{#19195}
1 parent 36344a0 commit 773be36

13 files changed

+61
-29
lines changed

webrtc/api/mediastreaminterface.h

+11
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ class MediaStreamTrackInterface : public rtc::RefCountInterface,
111111

112112
// VideoTrackSourceInterface is a reference counted source used for
113113
// VideoTracks. The same source can be used by multiple VideoTracks.
114+
// VideoTrackSourceInterface is designed to be invoked on the signaling thread
115+
// except for rtc::VideoSourceInterface<VideoFrame> methods that will be invoked
116+
// on the worker thread via a VideoTrack. A custom implementation of a source
117+
// can inherit AdaptedVideoTrackSource instead of directly implementing this
118+
// interface.
114119
class VideoTrackSourceInterface
115120
: public MediaSourceInterface,
116121
public rtc::VideoSourceInterface<VideoFrame> {
@@ -145,6 +150,12 @@ class VideoTrackSourceInterface
145150
virtual ~VideoTrackSourceInterface() {}
146151
};
147152

153+
// VideoTrackInterface is designed to be invoked on the signaling thread except
154+
// for rtc::VideoSourceInterface<VideoFrame> methods that must be invoked
155+
// on the worker thread.
156+
// PeerConnectionFactory::CreateVideoTrack can be used for creating a VideoTrack
157+
// that ensures thread safety and that all methods are called on the right
158+
// thread.
148159
class VideoTrackInterface
149160
: public MediaStreamTrackInterface,
150161
public rtc::VideoSourceInterface<VideoFrame> {

webrtc/ortc/ortcfactory.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,8 @@ rtc::scoped_refptr<VideoTrackInterface> OrtcFactory::CreateVideoTrack(
479479
const std::string& id,
480480
VideoTrackSourceInterface* source) {
481481
RTC_DCHECK_RUN_ON(signaling_thread_);
482-
rtc::scoped_refptr<VideoTrackInterface> track(VideoTrack::Create(id, source));
482+
rtc::scoped_refptr<VideoTrackInterface> track(
483+
VideoTrack::Create(id, source, worker_thread_.get()));
483484
return VideoTrackProxy::Create(signaling_thread_, worker_thread_.get(),
484485
track);
485486
}

webrtc/pc/mediastream_unittest.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ class MediaStreamTest: public testing::Test {
5656
stream_ = MediaStream::Create(kStreamLabel1);
5757
ASSERT_TRUE(stream_.get() != NULL);
5858

59-
video_track_ =
60-
VideoTrack::Create(kVideoTrackId, FakeVideoTrackSource::Create());
59+
video_track_ = VideoTrack::Create(
60+
kVideoTrackId, FakeVideoTrackSource::Create(), rtc::Thread::Current());
6161
ASSERT_TRUE(video_track_.get() != NULL);
6262
EXPECT_EQ(MediaStreamTrackInterface::kLive, video_track_->state());
6363

webrtc/pc/peerconnectionfactory.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ rtc::scoped_refptr<VideoTrackInterface> PeerConnectionFactory::CreateVideoTrack(
292292
VideoTrackSourceInterface* source) {
293293
RTC_DCHECK(signaling_thread_->IsCurrent());
294294
rtc::scoped_refptr<VideoTrackInterface> track(
295-
VideoTrack::Create(id, source));
295+
VideoTrack::Create(id, source, worker_thread_));
296296
return VideoTrackProxy::Create(signaling_thread_, worker_thread_, track);
297297
}
298298

webrtc/pc/peerconnectioninterface_unittest.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,8 @@ rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
460460
// Add a local video track.
461461
rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
462462
webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
463-
webrtc::FakeVideoTrackSource::Create()));
463+
webrtc::FakeVideoTrackSource::Create(),
464+
rtc::Thread::Current()));
464465
stream->AddTrack(video_track);
465466
}
466467

@@ -1150,7 +1151,8 @@ class PeerConnectionInterfaceTest : public testing::Test {
11501151
MediaStreamInterface* stream) {
11511152
rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
11521153
webrtc::VideoTrack::Create(track_id,
1153-
webrtc::FakeVideoTrackSource::Create()));
1154+
webrtc::FakeVideoTrackSource::Create(),
1155+
rtc::Thread::Current()));
11541156
ASSERT_TRUE(stream->AddTrack(video_track));
11551157
}
11561158

webrtc/pc/rtcstatscollector_unittest.cc

+5
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ class FakeVideoTrackForStats
218218
std::string kind() const override {
219219
return MediaStreamTrackInterface::kVideoKind;
220220
}
221+
222+
void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
223+
const rtc::VideoSinkWants& wants) override{};
224+
void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override{};
225+
221226
VideoTrackSourceInterface* GetSource() const override { return nullptr; }
222227
};
223228

webrtc/pc/rtpreceiver.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ VideoRtpReceiver::VideoRtpReceiver(const std::string& track_id,
155155
track_id,
156156
VideoTrackSourceProxy::Create(rtc::Thread::Current(),
157157
worker_thread,
158-
source_)))) {
158+
source_),
159+
worker_thread))) {
159160
source_->SetState(MediaSourceInterface::kLive);
160161
if (!channel_) {
161162
LOG(LS_ERROR)

webrtc/pc/rtpsenderreceiver_unittest.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ class RtpSenderReceiverTest : public testing::Test,
124124
void AddVideoTrack(bool is_screencast) {
125125
rtc::scoped_refptr<VideoTrackSourceInterface> source(
126126
FakeVideoTrackSource::Create(is_screencast));
127-
video_track_ = VideoTrack::Create(kVideoTrackId, source);
127+
video_track_ =
128+
VideoTrack::Create(kVideoTrackId, source, rtc::Thread::Current());
128129
EXPECT_TRUE(local_stream_->AddTrack(video_track_));
129130
}
130131

webrtc/pc/statscollector_unittest.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,8 @@ class StatsCollectorTest : public testing::Test {
545545
void AddOutgoingVideoTrackStats() {
546546
stream_ = webrtc::MediaStream::Create("streamlabel");
547547
track_ = webrtc::VideoTrack::Create(kLocalTrackId,
548-
webrtc::FakeVideoTrackSource::Create());
548+
webrtc::FakeVideoTrackSource::Create(),
549+
rtc::Thread::Current());
549550
stream_->AddTrack(track_);
550551
EXPECT_CALL(session_, GetLocalTrackIdBySsrc(kSsrcOfTrack, _))
551552
.WillRepeatedly(DoAll(SetArgPointee<1>(kLocalTrackId), Return(true)));
@@ -555,7 +556,8 @@ class StatsCollectorTest : public testing::Test {
555556
void AddIncomingVideoTrackStats() {
556557
stream_ = webrtc::MediaStream::Create("streamlabel");
557558
track_ = webrtc::VideoTrack::Create(kRemoteTrackId,
558-
webrtc::FakeVideoTrackSource::Create());
559+
webrtc::FakeVideoTrackSource::Create(),
560+
rtc::Thread::Current());
559561
stream_->AddTrack(track_);
560562
EXPECT_CALL(session_, GetRemoteTrackIdBySsrc(kSsrcOfTrack, _))
561563
.WillRepeatedly(DoAll(SetArgPointee<1>(kRemoteTrackId), Return(true)));

webrtc/pc/trackmediainfomap_unittest.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,12 @@ class TrackMediaInfoMapTest : public testing::Test {
8585
remote_audio_track_(AudioTrack::Create("RemoteAudioTrack", nullptr)),
8686
local_video_track_(
8787
VideoTrack::Create("LocalVideoTrack",
88-
FakeVideoTrackSource::Create(false))),
88+
FakeVideoTrackSource::Create(false),
89+
rtc::Thread::Current())),
8990
remote_video_track_(
9091
VideoTrack::Create("RemoteVideoTrack",
91-
FakeVideoTrackSource::Create(false))) {}
92+
FakeVideoTrackSource::Create(false),
93+
rtc::Thread::Current())) {}
9294

9395
~TrackMediaInfoMapTest() {
9496
// If we have a map the ownership has been passed to the map, only delete if

webrtc/pc/videotrack.cc

+16-13
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
namespace webrtc {
1616

1717
VideoTrack::VideoTrack(const std::string& label,
18-
VideoTrackSourceInterface* video_source)
18+
VideoTrackSourceInterface* video_source,
19+
rtc::Thread* worker_thread)
1920
: MediaStreamTrack<VideoTrackInterface>(label),
21+
worker_thread_(worker_thread),
2022
video_source_(video_source),
2123
content_hint_(ContentHint::kNone) {
22-
worker_thread_checker_.DetachFromThread();
2324
video_source_->RegisterObserver(this);
2425
}
2526

@@ -35,15 +36,15 @@ std::string VideoTrack::kind() const {
3536
// thread.
3637
void VideoTrack::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
3738
const rtc::VideoSinkWants& wants) {
38-
RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
39+
RTC_DCHECK(worker_thread_->IsCurrent());
3940
VideoSourceBase::AddOrUpdateSink(sink, wants);
4041
rtc::VideoSinkWants modified_wants = wants;
4142
modified_wants.black_frames = !enabled();
4243
video_source_->AddOrUpdateSink(sink, modified_wants);
4344
}
4445

4546
void VideoTrack::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
46-
RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
47+
RTC_DCHECK(worker_thread_->IsCurrent());
4748
VideoSourceBase::RemoveSink(sink);
4849
video_source_->RemoveSink(sink);
4950
}
@@ -63,13 +64,14 @@ void VideoTrack::set_content_hint(ContentHint hint) {
6364

6465
bool VideoTrack::set_enabled(bool enable) {
6566
RTC_DCHECK(signaling_thread_checker_.CalledOnValidThread());
66-
for (auto& sink_pair : sink_pairs()) {
67-
rtc::VideoSinkWants modified_wants = sink_pair.wants;
68-
modified_wants.black_frames = !enable;
69-
// video_source_ is a proxy object, marshalling the call to the
70-
// worker thread.
71-
video_source_->AddOrUpdateSink(sink_pair.sink, modified_wants);
72-
}
67+
worker_thread_->Invoke<void>(RTC_FROM_HERE, [enable, this] {
68+
RTC_DCHECK(worker_thread_->IsCurrent());
69+
for (auto& sink_pair : sink_pairs()) {
70+
rtc::VideoSinkWants modified_wants = sink_pair.wants;
71+
modified_wants.black_frames = !enable;
72+
video_source_->AddOrUpdateSink(sink_pair.sink, modified_wants);
73+
}
74+
});
7375
return MediaStreamTrack<VideoTrackInterface>::set_enabled(enable);
7476
}
7577

@@ -84,9 +86,10 @@ void VideoTrack::OnChanged() {
8486

8587
rtc::scoped_refptr<VideoTrack> VideoTrack::Create(
8688
const std::string& id,
87-
VideoTrackSourceInterface* source) {
89+
VideoTrackSourceInterface* source,
90+
rtc::Thread* worker_thread) {
8891
rtc::RefCountedObject<VideoTrack>* track =
89-
new rtc::RefCountedObject<VideoTrack>(id, source);
92+
new rtc::RefCountedObject<VideoTrack>(id, source, worker_thread);
9093
return track;
9194
}
9295

webrtc/pc/videotrack.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class VideoTrack : public MediaStreamTrack<VideoTrackInterface>,
2727
public:
2828
static rtc::scoped_refptr<VideoTrack> Create(
2929
const std::string& label,
30-
VideoTrackSourceInterface* source);
30+
VideoTrackSourceInterface* source,
31+
rtc::Thread* worker_thread);
3132

3233
void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
3334
const rtc::VideoSinkWants& wants) override;
@@ -42,15 +43,17 @@ class VideoTrack : public MediaStreamTrack<VideoTrackInterface>,
4243
std::string kind() const override;
4344

4445
protected:
45-
VideoTrack(const std::string& id, VideoTrackSourceInterface* video_source);
46+
VideoTrack(const std::string& id,
47+
VideoTrackSourceInterface* video_source,
48+
rtc::Thread* worker_thread);
4649
~VideoTrack();
4750

4851
private:
4952
// Implements ObserverInterface. Observes |video_source_| state.
5053
void OnChanged() override;
5154

55+
rtc::Thread* const worker_thread_;
5256
rtc::ThreadChecker signaling_thread_checker_;
53-
rtc::ThreadChecker worker_thread_checker_;
5457
rtc::scoped_refptr<VideoTrackSourceInterface> video_source_;
5558
ContentHint content_hint_ GUARDED_BY(signaling_thread_checker_);
5659
};

webrtc/pc/videotrack_unittest.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ class VideoTrackTest : public testing::Test {
3131
static const char kVideoTrackId[] = "track_id";
3232
video_track_source_ = new rtc::RefCountedObject<VideoTrackSource>(
3333
&capturer_, true /* remote */);
34-
video_track_ = VideoTrack::Create(kVideoTrackId, video_track_source_);
34+
video_track_ = VideoTrack::Create(kVideoTrackId, video_track_source_,
35+
rtc::Thread::Current());
3536
capturer_.Start(
3637
cricket::VideoFormat(640, 480, cricket::VideoFormat::FpsToInterval(30),
3738
cricket::FOURCC_I420));

0 commit comments

Comments
 (0)