Skip to content

Commit 2e753c1

Browse files
kanatsanthoshvai
authored andcommitted
Android improvements. (#20)
Start/Stop receiving stream method for VideoTrack (#25) Properly remove observer upon deconstruction (#26) feat: Expose setCodecPreferences/getCapabilities for android. (#61) fix: add WrappedVideoDecoderFactory.java. (#74) Exposing Adapter types in PeerConnectionFactory (#78) Co-authored-by: davidliu <[email protected]> Co-authored-by: Mohamed Risaldar UT <[email protected]> (cherry picked from commit e91f003) # Conflicts: # media/base/media_channel.h # media/engine/webrtc_video_engine.cc # media/engine/webrtc_video_engine.h
1 parent 1e4e96f commit 2e753c1

14 files changed

+151
-3
lines changed

api/media_stream_interface.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ const char* const MediaStreamTrackInterface::kVideoKind =
1919
const char* const MediaStreamTrackInterface::kAudioKind =
2020
cricket::kMediaTypeAudio;
2121

22+
bool VideoTrackInterface::should_receive() const {
23+
return true;
24+
}
25+
2226
VideoTrackInterface::ContentHint VideoTrackInterface::content_hint() const {
2327
return ContentHint::kNone;
2428
}

api/media_stream_interface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ class RTC_EXPORT VideoTrackInterface
188188

189189
virtual VideoTrackSourceInterface* GetSource() const = 0;
190190

191+
virtual void set_should_receive(bool should_receive) {}
192+
virtual bool should_receive() const;
191193
virtual ContentHint content_hint() const;
192194
virtual void set_content_hint(ContentHint hint) {}
193195

media/base/media_channel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,8 @@ class VideoMediaReceiveChannelInterface : public MediaReceiveChannelInterface {
10021002
webrtc::RtcpMode rtcp_mode,
10031003
absl::optional<int> rtx_time) = 0;
10041004
virtual bool AddDefaultRecvStreamForTesting(const StreamParams& sp) = 0;
1005+
virtual void StartReceive(uint32_t ssrc) {}
1006+
virtual void StopReceive(uint32_t ssrc) {}
10051007
};
10061008

10071009
} // namespace cricket

media/engine/webrtc_video_engine.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,6 +2604,24 @@ WebRtcVideoReceiveChannel::~WebRtcVideoReceiveChannel() {
26042604
delete kv.second;
26052605
}
26062606

2607+
void WebRtcVideoReceiveChannel::StartReceive(uint32_t ssrc) {
2608+
RTC_DCHECK_RUN_ON(&thread_checker_);
2609+
WebRtcVideoReceiveStream* stream = FindReceiveStream(ssrc);
2610+
if(!stream) {
2611+
return;
2612+
}
2613+
stream->StartStream();
2614+
}
2615+
2616+
void WebRtcVideoReceiveChannel::StopReceive(uint32_t ssrc) {
2617+
RTC_DCHECK_RUN_ON(&thread_checker_);
2618+
WebRtcVideoReceiveStream* stream = FindReceiveStream(ssrc);
2619+
if(!stream) {
2620+
return;
2621+
}
2622+
stream->StopStream();
2623+
}
2624+
26072625
void WebRtcVideoReceiveChannel::SetReceiverFeedbackParameters(
26082626
bool lntf_enabled,
26092627
bool nack_enabled,
@@ -3500,6 +3518,17 @@ void WebRtcVideoReceiveChannel::WebRtcVideoReceiveStream::SetReceiverParameters(
35003518
}
35013519
}
35023520

3521+
void WebRtcVideoReceiveChannel::WebRtcVideoReceiveStream::StartStream(){
3522+
if (stream_) {
3523+
stream_->Start();
3524+
}
3525+
}
3526+
void WebRtcVideoReceiveChannel::WebRtcVideoReceiveStream::StopStream(){
3527+
if (stream_) {
3528+
stream_->Stop();
3529+
}
3530+
}
3531+
35033532
void WebRtcVideoReceiveChannel::WebRtcVideoReceiveStream::
35043533
RecreateReceiveStream() {
35053534
RTC_DCHECK_RUN_ON(&thread_checker_);

media/engine/webrtc_video_engine.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ class WebRtcVideoSendChannel : public MediaChannelUtil,
290290
}
291291
return send_codec()->rtx_time;
292292
}
293-
294293
private:
295294
struct ChangedSenderParameters {
296295
// These optionals are unset if not changed.
@@ -633,6 +632,8 @@ class WebRtcVideoReceiveChannel : public MediaChannelUtil,
633632
webrtc::RtcpMode rtcp_mode,
634633
absl::optional<int> rtx_time) override;
635634

