Skip to content

Commit 570716c

Browse files
kanatipavlidakis
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 # Conflicts: # media/base/media_channel.h # pc/video_rtp_receiver.cc # pc/video_rtp_receiver.h
1 parent acab13c commit 570716c

File tree

14 files changed

+152
-3
lines changed

14 files changed

+152
-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 @@ namespace webrtc {
1919
const char* const MediaStreamTrackInterface::kVideoKind = kMediaTypeVideo;
2020
const char* const MediaStreamTrackInterface::kAudioKind = 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
@@ -189,6 +189,8 @@ class RTC_EXPORT VideoTrackInterface : public MediaStreamTrackInterface,
189189

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

192+
virtual void set_should_receive(bool should_receive) {}
193+
virtual bool should_receive() const;
192194
virtual ContentHint content_hint() const;
193195
virtual void set_content_hint(ContentHint /* hint */) {}
194196

media/base/media_channel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,8 @@ class VideoMediaReceiveChannelInterface : public MediaReceiveChannelInterface {
10151015
std::optional<int> rtx_time) = 0;
10161016
virtual bool AddDefaultRecvStreamForTesting(
10171017
const webrtc::StreamParams& sp) = 0;
1018+
virtual void StartReceive(uint32_t ssrc) {}
1019+
virtual void StopReceive(uint32_t ssrc) {}
10181020
};
10191021

10201022
} // namespace webrtc

media/engine/webrtc_video_engine.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2779,6 +2779,24 @@ WebRtcVideoReceiveChannel::~WebRtcVideoReceiveChannel() {
27792779
delete kv.second;
27802780
}
27812781

2782+
void WebRtcVideoReceiveChannel::StartReceive(uint32_t ssrc) {
2783+
RTC_DCHECK_RUN_ON(&thread_checker_);
2784+
WebRtcVideoReceiveStream* stream = FindReceiveStream(ssrc);
2785+
if(!stream) {
2786+
return;
2787+
}
2788+
stream->StartStream();
2789+
}
2790+
2791+
void WebRtcVideoReceiveChannel::StopReceive(uint32_t ssrc) {
2792+
RTC_DCHECK_RUN_ON(&thread_checker_);
2793+
WebRtcVideoReceiveStream* stream = FindReceiveStream(ssrc);
2794+
if(!stream) {
2795+
return;
2796+
}
2797+
stream->StopStream();
2798+
}
2799+
27822800
void WebRtcVideoReceiveChannel::SetReceiverFeedbackParameters(
27832801
bool lntf_enabled,
27842802
bool nack_enabled,
@@ -3676,6 +3694,17 @@ void WebRtcVideoReceiveChannel::WebRtcVideoReceiveStream::SetReceiverParameters(
36763694
}
36773695
}
36783696

3697+
void WebRtcVideoReceiveChannel::WebRtcVideoReceiveStream::StartStream(){
3698+
if (stream_) {
3699+
stream_->Start();
3700+
}
3701+
}
3702+
void WebRtcVideoReceiveChannel::WebRtcVideoReceiveStream::StopStream(){
3703+
if (stream_) {
3704+
stream_->Stop();
3705+
}
3706+
}
3707+
36793708
void WebRtcVideoReceiveChannel::WebRtcVideoReceiveStream::
36803709
RecreateReceiveStream() {
36813710
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
@@ -286,7 +286,6 @@ class WebRtcVideoSendChannel : public MediaChannelUtil,
286286
}
287287
return send_codec()->rtx_time;
288288
}
289-
290289
private:
291290
struct ChangedSenderParameters {
292291
// These optionals are unset if not changed.
@@ -621,6 +620,8 @@ class WebRtcVideoReceiveChannel : public MediaChannelUtil,
621620
RtcpMode rtcp_mode,
622621
std::optional<int> rtx_time) override;
623622

