deadbeef | 6979b02 | 2015-09-24 16:47:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2015 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 28 | // This file contains classes that implement RtpSenderInterface. |
| 29 | // An RtpSender associates a MediaStreamTrackInterface with an underlying |
| 30 | // transport (provided by AudioProviderInterface/VideoProviderInterface) |
| 31 | |
| 32 | #ifndef TALK_APP_WEBRTC_RTPSENDER_H_ |
| 33 | #define TALK_APP_WEBRTC_RTPSENDER_H_ |
| 34 | |
| 35 | #include <string> |
| 36 | |
| 37 | #include "talk/app/webrtc/mediastreamprovider.h" |
| 38 | #include "talk/app/webrtc/rtpsenderinterface.h" |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame^] | 39 | #include "talk/app/webrtc/statscollector.h" |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 40 | #include "talk/media/base/audiorenderer.h" |
| 41 | #include "webrtc/base/basictypes.h" |
| 42 | #include "webrtc/base/criticalsection.h" |
| 43 | #include "webrtc/base/scoped_ptr.h" |
| 44 | |
| 45 | namespace webrtc { |
| 46 | |
| 47 | // LocalAudioSinkAdapter receives data callback as a sink to the local |
| 48 | // AudioTrack, and passes the data to the sink of AudioRenderer. |
| 49 | class LocalAudioSinkAdapter : public AudioTrackSinkInterface, |
| 50 | public cricket::AudioRenderer { |
| 51 | 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, |
| 60 | int number_of_channels, |
| 61 | size_t number_of_frames) override; |
| 62 | |
| 63 | // cricket::AudioRenderer implementation. |
| 64 | void SetSink(cricket::AudioRenderer::Sink* sink) override; |
| 65 | |
| 66 | cricket::AudioRenderer::Sink* sink_; |
| 67 | // Critical section protecting |sink_|. |
| 68 | rtc::CriticalSection lock_; |
| 69 | }; |
| 70 | |
| 71 | class AudioRtpSender : public ObserverInterface, |
| 72 | public rtc::RefCountedObject<RtpSenderInterface> { |
| 73 | public: |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame^] | 74 | // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called |
| 75 | // at the appropriate times. |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 76 | AudioRtpSender(AudioTrackInterface* track, |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame^] | 77 | const std::string& stream_id, |
| 78 | AudioProviderInterface* provider, |
| 79 | StatsCollector* stats); |
| 80 | |
| 81 | // Randomly generates id and stream_id. |
| 82 | AudioRtpSender(AudioProviderInterface* provider, StatsCollector* stats); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 83 | |
| 84 | virtual ~AudioRtpSender(); |
| 85 | |
| 86 | // ObserverInterface implementation |
| 87 | void OnChanged() override; |
| 88 | |
| 89 | // RtpSenderInterface implementation |
| 90 | bool SetTrack(MediaStreamTrackInterface* track) override; |
| 91 | rtc::scoped_refptr<MediaStreamTrackInterface> track() const override { |
| 92 | return track_.get(); |
| 93 | } |
| 94 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame^] | 95 | void SetSsrc(uint32_t ssrc) override; |
| 96 | |
| 97 | uint32_t ssrc() const override { return ssrc_; } |
| 98 | |
| 99 | cricket::MediaType media_type() const override { |
| 100 | return cricket::MEDIA_TYPE_AUDIO; |
| 101 | } |
| 102 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 103 | std::string id() const override { return id_; } |
| 104 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame^] | 105 | void set_stream_id(const std::string& stream_id) override { |
| 106 | stream_id_ = stream_id; |
| 107 | } |
| 108 | std::string stream_id() const override { return stream_id_; } |
| 109 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 110 | void Stop() override; |
| 111 | |
| 112 | private: |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame^] | 113 | bool can_send_track() const { return track_ && ssrc_; } |
| 114 | // Helper function to construct options for |
| 115 | // AudioProviderInterface::SetAudioSend. |
| 116 | void SetAudioSend(); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 117 | |
| 118 | std::string id_; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame^] | 119 | std::string stream_id_; |
deadbeef | 5def7b9 | 2015-11-20 11:43:22 -0800 | [diff] [blame] | 120 | AudioProviderInterface* provider_; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame^] | 121 | StatsCollector* stats_; |
| 122 | rtc::scoped_refptr<AudioTrackInterface> track_; |
| 123 | uint32_t ssrc_ = 0; |
| 124 | bool cached_track_enabled_ = false; |
| 125 | bool stopped_ = false; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 126 | |
| 127 | // Used to pass the data callback from the |track_| to the other end of |
| 128 | // cricket::AudioRenderer. |
| 129 | rtc::scoped_ptr<LocalAudioSinkAdapter> sink_adapter_; |
| 130 | }; |
| 131 | |
| 132 | class VideoRtpSender : public ObserverInterface, |
| 133 | public rtc::RefCountedObject<RtpSenderInterface> { |
| 134 | public: |
| 135 | VideoRtpSender(VideoTrackInterface* track, |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame^] | 136 | const std::string& stream_id, |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 137 | VideoProviderInterface* provider); |
| 138 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame^] | 139 | // Randomly generates id and stream_id. |
| 140 | explicit VideoRtpSender(VideoProviderInterface* provider); |
| 141 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 142 | virtual ~VideoRtpSender(); |
| 143 | |
| 144 | // ObserverInterface implementation |
| 145 | void OnChanged() override; |
| 146 | |
| 147 | // RtpSenderInterface implementation |
| 148 | bool SetTrack(MediaStreamTrackInterface* track) override; |
| 149 | rtc::scoped_refptr<MediaStreamTrackInterface> track() const override { |
| 150 | return track_.get(); |
| 151 | } |
| 152 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame^] | 153 | void SetSsrc(uint32_t ssrc) override; |
| 154 | |
| 155 | uint32_t ssrc() const override { return ssrc_; } |
| 156 | |
| 157 | cricket::MediaType media_type() const override { |
| 158 | return cricket::MEDIA_TYPE_VIDEO; |
| 159 | } |
| 160 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 161 | std::string id() const override { return id_; } |
| 162 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame^] | 163 | void set_stream_id(const std::string& stream_id) override { |
| 164 | stream_id_ = stream_id; |
| 165 | } |
| 166 | std::string stream_id() const override { return stream_id_; } |
| 167 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 168 | void Stop() override; |
| 169 | |
| 170 | private: |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame^] | 171 | bool can_send_track() const { return track_ && ssrc_; } |
| 172 | // Helper function to construct options for |
| 173 | // VideoProviderInterface::SetVideoSend. |
| 174 | void SetVideoSend(); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 175 | |
| 176 | std::string id_; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame^] | 177 | std::string stream_id_; |
deadbeef | 5def7b9 | 2015-11-20 11:43:22 -0800 | [diff] [blame] | 178 | VideoProviderInterface* provider_; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame^] | 179 | rtc::scoped_refptr<VideoTrackInterface> track_; |
| 180 | uint32_t ssrc_ = 0; |
| 181 | bool cached_track_enabled_ = false; |
| 182 | bool stopped_ = false; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | } // namespace webrtc |
| 186 | |
| 187 | #endif // TALK_APP_WEBRTC_RTPSENDER_H_ |