blob: 119d91f11a81412a8cc2bedfc81b613a427d636c [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"
24#include "rtc_base/basictypes.h"
25#include "rtc_base/criticalsection.h"
Patrik Höglundaaaf1cf2018-01-12 12:48:07 +000026// Adding 'nogncheck' to disable the gn include headers check to support modular
27// WebRTC build targets.
28#include "media/base/audiosource.h" // nogncheck
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "pc/dtmfsender.h"
30#include "pc/statscollector.h"
ossu7bb87ee2017-01-23 04:56:25 -080031
32namespace webrtc {
33
34// Internal interface used by PeerConnection.
35class RtpSenderInternal : public RtpSenderInterface {
36 public:
37 // Used to set the SSRC of the sender, once a local description has been set.
38 // If |ssrc| is 0, this indiates that the sender should disconnect from the
39 // underlying transport (this occurs if the sender isn't seen in a local
40 // description).
41 virtual void SetSsrc(uint32_t ssrc) = 0;
42
Steve Anton8ffb9c32017-08-31 15:45:38 -070043 // TODO(steveanton): With Unified Plan, a track/RTCRTPSender can be part of
44 // multiple streams (or no stream at all). Replace these singular methods with
45 // their corresponding plural methods.
46 // Until these are removed, RtpSenders must have exactly one stream.
ossu7bb87ee2017-01-23 04:56:25 -080047 virtual void set_stream_id(const std::string& stream_id) = 0;
48 virtual std::string stream_id() const = 0;
Steve Anton8ffb9c32017-08-31 15:45:38 -070049 virtual void set_stream_ids(const std::vector<std::string>& stream_ids) = 0;
ossu7bb87ee2017-01-23 04:56:25 -080050
51 virtual void Stop() = 0;
52};
53
54// LocalAudioSinkAdapter receives data callback as a sink to the local
55// AudioTrack, and passes the data to the sink of AudioSource.
56class LocalAudioSinkAdapter : public AudioTrackSinkInterface,
57 public cricket::AudioSource {
58 public:
59 LocalAudioSinkAdapter();
60 virtual ~LocalAudioSinkAdapter();
61
62 private:
63 // AudioSinkInterface implementation.
64 void OnData(const void* audio_data,
65 int bits_per_sample,
66 int sample_rate,
67 size_t number_of_channels,
68 size_t number_of_frames) override;
69
70 // cricket::AudioSource implementation.
71 void SetSink(cricket::AudioSource::Sink* sink) override;
72
73 cricket::AudioSource::Sink* sink_;
74 // Critical section protecting |sink_|.
75 rtc::CriticalSection lock_;
76};
77
deadbeef20cb0c12017-02-01 20:27:00 -080078class AudioRtpSender : public DtmfProviderInterface,
79 public ObserverInterface,
ossu7bb87ee2017-01-23 04:56:25 -080080 public rtc::RefCountedObject<RtpSenderInternal> {
81 public:
82 // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
83 // at the appropriate times.
ossu7bb87ee2017-01-23 04:56:25 -080084
Steve Anton02ee47c2018-01-10 16:26:06 -080085 // Construct an AudioRtpSender with a null track, a single, randomly generated
86 // stream label, and a randomly generated ID.
Steve Anton47136dd2018-01-12 10:49:35 -080087 AudioRtpSender(rtc::Thread* worker_thread, StatsCollector* stats);
ossu7bb87ee2017-01-23 04:56:25 -080088
Steve Anton02ee47c2018-01-10 16:26:06 -080089 // Construct an AudioRtpSender with the given track and stream labels. The
90 // sender ID will be set to the track's ID.
Steve Anton47136dd2018-01-12 10:49:35 -080091 AudioRtpSender(rtc::Thread* worker_thread,
92 rtc::scoped_refptr<AudioTrackInterface> track,
Steve Anton02ee47c2018-01-10 16:26:06 -080093 const std::vector<std::string>& stream_labels,
94 StatsCollector* stats);
ossu7bb87ee2017-01-23 04:56:25 -080095
96 virtual ~AudioRtpSender();
97
deadbeef20cb0c12017-02-01 20:27:00 -080098 // DtmfSenderProvider implementation.
99 bool CanInsertDtmf() override;
100 bool InsertDtmf(int code, int duration) override;
101 sigslot::signal0<>* GetOnDestroyedSignal() override;
102
103 // ObserverInterface implementation.
ossu7bb87ee2017-01-23 04:56:25 -0800104 void OnChanged() override;
105
deadbeef20cb0c12017-02-01 20:27:00 -0800106 // RtpSenderInterface implementation.
ossu7bb87ee2017-01-23 04:56:25 -0800107 bool SetTrack(MediaStreamTrackInterface* track) override;
108 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
109 return track_;
110 }
111
112 uint32_t ssrc() const override { return ssrc_; }
113
114 cricket::MediaType media_type() const override {
115 return cricket::MEDIA_TYPE_AUDIO;
116 }
117
118 std::string id() const override { return id_; }
119
Steve Anton8ffb9c32017-08-31 15:45:38 -0700120 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 04:56:25 -0800121
122 RtpParameters GetParameters() const override;
123 bool SetParameters(const RtpParameters& parameters) override;
124
deadbeef20cb0c12017-02-01 20:27:00 -0800125 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
126
ossu7bb87ee2017-01-23 04:56:25 -0800127 // RtpSenderInternal implementation.
128 void SetSsrc(uint32_t ssrc) override;
129
130 void set_stream_id(const std::string& stream_id) override {
Steve Anton8ffb9c32017-08-31 15:45:38 -0700131 stream_ids_ = {stream_id};
ossu7bb87ee2017-01-23 04:56:25 -0800132 }
Steve Anton8ffb9c32017-08-31 15:45:38 -0700133 std::string stream_id() const override { return stream_ids_[0]; }
134 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
135 stream_ids_ = stream_ids;
136 }
ossu7bb87ee2017-01-23 04:56:25 -0800137
138 void Stop() override;
139
Harald Alvestrandc72af932018-01-11 17:18:19 +0100140 int AttachmentId() const override { return attachment_id_; }
141
ossu7bb87ee2017-01-23 04:56:25 -0800142 // Does not take ownership.
Steve Anton47136dd2018-01-12 10:49:35 -0800143 // Should call SetMediaChannel(nullptr) before |media_channel| is destroyed.
144 void SetMediaChannel(cricket::VoiceMediaChannel* media_channel) {
145 media_channel_ = media_channel;
146 }
ossu7bb87ee2017-01-23 04:56:25 -0800147
148 private:
149 // TODO(nisse): Since SSRC == 0 is technically valid, figure out
150 // some other way to test if we have a valid SSRC.
151 bool can_send_track() const { return track_ && ssrc_; }
152 // Helper function to construct options for
153 // AudioProviderInterface::SetAudioSend.
154 void SetAudioSend();
155 // Helper function to call SetAudioSend with "stop sending" parameters.
156 void ClearAudioSend();
157
deadbeef20cb0c12017-02-01 20:27:00 -0800158 sigslot::signal0<> SignalDestroyed;
159
Steve Anton47136dd2018-01-12 10:49:35 -0800160 rtc::Thread* const worker_thread_;
Steve Anton02ee47c2018-01-10 16:26:06 -0800161 const std::string id_;
Steve Anton8ffb9c32017-08-31 15:45:38 -0700162 // TODO(steveanton): Until more Unified Plan work is done, this can only have
163 // exactly one element.
164 std::vector<std::string> stream_ids_;
Steve Anton47136dd2018-01-12 10:49:35 -0800165 cricket::VoiceMediaChannel* media_channel_ = nullptr;
ossu7bb87ee2017-01-23 04:56:25 -0800166 StatsCollector* stats_;
167 rtc::scoped_refptr<AudioTrackInterface> track_;
deadbeef20cb0c12017-02-01 20:27:00 -0800168 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_;
ossu7bb87ee2017-01-23 04:56:25 -0800169 uint32_t ssrc_ = 0;
170 bool cached_track_enabled_ = false;
171 bool stopped_ = false;
172
173 // Used to pass the data callback from the |track_| to the other end of
174 // cricket::AudioSource.
175 std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100176 int attachment_id_ = 0;
ossu7bb87ee2017-01-23 04:56:25 -0800177};
178
179class VideoRtpSender : public ObserverInterface,
180 public rtc::RefCountedObject<RtpSenderInternal> {
181 public:
Steve Anton02ee47c2018-01-10 16:26:06 -0800182 // Construct a VideoRtpSender with a null track, a single, randomly generated
183 // stream label, and a randomly generated ID.
Steve Anton47136dd2018-01-12 10:49:35 -0800184 explicit VideoRtpSender(rtc::Thread* worker_thread);
ossu7bb87ee2017-01-23 04:56:25 -0800185
Steve Anton02ee47c2018-01-10 16:26:06 -0800186 // Construct a VideoRtpSender with the given track and stream labels. The
187 // sender ID will be set to the track's ID.
Steve Anton47136dd2018-01-12 10:49:35 -0800188 VideoRtpSender(rtc::Thread* worker_thread,
189 rtc::scoped_refptr<VideoTrackInterface> track,
Steve Anton02ee47c2018-01-10 16:26:06 -0800190 const std::vector<std::string>& stream_labels);
ossu7bb87ee2017-01-23 04:56:25 -0800191
192 virtual ~VideoRtpSender();
193
194 // ObserverInterface implementation
195 void OnChanged() override;
196
197 // RtpSenderInterface implementation
198 bool SetTrack(MediaStreamTrackInterface* track) override;
199 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
200 return track_;
201 }
202
203 uint32_t ssrc() const override { return ssrc_; }
204
205 cricket::MediaType media_type() const override {
206 return cricket::MEDIA_TYPE_VIDEO;
207 }
208
209 std::string id() const override { return id_; }
210
Steve Anton8ffb9c32017-08-31 15:45:38 -0700211 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 04:56:25 -0800212
213 RtpParameters GetParameters() const override;
214 bool SetParameters(const RtpParameters& parameters) override;
215
deadbeef20cb0c12017-02-01 20:27:00 -0800216 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
217
ossu7bb87ee2017-01-23 04:56:25 -0800218 // RtpSenderInternal implementation.
219 void SetSsrc(uint32_t ssrc) override;
220
221 void set_stream_id(const std::string& stream_id) override {
Steve Anton8ffb9c32017-08-31 15:45:38 -0700222 stream_ids_ = {stream_id};
ossu7bb87ee2017-01-23 04:56:25 -0800223 }
Steve Anton8ffb9c32017-08-31 15:45:38 -0700224 std::string stream_id() const override { return stream_ids_[0]; }
225 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
226 stream_ids_ = stream_ids;
227 }
ossu7bb87ee2017-01-23 04:56:25 -0800228
229 void Stop() override;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100230 int AttachmentId() const override { return attachment_id_; }
ossu7bb87ee2017-01-23 04:56:25 -0800231
232 // Does not take ownership.
Steve Anton47136dd2018-01-12 10:49:35 -0800233 // Should call SetMediaChannel(nullptr) before |media_channel| is destroyed.
234 void SetMediaChannel(cricket::VideoMediaChannel* media_channel) {
235 media_channel_ = media_channel;
236 }
ossu7bb87ee2017-01-23 04:56:25 -0800237
238 private:
239 bool can_send_track() const { return track_ && ssrc_; }
240 // Helper function to construct options for
241 // VideoProviderInterface::SetVideoSend.
242 void SetVideoSend();
243 // Helper function to call SetVideoSend with "stop sending" parameters.
244 void ClearVideoSend();
245
Steve Anton47136dd2018-01-12 10:49:35 -0800246 rtc::Thread* worker_thread_;
Steve Anton02ee47c2018-01-10 16:26:06 -0800247 const std::string id_;
Steve Anton8ffb9c32017-08-31 15:45:38 -0700248 // TODO(steveanton): Until more Unified Plan work is done, this can only have
249 // exactly one element.
250 std::vector<std::string> stream_ids_;
Steve Anton47136dd2018-01-12 10:49:35 -0800251 cricket::VideoMediaChannel* media_channel_ = nullptr;
ossu7bb87ee2017-01-23 04:56:25 -0800252 rtc::scoped_refptr<VideoTrackInterface> track_;
253 uint32_t ssrc_ = 0;
254 bool cached_track_enabled_ = false;
255 VideoTrackInterface::ContentHint cached_track_content_hint_ =
256 VideoTrackInterface::ContentHint::kNone;
257 bool stopped_ = false;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100258 int attachment_id_ = 0;
ossu7bb87ee2017-01-23 04:56:25 -0800259};
260
261} // namespace webrtc
262
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200263#endif // PC_RTPSENDER_H_