blob: ca477a6064037bc0c7639f3e410383b7ca78f6ad [file] [log] [blame]
deadbeef6979b022015-09-24 16:47:53 -07001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
deadbeef6979b022015-09-24 16:47:53 -07003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
deadbeef6979b022015-09-24 16:47:53 -07009 */
10
deadbeef70ab1a12015-09-28 16:53:55 -070011// This file contains classes that implement RtpReceiverInterface.
12// An RtpReceiver associates a MediaStreamTrackInterface with an underlying
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070013// transport (provided by cricket::VoiceChannel/cricket::VideoChannel)
deadbeef70ab1a12015-09-28 16:53:55 -070014
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#ifndef PC_RTPRECEIVER_H_
16#define PC_RTPRECEIVER_H_
deadbeef70ab1a12015-09-28 16:53:55 -070017
pbosc7c26a02017-01-02 08:42:32 -080018#include <stdint.h>
deadbeef70ab1a12015-09-28 16:53:55 -070019#include <string>
Steve Anton36b29d12017-10-30 09:57:42 -070020#include <vector>
deadbeef70ab1a12015-09-28 16:53:55 -070021
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "absl/types/optional.h"
23#include "api/crypto/framedecryptorinterface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "api/mediastreaminterface.h"
Yves Gerey3e707812018-11-28 16:47:49 +010025#include "api/mediatypes.h"
26#include "api/rtpparameters.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "api/rtpreceiverinterface.h"
Yves Gerey3e707812018-11-28 16:47:49 +010028#include "api/video/video_frame.h"
29#include "api/video/video_sink_interface.h"
30#include "api/video/video_source_interface.h"
31#include "media/base/mediachannel.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "media/base/videobroadcaster.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "pc/remoteaudiosource.h"
34#include "pc/videotracksource.h"
Yves Gerey3e707812018-11-28 16:47:49 +010035#include "rtc_base/refcountedobject.h"
36#include "rtc_base/scoped_ref_ptr.h"
37#include "rtc_base/thread.h"
deadbeef70ab1a12015-09-28 16:53:55 -070038
39namespace webrtc {
40
deadbeefa601f5c2016-06-06 14:27:39 -070041// Internal class used by PeerConnection.
42class RtpReceiverInternal : public RtpReceiverInterface {
43 public:
44 virtual void Stop() = 0;
Steve Antonef65ef12018-01-10 17:15:20 -080045
Steve Anton57858b32018-02-15 15:19:50 -080046 // Sets the underlying MediaEngine channel associated with this RtpSender.
Amit Hilbuchdd9390c2018-11-13 16:26:05 -080047 // A VoiceMediaChannel should be used for audio RtpSenders and
48 // a VideoMediaChannel should be used for video RtpSenders.
49 // Must call SetMediaChannel(nullptr) before the media channel is destroyed.
50 virtual void SetMediaChannel(cricket::MediaChannel* media_channel) = 0;
Steve Anton57858b32018-02-15 15:19:50 -080051
Steve Antond3679212018-01-17 17:41:02 -080052 // Configures the RtpReceiver with the underlying media channel, with the
53 // given SSRC as the stream identifier. If |ssrc| is 0, the receiver will
54 // receive packets on unsignaled SSRCs.
55 virtual void SetupMediaChannel(uint32_t ssrc) = 0;
56
deadbeefe814a0d2017-02-25 18:15:09 -080057 // This SSRC is used as an identifier for the receiver between the API layer
eladalonf1841382017-06-12 01:16:46 -070058 // and the WebRtcVideoEngine, WebRtcVoiceEngine layer.
deadbeefe814a0d2017-02-25 18:15:09 -080059 virtual uint32_t ssrc() const = 0;
Steve Anton60776752018-01-10 11:51:34 -080060
61 // Call this to notify the RtpReceiver when the first packet has been received
62 // on the corresponding channel.
63 virtual void NotifyFirstPacketReceived() = 0;
Steve Antonef65ef12018-01-10 17:15:20 -080064
65 // Set the associated remote media streams for this receiver. The remote track
66 // will be removed from any streams that are no longer present and added to
67 // any new streams.
Henrik Boström199e27b2018-07-04 20:51:53 +020068 virtual void set_stream_ids(std::vector<std::string> stream_ids) = 0;
69 // TODO(https://crbug.com/webrtc/9480): Remove SetStreams() in favor of
70 // set_stream_ids() as soon as downstream projects are no longer dependent on
71 // stream objects.
Steve Antonef65ef12018-01-10 17:15:20 -080072 virtual void SetStreams(
73 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) = 0;
Steve Anton57858b32018-02-15 15:19:50 -080074
75 // Returns an ID that changes if the attached track changes, but
76 // otherwise remains constant. Used to generate IDs for stats.
77 // The special value zero means that no track is attached.
78 virtual int AttachmentId() const = 0;
deadbeefa601f5c2016-06-06 14:27:39 -070079};
80
deadbeef70ab1a12015-09-28 16:53:55 -070081class AudioRtpReceiver : public ObserverInterface,
82 public AudioSourceInterface::AudioObserver,
Steve Anton60776752018-01-10 11:51:34 -080083 public rtc::RefCountedObject<RtpReceiverInternal> {
deadbeef70ab1a12015-09-28 16:53:55 -070084 public:
Henrik Boström199e27b2018-07-04 20:51:53 +020085 AudioRtpReceiver(rtc::Thread* worker_thread,
86 std::string receiver_id,
87 std::vector<std::string> stream_ids);
88 // TODO(https://crbug.com/webrtc/9480): Remove this when streams() is removed.
Henrik Boström9e6fd2b2017-11-21 13:41:51 +010089 AudioRtpReceiver(
Steve Anton60776752018-01-10 11:51:34 -080090 rtc::Thread* worker_thread,
Steve Anton9158ef62017-11-27 13:01:52 -080091 const std::string& receiver_id,
Steve Antond3679212018-01-17 17:41:02 -080092 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams);
deadbeef70ab1a12015-09-28 16:53:55 -070093 virtual ~AudioRtpReceiver();
94
95 // ObserverInterface implementation
96 void OnChanged() override;
97
98 // AudioSourceInterface::AudioObserver implementation
99 void OnSetVolume(double volume) override;
100
perkjd61bf802016-03-24 03:16:19 -0700101 rtc::scoped_refptr<AudioTrackInterface> audio_track() const {
102 return track_.get();
103 }
104
deadbeef70ab1a12015-09-28 16:53:55 -0700105 // RtpReceiverInterface implementation
106 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
107 return track_.get();
108 }
Henrik Boström199e27b2018-07-04 20:51:53 +0200109 std::vector<std::string> stream_ids() const override;
Henrik Boström9e6fd2b2017-11-21 13:41:51 +0100110 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams()
111 const override {
112 return streams_;
113 }
deadbeef70ab1a12015-09-28 16:53:55 -0700114
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700115 cricket::MediaType media_type() const override {
116 return cricket::MEDIA_TYPE_AUDIO;
117 }
118
deadbeef70ab1a12015-09-28 16:53:55 -0700119 std::string id() const override { return id_; }
120
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700121 RtpParameters GetParameters() const override;
122 bool SetParameters(const RtpParameters& parameters) override;
123
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700124 void SetFrameDecryptor(
125 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) override;
126
127 rtc::scoped_refptr<FrameDecryptorInterface> GetFrameDecryptor()
128 const override;
129
deadbeefa601f5c2016-06-06 14:27:39 -0700130 // RtpReceiverInternal implementation.
131 void Stop() override;
Steve Antond3679212018-01-17 17:41:02 -0800132 void SetupMediaChannel(uint32_t ssrc) override;
133 uint32_t ssrc() const override { return ssrc_.value_or(0); }
Steve Anton60776752018-01-10 11:51:34 -0800134 void NotifyFirstPacketReceived() override;
Henrik Boström199e27b2018-07-04 20:51:53 +0200135 void set_stream_ids(std::vector<std::string> stream_ids) override;
Steve Antonef65ef12018-01-10 17:15:20 -0800136 void SetStreams(const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
137 streams) override;
zhihuang184a3fd2016-06-14 11:47:14 -0700138 void SetObserver(RtpReceiverObserverInterface* observer) override;
Benjamin Wrightbfd412e2018-09-10 14:06:02 -0700139
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800140 void SetMediaChannel(cricket::MediaChannel* media_channel) override;
zhihuang184a3fd2016-06-14 11:47:14 -0700141
hbos8d609f62017-04-10 07:39:05 -0700142 std::vector<RtpSource> GetSources() const override;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100143 int AttachmentId() const override { return attachment_id_; }
hbos8d609f62017-04-10 07:39:05 -0700144
deadbeef70ab1a12015-09-28 16:53:55 -0700145 private:
146 void Reconfigure();
Steve Anton60776752018-01-10 11:51:34 -0800147 bool SetOutputVolume(double volume);
deadbeef70ab1a12015-09-28 16:53:55 -0700148
Steve Anton60776752018-01-10 11:51:34 -0800149 rtc::Thread* const worker_thread_;
Tommif888bb52015-12-12 01:37:01 +0100150 const std::string id_;
Steve Antond3679212018-01-17 17:41:02 -0800151 const rtc::scoped_refptr<RemoteAudioSource> source_;
perkjd61bf802016-03-24 03:16:19 -0700152 const rtc::scoped_refptr<AudioTrackInterface> track_;
Steve Antond3679212018-01-17 17:41:02 -0800153 cricket::VoiceMediaChannel* media_channel_ = nullptr;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200154 absl::optional<uint32_t> ssrc_;
Henrik Boström9e6fd2b2017-11-21 13:41:51 +0100155 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_;
deadbeef70ab1a12015-09-28 16:53:55 -0700156 bool cached_track_enabled_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700157 double cached_volume_ = 1;
158 bool stopped_ = false;
zhihuang184a3fd2016-06-14 11:47:14 -0700159 RtpReceiverObserverInterface* observer_ = nullptr;
160 bool received_first_packet_ = false;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100161 int attachment_id_ = 0;
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700162 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor_;
deadbeef70ab1a12015-09-28 16:53:55 -0700163};
164
Steve Anton60776752018-01-10 11:51:34 -0800165class VideoRtpReceiver : public rtc::RefCountedObject<RtpReceiverInternal> {
deadbeef70ab1a12015-09-28 16:53:55 -0700166 public:
deadbeefe814a0d2017-02-25 18:15:09 -0800167 // An SSRC of 0 will create a receiver that will match the first SSRC it
168 // sees.
Henrik Boström199e27b2018-07-04 20:51:53 +0200169 VideoRtpReceiver(rtc::Thread* worker_thread,
170 std::string receiver_id,
171 std::vector<std::string> streams_ids);
172 // TODO(hbos): Remove this when streams() is removed.
173 // https://crbug.com/webrtc/9480
Henrik Boström9e6fd2b2017-11-21 13:41:51 +0100174 VideoRtpReceiver(
Steve Anton60776752018-01-10 11:51:34 -0800175 rtc::Thread* worker_thread,
Steve Antonef65ef12018-01-10 17:15:20 -0800176 const std::string& receiver_id,
Steve Antond3679212018-01-17 17:41:02 -0800177 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams);
deadbeef70ab1a12015-09-28 16:53:55 -0700178
179 virtual ~VideoRtpReceiver();
180
perkjf0dcfe22016-03-10 18:32:00 +0100181 rtc::scoped_refptr<VideoTrackInterface> video_track() const {
182 return track_.get();
183 }
184
deadbeef70ab1a12015-09-28 16:53:55 -0700185 // RtpReceiverInterface implementation
186 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
187 return track_.get();
188 }
Henrik Boström199e27b2018-07-04 20:51:53 +0200189 std::vector<std::string> stream_ids() const override;
Henrik Boström9e6fd2b2017-11-21 13:41:51 +0100190 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams()
191 const override {
192 return streams_;
193 }
deadbeef70ab1a12015-09-28 16:53:55 -0700194
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700195 cricket::MediaType media_type() const override {
196 return cricket::MEDIA_TYPE_VIDEO;
197 }
198
deadbeef70ab1a12015-09-28 16:53:55 -0700199 std::string id() const override { return id_; }
200
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700201 RtpParameters GetParameters() const override;
202 bool SetParameters(const RtpParameters& parameters) override;
203
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700204 void SetFrameDecryptor(
205 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) override;
206
207 rtc::scoped_refptr<FrameDecryptorInterface> GetFrameDecryptor()
208 const override;
209
deadbeefa601f5c2016-06-06 14:27:39 -0700210 // RtpReceiverInternal implementation.
211 void Stop() override;
Steve Antond3679212018-01-17 17:41:02 -0800212 void SetupMediaChannel(uint32_t ssrc) override;
213 uint32_t ssrc() const override { return ssrc_.value_or(0); }
Steve Anton60776752018-01-10 11:51:34 -0800214 void NotifyFirstPacketReceived() override;
Henrik Boström199e27b2018-07-04 20:51:53 +0200215 void set_stream_ids(std::vector<std::string> stream_ids) override;
Steve Antonef65ef12018-01-10 17:15:20 -0800216 void SetStreams(const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
217 streams) override;
deadbeefa601f5c2016-06-06 14:27:39 -0700218
zhihuang184a3fd2016-06-14 11:47:14 -0700219 void SetObserver(RtpReceiverObserverInterface* observer) override;
220
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800221 void SetMediaChannel(cricket::MediaChannel* media_channel) override;
Steve Anton57858b32018-02-15 15:19:50 -0800222
223 int AttachmentId() const override { return attachment_id_; }
zhihuang184a3fd2016-06-14 11:47:14 -0700224
Jonas Oreland49ac5952018-09-26 16:04:32 +0200225 std::vector<RtpSource> GetSources() const override;
226
deadbeef70ab1a12015-09-28 16:53:55 -0700227 private:
Niels Möller5d67f822018-05-23 16:28:17 +0200228 class VideoRtpTrackSource : public VideoTrackSource {
229 public:
230 VideoRtpTrackSource() : VideoTrackSource(true /* remote */) {}
231
232 rtc::VideoSourceInterface<VideoFrame>* source() override {
233 return &broadcaster_;
234 }
235 rtc::VideoSinkInterface<VideoFrame>* sink() { return &broadcaster_; }
236
237 private:
238 // |broadcaster_| is needed since the decoder can only handle one sink.
239 // It might be better if the decoder can handle multiple sinks and consider
240 // the VideoSinkWants.
241 rtc::VideoBroadcaster broadcaster_;
242 };
243
Steve Anton60776752018-01-10 11:51:34 -0800244 bool SetSink(rtc::VideoSinkInterface<VideoFrame>* sink);
zhihuang184a3fd2016-06-14 11:47:14 -0700245
Steve Anton60776752018-01-10 11:51:34 -0800246 rtc::Thread* const worker_thread_;
Steve Antonef65ef12018-01-10 17:15:20 -0800247 const std::string id_;
Steve Anton60776752018-01-10 11:51:34 -0800248 cricket::VideoMediaChannel* media_channel_ = nullptr;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200249 absl::optional<uint32_t> ssrc_;
perkjf0dcfe22016-03-10 18:32:00 +0100250 // |source_| is held here to be able to change the state of the source when
251 // the VideoRtpReceiver is stopped.
Niels Möller5d67f822018-05-23 16:28:17 +0200252 rtc::scoped_refptr<VideoRtpTrackSource> source_;
perkjf0dcfe22016-03-10 18:32:00 +0100253 rtc::scoped_refptr<VideoTrackInterface> track_;
Henrik Boström9e6fd2b2017-11-21 13:41:51 +0100254 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700255 bool stopped_ = false;
zhihuang184a3fd2016-06-14 11:47:14 -0700256 RtpReceiverObserverInterface* observer_ = nullptr;
257 bool received_first_packet_ = false;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100258 int attachment_id_ = 0;
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700259 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor_;
deadbeef70ab1a12015-09-28 16:53:55 -0700260};
261
262} // namespace webrtc
263
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200264#endif // PC_RTPRECEIVER_H_