blob: 983bb20ae0af839c6518275fa25470dbe4e97821 [file] [log] [blame]
ossu7bb87ee2017-01-23 12:56:251/*
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 04:47:3115#ifndef PC_RTPSENDER_H_
16#define PC_RTPSENDER_H_
ossu7bb87ee2017-01-23 12:56:2517
18#include <memory>
19#include <string>
Steve Anton36b29d12017-10-30 16:57:4220#include <vector>
ossu7bb87ee2017-01-23 12:56:2521
Mirko Bonadei92ea95e2017-09-15 04:47:3122#include "api/mediastreaminterface.h"
23#include "api/rtpsenderinterface.h"
Niels Möller6daa2782018-01-23 09:37:4224#include "media/base/audiosource.h"
25#include "media/base/mediachannel.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3126#include "pc/dtmfsender.h"
Yves Gerey665174f2018-06-19 13:03:0527#include "rtc_base/criticalsection.h"
ossu7bb87ee2017-01-23 12:56:2528
29namespace webrtc {
30
Steve Anton2d8609c2018-01-24 00:38:4631class StatsCollector;
32
ossu7bb87ee2017-01-23 12:56:2533// Internal interface used by PeerConnection.
34class RtpSenderInternal : public RtpSenderInterface {
35 public:
Steve Anton57858b32018-02-15 23:19:5036 // 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 12:56:2546 // 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 22:45:3852 virtual void set_stream_ids(const std::vector<std::string>& stream_ids) = 0;
ossu7bb87ee2017-01-23 12:56:2553
54 virtual void Stop() = 0;
Steve Anton57858b32018-02-15 23:19:5055
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 12:56:2560};
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-02 04:27:0086class AudioRtpSender : public DtmfProviderInterface,
87 public ObserverInterface,
ossu7bb87ee2017-01-23 12:56:2588 public rtc::RefCountedObject<RtpSenderInternal> {
89 public:
90 // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
91 // at the appropriate times.
ossu7bb87ee2017-01-23 12:56:2592
Steve Anton111fdfd2018-06-25 20:03:3693 // Construct an RtpSender for audio with the given sender ID.
94 // The sender is initialized with no track to send and no associated streams.
Steve Anton47136dd2018-01-12 18:49:3595 AudioRtpSender(rtc::Thread* worker_thread,
Steve Anton111fdfd2018-06-25 20:03:3696 const std::string& id,
Steve Anton02ee47c2018-01-11 00:26:0697 StatsCollector* stats);
ossu7bb87ee2017-01-23 12:56:2598
99 virtual ~AudioRtpSender();
100
deadbeef20cb0c12017-02-02 04:27:00101 // DtmfSenderProvider implementation.
102 bool CanInsertDtmf() override;
103 bool InsertDtmf(int code, int duration) override;
104 sigslot::signal0<>* GetOnDestroyedSignal() override;
105
106 // ObserverInterface implementation.
ossu7bb87ee2017-01-23 12:56:25107 void OnChanged() override;
108
deadbeef20cb0c12017-02-02 04:27:00109 // RtpSenderInterface implementation.
ossu7bb87ee2017-01-23 12:56:25110 bool SetTrack(MediaStreamTrackInterface* track) override;
111 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
112 return track_;
113 }
114
115 uint32_t ssrc() const override { return ssrc_; }
116
117 cricket::MediaType media_type() const override {
118 return cricket::MEDIA_TYPE_AUDIO;
119 }
120
121 std::string id() const override { return id_; }
122
Steve Anton8ffb9c32017-08-31 22:45:38123 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 12:56:25124
Florent Castellicebf50f2018-05-03 13:31:53125 RtpParameters GetParameters() override;
Zach Steinba37b4b2018-01-23 23:02:36126 RTCError SetParameters(const RtpParameters& parameters) override;
ossu7bb87ee2017-01-23 12:56:25127
deadbeef20cb0c12017-02-02 04:27:00128 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
129
ossu7bb87ee2017-01-23 12:56:25130 // RtpSenderInternal implementation.
131 void SetSsrc(uint32_t ssrc) override;
132
Steve Anton8ffb9c32017-08-31 22:45:38133 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
134 stream_ids_ = stream_ids;
135 }
ossu7bb87ee2017-01-23 12:56:25136
137 void Stop() override;
138
Harald Alvestrandc72af932018-01-11 16:18:19139 int AttachmentId() const override { return attachment_id_; }
140
Steve Anton57858b32018-02-15 23:19:50141 void SetVoiceMediaChannel(
142 cricket::VoiceMediaChannel* voice_media_channel) override {
143 media_channel_ = voice_media_channel;
144 }
145 void SetVideoMediaChannel(
146 cricket::VideoMediaChannel* video_media_channel) override {
147 RTC_NOTREACHED();
Steve Anton47136dd2018-01-12 18:49:35148 }
ossu7bb87ee2017-01-23 12:56:25149
150 private:
151 // TODO(nisse): Since SSRC == 0 is technically valid, figure out
152 // some other way to test if we have a valid SSRC.
153 bool can_send_track() const { return track_ && ssrc_; }
154 // Helper function to construct options for
155 // AudioProviderInterface::SetAudioSend.
156 void SetAudioSend();
157 // Helper function to call SetAudioSend with "stop sending" parameters.
158 void ClearAudioSend();
159
deadbeef20cb0c12017-02-02 04:27:00160 sigslot::signal0<> SignalDestroyed;
161
Steve Anton47136dd2018-01-12 18:49:35162 rtc::Thread* const worker_thread_;
Steve Anton02ee47c2018-01-11 00:26:06163 const std::string id_;
Steve Anton8ffb9c32017-08-31 22:45:38164 std::vector<std::string> stream_ids_;
Steve Anton47136dd2018-01-12 18:49:35165 cricket::VoiceMediaChannel* media_channel_ = nullptr;
Steve Anton111fdfd2018-06-25 20:03:36166 StatsCollector* stats_ = nullptr;
ossu7bb87ee2017-01-23 12:56:25167 rtc::scoped_refptr<AudioTrackInterface> track_;
deadbeef20cb0c12017-02-02 04:27:00168 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_;
Danil Chapovalov66cadcc2018-06-19 14:47:43169 absl::optional<std::string> last_transaction_id_;
ossu7bb87ee2017-01-23 12:56:25170 uint32_t ssrc_ = 0;
171 bool cached_track_enabled_ = false;
172 bool stopped_ = false;
173
174 // Used to pass the data callback from the |track_| to the other end of
175 // cricket::AudioSource.
176 std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_;
Harald Alvestrandc72af932018-01-11 16:18:19177 int attachment_id_ = 0;
ossu7bb87ee2017-01-23 12:56:25178};
179
180class VideoRtpSender : public ObserverInterface,
181 public rtc::RefCountedObject<RtpSenderInternal> {
182 public:
Steve Anton111fdfd2018-06-25 20:03:36183 // Construct an RtpSender for video with the given sender ID.
184 // The sender is initialized with no track to send and no associated streams.
185 VideoRtpSender(rtc::Thread* worker_thread, const std::string& id);
ossu7bb87ee2017-01-23 12:56:25186
187 virtual ~VideoRtpSender();
188
189 // ObserverInterface implementation
190 void OnChanged() override;
191
192 // RtpSenderInterface implementation
193 bool SetTrack(MediaStreamTrackInterface* track) override;
194 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
195 return track_;
196 }
197
198 uint32_t ssrc() const override { return ssrc_; }
199
200 cricket::MediaType media_type() const override {
201 return cricket::MEDIA_TYPE_VIDEO;
202 }
203
204 std::string id() const override { return id_; }
205
Steve Anton8ffb9c32017-08-31 22:45:38206 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 12:56:25207
Florent Castellicebf50f2018-05-03 13:31:53208 RtpParameters GetParameters() override;
Zach Steinba37b4b2018-01-23 23:02:36209 RTCError SetParameters(const RtpParameters& parameters) override;
ossu7bb87ee2017-01-23 12:56:25210
deadbeef20cb0c12017-02-02 04:27:00211 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
212
ossu7bb87ee2017-01-23 12:56:25213 // RtpSenderInternal implementation.
214 void SetSsrc(uint32_t ssrc) override;
215
Steve Anton8ffb9c32017-08-31 22:45:38216 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
217 stream_ids_ = stream_ids;
218 }
ossu7bb87ee2017-01-23 12:56:25219
220 void Stop() override;
Harald Alvestrandc72af932018-01-11 16:18:19221 int AttachmentId() const override { return attachment_id_; }
ossu7bb87ee2017-01-23 12:56:25222
Steve Anton57858b32018-02-15 23:19:50223 void SetVoiceMediaChannel(
224 cricket::VoiceMediaChannel* voice_media_channel) override {
225 RTC_NOTREACHED();
226 }
227 void SetVideoMediaChannel(
228 cricket::VideoMediaChannel* video_media_channel) override {
229 media_channel_ = video_media_channel;
Steve Anton47136dd2018-01-12 18:49:35230 }
ossu7bb87ee2017-01-23 12:56:25231
232 private:
233 bool can_send_track() const { return track_ && ssrc_; }
234 // Helper function to construct options for
235 // VideoProviderInterface::SetVideoSend.
236 void SetVideoSend();
237 // Helper function to call SetVideoSend with "stop sending" parameters.
238 void ClearVideoSend();
239
Steve Anton47136dd2018-01-12 18:49:35240 rtc::Thread* worker_thread_;
Steve Anton02ee47c2018-01-11 00:26:06241 const std::string id_;
Steve Anton8ffb9c32017-08-31 22:45:38242 std::vector<std::string> stream_ids_;
Steve Anton47136dd2018-01-12 18:49:35243 cricket::VideoMediaChannel* media_channel_ = nullptr;
ossu7bb87ee2017-01-23 12:56:25244 rtc::scoped_refptr<VideoTrackInterface> track_;
Danil Chapovalov66cadcc2018-06-19 14:47:43245 absl::optional<std::string> last_transaction_id_;
ossu7bb87ee2017-01-23 12:56:25246 uint32_t ssrc_ = 0;
ossu7bb87ee2017-01-23 12:56:25247 VideoTrackInterface::ContentHint cached_track_content_hint_ =
248 VideoTrackInterface::ContentHint::kNone;
249 bool stopped_ = false;
Harald Alvestrandc72af932018-01-11 16:18:19250 int attachment_id_ = 0;
ossu7bb87ee2017-01-23 12:56:25251};
252
253} // namespace webrtc
254
Mirko Bonadei92ea95e2017-09-15 04:47:31255#endif // PC_RTPSENDER_H_