Skip to content

Commit e9891d8

Browse files
committed
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 2ab32cf commit e9891d8

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
@@ -1001,6 +1001,8 @@ class VideoMediaReceiveChannelInterface : public MediaReceiveChannelInterface {
10011001
webrtc::RtcpMode rtcp_mode,
10021002
absl::optional<int> rtx_time) = 0;
10031003
virtual bool AddDefaultRecvStreamForTesting(const StreamParams& sp) = 0;
1004+
virtual void StartReceive(uint32_t ssrc) {}
1005+
virtual void StopReceive(uint32_t ssrc) {}
10041006
};
10051007

10061008
} // namespace cricket

media/engine/webrtc_video_engine.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,6 +2579,24 @@ WebRtcVideoReceiveChannel::~WebRtcVideoReceiveChannel() {
25792579
delete kv.second;
25802580
}
25812581

2582+
void WebRtcVideoReceiveChannel::StartReceive(uint32_t ssrc) {
2583+
RTC_DCHECK_RUN_ON(&thread_checker_);
2584+
WebRtcVideoReceiveStream* stream = FindReceiveStream(ssrc);
2585+
if(!stream) {
2586+
return;
2587+
}
2588+
stream->StartStream();
2589+
}
2590+
2591+
void WebRtcVideoReceiveChannel::StopReceive(uint32_t ssrc) {
2592+
RTC_DCHECK_RUN_ON(&thread_checker_);
2593+
WebRtcVideoReceiveStream* stream = FindReceiveStream(ssrc);
2594+
if(!stream) {
2595+
return;
2596+
}
2597+
stream->StopStream();
2598+
}
2599+
25822600
void WebRtcVideoReceiveChannel::SetReceiverFeedbackParameters(
25832601
bool lntf_enabled,
25842602
bool nack_enabled,
@@ -3478,6 +3496,17 @@ void WebRtcVideoReceiveChannel::WebRtcVideoReceiveStream::SetReceiverParameters(
34783496
}
34793497
}
34803498

3499+
void WebRtcVideoReceiveChannel::WebRtcVideoReceiveStream::StartStream(){
3500+
if (stream_) {
3501+
stream_->Start();
3502+
}
3503+
}
3504+
void WebRtcVideoReceiveChannel::WebRtcVideoReceiveStream::StopStream(){
3505+
if (stream_) {
3506+
stream_->Stop();
3507+
}
3508+
}
3509+
34813510
void WebRtcVideoReceiveChannel::WebRtcVideoReceiveStream::
34823511
RecreateReceiveStream() {
34833512
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
@@ -294,7 +294,6 @@ class WebRtcVideoSendChannel : public MediaChannelUtil,
294294
}
295295
return send_codec()->rtx_time;
296296
}
297-
298297
private:
299298
struct ChangedSenderParameters {
300299
// These optionals are unset if not changed.
@@ -643,6 +642,8 @@ class WebRtcVideoReceiveChannel : public MediaChannelUtil,
643642
webrtc::RtcpMode rtcp_mode,
644643
absl::optional<int> rtx_time) override;
645644

645+
void StartReceive(uint32_t ssrc) override;
646+
void StopReceive(uint32_t ssrc) override;
646647
private:
647648
class WebRtcVideoReceiveStream;
648649
struct ChangedReceiverParameters {
@@ -744,6 +745,9 @@ class WebRtcVideoReceiveChannel : public MediaChannelUtil,
744745
rtc::scoped_refptr<webrtc::FrameTransformerInterface>
745746
frame_transformer);
746747

748+
void StartStream();
749+
void StopStream();
750+
747751
void SetLocalSsrc(uint32_t local_ssrc);
748752
void UpdateRtxSsrc(uint32_t ssrc);
749753
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

sdk/android/api/org/webrtc/VideoTrack.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,24 @@ public void removeSink(VideoSink sink) {
5454
}
5555
}
5656

57+
/**
58+
* For a remote video track, starts/stops receiving the video stream.
59+
*
60+
* If this is a local video track, this is a no-op.
61+
*/
62+
public void setShouldReceive(boolean shouldReceive){
63+
nativeSetShouldReceive(getNativeMediaStreamTrack(), shouldReceive);
64+
}
65+
66+
/**
67+
* The current receive status for a remote video track.
68+
*
69+
* This has no meaning for a local video track.
70+
*/
71+
public boolean shouldReceive(){
72+
return nativeGetShouldReceive(getNativeMediaStreamTrack());
73+
}
74+
5775
@Override
5876
public void dispose() {
5977
for (long nativeSink : sinks.values()) {
@@ -73,4 +91,6 @@ public long getNativeVideoTrack() {
7391
private static native void nativeRemoveSink(long track, long nativeSink);
7492
private static native long nativeWrapSink(VideoSink sink);
7593
private static native void nativeFreeSink(long sink);
94+
private static native void nativeSetShouldReceive(long track, boolean shouldReceive);
95+
private static native boolean nativeGetShouldReceive(long track);
7696
}

sdk/android/src/jni/video_track.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,16 @@ static void JNI_VideoTrack_FreeSink(JNIEnv* jni, jlong j_native_sink) {
4444
delete reinterpret_cast<rtc::VideoSinkInterface<VideoFrame>*>(j_native_sink);
4545
}
4646

47+
static void JNI_VideoTrack_SetShouldReceive(JNIEnv* jni,
48+
jlong j_native_track,
49+
jboolean should_receive) {
50+
reinterpret_cast<VideoTrackInterface*>(j_native_track)->set_should_receive(should_receive);
51+
}
52+
53+
static jboolean JNI_VideoTrack_GetShouldReceive(JNIEnv* jni,
54+
jlong j_native_track) {
55+
return reinterpret_cast<VideoTrackInterface*>(j_native_track)->should_receive();
56+
}
57+
4758
} // namespace jni
4859
} // namespace webrtc

sdk/objc/api/peerconnection/RTCVideoTrack.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ RTC_OBJC_EXPORT
2525
/** The video source for this video track. */
2626
@property(nonatomic, readonly) RTC_OBJC_TYPE(RTCVideoSource) *source;
2727

28+
/** The receive state, if this is a remote video track. */
29+
@property(nonatomic, assign) BOOL shouldReceive;
30+
2831
- (instancetype)init NS_UNAVAILABLE;
2932

3033
/** Register a renderer that will render all frames received on this track. */

sdk/objc/api/peerconnection/RTCVideoTrack.mm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ - (void)dealloc {
7070
return _source;
7171
}
7272

73+
- (BOOL)shouldReceive {
74+
return self.nativeVideoTrack->should_receive();
75+
}
76+
77+
- (void)setShouldReceive:(BOOL)shouldReceive {
78+
self.nativeVideoTrack->set_should_receive(shouldReceive);
79+
}
80+
7381
- (void)addRenderer:(id<RTC_OBJC_TYPE(RTCVideoRenderer)>)renderer {
7482
if (!_workerThread->IsCurrent()) {
7583
_workerThread->BlockingCall([renderer, self] { [self addRenderer:renderer]; });

0 commit comments

Comments
 (0)