blob: a0bcc9407abf10bba024cf625f880a6bf8ee5885 [file] [log] [blame]
deadbeef6979b022015-09-24 16:47:53 -07001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
deadbeef6979b022015-09-24 16:47:53 -07003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
deadbeef6979b022015-09-24 16:47:53 -07009 */
10
deadbeef70ab1a12015-09-28 16:53:55 -070011// This file contains classes that implement RtpSenderInterface.
12// An RtpSender associates a MediaStreamTrackInterface with an underlying
13// transport (provided by AudioProviderInterface/VideoProviderInterface)
14
Henrik Kjellander15583c12016-02-10 10:53:12 +010015#ifndef WEBRTC_API_RTPSENDER_H_
16#define WEBRTC_API_RTPSENDER_H_
deadbeef70ab1a12015-09-28 16:53:55 -070017
kwibergd1fe2812016-04-27 06:47:29 -070018#include <memory>
deadbeef70ab1a12015-09-28 16:53:55 -070019#include <string>
20
tkchin3784b4a2016-06-24 19:31:47 -070021#include "webrtc/api/mediastreamprovider.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010022#include "webrtc/api/rtpsenderinterface.h"
23#include "webrtc/api/statscollector.h"
deadbeef70ab1a12015-09-28 16:53:55 -070024#include "webrtc/base/basictypes.h"
25#include "webrtc/base/criticalsection.h"
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080026#include "webrtc/media/base/audiosource.h"
deadbeef70ab1a12015-09-28 16:53:55 -070027
28namespace webrtc {
29
deadbeefa601f5c2016-06-06 14:27:39 -070030// Internal interface used by PeerConnection.
31class RtpSenderInternal : public RtpSenderInterface {
32 public:
33 // Used to set the SSRC of the sender, once a local description has been set.
34 // If |ssrc| is 0, this indiates that the sender should disconnect from the
35 // underlying transport (this occurs if the sender isn't seen in a local
36 // description).
37 virtual void SetSsrc(uint32_t ssrc) = 0;
38
39 // TODO(deadbeef): Support one sender having multiple stream ids.
40 virtual void set_stream_id(const std::string& stream_id) = 0;
41 virtual std::string stream_id() const = 0;
42
43 virtual void Stop() = 0;
44};
45
deadbeef70ab1a12015-09-28 16:53:55 -070046// LocalAudioSinkAdapter receives data callback as a sink to the local
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080047// AudioTrack, and passes the data to the sink of AudioSource.
deadbeef70ab1a12015-09-28 16:53:55 -070048class LocalAudioSinkAdapter : public AudioTrackSinkInterface,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080049 public cricket::AudioSource {
deadbeef70ab1a12015-09-28 16:53:55 -070050 public:
51 LocalAudioSinkAdapter();
52 virtual ~LocalAudioSinkAdapter();
53
54 private:
55 // AudioSinkInterface implementation.
56 void OnData(const void* audio_data,
57 int bits_per_sample,
58 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -080059 size_t number_of_channels,
deadbeef70ab1a12015-09-28 16:53:55 -070060 size_t number_of_frames) override;
61
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080062 // cricket::AudioSource implementation.
63 void SetSink(cricket::AudioSource::Sink* sink) override;
deadbeef70ab1a12015-09-28 16:53:55 -070064
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080065 cricket::AudioSource::Sink* sink_;
deadbeef70ab1a12015-09-28 16:53:55 -070066 // Critical section protecting |sink_|.
67 rtc::CriticalSection lock_;
68};
69
70class AudioRtpSender : public ObserverInterface,
deadbeefa601f5c2016-06-06 14:27:39 -070071 public rtc::RefCountedObject<RtpSenderInternal> {
deadbeef70ab1a12015-09-28 16:53:55 -070072 public:
deadbeeffac06552015-11-25 11:26:01 -080073 // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
74 // at the appropriate times.
deadbeef70ab1a12015-09-28 16:53:55 -070075 AudioRtpSender(AudioTrackInterface* track,
deadbeeffac06552015-11-25 11:26:01 -080076 const std::string& stream_id,
tkchin3784b4a2016-06-24 19:31:47 -070077 AudioProviderInterface* provider,
deadbeeffac06552015-11-25 11:26:01 -080078 StatsCollector* stats);
79
deadbeefe1f9d832016-01-14 15:35:42 -080080 // Randomly generates stream_id.
81 AudioRtpSender(AudioTrackInterface* track,
tkchin3784b4a2016-06-24 19:31:47 -070082 AudioProviderInterface* provider,
deadbeefe1f9d832016-01-14 15:35:42 -080083 StatsCollector* stats);
84
deadbeeffac06552015-11-25 11:26:01 -080085 // Randomly generates id and stream_id.
tkchin3784b4a2016-06-24 19:31:47 -070086 AudioRtpSender(AudioProviderInterface* provider, StatsCollector* stats);
deadbeef70ab1a12015-09-28 16:53:55 -070087
88 virtual ~AudioRtpSender();
89
90 // ObserverInterface implementation
91 void OnChanged() override;
92
93 // RtpSenderInterface implementation
94 bool SetTrack(MediaStreamTrackInterface* track) override;
95 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
deadbeefa601f5c2016-06-06 14:27:39 -070096 return track_;
deadbeef70ab1a12015-09-28 16:53:55 -070097 }
98
deadbeeffac06552015-11-25 11:26:01 -080099 uint32_t ssrc() const override { return ssrc_; }
100
101 cricket::MediaType media_type() const override {
102 return cricket::MEDIA_TYPE_AUDIO;
103 }
104
deadbeef70ab1a12015-09-28 16:53:55 -0700105 std::string id() const override { return id_; }
106
deadbeefa601f5c2016-06-06 14:27:39 -0700107 std::vector<std::string> stream_ids() const override {
108 std::vector<std::string> ret = {stream_id_};
109 return ret;
110 }
111
112 RtpParameters GetParameters() const override;
113 bool SetParameters(const RtpParameters& parameters) override;
114
115 // RtpSenderInternal implementation.
116 void SetSsrc(uint32_t ssrc) override;
117
deadbeeffac06552015-11-25 11:26:01 -0800118 void set_stream_id(const std::string& stream_id) override {
119 stream_id_ = stream_id;
120 }
121 std::string stream_id() const override { return stream_id_; }
122
deadbeef70ab1a12015-09-28 16:53:55 -0700123 void Stop() override;
124
125 private:
nissefcc640f2016-04-01 01:10:42 -0700126 // TODO(nisse): Since SSRC == 0 is technically valid, figure out
127 // some other way to test if we have a valid SSRC.
deadbeeffac06552015-11-25 11:26:01 -0800128 bool can_send_track() const { return track_ && ssrc_; }
129 // Helper function to construct options for
130 // AudioProviderInterface::SetAudioSend.
131 void SetAudioSend();
deadbeef70ab1a12015-09-28 16:53:55 -0700132
133 std::string id_;
deadbeeffac06552015-11-25 11:26:01 -0800134 std::string stream_id_;
tkchin3784b4a2016-06-24 19:31:47 -0700135 AudioProviderInterface* provider_;
deadbeeffac06552015-11-25 11:26:01 -0800136 StatsCollector* stats_;
137 rtc::scoped_refptr<AudioTrackInterface> track_;
138 uint32_t ssrc_ = 0;
139 bool cached_track_enabled_ = false;
140 bool stopped_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700141
142 // Used to pass the data callback from the |track_| to the other end of
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800143 // cricket::AudioSource.
kwibergd1fe2812016-04-27 06:47:29 -0700144 std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_;
deadbeef70ab1a12015-09-28 16:53:55 -0700145};
146
147class VideoRtpSender : public ObserverInterface,
deadbeefa601f5c2016-06-06 14:27:39 -0700148 public rtc::RefCountedObject<RtpSenderInternal> {
deadbeef70ab1a12015-09-28 16:53:55 -0700149 public:
150 VideoRtpSender(VideoTrackInterface* track,
deadbeeffac06552015-11-25 11:26:01 -0800151 const std::string& stream_id,
tkchin3784b4a2016-06-24 19:31:47 -0700152 VideoProviderInterface* provider);
deadbeef70ab1a12015-09-28 16:53:55 -0700153
deadbeefe1f9d832016-01-14 15:35:42 -0800154 // Randomly generates stream_id.
tkchin3784b4a2016-06-24 19:31:47 -0700155 VideoRtpSender(VideoTrackInterface* track, VideoProviderInterface* provider);
deadbeefe1f9d832016-01-14 15:35:42 -0800156
deadbeeffac06552015-11-25 11:26:01 -0800157 // Randomly generates id and stream_id.
tkchin3784b4a2016-06-24 19:31:47 -0700158 explicit VideoRtpSender(VideoProviderInterface* provider);
deadbeeffac06552015-11-25 11:26:01 -0800159
deadbeef70ab1a12015-09-28 16:53:55 -0700160 virtual ~VideoRtpSender();
161
162 // ObserverInterface implementation
163 void OnChanged() override;
164
165 // RtpSenderInterface implementation
166 bool SetTrack(MediaStreamTrackInterface* track) override;
167 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
deadbeefa601f5c2016-06-06 14:27:39 -0700168 return track_;
deadbeef70ab1a12015-09-28 16:53:55 -0700169 }
170
deadbeeffac06552015-11-25 11:26:01 -0800171 uint32_t ssrc() const override { return ssrc_; }
172
173 cricket::MediaType media_type() const override {
174 return cricket::MEDIA_TYPE_VIDEO;
175 }
176
deadbeef70ab1a12015-09-28 16:53:55 -0700177 std::string id() const override { return id_; }
178
deadbeefa601f5c2016-06-06 14:27:39 -0700179 std::vector<std::string> stream_ids() const override {
180 std::vector<std::string> ret = {stream_id_};
181 return ret;
182 }
183
184 RtpParameters GetParameters() const override;
185 bool SetParameters(const RtpParameters& parameters) override;
186
187 // RtpSenderInternal implementation.
188 void SetSsrc(uint32_t ssrc) override;
189
deadbeeffac06552015-11-25 11:26:01 -0800190 void set_stream_id(const std::string& stream_id) override {
191 stream_id_ = stream_id;
192 }
193 std::string stream_id() const override { return stream_id_; }
194
deadbeef70ab1a12015-09-28 16:53:55 -0700195 void Stop() override;
196
197 private:
deadbeeffac06552015-11-25 11:26:01 -0800198 bool can_send_track() const { return track_ && ssrc_; }
199 // Helper function to construct options for
200 // VideoProviderInterface::SetVideoSend.
201 void SetVideoSend();
deadbeef5a4a75a2016-06-02 16:23:38 -0700202 // Helper function to call SetVideoSend with "stop sending" parameters.
203 void ClearVideoSend();
deadbeef70ab1a12015-09-28 16:53:55 -0700204
205 std::string id_;
deadbeeffac06552015-11-25 11:26:01 -0800206 std::string stream_id_;
tkchin3784b4a2016-06-24 19:31:47 -0700207 VideoProviderInterface* provider_;
deadbeeffac06552015-11-25 11:26:01 -0800208 rtc::scoped_refptr<VideoTrackInterface> track_;
209 uint32_t ssrc_ = 0;
210 bool cached_track_enabled_ = false;
211 bool stopped_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700212};
213
214} // namespace webrtc
215
Henrik Kjellander15583c12016-02-10 10:53:12 +0100216#endif // WEBRTC_API_RTPSENDER_H_