blob: 6c0592148bb06d264bdb84b3bdff51b262b03e08 [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
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070021#include "webrtc/api/mediastreaminterface.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"
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070027#include "webrtc/pc/channel.h"
deadbeef70ab1a12015-09-28 16:53:55 -070028
29namespace webrtc {
30
deadbeefa601f5c2016-06-06 14:27:39 -070031// Internal interface used by PeerConnection.
32class RtpSenderInternal : public RtpSenderInterface {
33 public:
34 // Used to set the SSRC of the sender, once a local description has been set.
35 // If |ssrc| is 0, this indiates that the sender should disconnect from the
36 // underlying transport (this occurs if the sender isn't seen in a local
37 // description).
38 virtual void SetSsrc(uint32_t ssrc) = 0;
39
40 // TODO(deadbeef): Support one sender having multiple stream ids.
41 virtual void set_stream_id(const std::string& stream_id) = 0;
42 virtual std::string stream_id() const = 0;
43
44 virtual void Stop() = 0;
45};
46
deadbeef70ab1a12015-09-28 16:53:55 -070047// LocalAudioSinkAdapter receives data callback as a sink to the local
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080048// AudioTrack, and passes the data to the sink of AudioSource.
deadbeef70ab1a12015-09-28 16:53:55 -070049class LocalAudioSinkAdapter : public AudioTrackSinkInterface,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080050 public cricket::AudioSource {
deadbeef70ab1a12015-09-28 16:53:55 -070051 public:
52 LocalAudioSinkAdapter();
53 virtual ~LocalAudioSinkAdapter();
54
55 private:
56 // AudioSinkInterface implementation.
57 void OnData(const void* audio_data,
58 int bits_per_sample,
59 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -080060 size_t number_of_channels,
deadbeef70ab1a12015-09-28 16:53:55 -070061 size_t number_of_frames) override;
62
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080063 // cricket::AudioSource implementation.
64 void SetSink(cricket::AudioSource::Sink* sink) override;
deadbeef70ab1a12015-09-28 16:53:55 -070065
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080066 cricket::AudioSource::Sink* sink_;
deadbeef70ab1a12015-09-28 16:53:55 -070067 // Critical section protecting |sink_|.
68 rtc::CriticalSection lock_;
69};
70
71class AudioRtpSender : public ObserverInterface,
deadbeefa601f5c2016-06-06 14:27:39 -070072 public rtc::RefCountedObject<RtpSenderInternal> {
deadbeef70ab1a12015-09-28 16:53:55 -070073 public:
deadbeeffac06552015-11-25 11:26:01 -080074 // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
75 // at the appropriate times.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070076 // |channel| can be null if one does not exist yet.
deadbeef70ab1a12015-09-28 16:53:55 -070077 AudioRtpSender(AudioTrackInterface* track,
deadbeeffac06552015-11-25 11:26:01 -080078 const std::string& stream_id,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070079 cricket::VoiceChannel* channel,
deadbeeffac06552015-11-25 11:26:01 -080080 StatsCollector* stats);
81
deadbeefe1f9d832016-01-14 15:35:42 -080082 // Randomly generates stream_id.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070083 // |channel| can be null if one does not exist yet.
deadbeefe1f9d832016-01-14 15:35:42 -080084 AudioRtpSender(AudioTrackInterface* track,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070085 cricket::VoiceChannel* channel,
deadbeefe1f9d832016-01-14 15:35:42 -080086 StatsCollector* stats);
87
deadbeeffac06552015-11-25 11:26:01 -080088 // Randomly generates id and stream_id.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070089 // |channel| can be null if one does not exist yet.
90 AudioRtpSender(cricket::VoiceChannel* channel, StatsCollector* stats);
deadbeef70ab1a12015-09-28 16:53:55 -070091
92 virtual ~AudioRtpSender();
93
94 // ObserverInterface implementation
95 void OnChanged() override;
96
97 // RtpSenderInterface implementation
98 bool SetTrack(MediaStreamTrackInterface* track) override;
99 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
deadbeefa601f5c2016-06-06 14:27:39 -0700100 return track_;
deadbeef70ab1a12015-09-28 16:53:55 -0700101 }
102
deadbeeffac06552015-11-25 11:26:01 -0800103 uint32_t ssrc() const override { return ssrc_; }
104
105 cricket::MediaType media_type() const override {
106 return cricket::MEDIA_TYPE_AUDIO;
107 }
108
deadbeef70ab1a12015-09-28 16:53:55 -0700109 std::string id() const override { return id_; }
110
deadbeefa601f5c2016-06-06 14:27:39 -0700111 std::vector<std::string> stream_ids() const override {
112 std::vector<std::string> ret = {stream_id_};
113 return ret;
114 }
115
116 RtpParameters GetParameters() const override;
117 bool SetParameters(const RtpParameters& parameters) override;
118
119 // RtpSenderInternal implementation.
120 void SetSsrc(uint32_t ssrc) override;
121
deadbeeffac06552015-11-25 11:26:01 -0800122 void set_stream_id(const std::string& stream_id) override {
123 stream_id_ = stream_id;
124 }
125 std::string stream_id() const override { return stream_id_; }
126
deadbeef70ab1a12015-09-28 16:53:55 -0700127 void Stop() override;
128
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700129 // Does not take ownership.
130 // Should call SetChannel(nullptr) before |channel| is destroyed.
131 void SetChannel(cricket::VoiceChannel* channel) { channel_ = channel; }
132
deadbeef70ab1a12015-09-28 16:53:55 -0700133 private:
nissefcc640f2016-04-01 01:10:42 -0700134 // TODO(nisse): Since SSRC == 0 is technically valid, figure out
135 // some other way to test if we have a valid SSRC.
deadbeeffac06552015-11-25 11:26:01 -0800136 bool can_send_track() const { return track_ && ssrc_; }
137 // Helper function to construct options for
138 // AudioProviderInterface::SetAudioSend.
139 void SetAudioSend();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700140 // Helper function to call SetAudioSend with "stop sending" parameters.
141 void ClearAudioSend();
deadbeef70ab1a12015-09-28 16:53:55 -0700142
143 std::string id_;
deadbeeffac06552015-11-25 11:26:01 -0800144 std::string stream_id_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700145 cricket::VoiceChannel* channel_ = nullptr;
deadbeeffac06552015-11-25 11:26:01 -0800146 StatsCollector* stats_;
147 rtc::scoped_refptr<AudioTrackInterface> track_;
148 uint32_t ssrc_ = 0;
149 bool cached_track_enabled_ = false;
150 bool stopped_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700151
152 // Used to pass the data callback from the |track_| to the other end of
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800153 // cricket::AudioSource.
kwibergd1fe2812016-04-27 06:47:29 -0700154 std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_;
deadbeef70ab1a12015-09-28 16:53:55 -0700155};
156
157class VideoRtpSender : public ObserverInterface,
deadbeefa601f5c2016-06-06 14:27:39 -0700158 public rtc::RefCountedObject<RtpSenderInternal> {
deadbeef70ab1a12015-09-28 16:53:55 -0700159 public:
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700160 // |channel| can be null if one does not exist yet.
deadbeef70ab1a12015-09-28 16:53:55 -0700161 VideoRtpSender(VideoTrackInterface* track,
deadbeeffac06552015-11-25 11:26:01 -0800162 const std::string& stream_id,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700163 cricket::VideoChannel* channel);
deadbeef70ab1a12015-09-28 16:53:55 -0700164
deadbeefe1f9d832016-01-14 15:35:42 -0800165 // Randomly generates stream_id.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700166 // |channel| can be null if one does not exist yet.
167 VideoRtpSender(VideoTrackInterface* track, cricket::VideoChannel* channel);
deadbeefe1f9d832016-01-14 15:35:42 -0800168
deadbeeffac06552015-11-25 11:26:01 -0800169 // Randomly generates id and stream_id.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700170 // |channel| can be null if one does not exist yet.
171 explicit VideoRtpSender(cricket::VideoChannel* channel);
deadbeeffac06552015-11-25 11:26:01 -0800172
deadbeef70ab1a12015-09-28 16:53:55 -0700173 virtual ~VideoRtpSender();
174
175 // ObserverInterface implementation
176 void OnChanged() override;
177
178 // RtpSenderInterface implementation
179 bool SetTrack(MediaStreamTrackInterface* track) override;
180 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
deadbeefa601f5c2016-06-06 14:27:39 -0700181 return track_;
deadbeef70ab1a12015-09-28 16:53:55 -0700182 }
183
deadbeeffac06552015-11-25 11:26:01 -0800184 uint32_t ssrc() const override { return ssrc_; }
185
186 cricket::MediaType media_type() const override {
187 return cricket::MEDIA_TYPE_VIDEO;
188 }
189
deadbeef70ab1a12015-09-28 16:53:55 -0700190 std::string id() const override { return id_; }
191
deadbeefa601f5c2016-06-06 14:27:39 -0700192 std::vector<std::string> stream_ids() const override {
193 std::vector<std::string> ret = {stream_id_};
194 return ret;
195 }
196
197 RtpParameters GetParameters() const override;
198 bool SetParameters(const RtpParameters& parameters) override;
199
200 // RtpSenderInternal implementation.
201 void SetSsrc(uint32_t ssrc) override;
202
deadbeeffac06552015-11-25 11:26:01 -0800203 void set_stream_id(const std::string& stream_id) override {
204 stream_id_ = stream_id;
205 }
206 std::string stream_id() const override { return stream_id_; }
207
deadbeef70ab1a12015-09-28 16:53:55 -0700208 void Stop() override;
209
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700210 // Does not take ownership.
211 // Should call SetChannel(nullptr) before |channel| is destroyed.
212 void SetChannel(cricket::VideoChannel* channel) { channel_ = channel; }
213
deadbeef70ab1a12015-09-28 16:53:55 -0700214 private:
deadbeeffac06552015-11-25 11:26:01 -0800215 bool can_send_track() const { return track_ && ssrc_; }
216 // Helper function to construct options for
217 // VideoProviderInterface::SetVideoSend.
218 void SetVideoSend();
deadbeef5a4a75a2016-06-02 16:23:38 -0700219 // Helper function to call SetVideoSend with "stop sending" parameters.
220 void ClearVideoSend();
deadbeef70ab1a12015-09-28 16:53:55 -0700221
222 std::string id_;
deadbeeffac06552015-11-25 11:26:01 -0800223 std::string stream_id_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700224 cricket::VideoChannel* channel_ = nullptr;
deadbeeffac06552015-11-25 11:26:01 -0800225 rtc::scoped_refptr<VideoTrackInterface> track_;
226 uint32_t ssrc_ = 0;
227 bool cached_track_enabled_ = false;
pbos5214a0a2016-12-16 15:39:11 -0800228 VideoTrackInterface::ContentHint cached_track_content_hint_ =
229 VideoTrackInterface::ContentHint::kNone;
deadbeeffac06552015-11-25 11:26:01 -0800230 bool stopped_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700231};
232
233} // namespace webrtc
234
Henrik Kjellander15583c12016-02-10 10:53:12 +0100235#endif // WEBRTC_API_RTPSENDER_H_