blob: 739ed0f3a4e66f7996c0a913811830c432be81fd [file] [log] [blame]
ossu7bb87ee2017-01-23 04:56:25 -08001/*
2 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
3 *
4 * 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.
9 */
10
11// This file contains classes that implement RtpSenderInterface.
12// An RtpSender associates a MediaStreamTrackInterface with an underlying
13// transport (provided by AudioProviderInterface/VideoProviderInterface)
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#ifndef PC_RTPSENDER_H_
16#define PC_RTPSENDER_H_
ossu7bb87ee2017-01-23 04:56:25 -080017
18#include <memory>
19#include <string>
Steve Anton36b29d12017-10-30 09:57:42 -070020#include <vector>
ossu7bb87ee2017-01-23 04:56:25 -080021
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/mediastreaminterface.h"
23#include "api/rtpsenderinterface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/criticalsection.h"
Niels Möller6daa2782018-01-23 10:37:42 +010025#include "media/base/audiosource.h"
26#include "media/base/mediachannel.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "pc/dtmfsender.h"
ossu7bb87ee2017-01-23 04:56:25 -080028
29namespace webrtc {
30
Steve Anton2d8609c2018-01-23 16:38:46 -080031class StatsCollector;
32
ossu7bb87ee2017-01-23 04:56:25 -080033// Internal interface used by PeerConnection.
34class RtpSenderInternal : public RtpSenderInterface {
35 public:
Steve Anton57858b32018-02-15 15:19:50 -080036 // Sets the underlying MediaEngine channel associated with this RtpSender.
37 // SetVoiceMediaChannel should be used for audio RtpSenders and
38 // SetVideoMediaChannel should be used for video RtpSenders. Must call the
39 // appropriate SetXxxMediaChannel(nullptr) before the media channel is
40 // destroyed.
41 virtual void SetVoiceMediaChannel(
42 cricket::VoiceMediaChannel* voice_media_channel) = 0;
43 virtual void SetVideoMediaChannel(
44 cricket::VideoMediaChannel* video_media_channel) = 0;
45
ossu7bb87ee2017-01-23 04:56:25 -080046 // Used to set the SSRC of the sender, once a local description has been set.
47 // If |ssrc| is 0, this indiates that the sender should disconnect from the
48 // underlying transport (this occurs if the sender isn't seen in a local
49 // description).
50 virtual void SetSsrc(uint32_t ssrc) = 0;
51
Steve Anton8ffb9c32017-08-31 15:45:38 -070052 virtual void set_stream_ids(const std::vector<std::string>& stream_ids) = 0;
ossu7bb87ee2017-01-23 04:56:25 -080053
54 virtual void Stop() = 0;
Steve Anton57858b32018-02-15 15:19:50 -080055
56 // Returns an ID that changes every time SetTrack() is called, but
57 // otherwise remains constant. Used to generate IDs for stats.
58 // The special value zero means that no track is attached.
59 virtual int AttachmentId() const = 0;
ossu7bb87ee2017-01-23 04:56:25 -080060};
61
62// LocalAudioSinkAdapter receives data callback as a sink to the local
63// AudioTrack, and passes the data to the sink of AudioSource.
64class LocalAudioSinkAdapter : public AudioTrackSinkInterface,
65 public cricket::AudioSource {
66 public:
67 LocalAudioSinkAdapter();
68 virtual ~LocalAudioSinkAdapter();
69
70 private:
71 // AudioSinkInterface implementation.
72 void OnData(const void* audio_data,
73 int bits_per_sample,
74 int sample_rate,
75 size_t number_of_channels,
76 size_t number_of_frames) override;
77
78 // cricket::AudioSource implementation.
79 void SetSink(cricket::AudioSource::Sink* sink) override;
80
81 cricket::AudioSource::Sink* sink_;
82 // Critical section protecting |sink_|.
83 rtc::CriticalSection lock_;
84};
85
deadbeef20cb0c12017-02-01 20:27:00 -080086class AudioRtpSender : public DtmfProviderInterface,
87 public ObserverInterface,
ossu7bb87ee2017-01-23 04:56:25 -080088 public rtc::RefCountedObject<RtpSenderInternal> {
89 public:
90 // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
91 // at the appropriate times.
ossu7bb87ee2017-01-23 04:56:25 -080092
Steve Anton02ee47c2018-01-10 16:26:06 -080093 // Construct an AudioRtpSender with a null track, a single, randomly generated
Seth Hampson845e8782018-03-02 11:34:10 -080094 // stream id, and a randomly generated ID.
Steve Anton47136dd2018-01-12 10:49:35 -080095 AudioRtpSender(rtc::Thread* worker_thread, StatsCollector* stats);
ossu7bb87ee2017-01-23 04:56:25 -080096
Seth Hampson845e8782018-03-02 11:34:10 -080097 // Construct an AudioRtpSender with the given track and stream ids. The
Steve Anton02ee47c2018-01-10 16:26:06 -080098 // sender ID will be set to the track's ID.
Steve Anton47136dd2018-01-12 10:49:35 -080099 AudioRtpSender(rtc::Thread* worker_thread,
100 rtc::scoped_refptr<AudioTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800101 const std::vector<std::string>& stream_ids,
Steve Anton02ee47c2018-01-10 16:26:06 -0800102 StatsCollector* stats);
ossu7bb87ee2017-01-23 04:56:25 -0800103
104 virtual ~AudioRtpSender();
105
deadbeef20cb0c12017-02-01 20:27:00 -0800106 // DtmfSenderProvider implementation.
107 bool CanInsertDtmf() override;
108 bool InsertDtmf(int code, int duration) override;
109 sigslot::signal0<>* GetOnDestroyedSignal() override;
110
111 // ObserverInterface implementation.
ossu7bb87ee2017-01-23 04:56:25 -0800112 void OnChanged() override;
113
deadbeef20cb0c12017-02-01 20:27:00 -0800114 // RtpSenderInterface implementation.
ossu7bb87ee2017-01-23 04:56:25 -0800115 bool SetTrack(MediaStreamTrackInterface* track) override;
116 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
117 return track_;
118 }
119
120 uint32_t ssrc() const override { return ssrc_; }
121
122 cricket::MediaType media_type() const override {
123 return cricket::MEDIA_TYPE_AUDIO;
124 }
125
126 std::string id() const override { return id_; }
127
Steve Anton8ffb9c32017-08-31 15:45:38 -0700128 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 04:56:25 -0800129
Florent Castellicebf50f2018-05-03 15:31:53 +0200130 RtpParameters GetParameters() override;
Zach Steinba37b4b2018-01-23 15:02:36 -0800131 RTCError SetParameters(const RtpParameters& parameters) override;
ossu7bb87ee2017-01-23 04:56:25 -0800132
deadbeef20cb0c12017-02-01 20:27:00 -0800133 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
134
ossu7bb87ee2017-01-23 04:56:25 -0800135 // RtpSenderInternal implementation.
136 void SetSsrc(uint32_t ssrc) override;
137
Steve Anton8ffb9c32017-08-31 15:45:38 -0700138 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
139 stream_ids_ = stream_ids;
140 }
ossu7bb87ee2017-01-23 04:56:25 -0800141
142 void Stop() override;
143
Harald Alvestrandc72af932018-01-11 17:18:19 +0100144 int AttachmentId() const override { return attachment_id_; }
145
Steve Anton57858b32018-02-15 15:19:50 -0800146 void SetVoiceMediaChannel(
147 cricket::VoiceMediaChannel* voice_media_channel) override {
148 media_channel_ = voice_media_channel;
149 }
150 void SetVideoMediaChannel(
151 cricket::VideoMediaChannel* video_media_channel) override {
152 RTC_NOTREACHED();
Steve Anton47136dd2018-01-12 10:49:35 -0800153 }
ossu7bb87ee2017-01-23 04:56:25 -0800154
155 private:
156 // TODO(nisse): Since SSRC == 0 is technically valid, figure out
157 // some other way to test if we have a valid SSRC.
158 bool can_send_track() const { return track_ && ssrc_; }
159 // Helper function to construct options for
160 // AudioProviderInterface::SetAudioSend.
161 void SetAudioSend();
162 // Helper function to call SetAudioSend with "stop sending" parameters.
163 void ClearAudioSend();
164
deadbeef20cb0c12017-02-01 20:27:00 -0800165 sigslot::signal0<> SignalDestroyed;
166
Steve Anton47136dd2018-01-12 10:49:35 -0800167 rtc::Thread* const worker_thread_;
Steve Anton02ee47c2018-01-10 16:26:06 -0800168 const std::string id_;
Steve Anton8ffb9c32017-08-31 15:45:38 -0700169 std::vector<std::string> stream_ids_;
Steve Anton47136dd2018-01-12 10:49:35 -0800170 cricket::VoiceMediaChannel* media_channel_ = nullptr;
ossu7bb87ee2017-01-23 04:56:25 -0800171 StatsCollector* stats_;
172 rtc::scoped_refptr<AudioTrackInterface> track_;
deadbeef20cb0c12017-02-01 20:27:00 -0800173 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_;
Florent Castellicebf50f2018-05-03 15:31:53 +0200174 rtc::Optional<std::string> last_transaction_id_;
ossu7bb87ee2017-01-23 04:56:25 -0800175 uint32_t ssrc_ = 0;
176 bool cached_track_enabled_ = false;
177 bool stopped_ = false;
178
179 // Used to pass the data callback from the |track_| to the other end of
180 // cricket::AudioSource.
181 std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100182 int attachment_id_ = 0;
ossu7bb87ee2017-01-23 04:56:25 -0800183};
184
185class VideoRtpSender : public ObserverInterface,
186 public rtc::RefCountedObject<RtpSenderInternal> {
187 public:
Steve Anton02ee47c2018-01-10 16:26:06 -0800188 // Construct a VideoRtpSender with a null track, a single, randomly generated
Seth Hampson845e8782018-03-02 11:34:10 -0800189 // stream id, and a randomly generated ID.
Steve Anton47136dd2018-01-12 10:49:35 -0800190 explicit VideoRtpSender(rtc::Thread* worker_thread);
ossu7bb87ee2017-01-23 04:56:25 -0800191
Seth Hampson845e8782018-03-02 11:34:10 -0800192 // Construct a VideoRtpSender with the given track and stream ids. The
Steve Anton02ee47c2018-01-10 16:26:06 -0800193 // sender ID will be set to the track's ID.
Steve Anton47136dd2018-01-12 10:49:35 -0800194 VideoRtpSender(rtc::Thread* worker_thread,
195 rtc::scoped_refptr<VideoTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800196 const std::vector<std::string>& stream_ids);
ossu7bb87ee2017-01-23 04:56:25 -0800197
198 virtual ~VideoRtpSender();
199
200 // ObserverInterface implementation
201 void OnChanged() override;
202
203 // RtpSenderInterface implementation
204 bool SetTrack(MediaStreamTrackInterface* track) override;
205 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
206 return track_;
207 }
208
209 uint32_t ssrc() const override { return ssrc_; }
210
211 cricket::MediaType media_type() const override {
212 return cricket::MEDIA_TYPE_VIDEO;
213 }
214
215 std::string id() const override { return id_; }
216
Steve Anton8ffb9c32017-08-31 15:45:38 -0700217 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 04:56:25 -0800218
Florent Castellicebf50f2018-05-03 15:31:53 +0200219 RtpParameters GetParameters() override;
Zach Steinba37b4b2018-01-23 15:02:36 -0800220 RTCError SetParameters(const RtpParameters& parameters) override;
ossu7bb87ee2017-01-23 04:56:25 -0800221
deadbeef20cb0c12017-02-01 20:27:00 -0800222 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
223
ossu7bb87ee2017-01-23 04:56:25 -0800224 // RtpSenderInternal implementation.
225 void SetSsrc(uint32_t ssrc) override;
226
Steve Anton8ffb9c32017-08-31 15:45:38 -0700227 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
228 stream_ids_ = stream_ids;
229 }
ossu7bb87ee2017-01-23 04:56:25 -0800230
231 void Stop() override;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100232 int AttachmentId() const override { return attachment_id_; }
ossu7bb87ee2017-01-23 04:56:25 -0800233
Steve Anton57858b32018-02-15 15:19:50 -0800234 void SetVoiceMediaChannel(
235 cricket::VoiceMediaChannel* voice_media_channel) override {
236 RTC_NOTREACHED();
237 }
238 void SetVideoMediaChannel(
239 cricket::VideoMediaChannel* video_media_channel) override {
240 media_channel_ = video_media_channel;
Steve Anton47136dd2018-01-12 10:49:35 -0800241 }
ossu7bb87ee2017-01-23 04:56:25 -0800242
243 private:
244 bool can_send_track() const { return track_ && ssrc_; }
245 // Helper function to construct options for
246 // VideoProviderInterface::SetVideoSend.
247 void SetVideoSend();
248 // Helper function to call SetVideoSend with "stop sending" parameters.
249 void ClearVideoSend();
250
Steve Anton47136dd2018-01-12 10:49:35 -0800251 rtc::Thread* worker_thread_;
Steve Anton02ee47c2018-01-10 16:26:06 -0800252 const std::string id_;
Steve Anton8ffb9c32017-08-31 15:45:38 -0700253 std::vector<std::string> stream_ids_;
Steve Anton47136dd2018-01-12 10:49:35 -0800254 cricket::VideoMediaChannel* media_channel_ = nullptr;
ossu7bb87ee2017-01-23 04:56:25 -0800255 rtc::scoped_refptr<VideoTrackInterface> track_;
Florent Castellicebf50f2018-05-03 15:31:53 +0200256 rtc::Optional<std::string> last_transaction_id_;
ossu7bb87ee2017-01-23 04:56:25 -0800257 uint32_t ssrc_ = 0;
ossu7bb87ee2017-01-23 04:56:25 -0800258 VideoTrackInterface::ContentHint cached_track_content_hint_ =
259 VideoTrackInterface::ContentHint::kNone;
260 bool stopped_ = false;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100261 int attachment_id_ = 0;
ossu7bb87ee2017-01-23 04:56:25 -0800262};
263
264} // namespace webrtc
265
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200266#endif // PC_RTPSENDER_H_