blob: ce8c65719962b658b5f9e7ef6bec3448fba59198 [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"
zhihuang38ede132017-06-15 12:52:32 -070026// Adding 'nogncheck' to disable the gn include headers check to support modular
27// WebRTC build targets.
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "media/base/audiosource.h" // nogncheck
29#include "pc/channel.h"
30#include "pc/dtmfsender.h"
31#include "pc/statscollector.h"
ossu7bb87ee2017-01-23 04:56:25 -080032
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 15:45:38 -070044 // 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 04:56:25 -080048 virtual void set_stream_id(const std::string& stream_id) = 0;
49 virtual std::string stream_id() const = 0;
Steve Anton8ffb9c32017-08-31 15:45:38 -070050 virtual void set_stream_ids(const std::vector<std::string>& stream_ids) = 0;
ossu7bb87ee2017-01-23 04:56:25 -080051
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-01 20:27:00 -080079class AudioRtpSender : public DtmfProviderInterface,
80 public ObserverInterface,
ossu7bb87ee2017-01-23 04:56:25 -080081 public rtc::RefCountedObject<RtpSenderInternal> {
82 public:
83 // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
84 // at the appropriate times.
85 // |channel| can be null if one does not exist yet.
86 AudioRtpSender(AudioTrackInterface* track,
Steve Anton8ffb9c32017-08-31 15:45:38 -070087 const std::vector<std::string>& stream_id,
ossu7bb87ee2017-01-23 04:56:25 -080088 cricket::VoiceChannel* channel,
89 StatsCollector* stats);
90
91 // Randomly generates stream_id.
92 // |channel| can be null if one does not exist yet.
93 AudioRtpSender(AudioTrackInterface* track,
94 cricket::VoiceChannel* channel,
95 StatsCollector* stats);
96
97 // Randomly generates id and stream_id.
98 // |channel| can be null if one does not exist yet.
99 AudioRtpSender(cricket::VoiceChannel* channel, StatsCollector* stats);
100
101 virtual ~AudioRtpSender();
102
deadbeef20cb0c12017-02-01 20:27:00 -0800103 // DtmfSenderProvider implementation.
104 bool CanInsertDtmf() override;
105 bool InsertDtmf(int code, int duration) override;
106 sigslot::signal0<>* GetOnDestroyedSignal() override;
107
108 // ObserverInterface implementation.
ossu7bb87ee2017-01-23 04:56:25 -0800109 void OnChanged() override;
110
deadbeef20cb0c12017-02-01 20:27:00 -0800111 // RtpSenderInterface implementation.
ossu7bb87ee2017-01-23 04:56:25 -0800112 bool SetTrack(MediaStreamTrackInterface* track) override;
113 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
114 return track_;
115 }
116
117 uint32_t ssrc() const override { return ssrc_; }
118
119 cricket::MediaType media_type() const override {
120 return cricket::MEDIA_TYPE_AUDIO;
121 }
122
123 std::string id() const override { return id_; }
124
Steve Anton8ffb9c32017-08-31 15:45:38 -0700125 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 04:56:25 -0800126
127 RtpParameters GetParameters() const override;
128 bool SetParameters(const RtpParameters& parameters) override;
129
deadbeef20cb0c12017-02-01 20:27:00 -0800130 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
131
ossu7bb87ee2017-01-23 04:56:25 -0800132 // RtpSenderInternal implementation.
133 void SetSsrc(uint32_t ssrc) override;
134
135 void set_stream_id(const std::string& stream_id) override {
Steve Anton8ffb9c32017-08-31 15:45:38 -0700136 stream_ids_ = {stream_id};
ossu7bb87ee2017-01-23 04:56:25 -0800137 }
Steve Anton8ffb9c32017-08-31 15:45:38 -0700138 std::string stream_id() const override { return stream_ids_[0]; }
139 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
140 stream_ids_ = stream_ids;
141 }
ossu7bb87ee2017-01-23 04:56:25 -0800142
143 void Stop() override;
144
145 // Does not take ownership.
146 // Should call SetChannel(nullptr) before |channel| is destroyed.
147 void SetChannel(cricket::VoiceChannel* channel) { channel_ = channel; }
148
149 private:
150 // TODO(nisse): Since SSRC == 0 is technically valid, figure out
151 // some other way to test if we have a valid SSRC.
152 bool can_send_track() const { return track_ && ssrc_; }
153 // Helper function to construct options for
154 // AudioProviderInterface::SetAudioSend.
155 void SetAudioSend();
156 // Helper function to call SetAudioSend with "stop sending" parameters.
157 void ClearAudioSend();
158
deadbeef20cb0c12017-02-01 20:27:00 -0800159 void CreateDtmfSender();
160
161 sigslot::signal0<> SignalDestroyed;
162
ossu7bb87ee2017-01-23 04:56:25 -0800163 std::string id_;
Steve Anton8ffb9c32017-08-31 15:45:38 -0700164 // TODO(steveanton): Until more Unified Plan work is done, this can only have
165 // exactly one element.
166 std::vector<std::string> stream_ids_;
ossu7bb87ee2017-01-23 04:56:25 -0800167 cricket::VoiceChannel* channel_ = nullptr;
168 StatsCollector* stats_;
169 rtc::scoped_refptr<AudioTrackInterface> track_;
deadbeef20cb0c12017-02-01 20:27:00 -0800170 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_;
ossu7bb87ee2017-01-23 04:56:25 -0800171 uint32_t ssrc_ = 0;
172 bool cached_track_enabled_ = false;
173 bool stopped_ = false;
174
175 // Used to pass the data callback from the |track_| to the other end of
176 // cricket::AudioSource.
177 std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_;
178};
179
180class VideoRtpSender : public ObserverInterface,
181 public rtc::RefCountedObject<RtpSenderInternal> {
182 public:
183 // |channel| can be null if one does not exist yet.
184 VideoRtpSender(VideoTrackInterface* track,
Steve Anton8ffb9c32017-08-31 15:45:38 -0700185 const std::vector<std::string>& stream_id,
ossu7bb87ee2017-01-23 04:56:25 -0800186 cricket::VideoChannel* channel);
187
188 // Randomly generates stream_id.
189 // |channel| can be null if one does not exist yet.
190 VideoRtpSender(VideoTrackInterface* track, cricket::VideoChannel* channel);
191
192 // Randomly generates id and stream_id.
193 // |channel| can be null if one does not exist yet.
194 explicit VideoRtpSender(cricket::VideoChannel* channel);
195
196 virtual ~VideoRtpSender();
197
198 // ObserverInterface implementation
199 void OnChanged() override;
200
201 // RtpSenderInterface implementation
202 bool SetTrack(MediaStreamTrackInterface* track) override;
203 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
204 return track_;
205 }
206
207 uint32_t ssrc() const override { return ssrc_; }
208
209 cricket::MediaType media_type() const override {
210 return cricket::MEDIA_TYPE_VIDEO;
211 }
212
213 std::string id() const override { return id_; }
214
Steve Anton8ffb9c32017-08-31 15:45:38 -0700215 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 04:56:25 -0800216
217 RtpParameters GetParameters() const override;
218 bool SetParameters(const RtpParameters& parameters) override;
219
deadbeef20cb0c12017-02-01 20:27:00 -0800220 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
221
ossu7bb87ee2017-01-23 04:56:25 -0800222 // RtpSenderInternal implementation.
223 void SetSsrc(uint32_t ssrc) override;
224
225 void set_stream_id(const std::string& stream_id) override {
Steve Anton8ffb9c32017-08-31 15:45:38 -0700226 stream_ids_ = {stream_id};
ossu7bb87ee2017-01-23 04:56:25 -0800227 }
Steve Anton8ffb9c32017-08-31 15:45:38 -0700228 std::string stream_id() const override { return stream_ids_[0]; }
229 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
230 stream_ids_ = stream_ids;
231 }
ossu7bb87ee2017-01-23 04:56:25 -0800232
233 void Stop() override;
234
235 // Does not take ownership.
236 // Should call SetChannel(nullptr) before |channel| is destroyed.
237 void SetChannel(cricket::VideoChannel* channel) { channel_ = channel; }
238
239 private:
240 bool can_send_track() const { return track_ && ssrc_; }
241 // Helper function to construct options for
242 // VideoProviderInterface::SetVideoSend.
243 void SetVideoSend();
244 // Helper function to call SetVideoSend with "stop sending" parameters.
245 void ClearVideoSend();
246
247 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_;
ossu7bb87ee2017-01-23 04:56:25 -0800251 cricket::VideoChannel* channel_ = nullptr;
252 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;
258};
259
260} // namespace webrtc
261
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200262#endif // PC_RTPSENDER_H_