blob: 672637f38540bf2b8ceb2900eb4aa7d05fc5eb57 [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>
20
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/mediastreaminterface.h"
22#include "api/rtpsenderinterface.h"
23#include "rtc_base/basictypes.h"
24#include "rtc_base/criticalsection.h"
zhihuang38ede132017-06-15 12:52:32 -070025// Adding 'nogncheck' to disable the gn include headers check to support modular
26// WebRTC build targets.
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "media/base/audiosource.h" // nogncheck
28#include "pc/channel.h"
29#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.
84 // |channel| can be null if one does not exist yet.
85 AudioRtpSender(AudioTrackInterface* track,
Steve Anton8ffb9c32017-08-31 15:45:38 -070086 const std::vector<std::string>& stream_id,
ossu7bb87ee2017-01-23 04:56:25 -080087 cricket::VoiceChannel* channel,
88 StatsCollector* stats);
89
90 // Randomly generates stream_id.
91 // |channel| can be null if one does not exist yet.
92 AudioRtpSender(AudioTrackInterface* track,
93 cricket::VoiceChannel* channel,
94 StatsCollector* stats);
95
96 // Randomly generates id and stream_id.
97 // |channel| can be null if one does not exist yet.
98 AudioRtpSender(cricket::VoiceChannel* channel, StatsCollector* stats);
99
100 virtual ~AudioRtpSender();
101
deadbeef20cb0c12017-02-01 20:27:00 -0800102 // DtmfSenderProvider implementation.
103 bool CanInsertDtmf() override;
104 bool InsertDtmf(int code, int duration) override;
105 sigslot::signal0<>* GetOnDestroyedSignal() override;
106
107 // ObserverInterface implementation.
ossu7bb87ee2017-01-23 04:56:25 -0800108 void OnChanged() override;
109
deadbeef20cb0c12017-02-01 20:27:00 -0800110 // RtpSenderInterface implementation.
ossu7bb87ee2017-01-23 04:56:25 -0800111 bool SetTrack(MediaStreamTrackInterface* track) override;
112 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
113 return track_;
114 }
115
116 uint32_t ssrc() const override { return ssrc_; }
117
118 cricket::MediaType media_type() const override {
119 return cricket::MEDIA_TYPE_AUDIO;
120 }
121
122 std::string id() const override { return id_; }
123
Steve Anton8ffb9c32017-08-31 15:45:38 -0700124 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 04:56:25 -0800125
126 RtpParameters GetParameters() const override;
127 bool SetParameters(const RtpParameters& parameters) override;
128
deadbeef20cb0c12017-02-01 20:27:00 -0800129 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
130
ossu7bb87ee2017-01-23 04:56:25 -0800131 // RtpSenderInternal implementation.
132 void SetSsrc(uint32_t ssrc) override;
133
134 void set_stream_id(const std::string& stream_id) override {
Steve Anton8ffb9c32017-08-31 15:45:38 -0700135 stream_ids_ = {stream_id};
ossu7bb87ee2017-01-23 04:56:25 -0800136 }
Steve Anton8ffb9c32017-08-31 15:45:38 -0700137 std::string stream_id() const override { return stream_ids_[0]; }
138 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
144 // Does not take ownership.
145 // Should call SetChannel(nullptr) before |channel| is destroyed.
146 void SetChannel(cricket::VoiceChannel* channel) { channel_ = channel; }
147
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 void CreateDtmfSender();
159
160 sigslot::signal0<> SignalDestroyed;
161
ossu7bb87ee2017-01-23 04:56:25 -0800162 std::string id_;
Steve Anton8ffb9c32017-08-31 15:45:38 -0700163 // TODO(steveanton): Until more Unified Plan work is done, this can only have
164 // exactly one element.
165 std::vector<std::string> stream_ids_;
ossu7bb87ee2017-01-23 04:56:25 -0800166 cricket::VoiceChannel* channel_ = nullptr;
167 StatsCollector* stats_;
168 rtc::scoped_refptr<AudioTrackInterface> track_;
deadbeef20cb0c12017-02-01 20:27:00 -0800169 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_;
ossu7bb87ee2017-01-23 04:56:25 -0800170 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_;
177};
178
179class VideoRtpSender : public ObserverInterface,
180 public rtc::RefCountedObject<RtpSenderInternal> {
181 public:
182 // |channel| can be null if one does not exist yet.
183 VideoRtpSender(VideoTrackInterface* track,
Steve Anton8ffb9c32017-08-31 15:45:38 -0700184 const std::vector<std::string>& stream_id,
ossu7bb87ee2017-01-23 04:56:25 -0800185 cricket::VideoChannel* channel);
186
187 // Randomly generates stream_id.
188 // |channel| can be null if one does not exist yet.
189 VideoRtpSender(VideoTrackInterface* track, cricket::VideoChannel* channel);
190
191 // Randomly generates id and stream_id.
192 // |channel| can be null if one does not exist yet.
193 explicit VideoRtpSender(cricket::VideoChannel* channel);
194
195 virtual ~VideoRtpSender();
196
197 // ObserverInterface implementation
198 void OnChanged() override;
199
200 // RtpSenderInterface implementation
201 bool SetTrack(MediaStreamTrackInterface* track) override;
202 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
203 return track_;
204 }
205
206 uint32_t ssrc() const override { return ssrc_; }
207
208 cricket::MediaType media_type() const override {
209 return cricket::MEDIA_TYPE_VIDEO;
210 }
211
212 std::string id() const override { return id_; }
213
Steve Anton8ffb9c32017-08-31 15:45:38 -0700214 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 04:56:25 -0800215
216 RtpParameters GetParameters() const override;
217 bool SetParameters(const RtpParameters& parameters) override;
218
deadbeef20cb0c12017-02-01 20:27:00 -0800219 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
220
ossu7bb87ee2017-01-23 04:56:25 -0800221 // RtpSenderInternal implementation.
222 void SetSsrc(uint32_t ssrc) override;
223
224 void set_stream_id(const std::string& stream_id) override {
Steve Anton8ffb9c32017-08-31 15:45:38 -0700225 stream_ids_ = {stream_id};
ossu7bb87ee2017-01-23 04:56:25 -0800226 }
Steve Anton8ffb9c32017-08-31 15:45:38 -0700227 std::string stream_id() const override { return stream_ids_[0]; }
228 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
229 stream_ids_ = stream_ids;
230 }
ossu7bb87ee2017-01-23 04:56:25 -0800231
232 void Stop() override;
233
234 // Does not take ownership.
235 // Should call SetChannel(nullptr) before |channel| is destroyed.
236 void SetChannel(cricket::VideoChannel* channel) { channel_ = channel; }
237
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
246 std::string id_;
Steve Anton8ffb9c32017-08-31 15:45:38 -0700247 // TODO(steveanton): Until more Unified Plan work is done, this can only have
248 // exactly one element.
249 std::vector<std::string> stream_ids_;
ossu7bb87ee2017-01-23 04:56:25 -0800250 cricket::VideoChannel* channel_ = nullptr;
251 rtc::scoped_refptr<VideoTrackInterface> track_;
252 uint32_t ssrc_ = 0;
253 bool cached_track_enabled_ = false;
254 VideoTrackInterface::ContentHint cached_track_content_hint_ =
255 VideoTrackInterface::ContentHint::kNone;
256 bool stopped_ = false;
257};
258
259} // namespace webrtc
260
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200261#endif // PC_RTPSENDER_H_