635+
void StartReceive(uint32_t ssrc) override;
636+
void StopReceive(uint32_t ssrc) override;
636637
private:
637638
class WebRtcVideoReceiveStream;
638639
struct ChangedReceiverParameters {
@@ -734,6 +735,9 @@ class WebRtcVideoReceiveChannel : public MediaChannelUtil,
734735
rtc::scoped_refptr<webrtc::FrameTransformerInterface>
735736
frame_transformer);
736737

738+
void StartStream();
739+
void StopStream();
740+
737741
void SetLocalSsrc(uint32_t local_ssrc);
738742
void UpdateRtxSsrc(uint32_t ssrc);
739743
void StartReceiveStream();

pc/media_stream_track_proxy.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ PROXY_SECONDARY_METHOD2(void,
5555
PROXY_SECONDARY_METHOD1(void, RemoveSink, rtc::VideoSinkInterface<VideoFrame>*)
5656
PROXY_SECONDARY_METHOD0(void, RequestRefreshFrame)
5757
BYPASS_PROXY_CONSTMETHOD0(VideoTrackSourceInterface*, GetSource)
58+
PROXY_CONSTMETHOD0(bool, should_receive)
59+
PROXY_METHOD1(void, set_should_receive, bool)
5860

5961
PROXY_METHOD1(void, RegisterObserver, ObserverInterface*)
6062
PROXY_METHOD1(void, UnregisterObserver, ObserverInterface*)

pc/video_rtp_receiver.cc

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ VideoRtpReceiver::VideoRtpReceiver(
4141
rtc::Thread::Current(),
4242
worker_thread,
4343
VideoTrack::Create(receiver_id, source_, worker_thread))),
44-
attachment_id_(GenerateUniqueId()) {
44+
cached_track_should_receive_(track_->should_receive()),
45+
attachment_id_(GenerateUniqueId()),
46+
worker_thread_safety_(PendingTaskSafetyFlag::CreateDetachedInactive()) {
4547
RTC_DCHECK(worker_thread_);
4648
SetStreams(streams);
49+
track_->RegisterObserver(this);
4750
RTC_DCHECK_EQ(source_->state(), MediaSourceInterface::kInitializing);
4851
}
4952

@@ -116,6 +119,39 @@ void VideoRtpReceiver::Stop() {
116119
track_->internal()->set_ended();
117120
}
118121

122+
void VideoRtpReceiver::OnChanged() {
123+
RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
124+
if (cached_track_should_receive_ != track_->should_receive()) {
125+
cached_track_should_receive_ = track_->should_receive();
126+
worker_thread_->PostTask(
127+
[this, receive = cached_track_should_receive_]() {
128+
RTC_DCHECK_RUN_ON(worker_thread_);
129+
if(receive) {
130+
StartMediaChannel();
131+
} else {
132+
StopMediaChannel();
133+
}
134+
});
135+
}
136+
}
137+
138+
void VideoRtpReceiver::StartMediaChannel() {
139+
RTC_DCHECK_RUN_ON(worker_thread_);
140+
if (!media_channel_) {
141+
return;
142+
}
143+
media_channel_->StartReceive(signaled_ssrc_.value_or(0));
144+
OnGenerateKeyFrame();
145+
}
146+
147+
void VideoRtpReceiver::StopMediaChannel() {
148+
RTC_DCHECK_RUN_ON(worker_thread_);
149+
if (!media_channel_) {
150+
return;
151+
}
152+
media_channel_->StopReceive(signaled_ssrc_.value_or(0));
153+
}
154+
119155
void VideoRtpReceiver::RestartMediaChannel(absl::optional<uint32_t> ssrc) {
120156
RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
121157
MediaSourceInterface::SourceState state = source_->state();
@@ -211,6 +247,7 @@ void VideoRtpReceiver::set_transport(
211247
void VideoRtpReceiver::SetStreams(
212248
const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
213249
RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
250+
214251
// Remove remote track from any streams that are going away.
215252
for (const auto& existing_stream : streams_) {
216253
bool removed = true;

pc/video_rtp_receiver.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242

4343
namespace webrtc {
4444

45-
class VideoRtpReceiver : public RtpReceiverInternal {
45+
class VideoRtpReceiver : public RtpReceiverInternal,
46+
public ObserverInterface {
4647
public:
4748
// An SSRC of 0 will create a receiver that will match the first SSRC it
4849
// sees. Must be called on signaling thread.
@@ -60,6 +61,9 @@ class VideoRtpReceiver : public RtpReceiverInternal {
6061

6162
rtc::scoped_refptr<VideoTrackInterface> video_track() const { return track_; }
6263

64+
// ObserverInterface implementation
65+
void OnChanged() override;
66+
6367
// RtpReceiverInterface implementation
6468
rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
6569
return track_;
@@ -115,6 +119,8 @@ class VideoRtpReceiver : public RtpReceiverInternal {
115119
cricket::MediaReceiveChannelInterface* media_channel);
116120

117121
private:
122+
void StartMediaChannel();
123+
void StopMediaChannel();
118124
void RestartMediaChannel(absl::optional<uint32_t> ssrc)
119125
RTC_RUN_ON(&signaling_thread_checker_);
120126
void RestartMediaChannel_w(absl::optional<uint32_t> ssrc,
@@ -162,6 +168,8 @@ class VideoRtpReceiver : public RtpReceiverInternal {
162168
RTC_GUARDED_BY(&signaling_thread_checker_) = nullptr;
163169
bool received_first_packet_ RTC_GUARDED_BY(&signaling_thread_checker_) =
164170
false;
171+
172+
bool cached_track_should_receive_ RTC_GUARDED_BY(&signaling_thread_checker_);
165173
const int attachment_id_;
166174
rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor_
167175
RTC_GUARDED_BY(worker_thread_);
@@ -177,6 +185,7 @@ class VideoRtpReceiver : public RtpReceiverInternal {
177185
// or switched.
178186
bool saved_generate_keyframe_ RTC_GUARDED_BY(worker_thread_) = false;
179187
bool saved_encoded_sink_enabled_ RTC_GUARDED_BY(worker_thread_) = false;
188+
const rtc::scoped_refptr<PendingTaskSafetyFlag> worker_thread_safety_;
180189
};
181190

182191
} // namespace webrtc

pc/video_track.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ VideoTrackSourceInterface* VideoTrack::GetSourceInternal() const {
7676
return video_source_->internal();
7777
}
7878

79+
void VideoTrack::set_should_receive(bool receive) {
80+
RTC_DCHECK_RUN_ON(&signaling_thread_);
81+
if (should_receive_ == receive)
82+
return;
83+
should_receive_ = receive;
84+
Notifier<VideoTrackInterface>::FireOnChanged();
85+
}
86+
87+
bool VideoTrack::should_receive() const {
88+
RTC_DCHECK_RUN_ON(&signaling_thread_);
89+
return should_receive_;
90+
}
91+
7992
VideoTrackInterface::ContentHint VideoTrack::content_hint() const {
8093
RTC_DCHECK_RUN_ON(&signaling_thread_);
8194
return content_hint_;

pc/video_track.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class VideoTrack : public MediaStreamTrack<VideoTrackInterface>,
4848
void RequestRefreshFrame() override;
4949
VideoTrackSourceInterface* GetSource() const override;
5050

51+
void set_should_receive(bool should_receive) override;
52+
bool should_receive() const override;
53+
5154
ContentHint content_hint() const override;
5255
void set_content_hint(ContentHint hint) override;
5356
bool set_enabled(bool enable) override;
@@ -81,6 +84,7 @@ class VideoTrack : public MediaStreamTrack<VideoTrackInterface>,
8184
// be queried without blocking on the worker thread by callers that don't
8285
// use an api proxy to call the `enabled()` method.
8386
bool enabled_w_ RTC_GUARDED_BY(worker_thread_) = true;
87+
bool should_receive_ RTC_GUARDED_BY(signaling_thread_) = true;
8488
};
8589

8690
} // namespace webrtc

0 commit comments

Comments
 (0)