blob: a0c759ad1cb9cb2710232f2c64f2a477a26a8428 [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"
24#include "rtc_base/basictypes.h"
25#include "rtc_base/criticalsection.h"
zhihuang38ede132017-06-15 19:52:3226// Adding 'nogncheck' to disable the gn include headers check to support modular
27// WebRTC build targets.
Mirko Bonadei92ea95e2017-09-15 04:47:3128#include "media/base/audiosource.h" // nogncheck
29#include "pc/channel.h"
30#include "pc/dtmfsender.h"
31#include "pc/statscollector.h"
ossu7bb87ee2017-01-23 12:56:2532
33namespace webrtc {
34
35// Internal interface used by PeerConnection.
36class RtpSenderInternal : public RtpSenderInterface {
37 public:
38 // Used to set the SSRC of the sender, once a local description has been set.
39 // If |ssrc| is 0, this indiates that the sender should disconnect from the
40 // underlying transport (this occurs if the sender isn't seen in a local
41 // description).
42 virtual void SetSsrc(uint32_t ssrc) = 0;
43
Steve Anton8ffb9c32017-08-31 22:45:3844 // TODO(steveanton): With Unified Plan, a track/RTCRTPSender can be part of
45 // multiple streams (or no stream at all). Replace these singular methods with
46 // their corresponding plural methods.
47 // Until these are removed, RtpSenders must have exactly one stream.
ossu7bb87ee2017-01-23 12:56:2548 virtual void set_stream_id(const std::string& stream_id) = 0;
49 virtual std::string stream_id() const = 0;
Steve Anton8ffb9c32017-08-31 22:45:3850 virtual void set_stream_ids(const std::vector<std::string>& stream_ids) = 0;
ossu7bb87ee2017-01-23 12:56:2551
52 virtual void Stop() = 0;
53};
54
55// LocalAudioSinkAdapter receives data callback as a sink to the local
56// AudioTrack, and passes the data to the sink of AudioSource.
57class LocalAudioSinkAdapter : public AudioTrackSinkInterface,
58 public cricket::AudioSource {
59 public:
60 LocalAudioSinkAdapter();
61 virtual ~LocalAudioSinkAdapter();
62
63 private:
64 // AudioSinkInterface implementation.
65 void OnData(const void* audio_data,
66 int bits_per_sample,
67 int sample_rate,
68 size_t number_of_channels,
69 size_t number_of_frames) override;
70
71 // cricket::AudioSource implementation.
72 void SetSink(cricket::AudioSource::Sink* sink) override;
73
74 cricket::AudioSource::Sink* sink_;
75 // Critical section protecting |sink_|.
76 rtc::CriticalSection lock_;
77};
78
deadbeef20cb0c12017-02-02 04:27:0079class AudioRtpSender : public DtmfProviderInterface,
80 public ObserverInterface,
ossu7bb87ee2017-01-23 12:56:2581 public rtc::RefCountedObject<RtpSenderInternal> {
82 public:
83 // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
84 // at the appropriate times.
ossu7bb87ee2017-01-23 12:56:2585
Steve Anton02ee47c2018-01-11 00:26:0686 // Construct an AudioRtpSender with a null track, a single, randomly generated
87 // stream label, and a randomly generated ID.
88 explicit AudioRtpSender(StatsCollector* stats);
ossu7bb87ee2017-01-23 12:56:2589
Steve Anton02ee47c2018-01-11 00:26:0690 // Construct an AudioRtpSender with the given track and stream labels. The
91 // sender ID will be set to the track's ID.
92 AudioRtpSender(rtc::scoped_refptr<AudioTrackInterface> track,
93 const std::vector<std::string>& stream_labels,
94 StatsCollector* stats);
ossu7bb87ee2017-01-23 12:56:2595
96 virtual ~AudioRtpSender();
97
deadbeef20cb0c12017-02-02 04:27:0098 // 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 12:56:25104 void OnChanged() override;
105
deadbeef20cb0c12017-02-02 04:27:00106 // RtpSenderInterface implementation.
ossu7bb87ee2017-01-23 12:56:25107 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 22:45:38120 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 12:56:25121
122 RtpParameters GetParameters() const override;
123 bool SetParameters(const RtpParameters& parameters) override;
124
deadbeef20cb0c12017-02-02 04:27:00125 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
126
ossu7bb87ee2017-01-23 12:56:25127 // 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 22:45:38131 stream_ids_ = {stream_id};
ossu7bb87ee2017-01-23 12:56:25132 }
Steve Anton8ffb9c32017-08-31 22:45:38133 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 12:56:25137
138 void Stop() override;
139
140 // Does not take ownership.
141 // Should call SetChannel(nullptr) before |channel| is destroyed.
142 void SetChannel(cricket::VoiceChannel* channel) { channel_ = channel; }
143
144 private:
145 // TODO(nisse): Since SSRC == 0 is technically valid, figure out
146 // some other way to test if we have a valid SSRC.
147 bool can_send_track() const { return track_ && ssrc_; }
148 // Helper function to construct options for
149 // AudioProviderInterface::SetAudioSend.
150 void SetAudioSend();
151 // Helper function to call SetAudioSend with "stop sending" parameters.
152 void ClearAudioSend();
153
deadbeef20cb0c12017-02-02 04:27:00154 sigslot::signal0<> SignalDestroyed;
155
Steve Anton02ee47c2018-01-11 00:26:06156 const std::string id_;
Steve Anton8ffb9c32017-08-31 22:45:38157 // TODO(steveanton): Until more Unified Plan work is done, this can only have
158 // exactly one element.
159 std::vector<std::string> stream_ids_;
ossu7bb87ee2017-01-23 12:56:25160 cricket::VoiceChannel* channel_ = nullptr;
161 StatsCollector* stats_;
162 rtc::scoped_refptr<AudioTrackInterface> track_;
deadbeef20cb0c12017-02-02 04:27:00163 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_;
ossu7bb87ee2017-01-23 12:56:25164 uint32_t ssrc_ = 0;
165 bool cached_track_enabled_ = false;
166 bool stopped_ = false;
167
168 // Used to pass the data callback from the |track_| to the other end of
169 // cricket::AudioSource.
170 std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_;
171};
172
173class VideoRtpSender : public ObserverInterface,
174 public rtc::RefCountedObject<RtpSenderInternal> {
175 public:
Steve Anton02ee47c2018-01-11 00:26:06176 // Construct a VideoRtpSender with a null track, a single, randomly generated
177 // stream label, and a randomly generated ID.
178 VideoRtpSender();
ossu7bb87ee2017-01-23 12:56:25179
Steve Anton02ee47c2018-01-11 00:26:06180 // Construct a VideoRtpSender with the given track and stream labels. The
181 // sender ID will be set to the track's ID.
Steve Antonf9381f02017-12-14 18:23:57182 VideoRtpSender(rtc::scoped_refptr<VideoTrackInterface> track,
Steve Anton02ee47c2018-01-11 00:26:06183 const std::vector<std::string>& stream_labels);
ossu7bb87ee2017-01-23 12:56:25184
185 virtual ~VideoRtpSender();
186
187 // ObserverInterface implementation
188 void OnChanged() override;
189
190 // RtpSenderInterface implementation
191 bool SetTrack(MediaStreamTrackInterface* track) override;
192 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
193 return track_;
194 }
195
196 uint32_t ssrc() const override { return ssrc_; }
197
198 cricket::MediaType media_type() const override {
199 return cricket::MEDIA_TYPE_VIDEO;
200 }
201
202 std::string id() const override { return id_; }
203
Steve Anton8ffb9c32017-08-31 22:45:38204 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 12:56:25205
206 RtpParameters GetParameters() const override;
207 bool SetParameters(const RtpParameters& parameters) override;
208
deadbeef20cb0c12017-02-02 04:27:00209 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
210
ossu7bb87ee2017-01-23 12:56:25211 // RtpSenderInternal implementation.
212 void SetSsrc(uint32_t ssrc) override;
213
214 void set_stream_id(const std::string& stream_id) override {
Steve Anton8ffb9c32017-08-31 22:45:38215 stream_ids_ = {stream_id};
ossu7bb87ee2017-01-23 12:56:25216 }
Steve Anton8ffb9c32017-08-31 22:45:38217 std::string stream_id() const override { return stream_ids_[0]; }
218 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
219 stream_ids_ = stream_ids;
220 }
ossu7bb87ee2017-01-23 12:56:25221
222 void Stop() override;
223
224 // Does not take ownership.
225 // Should call SetChannel(nullptr) before |channel| is destroyed.
226 void SetChannel(cricket::VideoChannel* channel) { channel_ = channel; }
227
228 private:
229 bool can_send_track() const { return track_ && ssrc_; }
230 // Helper function to construct options for
231 // VideoProviderInterface::SetVideoSend.
232 void SetVideoSend();
233 // Helper function to call SetVideoSend with "stop sending" parameters.
234 void ClearVideoSend();
235
Steve Anton02ee47c2018-01-11 00:26:06236 const std::string id_;
Steve Anton8ffb9c32017-08-31 22:45:38237 // TODO(steveanton): Until more Unified Plan work is done, this can only have
238 // exactly one element.
239 std::vector<std::string> stream_ids_;
ossu7bb87ee2017-01-23 12:56:25240 cricket::VideoChannel* channel_ = nullptr;
241 rtc::scoped_refptr<VideoTrackInterface> track_;
242 uint32_t ssrc_ = 0;
243 bool cached_track_enabled_ = false;
244 VideoTrackInterface::ContentHint cached_track_content_hint_ =
245 VideoTrackInterface::ContentHint::kNone;
246 bool stopped_ = false;
247};
248
249} // namespace webrtc
250
Mirko Bonadei92ea95e2017-09-15 04:47:31251#endif // PC_RTPSENDER_H_