2015-09-24 16:47:53 -07:00
|
|
|
/*
|
2016-02-10 07:54:43 -08:00
|
|
|
* Copyright 2015 The WebRTC project authors. All Rights Reserved.
|
2015-09-24 16:47:53 -07:00
|
|
|
*
|
2016-02-10 07:54:43 -08:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2015-09-24 16:47:53 -07:00
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "pc/rtpreceiver.h"
|
2015-09-24 16:47:53 -07:00
|
|
|
|
2017-11-21 13:41:51 +01:00
|
|
|
#include <utility>
|
2017-10-30 09:57:42 -07:00
|
|
|
#include <vector>
|
|
|
|
|
|
2018-07-04 20:51:53 +02:00
|
|
|
#include "api/mediastreamproxy.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "api/mediastreamtrackproxy.h"
|
|
|
|
|
#include "api/videosourceproxy.h"
|
|
|
|
|
#include "pc/audiotrack.h"
|
2018-07-04 20:51:53 +02:00
|
|
|
#include "pc/mediastream.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "pc/videotrack.h"
|
|
|
|
|
#include "rtc_base/trace_event.h"
|
2015-09-28 16:53:55 -07:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2018-01-11 17:18:19 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
// This function is only expected to be called on the signalling thread.
|
|
|
|
|
int GenerateUniqueId() {
|
|
|
|
|
static int g_unique_id = 0;
|
|
|
|
|
|
|
|
|
|
return ++g_unique_id;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-04 20:51:53 +02:00
|
|
|
std::vector<rtc::scoped_refptr<MediaStreamInterface>> CreateStreamsFromIds(
|
|
|
|
|
std::vector<std::string> stream_ids) {
|
|
|
|
|
std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams(
|
|
|
|
|
stream_ids.size());
|
|
|
|
|
for (size_t i = 0; i < stream_ids.size(); ++i) {
|
|
|
|
|
streams[i] = MediaStreamProxy::Create(
|
|
|
|
|
rtc::Thread::Current(), MediaStream::Create(std::move(stream_ids[i])));
|
|
|
|
|
}
|
|
|
|
|
return streams;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-04 14:22:34 -07:00
|
|
|
// Attempt to attach the frame decryptor to the current media channel on the
|
|
|
|
|
// correct worker thread only if both the media channel exists and a ssrc has
|
|
|
|
|
// been allocated to the stream.
|
|
|
|
|
void MaybeAttachFrameDecryptorToMediaChannel(
|
|
|
|
|
const absl::optional<uint32_t>& ssrc,
|
2018-09-10 14:06:02 -07:00
|
|
|
rtc::Thread* worker_thread,
|
2018-10-04 14:22:34 -07:00
|
|
|
rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor,
|
2018-10-26 13:16:16 -07:00
|
|
|
cricket::MediaChannel* media_channel,
|
|
|
|
|
bool stopped) {
|
|
|
|
|
if (media_channel && frame_decryptor && ssrc.has_value() && !stopped) {
|
2018-10-09 17:29:54 -07:00
|
|
|
worker_thread->Invoke<void>(RTC_FROM_HERE, [&] {
|
2018-10-04 14:22:34 -07:00
|
|
|
media_channel->SetFrameDecryptor(*ssrc, frame_decryptor);
|
2018-09-10 14:06:02 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-11 17:18:19 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
2018-07-04 20:51:53 +02:00
|
|
|
AudioRtpReceiver::AudioRtpReceiver(rtc::Thread* worker_thread,
|
|
|
|
|
std::string receiver_id,
|
|
|
|
|
std::vector<std::string> stream_ids)
|
|
|
|
|
: AudioRtpReceiver(worker_thread,
|
|
|
|
|
receiver_id,
|
|
|
|
|
CreateStreamsFromIds(std::move(stream_ids))) {}
|
|
|
|
|
|
2017-11-21 13:41:51 +01:00
|
|
|
AudioRtpReceiver::AudioRtpReceiver(
|
2018-01-10 11:51:34 -08:00
|
|
|
rtc::Thread* worker_thread,
|
2017-11-27 13:01:52 -08:00
|
|
|
const std::string& receiver_id,
|
2018-01-17 17:41:02 -08:00
|
|
|
const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams)
|
2018-01-10 11:51:34 -08:00
|
|
|
: worker_thread_(worker_thread),
|
|
|
|
|
id_(receiver_id),
|
2018-01-17 17:41:02 -08:00
|
|
|
source_(new rtc::RefCountedObject<RemoteAudioSource>(worker_thread)),
|
|
|
|
|
track_(AudioTrackProxy::Create(rtc::Thread::Current(),
|
|
|
|
|
AudioTrack::Create(receiver_id, source_))),
|
2018-01-11 17:18:19 +01:00
|
|
|
cached_track_enabled_(track_->enabled()),
|
|
|
|
|
attachment_id_(GenerateUniqueId()) {
|
2018-01-10 11:51:34 -08:00
|
|
|
RTC_DCHECK(worker_thread_);
|
2015-12-15 04:27:11 -08:00
|
|
|
RTC_DCHECK(track_->GetSource()->remote());
|
2015-09-28 16:53:55 -07:00
|
|
|
track_->RegisterObserver(this);
|
|
|
|
|
track_->GetSource()->RegisterAudioObserver(this);
|
2018-01-10 17:15:20 -08:00
|
|
|
SetStreams(streams);
|
2015-09-28 16:53:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AudioRtpReceiver::~AudioRtpReceiver() {
|
|
|
|
|
track_->GetSource()->UnregisterAudioObserver(this);
|
|
|
|
|
track_->UnregisterObserver(this);
|
|
|
|
|
Stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AudioRtpReceiver::OnChanged() {
|
|
|
|
|
if (cached_track_enabled_ != track_->enabled()) {
|
|
|
|
|
cached_track_enabled_ = track_->enabled();
|
|
|
|
|
Reconfigure();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-10 11:51:34 -08:00
|
|
|
bool AudioRtpReceiver::SetOutputVolume(double volume) {
|
|
|
|
|
RTC_DCHECK_GE(volume, 0.0);
|
|
|
|
|
RTC_DCHECK_LE(volume, 10.0);
|
|
|
|
|
RTC_DCHECK(media_channel_);
|
2018-01-17 17:41:02 -08:00
|
|
|
RTC_DCHECK(ssrc_);
|
2018-01-10 11:51:34 -08:00
|
|
|
return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
|
2018-01-17 17:41:02 -08:00
|
|
|
return media_channel_->SetOutputVolume(*ssrc_, volume);
|
2018-01-10 11:51:34 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-28 16:53:55 -07:00
|
|
|
void AudioRtpReceiver::OnSetVolume(double volume) {
|
2017-08-09 17:22:01 -07:00
|
|
|
RTC_DCHECK_GE(volume, 0);
|
|
|
|
|
RTC_DCHECK_LE(volume, 10);
|
2016-06-27 16:30:35 -07:00
|
|
|
cached_volume_ = volume;
|
2018-01-17 17:41:02 -08:00
|
|
|
if (!media_channel_ || !ssrc_) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_ERROR)
|
|
|
|
|
<< "AudioRtpReceiver::OnSetVolume: No audio channel exists.";
|
2016-06-27 16:30:35 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2015-09-28 16:53:55 -07:00
|
|
|
// When the track is disabled, the volume of the source, which is the
|
|
|
|
|
// corresponding WebRtc Voice Engine channel will be 0. So we do not allow
|
|
|
|
|
// setting the volume to the source when the track is disabled.
|
2016-06-27 16:30:35 -07:00
|
|
|
if (!stopped_ && track_->enabled()) {
|
2018-01-10 11:51:34 -08:00
|
|
|
if (!SetOutputVolume(cached_volume_)) {
|
2017-01-12 02:24:27 -08:00
|
|
|
RTC_NOTREACHED();
|
2016-06-27 16:30:35 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-09-28 16:53:55 -07:00
|
|
|
}
|
|
|
|
|
|
2018-07-04 20:51:53 +02:00
|
|
|
std::vector<std::string> AudioRtpReceiver::stream_ids() const {
|
|
|
|
|
std::vector<std::string> stream_ids(streams_.size());
|
|
|
|
|
for (size_t i = 0; i < streams_.size(); ++i)
|
|
|
|
|
stream_ids[i] = streams_[i]->id();
|
|
|
|
|
return stream_ids;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-16 11:40:30 -07:00
|
|
|
RtpParameters AudioRtpReceiver::GetParameters() const {
|
2018-01-17 17:41:02 -08:00
|
|
|
if (!media_channel_ || !ssrc_ || stopped_) {
|
2016-06-27 16:30:35 -07:00
|
|
|
return RtpParameters();
|
|
|
|
|
}
|
2018-01-10 11:51:34 -08:00
|
|
|
return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] {
|
2018-01-17 17:41:02 -08:00
|
|
|
return media_channel_->GetRtpReceiveParameters(*ssrc_);
|
2018-01-10 11:51:34 -08:00
|
|
|
});
|
2016-05-16 11:40:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AudioRtpReceiver::SetParameters(const RtpParameters& parameters) {
|
|
|
|
|
TRACE_EVENT0("webrtc", "AudioRtpReceiver::SetParameters");
|
2018-01-17 17:41:02 -08:00
|
|
|
if (!media_channel_ || !ssrc_ || stopped_) {
|
2016-06-27 16:30:35 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
2018-01-10 11:51:34 -08:00
|
|
|
return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
|
2018-01-17 17:41:02 -08:00
|
|
|
return media_channel_->SetRtpReceiveParameters(*ssrc_, parameters);
|
2018-01-10 11:51:34 -08:00
|
|
|
});
|
2016-05-16 11:40:30 -07:00
|
|
|
}
|
|
|
|
|
|
2018-08-29 17:02:10 -07:00
|
|
|
void AudioRtpReceiver::SetFrameDecryptor(
|
|
|
|
|
rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) {
|
|
|
|
|
frame_decryptor_ = std::move(frame_decryptor);
|
2018-10-09 17:29:54 -07:00
|
|
|
// Special Case: Set the frame decryptor to any value on any existing channel.
|
2018-10-26 13:16:16 -07:00
|
|
|
if (media_channel_ && ssrc_.has_value() && !stopped_) {
|
2018-10-09 17:29:54 -07:00
|
|
|
worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
|
|
|
|
|
media_channel_->SetFrameDecryptor(*ssrc_, frame_decryptor_);
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-08-29 17:02:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rtc::scoped_refptr<FrameDecryptorInterface>
|
|
|
|
|
AudioRtpReceiver::GetFrameDecryptor() const {
|
|
|
|
|
return frame_decryptor_;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-06 14:27:39 -07:00
|
|
|
void AudioRtpReceiver::Stop() {
|
|
|
|
|
// TODO(deadbeef): Need to do more here to fully stop receiving packets.
|
2016-06-27 16:30:35 -07:00
|
|
|
if (stopped_) {
|
2016-06-06 14:27:39 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2018-01-17 17:41:02 -08:00
|
|
|
if (media_channel_ && ssrc_) {
|
2016-06-27 16:30:35 -07:00
|
|
|
// Allow that SetOutputVolume fail. This is the normal case when the
|
|
|
|
|
// underlying media channel has already been deleted.
|
2018-01-10 11:51:34 -08:00
|
|
|
SetOutputVolume(0.0);
|
2016-06-27 16:30:35 -07:00
|
|
|
}
|
|
|
|
|
stopped_ = true;
|
2016-06-06 14:27:39 -07:00
|
|
|
}
|
|
|
|
|
|
2018-01-17 17:41:02 -08:00
|
|
|
void AudioRtpReceiver::SetupMediaChannel(uint32_t ssrc) {
|
|
|
|
|
if (!media_channel_) {
|
|
|
|
|
RTC_LOG(LS_ERROR)
|
|
|
|
|
<< "AudioRtpReceiver::SetupMediaChannel: No audio channel exists.";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (ssrc_ == ssrc) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (ssrc_) {
|
|
|
|
|
source_->Stop(media_channel_, *ssrc_);
|
|
|
|
|
}
|
|
|
|
|
ssrc_ = ssrc;
|
|
|
|
|
source_->Start(media_channel_, *ssrc_);
|
|
|
|
|
Reconfigure();
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-04 20:51:53 +02:00
|
|
|
void AudioRtpReceiver::set_stream_ids(std::vector<std::string> stream_ids) {
|
|
|
|
|
SetStreams(CreateStreamsFromIds(std::move(stream_ids)));
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-10 17:15:20 -08:00
|
|
|
void AudioRtpReceiver::SetStreams(
|
|
|
|
|
const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
|
|
|
|
|
// Remove remote track from any streams that are going away.
|
|
|
|
|
for (auto existing_stream : streams_) {
|
|
|
|
|
bool removed = true;
|
|
|
|
|
for (auto stream : streams) {
|
2018-03-13 16:05:28 -07:00
|
|
|
if (existing_stream->id() == stream->id()) {
|
2018-01-10 17:15:20 -08:00
|
|
|
RTC_DCHECK_EQ(existing_stream.get(), stream.get());
|
|
|
|
|
removed = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (removed) {
|
|
|
|
|
existing_stream->RemoveTrack(track_);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Add remote track to any streams that are new.
|
|
|
|
|
for (auto stream : streams) {
|
|
|
|
|
bool added = true;
|
|
|
|
|
for (auto existing_stream : streams_) {
|
2018-03-13 16:05:28 -07:00
|
|
|
if (stream->id() == existing_stream->id()) {
|
2018-01-10 17:15:20 -08:00
|
|
|
RTC_DCHECK_EQ(stream.get(), existing_stream.get());
|
|
|
|
|
added = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (added) {
|
|
|
|
|
stream->AddTrack(track_);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
streams_ = streams;
|
|
|
|
|
}
|
|
|
|
|
|
Reland of Implemented the GetSources() in native code. (patchset #1 id:1 of https://codereview.webrtc.org/2809613002/ )
Reason for revert:
Re-land, reverting did not fix bug.
https://bugs.chromium.org/p/webrtc/issues/detail?id=7465
Original issue's description:
> Revert of Implemented the GetSources() in native code. (patchset #11 id:510001 of https://codereview.webrtc.org/2770233003/ )
>
> Reason for revert:
> Suspected of WebRtcApprtcBrowserTest.MANUAL_WorksOnApprtc breakage, see
>
> https://bugs.chromium.org/p/webrtc/issues/detail?id=7465
>
> Original issue's description:
> > Added the GetSources() to the RtpReceiverInterface and implemented
> > it for the AudioRtpReceiver.
> >
> > This method returns a vector of RtpSource(both CSRC source and SSRC
> > source) which contains the ID of a source, the timestamp, the source
> > type (SSRC or CSRC) and the audio level.
> >
> > The RtpSource objects are buffered and maintained by the
> > RtpReceiver in webrtc/modules/rtp_rtcp/. When the method is called,
> > the info of the contributing source will be pulled along the object
> > chain:
> > AudioRtpReceiver -> VoiceChannel -> WebRtcVoiceMediaChannel ->
> > AudioReceiveStream -> voe::Channel -> RtpRtcp module
> >
> > Spec:https://w3c.github.io/webrtc-pc/archives/20151006/webrtc.html#widl-RTCRtpReceiver-getContributingSources-sequence-RTCRtpContributingSource
> >
> > BUG=chromium:703122
> > TBR=stefan@webrtc.org, danilchap@webrtc.org
> >
> > Review-Url: https://codereview.webrtc.org/2770233003
> > Cr-Commit-Position: refs/heads/master@{#17591}
> > Committed: https://chromium.googlesource.com/external/webrtc/+/292084c3765d9f3ee406ca2ec86eae206b540053
>
> TBR=deadbeef@webrtc.org,solenberg@webrtc.org,hbos@webrtc.org,philipel@webrtc.org,stefan@webrtc.org,danilchap@webrtc.org,zhihuang@webrtc.org
> # Not skipping CQ checks because original CL landed more than 1 days ago.
> BUG=chromium:703122
>
> Review-Url: https://codereview.webrtc.org/2809613002
> Cr-Commit-Position: refs/heads/master@{#17616}
> Committed: https://chromium.googlesource.com/external/webrtc/+/fbcc5cb3869d1370008e40f24fc03ac8fb69c675
TBR=deadbeef@webrtc.org,solenberg@webrtc.org,philipel@webrtc.org,stefan@webrtc.org,danilchap@webrtc.org,zhihuang@webrtc.org,olka@webrtc.org
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=chromium:703122
Review-Url: https://codereview.webrtc.org/2810623003
Cr-Commit-Position: refs/heads/master@{#17621}
2017-04-10 07:39:05 -07:00
|
|
|
std::vector<RtpSource> AudioRtpReceiver::GetSources() const {
|
2018-01-17 17:41:02 -08:00
|
|
|
if (!media_channel_ || !ssrc_ || stopped_) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2018-01-10 11:51:34 -08:00
|
|
|
return worker_thread_->Invoke<std::vector<RtpSource>>(
|
2018-01-17 17:41:02 -08:00
|
|
|
RTC_FROM_HERE, [&] { return media_channel_->GetSources(*ssrc_); });
|
Reland of Implemented the GetSources() in native code. (patchset #1 id:1 of https://codereview.webrtc.org/2809613002/ )
Reason for revert:
Re-land, reverting did not fix bug.
https://bugs.chromium.org/p/webrtc/issues/detail?id=7465
Original issue's description:
> Revert of Implemented the GetSources() in native code. (patchset #11 id:510001 of https://codereview.webrtc.org/2770233003/ )
>
> Reason for revert:
> Suspected of WebRtcApprtcBrowserTest.MANUAL_WorksOnApprtc breakage, see
>
> https://bugs.chromium.org/p/webrtc/issues/detail?id=7465
>
> Original issue's description:
> > Added the GetSources() to the RtpReceiverInterface and implemented
> > it for the AudioRtpReceiver.
> >
> > This method returns a vector of RtpSource(both CSRC source and SSRC
> > source) which contains the ID of a source, the timestamp, the source
> > type (SSRC or CSRC) and the audio level.
> >
> > The RtpSource objects are buffered and maintained by the
> > RtpReceiver in webrtc/modules/rtp_rtcp/. When the method is called,
> > the info of the contributing source will be pulled along the object
> > chain:
> > AudioRtpReceiver -> VoiceChannel -> WebRtcVoiceMediaChannel ->
> > AudioReceiveStream -> voe::Channel -> RtpRtcp module
> >
> > Spec:https://w3c.github.io/webrtc-pc/archives/20151006/webrtc.html#widl-RTCRtpReceiver-getContributingSources-sequence-RTCRtpContributingSource
> >
> > BUG=chromium:703122
> > TBR=stefan@webrtc.org, danilchap@webrtc.org
> >
> > Review-Url: https://codereview.webrtc.org/2770233003
> > Cr-Commit-Position: refs/heads/master@{#17591}
> > Committed: https://chromium.googlesource.com/external/webrtc/+/292084c3765d9f3ee406ca2ec86eae206b540053
>
> TBR=deadbeef@webrtc.org,solenberg@webrtc.org,hbos@webrtc.org,philipel@webrtc.org,stefan@webrtc.org,danilchap@webrtc.org,zhihuang@webrtc.org
> # Not skipping CQ checks because original CL landed more than 1 days ago.
> BUG=chromium:703122
>
> Review-Url: https://codereview.webrtc.org/2809613002
> Cr-Commit-Position: refs/heads/master@{#17616}
> Committed: https://chromium.googlesource.com/external/webrtc/+/fbcc5cb3869d1370008e40f24fc03ac8fb69c675
TBR=deadbeef@webrtc.org,solenberg@webrtc.org,philipel@webrtc.org,stefan@webrtc.org,danilchap@webrtc.org,zhihuang@webrtc.org,olka@webrtc.org
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=chromium:703122
Review-Url: https://codereview.webrtc.org/2810623003
Cr-Commit-Position: refs/heads/master@{#17621}
2017-04-10 07:39:05 -07:00
|
|
|
}
|
|
|
|
|
|
2015-09-28 16:53:55 -07:00
|
|
|
void AudioRtpReceiver::Reconfigure() {
|
2016-06-27 16:30:35 -07:00
|
|
|
RTC_DCHECK(!stopped_);
|
2018-01-17 17:41:02 -08:00
|
|
|
if (!media_channel_ || !ssrc_) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_ERROR)
|
|
|
|
|
<< "AudioRtpReceiver::Reconfigure: No audio channel exists.";
|
2015-09-28 16:53:55 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2018-01-10 11:51:34 -08:00
|
|
|
if (!SetOutputVolume(track_->enabled() ? cached_volume_ : 0)) {
|
2017-01-12 02:24:27 -08:00
|
|
|
RTC_NOTREACHED();
|
2016-06-27 16:30:35 -07:00
|
|
|
}
|
2018-10-04 14:22:34 -07:00
|
|
|
// Reattach the frame decryptor if we were reconfigured.
|
2018-10-26 13:16:16 -07:00
|
|
|
MaybeAttachFrameDecryptorToMediaChannel(
|
|
|
|
|
ssrc_, worker_thread_, frame_decryptor_, media_channel_, stopped_);
|
2015-09-28 16:53:55 -07:00
|
|
|
}
|
|
|
|
|
|
2016-06-14 11:47:14 -07:00
|
|
|
void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
|
|
|
|
|
observer_ = observer;
|
2016-06-27 16:30:35 -07:00
|
|
|
// Deliver any notifications the observer may have missed by being set late.
|
2016-12-07 10:36:40 -08:00
|
|
|
if (received_first_packet_ && observer_) {
|
2016-06-14 11:47:14 -07:00
|
|
|
observer_->OnFirstPacketReceived(media_type());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-13 16:26:05 -08:00
|
|
|
void AudioRtpReceiver::SetMediaChannel(cricket::MediaChannel* media_channel) {
|
|
|
|
|
RTC_DCHECK(media_channel == nullptr ||
|
|
|
|
|
media_channel->media_type() == media_type());
|
|
|
|
|
media_channel_ = static_cast<cricket::VoiceMediaChannel*>(media_channel);
|
2018-09-10 14:06:02 -07:00
|
|
|
}
|
|
|
|
|
|
2018-01-10 11:51:34 -08:00
|
|
|
void AudioRtpReceiver::NotifyFirstPacketReceived() {
|
2016-06-14 11:47:14 -07:00
|
|
|
if (observer_) {
|
|
|
|
|
observer_->OnFirstPacketReceived(media_type());
|
|
|
|
|
}
|
|
|
|
|
received_first_packet_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-04 20:51:53 +02:00
|
|
|
VideoRtpReceiver::VideoRtpReceiver(rtc::Thread* worker_thread,
|
|
|
|
|
std::string receiver_id,
|
|
|
|
|
std::vector<std::string> stream_ids)
|
|
|
|
|
: VideoRtpReceiver(worker_thread,
|
|
|
|
|
receiver_id,
|
|
|
|
|
CreateStreamsFromIds(std::move(stream_ids))) {}
|
|
|
|
|
|
2017-11-21 13:41:51 +01:00
|
|
|
VideoRtpReceiver::VideoRtpReceiver(
|
2018-01-10 11:51:34 -08:00
|
|
|
rtc::Thread* worker_thread,
|
2018-01-10 17:15:20 -08:00
|
|
|
const std::string& receiver_id,
|
2018-01-17 17:41:02 -08:00
|
|
|
const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams)
|
2018-01-10 11:51:34 -08:00
|
|
|
: worker_thread_(worker_thread),
|
2018-01-10 17:15:20 -08:00
|
|
|
id_(receiver_id),
|
2018-05-23 16:28:17 +02:00
|
|
|
source_(new RefCountedObject<VideoRtpTrackSource>()),
|
2016-03-10 18:32:00 +01:00
|
|
|
track_(VideoTrackProxy::Create(
|
|
|
|
|
rtc::Thread::Current(),
|
2016-04-07 07:45:54 -07:00
|
|
|
worker_thread,
|
|
|
|
|
VideoTrack::Create(
|
2018-01-10 17:15:20 -08:00
|
|
|
receiver_id,
|
2016-04-07 07:45:54 -07:00
|
|
|
VideoTrackSourceProxy::Create(rtc::Thread::Current(),
|
|
|
|
|
worker_thread,
|
2017-07-31 23:22:01 -07:00
|
|
|
source_),
|
2018-01-11 17:18:19 +01:00
|
|
|
worker_thread))),
|
|
|
|
|
attachment_id_(GenerateUniqueId()) {
|
2018-01-10 11:51:34 -08:00
|
|
|
RTC_DCHECK(worker_thread_);
|
2018-01-10 17:15:20 -08:00
|
|
|
SetStreams(streams);
|
2016-03-10 18:32:00 +01:00
|
|
|
source_->SetState(MediaSourceInterface::kLive);
|
2015-09-28 16:53:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VideoRtpReceiver::~VideoRtpReceiver() {
|
|
|
|
|
// Since cricket::VideoRenderer is not reference counted,
|
2016-06-27 16:30:35 -07:00
|
|
|
// we need to remove it from the channel before we are deleted.
|
2015-09-28 16:53:55 -07:00
|
|
|
Stop();
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-04 20:51:53 +02:00
|
|
|
std::vector<std::string> VideoRtpReceiver::stream_ids() const {
|
|
|
|
|
std::vector<std::string> stream_ids(streams_.size());
|
|
|
|
|
for (size_t i = 0; i < streams_.size(); ++i)
|
|
|
|
|
stream_ids[i] = streams_[i]->id();
|
|
|
|
|
return stream_ids;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-10 11:51:34 -08:00
|
|
|
bool VideoRtpReceiver::SetSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
|
|
|
|
|
RTC_DCHECK(media_channel_);
|
2018-01-17 17:41:02 -08:00
|
|
|
RTC_DCHECK(ssrc_);
|
2018-01-10 11:51:34 -08:00
|
|
|
return worker_thread_->Invoke<bool>(
|
2018-01-17 17:41:02 -08:00
|
|
|
RTC_FROM_HERE, [&] { return media_channel_->SetSink(*ssrc_, sink); });
|
2018-01-10 11:51:34 -08:00
|
|
|
}
|
|
|
|
|
|
2016-06-06 14:27:39 -07:00
|
|
|
RtpParameters VideoRtpReceiver::GetParameters() const {
|
2018-01-17 17:41:02 -08:00
|
|
|
if (!media_channel_ || !ssrc_ || stopped_) {
|
2016-06-27 16:30:35 -07:00
|
|
|
return RtpParameters();
|
|
|
|
|
}
|
2018-01-10 11:51:34 -08:00
|
|
|
return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] {
|
2018-01-17 17:41:02 -08:00
|
|
|
return media_channel_->GetRtpReceiveParameters(*ssrc_);
|
2018-01-10 11:51:34 -08:00
|
|
|
});
|
2016-06-06 14:27:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool VideoRtpReceiver::SetParameters(const RtpParameters& parameters) {
|
|
|
|
|
TRACE_EVENT0("webrtc", "VideoRtpReceiver::SetParameters");
|
2018-01-17 17:41:02 -08:00
|
|
|
if (!media_channel_ || !ssrc_ || stopped_) {
|
2016-06-27 16:30:35 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
2018-01-10 11:51:34 -08:00
|
|
|
return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
|
2018-01-17 17:41:02 -08:00
|
|
|
return media_channel_->SetRtpReceiveParameters(*ssrc_, parameters);
|
2018-01-10 11:51:34 -08:00
|
|
|
});
|
2016-06-06 14:27:39 -07:00
|
|
|
}
|
|
|
|
|
|
2018-08-29 17:02:10 -07:00
|
|
|
void VideoRtpReceiver::SetFrameDecryptor(
|
|
|
|
|
rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) {
|
|
|
|
|
frame_decryptor_ = std::move(frame_decryptor);
|
2018-10-09 17:29:54 -07:00
|
|
|
// Special Case: Set the frame decryptor to any value on any existing channel.
|
2018-10-26 13:16:16 -07:00
|
|
|
if (media_channel_ && ssrc_.has_value() && !stopped_) {
|
2018-10-09 17:29:54 -07:00
|
|
|
worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
|
|
|
|
|
media_channel_->SetFrameDecryptor(*ssrc_, frame_decryptor_);
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-08-29 17:02:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rtc::scoped_refptr<FrameDecryptorInterface>
|
|
|
|
|
VideoRtpReceiver::GetFrameDecryptor() const {
|
|
|
|
|
return frame_decryptor_;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-28 16:53:55 -07:00
|
|
|
void VideoRtpReceiver::Stop() {
|
|
|
|
|
// TODO(deadbeef): Need to do more here to fully stop receiving packets.
|
2016-06-27 16:30:35 -07:00
|
|
|
if (stopped_) {
|
2015-09-28 16:53:55 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2016-03-10 18:32:00 +01:00
|
|
|
source_->SetState(MediaSourceInterface::kEnded);
|
2018-01-17 17:41:02 -08:00
|
|
|
if (!media_channel_ || !ssrc_) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_WARNING) << "VideoRtpReceiver::Stop: No video channel exists.";
|
2016-06-27 16:30:35 -07:00
|
|
|
} else {
|
|
|
|
|
// Allow that SetSink fail. This is the normal case when the underlying
|
|
|
|
|
// media channel has already been deleted.
|
2018-01-10 11:51:34 -08:00
|
|
|
SetSink(nullptr);
|
2016-06-27 16:30:35 -07:00
|
|
|
}
|
|
|
|
|
stopped_ = true;
|
2015-09-28 16:53:55 -07:00
|
|
|
}
|
|
|
|
|
|
2018-01-17 17:41:02 -08:00
|
|
|
void VideoRtpReceiver::SetupMediaChannel(uint32_t ssrc) {
|
|
|
|
|
if (!media_channel_) {
|
|
|
|
|
RTC_LOG(LS_ERROR)
|
|
|
|
|
<< "VideoRtpReceiver::SetupMediaChannel: No video channel exists.";
|
|
|
|
|
}
|
|
|
|
|
if (ssrc_ == ssrc) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (ssrc_) {
|
|
|
|
|
SetSink(nullptr);
|
|
|
|
|
}
|
|
|
|
|
ssrc_ = ssrc;
|
2018-05-23 16:28:17 +02:00
|
|
|
SetSink(source_->sink());
|
2018-10-04 14:22:34 -07:00
|
|
|
// Attach any existing frame decryptor to the media channel.
|
2018-10-26 13:16:16 -07:00
|
|
|
MaybeAttachFrameDecryptorToMediaChannel(
|
|
|
|
|
ssrc_, worker_thread_, frame_decryptor_, media_channel_, stopped_);
|
2018-01-17 17:41:02 -08:00
|
|
|
}
|
|
|
|
|
|
2018-07-04 20:51:53 +02:00
|
|
|
void VideoRtpReceiver::set_stream_ids(std::vector<std::string> stream_ids) {
|
|
|
|
|
SetStreams(CreateStreamsFromIds(std::move(stream_ids)));
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-10 17:15:20 -08:00
|
|
|
void VideoRtpReceiver::SetStreams(
|
|
|
|
|
const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
|
|
|
|
|
// Remove remote track from any streams that are going away.
|
|
|
|
|
for (auto existing_stream : streams_) {
|
|
|
|
|
bool removed = true;
|
|
|
|
|
for (auto stream : streams) {
|
2018-03-13 16:05:28 -07:00
|
|
|
if (existing_stream->id() == stream->id()) {
|
2018-01-10 17:15:20 -08:00
|
|
|
RTC_DCHECK_EQ(existing_stream.get(), stream.get());
|
|
|
|
|
removed = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (removed) {
|
|
|
|
|
existing_stream->RemoveTrack(track_);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Add remote track to any streams that are new.
|
|
|
|
|
for (auto stream : streams) {
|
|
|
|
|
bool added = true;
|
|
|
|
|
for (auto existing_stream : streams_) {
|
2018-03-13 16:05:28 -07:00
|
|
|
if (stream->id() == existing_stream->id()) {
|
2018-01-10 17:15:20 -08:00
|
|
|
RTC_DCHECK_EQ(stream.get(), existing_stream.get());
|
|
|
|
|
added = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (added) {
|
|
|
|
|
stream->AddTrack(track_);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
streams_ = streams;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-14 11:47:14 -07:00
|
|
|
void VideoRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
|
|
|
|
|
observer_ = observer;
|
2016-06-27 16:30:35 -07:00
|
|
|
// Deliver any notifications the observer may have missed by being set late.
|
2016-12-07 10:36:40 -08:00
|
|
|
if (received_first_packet_ && observer_) {
|
2016-06-14 11:47:14 -07:00
|
|
|
observer_->OnFirstPacketReceived(media_type());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-13 16:26:05 -08:00
|
|
|
void VideoRtpReceiver::SetMediaChannel(cricket::MediaChannel* media_channel) {
|
|
|
|
|
RTC_DCHECK(media_channel == nullptr ||
|
|
|
|
|
media_channel->media_type() == media_type());
|
|
|
|
|
media_channel_ = static_cast<cricket::VideoMediaChannel*>(media_channel);
|
2018-09-10 14:06:02 -07:00
|
|
|
}
|
|
|
|
|
|
2018-01-10 11:51:34 -08:00
|
|
|
void VideoRtpReceiver::NotifyFirstPacketReceived() {
|
2016-06-14 11:47:14 -07:00
|
|
|
if (observer_) {
|
|
|
|
|
observer_->OnFirstPacketReceived(media_type());
|
|
|
|
|
}
|
|
|
|
|
received_first_packet_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-26 16:04:32 +02:00
|
|
|
std::vector<RtpSource> VideoRtpReceiver::GetSources() const {
|
|
|
|
|
if (!media_channel_ || !ssrc_ || stopped_) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
return worker_thread_->Invoke<std::vector<RtpSource>>(
|
|
|
|
|
RTC_FROM_HERE, [&] { return media_channel_->GetSources(*ssrc_); });
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-28 16:53:55 -07:00
|
|
|
} // namespace webrtc
|