Skip to content

Commit 378dc0b

Browse files
davidliucloudwebrtc
authored andcommitted
Android improvements.
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) Co-authored-by: davidliu <davidliu@deviange.net>
1 parent 03dc44e commit 378dc0b

23 files changed

+567
-2
lines changed

api/media_stream_interface.cc

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

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

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
@@ -997,6 +997,8 @@ class VideoMediaReceiveChannelInterface : public MediaReceiveChannelInterface {
997997
bool nack_enabled,
998998
webrtc::RtcpMode rtcp_mode,
999999
absl::optional<int> rtx_time) = 0;
1000+
virtual void StartReceive(uint32_t ssrc) {}
1001+
virtual void StopReceive(uint32_t ssrc) {}
10001002
};
10011003

10021004
} // namespace cricket

media/engine/webrtc_video_engine.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,24 @@ void WebRtcVideoChannel::RequestEncoderSwitch(
928928
}
929929
}
930930

931+
void WebRtcVideoChannel::StartReceive(uint32_t ssrc) {
932+
RTC_DCHECK_RUN_ON(&thread_checker_);
933+
WebRtcVideoReceiveStream* stream = FindReceiveStream(ssrc);
934+
if(!stream) {
935+
return;
936+
}
937+
stream->StartStream();
938+
}
939+
940+
void WebRtcVideoChannel::StopReceive(uint32_t ssrc) {
941+
RTC_DCHECK_RUN_ON(&thread_checker_);
942+
WebRtcVideoReceiveStream* stream = FindReceiveStream(ssrc);
943+
if(!stream) {
944+
return;
945+
}
946+
stream->StopStream();
947+
}
948+
931949
bool WebRtcVideoChannel::ApplyChangedParams(
932950
const ChangedSendParameters& changed_params) {
933951
RTC_DCHECK_RUN_ON(&thread_checker_);
@@ -3182,6 +3200,17 @@ void WebRtcVideoChannel::WebRtcVideoReceiveStream::SetRecvParameters(
31823200
}
31833201
}
31843202

3203+
void WebRtcVideoChannel::WebRtcVideoReceiveStream::StartStream(){
3204+
if (stream_) {
3205+
stream_->Start();
3206+
}
3207+
}
3208+
void WebRtcVideoChannel::WebRtcVideoReceiveStream::StopStream(){
3209+
if (stream_) {
3210+
stream_->Stop();
3211+
}
3212+
}
3213+
31853214
void WebRtcVideoChannel::WebRtcVideoReceiveStream::RecreateReceiveStream() {
31863215
RTC_DCHECK(stream_);
31873216
absl::optional<int> base_minimum_playout_delay_ms;

media/engine/webrtc_video_engine.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ class WebRtcVideoChannel : public VideoMediaChannel,
270270
webrtc::RtcpMode rtcp_mode,
271271
absl::optional<int> rtx_time) override;
272272

273+
void StartReceive(uint32_t ssrc) override;
274+
void StopReceive(uint32_t ssrc) override;
273275
private:
274276
class WebRtcVideoReceiveStream;
275277

@@ -537,6 +539,9 @@ class WebRtcVideoChannel : public VideoMediaChannel,
537539
void SetDepacketizerToDecoderFrameTransformer(
538540
rtc::scoped_refptr<webrtc::FrameTransformerInterface>
539541
frame_transformer);
542+
543+
void StartStream();
544+
void StopStream();
540545

541546
void SetLocalSsrc(uint32_t local_ssrc);
542547
void UpdateRtxSsrc(uint32_t ssrc);

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: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,20 @@ 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

5053
VideoRtpReceiver::~VideoRtpReceiver() {
5154
RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
5255
RTC_DCHECK(!media_channel_);
56+
57+
track_->UnregisterObserver(this);
5358
}
5459

5560
std::vector<std::string> VideoRtpReceiver::stream_ids() const {
@@ -114,6 +119,39 @@ void VideoRtpReceiver::Stop() {
114119
track_->internal()->set_ended();
115120
}
116121

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+
117155
void VideoRtpReceiver::RestartMediaChannel(absl::optional<uint32_t> ssrc) {
118156
RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
119157
MediaSourceInterface::SourceState state = source_->state();
@@ -209,6 +247,7 @@ void VideoRtpReceiver::set_transport(
209247
void VideoRtpReceiver::SetStreams(
210248
const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
211249
RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
250+
212251
// Remove remote track from any streams that are going away.
213252
for (const auto& existing_stream : streams_) {
214253
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)