623+
void StartReceive(uint32_t ssrc) override;
624+
void StopReceive(uint32_t ssrc) override;
624625
private:
625626
class WebRtcVideoReceiveStream;
626627
struct ChangedReceiverParameters {
@@ -718,6 +719,9 @@ class WebRtcVideoReceiveChannel : public MediaChannelUtil,
718719
void SetDepacketizerToDecoderFrameTransformer(
719720
scoped_refptr<FrameTransformerInterface> frame_transformer);
720721

722+
void StartStream();
723+
void StopStream();
724+
721725
void SetLocalSsrc(uint32_t local_ssrc);
722726
void UpdateRtxSsrc(uint32_t ssrc);
723727
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, 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
@@ -59,9 +59,12 @@ VideoRtpReceiver::VideoRtpReceiver(
5959
Thread::Current(),
6060
worker_thread,
6161
VideoTrack::Create(receiver_id, source_, worker_thread))),
62-
attachment_id_(GenerateUniqueId()) {
62+
cached_track_should_receive_(track_->should_receive()),
63+
attachment_id_(GenerateUniqueId()),
64+
worker_thread_safety_(PendingTaskSafetyFlag::CreateDetachedInactive()) {
6365
RTC_DCHECK(worker_thread_);
6466
SetStreams(streams);
67+
track_->RegisterObserver(this);
6568
RTC_DCHECK_EQ(source_->state(), MediaSourceInterface::kInitializing);
6669
}
6770

@@ -131,6 +134,39 @@ void VideoRtpReceiver::Stop() {
131134
track_->internal()->set_ended();
132135
}
133136

137+
void VideoRtpReceiver::OnChanged() {
138+
RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
139+
if (cached_track_should_receive_ != track_->should_receive()) {
140+
cached_track_should_receive_ = track_->should_receive();
141+
worker_thread_->PostTask(
142+
[this, receive = cached_track_should_receive_]() {
143+
RTC_DCHECK_RUN_ON(worker_thread_);
144+
if(receive) {
145+
StartMediaChannel();
146+
} else {
147+
StopMediaChannel();
148+
}
149+
});
150+
}
151+
}
152+
153+
void VideoRtpReceiver::StartMediaChannel() {
154+
RTC_DCHECK_RUN_ON(worker_thread_);
155+
if (!media_channel_) {
156+
return;
157+
}
158+
media_channel_->StartReceive(signaled_ssrc_.value_or(0));
159+
OnGenerateKeyFrame();
160+
}
161+
162+
void VideoRtpReceiver::StopMediaChannel() {
163+
RTC_DCHECK_RUN_ON(worker_thread_);
164+
if (!media_channel_) {
165+
return;
166+
}
167+
media_channel_->StopReceive(signaled_ssrc_.value_or(0));
168+
}
169+
134170
void VideoRtpReceiver::RestartMediaChannel(std::optional<uint32_t> ssrc) {
135171
RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
136172
MediaSourceInterface::SourceState state = source_->state();
@@ -226,6 +262,7 @@ void VideoRtpReceiver::set_transport(
226262
void VideoRtpReceiver::SetStreams(
227263
const std::vector<scoped_refptr<MediaStreamInterface>>& streams) {
228264
RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
265+
229266
// Remove remote track from any streams that are going away.
230267
for (const auto& existing_stream : streams_) {
231268
bool removed = true;

pc/video_rtp_receiver.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "api/scoped_refptr.h"
2828
#include "api/sequence_checker.h"
2929
#include "api/transport/rtp/rtp_source.h"
30+
#include "api/task_queue/pending_task_safety_flag.h"
3031
#include "api/video/video_frame.h"
3132
#include "api/video/video_sink_interface.h"
3233
#include "api/video/video_source_interface.h"
@@ -42,7 +43,8 @@
4243

4344
namespace webrtc {
4445

45-
class VideoRtpReceiver : public RtpReceiverInternal {
46+
class VideoRtpReceiver : public RtpReceiverInternal,
47+
public ObserverInterface {
4648
public:
4749
// An SSRC of 0 will create a receiver that will match the first SSRC it
4850
// sees. Must be called on signaling thread.
@@ -60,6 +62,9 @@ class VideoRtpReceiver : public RtpReceiverInternal {
6062

6163
scoped_refptr<VideoTrackInterface> video_track() const { return track_; }
6264

65+
// ObserverInterface implementation
66+
void OnChanged() override;
67+
6368
// RtpReceiverInterface implementation
6469
scoped_refptr<MediaStreamTrackInterface> track() const override {
6570
return track_;
@@ -114,6 +119,8 @@ class VideoRtpReceiver : public RtpReceiverInternal {
114119
private:
115120
void RestartMediaChannel(std::optional<uint32_t> ssrc)
116121
RTC_RUN_ON(&signaling_thread_checker_);
122+
void StartMediaChannel();
123+
void StopMediaChannel();
117124
void RestartMediaChannel_w(std::optional<uint32_t> ssrc,
118125
MediaSourceInterface::SourceState state)
119126
RTC_RUN_ON(worker_thread_);
@@ -158,6 +165,8 @@ class VideoRtpReceiver : public RtpReceiverInternal {
158165
RTC_GUARDED_BY(&signaling_thread_checker_) = nullptr;
159166
bool received_first_packet_ RTC_GUARDED_BY(&signaling_thread_checker_) =
160167
false;
168+
169+
bool cached_track_should_receive_ RTC_GUARDED_BY(&signaling_thread_checker_);
161170
const int attachment_id_;
162171
scoped_refptr<FrameDecryptorInterface> frame_decryptor_
163172
RTC_GUARDED_BY(worker_thread_);
@@ -173,6 +182,7 @@ class VideoRtpReceiver : public RtpReceiverInternal {
173182
// or switched.
174183
bool saved_generate_keyframe_ RTC_GUARDED_BY(worker_thread_) = false;
175184
bool saved_encoded_sink_enabled_ RTC_GUARDED_BY(worker_thread_) = false;
185+
const rtc::scoped_refptr<PendingTaskSafetyFlag> worker_thread_safety_;
176186
};
177187

178188
} // namespace webrtc

pc/video_track.cc

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

89+
void VideoTrack::set_should_receive(bool receive) {
90+
RTC_DCHECK_RUN_ON(&signaling_thread_);
91+
if (should_receive_ == receive)
92+
return;
93+
should_receive_ = receive;
94+
Notifier<VideoTrackInterface>::FireOnChanged();
95+
}
96+
97+
bool VideoTrack::should_receive() const {
98+
RTC_DCHECK_RUN_ON(&signaling_thread_);
99+
return should_receive_;
100+
}
101+
89102
VideoTrackInterface::ContentHint VideoTrack::content_hint() const {
90103
RTC_DCHECK_RUN_ON(&signaling_thread_);
91104
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)