Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 1 | /* |
kjellander | 95ed4e6 | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 3 | * |
kjellander | 95ed4e6 | 2016-02-10 15:54:43 | [diff] [blame] | 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. |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 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 | |
| 15 | #ifndef WEBRTC_API_RTPSENDER_H_ |
| 16 | #define WEBRTC_API_RTPSENDER_H_ |
| 17 | |
kwiberg | b4bfca4 | 2016-04-27 13:47:29 | [diff] [blame] | 18 | #include <memory> |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 19 | #include <string> |
| 20 | |
| 21 | #include "webrtc/api/mediastreamprovider.h" |
| 22 | #include "webrtc/api/rtpsenderinterface.h" |
| 23 | #include "webrtc/api/statscollector.h" |
| 24 | #include "webrtc/base/basictypes.h" |
| 25 | #include "webrtc/base/criticalsection.h" |
Taylor Brandstetter | e38b09a | 2016-03-08 20:37:39 | [diff] [blame] | 26 | #include "webrtc/media/base/audiosource.h" |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | |
| 30 | // LocalAudioSinkAdapter receives data callback as a sink to the local |
Taylor Brandstetter | e38b09a | 2016-03-08 20:37:39 | [diff] [blame] | 31 | // AudioTrack, and passes the data to the sink of AudioSource. |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 32 | class LocalAudioSinkAdapter : public AudioTrackSinkInterface, |
Taylor Brandstetter | e38b09a | 2016-03-08 20:37:39 | [diff] [blame] | 33 | public cricket::AudioSource { |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 34 | public: |
| 35 | LocalAudioSinkAdapter(); |
| 36 | virtual ~LocalAudioSinkAdapter(); |
| 37 | |
| 38 | private: |
| 39 | // AudioSinkInterface implementation. |
| 40 | void OnData(const void* audio_data, |
| 41 | int bits_per_sample, |
| 42 | int sample_rate, |
| 43 | size_t number_of_channels, |
| 44 | size_t number_of_frames) override; |
| 45 | |
Taylor Brandstetter | e38b09a | 2016-03-08 20:37:39 | [diff] [blame] | 46 | // cricket::AudioSource implementation. |
| 47 | void SetSink(cricket::AudioSource::Sink* sink) override; |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 48 | |
Taylor Brandstetter | e38b09a | 2016-03-08 20:37:39 | [diff] [blame] | 49 | cricket::AudioSource::Sink* sink_; |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 50 | // Critical section protecting |sink_|. |
| 51 | rtc::CriticalSection lock_; |
| 52 | }; |
| 53 | |
| 54 | class AudioRtpSender : public ObserverInterface, |
| 55 | public rtc::RefCountedObject<RtpSenderInterface> { |
| 56 | public: |
| 57 | // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called |
| 58 | // at the appropriate times. |
| 59 | AudioRtpSender(AudioTrackInterface* track, |
| 60 | const std::string& stream_id, |
| 61 | AudioProviderInterface* provider, |
| 62 | StatsCollector* stats); |
| 63 | |
| 64 | // Randomly generates stream_id. |
| 65 | AudioRtpSender(AudioTrackInterface* track, |
| 66 | AudioProviderInterface* provider, |
| 67 | StatsCollector* stats); |
| 68 | |
| 69 | // Randomly generates id and stream_id. |
| 70 | AudioRtpSender(AudioProviderInterface* provider, StatsCollector* stats); |
| 71 | |
| 72 | virtual ~AudioRtpSender(); |
| 73 | |
| 74 | // ObserverInterface implementation |
| 75 | void OnChanged() override; |
| 76 | |
| 77 | // RtpSenderInterface implementation |
| 78 | bool SetTrack(MediaStreamTrackInterface* track) override; |
| 79 | rtc::scoped_refptr<MediaStreamTrackInterface> track() const override { |
| 80 | return track_.get(); |
| 81 | } |
| 82 | |
| 83 | void SetSsrc(uint32_t ssrc) override; |
| 84 | |
| 85 | uint32_t ssrc() const override { return ssrc_; } |
| 86 | |
| 87 | cricket::MediaType media_type() const override { |
| 88 | return cricket::MEDIA_TYPE_AUDIO; |
| 89 | } |
| 90 | |
| 91 | std::string id() const override { return id_; } |
| 92 | |
| 93 | void set_stream_id(const std::string& stream_id) override { |
| 94 | stream_id_ = stream_id; |
| 95 | } |
| 96 | std::string stream_id() const override { return stream_id_; } |
| 97 | |
| 98 | void Stop() override; |
| 99 | |
nisse | 44dc40f | 2016-04-29 13:09:15 | [diff] [blame] | 100 | RtpParameters GetParameters() const override; |
| 101 | bool SetParameters(const RtpParameters& parameters) override; |
skvlad | 4ac195e | 2016-03-17 02:07:43 | [diff] [blame] | 102 | |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 103 | private: |
nisse | 66a510a | 2016-04-01 08:10:42 | [diff] [blame] | 104 | // TODO(nisse): Since SSRC == 0 is technically valid, figure out |
| 105 | // some other way to test if we have a valid SSRC. |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 106 | bool can_send_track() const { return track_ && ssrc_; } |
| 107 | // Helper function to construct options for |
| 108 | // AudioProviderInterface::SetAudioSend. |
| 109 | void SetAudioSend(); |
| 110 | |
| 111 | std::string id_; |
| 112 | std::string stream_id_; |
| 113 | AudioProviderInterface* provider_; |
| 114 | StatsCollector* stats_; |
| 115 | rtc::scoped_refptr<AudioTrackInterface> track_; |
| 116 | uint32_t ssrc_ = 0; |
| 117 | bool cached_track_enabled_ = false; |
| 118 | bool stopped_ = false; |
| 119 | |
| 120 | // Used to pass the data callback from the |track_| to the other end of |
Taylor Brandstetter | e38b09a | 2016-03-08 20:37:39 | [diff] [blame] | 121 | // cricket::AudioSource. |
kwiberg | b4bfca4 | 2016-04-27 13:47:29 | [diff] [blame] | 122 | std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_; |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | class VideoRtpSender : public ObserverInterface, |
| 126 | public rtc::RefCountedObject<RtpSenderInterface> { |
| 127 | public: |
| 128 | VideoRtpSender(VideoTrackInterface* track, |
| 129 | const std::string& stream_id, |
| 130 | VideoProviderInterface* provider); |
| 131 | |
| 132 | // Randomly generates stream_id. |
| 133 | VideoRtpSender(VideoTrackInterface* track, VideoProviderInterface* provider); |
| 134 | |
| 135 | // Randomly generates id and stream_id. |
| 136 | explicit VideoRtpSender(VideoProviderInterface* provider); |
| 137 | |
| 138 | virtual ~VideoRtpSender(); |
| 139 | |
| 140 | // ObserverInterface implementation |
| 141 | void OnChanged() override; |
| 142 | |
| 143 | // RtpSenderInterface implementation |
| 144 | bool SetTrack(MediaStreamTrackInterface* track) override; |
| 145 | rtc::scoped_refptr<MediaStreamTrackInterface> track() const override { |
| 146 | return track_.get(); |
| 147 | } |
| 148 | |
| 149 | void SetSsrc(uint32_t ssrc) override; |
| 150 | |
| 151 | uint32_t ssrc() const override { return ssrc_; } |
| 152 | |
| 153 | cricket::MediaType media_type() const override { |
| 154 | return cricket::MEDIA_TYPE_VIDEO; |
| 155 | } |
| 156 | |
| 157 | std::string id() const override { return id_; } |
| 158 | |
| 159 | void set_stream_id(const std::string& stream_id) override { |
| 160 | stream_id_ = stream_id; |
| 161 | } |
| 162 | std::string stream_id() const override { return stream_id_; } |
| 163 | |
| 164 | void Stop() override; |
| 165 | |
nisse | 44dc40f | 2016-04-29 13:09:15 | [diff] [blame] | 166 | RtpParameters GetParameters() const override; |
| 167 | bool SetParameters(const RtpParameters& parameters) override; |
skvlad | 4ac195e | 2016-03-17 02:07:43 | [diff] [blame] | 168 | |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 169 | private: |
| 170 | bool can_send_track() const { return track_ && ssrc_; } |
| 171 | // Helper function to construct options for |
| 172 | // VideoProviderInterface::SetVideoSend. |
| 173 | void SetVideoSend(); |
| 174 | |
| 175 | std::string id_; |
| 176 | std::string stream_id_; |
| 177 | VideoProviderInterface* provider_; |
| 178 | rtc::scoped_refptr<VideoTrackInterface> track_; |
| 179 | uint32_t ssrc_ = 0; |
| 180 | bool cached_track_enabled_ = false; |
| 181 | bool stopped_ = false; |
| 182 | }; |
| 183 | |
| 184 | } // namespace webrtc |
| 185 | |
| 186 | #endif // WEBRTC_API_RTPSENDER_H_ |