OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 // This file contains classes that implement RtpSenderInterface. | 11 // This file contains classes that implement RtpSenderInterface. |
12 // An RtpSender associates a MediaStreamTrackInterface with an underlying | 12 // An RtpSender associates a MediaStreamTrackInterface with an underlying |
13 // transport (provided by AudioProviderInterface/VideoProviderInterface) | 13 // transport (provided by AudioProviderInterface/VideoProviderInterface) |
14 | 14 |
15 #ifndef WEBRTC_API_RTPSENDER_H_ | 15 #ifndef WEBRTC_API_RTPSENDER_H_ |
16 #define WEBRTC_API_RTPSENDER_H_ | 16 #define WEBRTC_API_RTPSENDER_H_ |
17 | 17 |
18 #include <memory> | 18 #include <memory> |
19 #include <string> | 19 #include <string> |
20 | 20 |
21 #include "webrtc/api/mediastreamprovider.h" | 21 #include "webrtc/api/mediastreamprovider.h" |
22 #include "webrtc/api/rtpsenderinterface.h" | 22 #include "webrtc/api/rtpsenderinterface.h" |
23 #include "webrtc/api/statscollector.h" | 23 #include "webrtc/api/statscollector.h" |
24 #include "webrtc/base/basictypes.h" | 24 #include "webrtc/base/basictypes.h" |
25 #include "webrtc/base/criticalsection.h" | 25 #include "webrtc/base/criticalsection.h" |
26 #include "webrtc/media/base/audiosource.h" | 26 #include "webrtc/media/base/audiosource.h" |
27 | 27 |
28 namespace webrtc { | 28 namespace webrtc { |
29 | 29 |
| 30 // Internal interface used by PeerConnection. |
| 31 class 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 |
30 // LocalAudioSinkAdapter receives data callback as a sink to the local | 46 // LocalAudioSinkAdapter receives data callback as a sink to the local |
31 // AudioTrack, and passes the data to the sink of AudioSource. | 47 // AudioTrack, and passes the data to the sink of AudioSource. |
32 class LocalAudioSinkAdapter : public AudioTrackSinkInterface, | 48 class LocalAudioSinkAdapter : public AudioTrackSinkInterface, |
33 public cricket::AudioSource { | 49 public cricket::AudioSource { |
34 public: | 50 public: |
35 LocalAudioSinkAdapter(); | 51 LocalAudioSinkAdapter(); |
36 virtual ~LocalAudioSinkAdapter(); | 52 virtual ~LocalAudioSinkAdapter(); |
37 | 53 |
38 private: | 54 private: |
39 // AudioSinkInterface implementation. | 55 // AudioSinkInterface implementation. |
40 void OnData(const void* audio_data, | 56 void OnData(const void* audio_data, |
41 int bits_per_sample, | 57 int bits_per_sample, |
42 int sample_rate, | 58 int sample_rate, |
43 size_t number_of_channels, | 59 size_t number_of_channels, |
44 size_t number_of_frames) override; | 60 size_t number_of_frames) override; |
45 | 61 |
46 // cricket::AudioSource implementation. | 62 // cricket::AudioSource implementation. |
47 void SetSink(cricket::AudioSource::Sink* sink) override; | 63 void SetSink(cricket::AudioSource::Sink* sink) override; |
48 | 64 |
49 cricket::AudioSource::Sink* sink_; | 65 cricket::AudioSource::Sink* sink_; |
50 // Critical section protecting |sink_|. | 66 // Critical section protecting |sink_|. |
51 rtc::CriticalSection lock_; | 67 rtc::CriticalSection lock_; |
52 }; | 68 }; |
53 | 69 |
54 class AudioRtpSender : public ObserverInterface, | 70 class AudioRtpSender : public ObserverInterface, |
55 public rtc::RefCountedObject<RtpSenderInterface> { | 71 public rtc::RefCountedObject<RtpSenderInternal> { |
56 public: | 72 public: |
57 // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called | 73 // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called |
58 // at the appropriate times. | 74 // at the appropriate times. |
59 AudioRtpSender(AudioTrackInterface* track, | 75 AudioRtpSender(AudioTrackInterface* track, |
60 const std::string& stream_id, | 76 const std::string& stream_id, |
61 AudioProviderInterface* provider, | 77 AudioProviderInterface* provider, |
62 StatsCollector* stats); | 78 StatsCollector* stats); |
63 | 79 |
64 // Randomly generates stream_id. | 80 // Randomly generates stream_id. |
65 AudioRtpSender(AudioTrackInterface* track, | 81 AudioRtpSender(AudioTrackInterface* track, |
66 AudioProviderInterface* provider, | 82 AudioProviderInterface* provider, |
67 StatsCollector* stats); | 83 StatsCollector* stats); |
68 | 84 |
69 // Randomly generates id and stream_id. | 85 // Randomly generates id and stream_id. |
70 AudioRtpSender(AudioProviderInterface* provider, StatsCollector* stats); | 86 AudioRtpSender(AudioProviderInterface* provider, StatsCollector* stats); |
71 | 87 |
72 virtual ~AudioRtpSender(); | 88 virtual ~AudioRtpSender(); |
73 | 89 |
74 // ObserverInterface implementation | 90 // ObserverInterface implementation |
75 void OnChanged() override; | 91 void OnChanged() override; |
76 | 92 |
77 // RtpSenderInterface implementation | 93 // RtpSenderInterface implementation |
78 bool SetTrack(MediaStreamTrackInterface* track) override; | 94 bool SetTrack(MediaStreamTrackInterface* track) override; |
79 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override { | 95 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override { |
80 return track_.get(); | 96 return track_; |
81 } | 97 } |
82 | 98 |
83 void SetSsrc(uint32_t ssrc) override; | |
84 | |
85 uint32_t ssrc() const override { return ssrc_; } | 99 uint32_t ssrc() const override { return ssrc_; } |
86 | 100 |
87 cricket::MediaType media_type() const override { | 101 cricket::MediaType media_type() const override { |
88 return cricket::MEDIA_TYPE_AUDIO; | 102 return cricket::MEDIA_TYPE_AUDIO; |
89 } | 103 } |
90 | 104 |
91 std::string id() const override { return id_; } | 105 std::string id() const override { return id_; } |
92 | 106 |
| 107 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 |
93 void set_stream_id(const std::string& stream_id) override { | 118 void set_stream_id(const std::string& stream_id) override { |
94 stream_id_ = stream_id; | 119 stream_id_ = stream_id; |
95 } | 120 } |
96 std::string stream_id() const override { return stream_id_; } | 121 std::string stream_id() const override { return stream_id_; } |
97 | 122 |
98 void Stop() override; | 123 void Stop() override; |
99 | 124 |
100 RtpParameters GetParameters() const override; | |
101 bool SetParameters(const RtpParameters& parameters) override; | |
102 | |
103 private: | 125 private: |
104 // TODO(nisse): Since SSRC == 0 is technically valid, figure out | 126 // TODO(nisse): Since SSRC == 0 is technically valid, figure out |
105 // some other way to test if we have a valid SSRC. | 127 // some other way to test if we have a valid SSRC. |
106 bool can_send_track() const { return track_ && ssrc_; } | 128 bool can_send_track() const { return track_ && ssrc_; } |
107 // Helper function to construct options for | 129 // Helper function to construct options for |
108 // AudioProviderInterface::SetAudioSend. | 130 // AudioProviderInterface::SetAudioSend. |
109 void SetAudioSend(); | 131 void SetAudioSend(); |
110 | 132 |
111 std::string id_; | 133 std::string id_; |
112 std::string stream_id_; | 134 std::string stream_id_; |
113 AudioProviderInterface* provider_; | 135 AudioProviderInterface* provider_; |
114 StatsCollector* stats_; | 136 StatsCollector* stats_; |
115 rtc::scoped_refptr<AudioTrackInterface> track_; | 137 rtc::scoped_refptr<AudioTrackInterface> track_; |
116 uint32_t ssrc_ = 0; | 138 uint32_t ssrc_ = 0; |
117 bool cached_track_enabled_ = false; | 139 bool cached_track_enabled_ = false; |
118 bool stopped_ = false; | 140 bool stopped_ = false; |
119 | 141 |
120 // Used to pass the data callback from the |track_| to the other end of | 142 // Used to pass the data callback from the |track_| to the other end of |
121 // cricket::AudioSource. | 143 // cricket::AudioSource. |
122 std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_; | 144 std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_; |
123 }; | 145 }; |
124 | 146 |
125 class VideoRtpSender : public ObserverInterface, | 147 class VideoRtpSender : public ObserverInterface, |
126 public rtc::RefCountedObject<RtpSenderInterface> { | 148 public rtc::RefCountedObject<RtpSenderInternal> { |
127 public: | 149 public: |
128 VideoRtpSender(VideoTrackInterface* track, | 150 VideoRtpSender(VideoTrackInterface* track, |
129 const std::string& stream_id, | 151 const std::string& stream_id, |
130 VideoProviderInterface* provider); | 152 VideoProviderInterface* provider); |
131 | 153 |
132 // Randomly generates stream_id. | 154 // Randomly generates stream_id. |
133 VideoRtpSender(VideoTrackInterface* track, VideoProviderInterface* provider); | 155 VideoRtpSender(VideoTrackInterface* track, VideoProviderInterface* provider); |
134 | 156 |
135 // Randomly generates id and stream_id. | 157 // Randomly generates id and stream_id. |
136 explicit VideoRtpSender(VideoProviderInterface* provider); | 158 explicit VideoRtpSender(VideoProviderInterface* provider); |
137 | 159 |
138 virtual ~VideoRtpSender(); | 160 virtual ~VideoRtpSender(); |
139 | 161 |
140 // ObserverInterface implementation | 162 // ObserverInterface implementation |
141 void OnChanged() override; | 163 void OnChanged() override; |
142 | 164 |
143 // RtpSenderInterface implementation | 165 // RtpSenderInterface implementation |
144 bool SetTrack(MediaStreamTrackInterface* track) override; | 166 bool SetTrack(MediaStreamTrackInterface* track) override; |
145 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override { | 167 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override { |
146 return track_.get(); | 168 return track_; |
147 } | 169 } |
148 | 170 |
149 void SetSsrc(uint32_t ssrc) override; | |
150 | |
151 uint32_t ssrc() const override { return ssrc_; } | 171 uint32_t ssrc() const override { return ssrc_; } |
152 | 172 |
153 cricket::MediaType media_type() const override { | 173 cricket::MediaType media_type() const override { |
154 return cricket::MEDIA_TYPE_VIDEO; | 174 return cricket::MEDIA_TYPE_VIDEO; |
155 } | 175 } |
156 | 176 |
157 std::string id() const override { return id_; } | 177 std::string id() const override { return id_; } |
158 | 178 |
| 179 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 |
159 void set_stream_id(const std::string& stream_id) override { | 190 void set_stream_id(const std::string& stream_id) override { |
160 stream_id_ = stream_id; | 191 stream_id_ = stream_id; |
161 } | 192 } |
162 std::string stream_id() const override { return stream_id_; } | 193 std::string stream_id() const override { return stream_id_; } |
163 | 194 |
164 void Stop() override; | 195 void Stop() override; |
165 | 196 |
166 RtpParameters GetParameters() const override; | |
167 bool SetParameters(const RtpParameters& parameters) override; | |
168 | |
169 private: | 197 private: |
170 bool can_send_track() const { return track_ && ssrc_; } | 198 bool can_send_track() const { return track_ && ssrc_; } |
171 // Helper function to construct options for | 199 // Helper function to construct options for |
172 // VideoProviderInterface::SetVideoSend. | 200 // VideoProviderInterface::SetVideoSend. |
173 void SetVideoSend(); | 201 void SetVideoSend(); |
174 // Helper function to call SetVideoSend with "stop sending" parameters. | 202 // Helper function to call SetVideoSend with "stop sending" parameters. |
175 void ClearVideoSend(); | 203 void ClearVideoSend(); |
176 | 204 |
177 std::string id_; | 205 std::string id_; |
178 std::string stream_id_; | 206 std::string stream_id_; |
179 VideoProviderInterface* provider_; | 207 VideoProviderInterface* provider_; |
180 rtc::scoped_refptr<VideoTrackInterface> track_; | 208 rtc::scoped_refptr<VideoTrackInterface> track_; |
181 uint32_t ssrc_ = 0; | 209 uint32_t ssrc_ = 0; |
182 bool cached_track_enabled_ = false; | 210 bool cached_track_enabled_ = false; |
183 bool stopped_ = false; | 211 bool stopped_ = false; |
184 }; | 212 }; |
185 | 213 |
186 } // namespace webrtc | 214 } // namespace webrtc |
187 | 215 |
188 #endif // WEBRTC_API_RTPSENDER_H_ | 216 #endif // WEBRTC_API_RTPSENDER_H_ |
OLD | NEW |