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 | |
| 28 | #include "talk/app/webrtc/rtpsender.h" |
| 29 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame^] | 30 | #include "talk/app/webrtc/localaudiosource.h" |
| 31 | #include "talk/app/webrtc/videosourceinterface.h" |
| 32 | |
| 33 | namespace webrtc { |
| 34 | |
| 35 | LocalAudioSinkAdapter::LocalAudioSinkAdapter() : sink_(nullptr) {} |
| 36 | |
| 37 | LocalAudioSinkAdapter::~LocalAudioSinkAdapter() { |
| 38 | rtc::CritScope lock(&lock_); |
| 39 | if (sink_) |
| 40 | sink_->OnClose(); |
| 41 | } |
| 42 | |
| 43 | void LocalAudioSinkAdapter::OnData(const void* audio_data, |
| 44 | int bits_per_sample, |
| 45 | int sample_rate, |
| 46 | int number_of_channels, |
| 47 | size_t number_of_frames) { |
| 48 | rtc::CritScope lock(&lock_); |
| 49 | if (sink_) { |
| 50 | sink_->OnData(audio_data, bits_per_sample, sample_rate, number_of_channels, |
| 51 | number_of_frames); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void LocalAudioSinkAdapter::SetSink(cricket::AudioRenderer::Sink* sink) { |
| 56 | rtc::CritScope lock(&lock_); |
| 57 | ASSERT(!sink || !sink_); |
| 58 | sink_ = sink; |
| 59 | } |
| 60 | |
| 61 | AudioRtpSender::AudioRtpSender(AudioTrackInterface* track, |
| 62 | uint32 ssrc, |
| 63 | AudioProviderInterface* provider) |
| 64 | : id_(track->id()), |
| 65 | track_(track), |
| 66 | ssrc_(ssrc), |
| 67 | provider_(provider), |
| 68 | cached_track_enabled_(track->enabled()), |
| 69 | sink_adapter_(new LocalAudioSinkAdapter()) { |
| 70 | track_->RegisterObserver(this); |
| 71 | track_->AddSink(sink_adapter_.get()); |
| 72 | Reconfigure(); |
| 73 | } |
| 74 | |
| 75 | AudioRtpSender::~AudioRtpSender() { |
| 76 | track_->RemoveSink(sink_adapter_.get()); |
| 77 | track_->UnregisterObserver(this); |
| 78 | Stop(); |
| 79 | } |
| 80 | |
| 81 | void AudioRtpSender::OnChanged() { |
| 82 | if (cached_track_enabled_ != track_->enabled()) { |
| 83 | cached_track_enabled_ = track_->enabled(); |
| 84 | Reconfigure(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | bool AudioRtpSender::SetTrack(MediaStreamTrackInterface* track) { |
| 89 | if (track->kind() != "audio") { |
| 90 | LOG(LS_ERROR) << "SetTrack called on audio RtpSender with " << track->kind() |
| 91 | << " track."; |
| 92 | return false; |
| 93 | } |
| 94 | AudioTrackInterface* audio_track = static_cast<AudioTrackInterface*>(track); |
| 95 | |
| 96 | // Detach from old track. |
| 97 | track_->RemoveSink(sink_adapter_.get()); |
| 98 | track_->UnregisterObserver(this); |
| 99 | |
| 100 | // Attach to new track. |
| 101 | track_ = audio_track; |
| 102 | cached_track_enabled_ = track_->enabled(); |
| 103 | track_->RegisterObserver(this); |
| 104 | track_->AddSink(sink_adapter_.get()); |
| 105 | Reconfigure(); |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | void AudioRtpSender::Stop() { |
| 110 | // TODO(deadbeef): Need to do more here to fully stop sending packets. |
| 111 | if (!provider_) { |
| 112 | return; |
| 113 | } |
| 114 | cricket::AudioOptions options; |
| 115 | provider_->SetAudioSend(ssrc_, false, options, nullptr); |
| 116 | provider_ = nullptr; |
| 117 | } |
| 118 | |
| 119 | void AudioRtpSender::Reconfigure() { |
| 120 | if (!provider_) { |
| 121 | return; |
| 122 | } |
| 123 | cricket::AudioOptions options; |
| 124 | if (track_->enabled() && track_->GetSource()) { |
| 125 | // TODO(xians): Remove this static_cast since we should be able to connect |
| 126 | // a remote audio track to peer connection. |
| 127 | options = static_cast<LocalAudioSource*>(track_->GetSource())->options(); |
| 128 | } |
| 129 | |
| 130 | // Use the renderer if the audio track has one, otherwise use the sink |
| 131 | // adapter owned by this class. |
| 132 | cricket::AudioRenderer* renderer = |
| 133 | track_->GetRenderer() ? track_->GetRenderer() : sink_adapter_.get(); |
| 134 | ASSERT(renderer != nullptr); |
| 135 | provider_->SetAudioSend(ssrc_, track_->enabled(), options, renderer); |
| 136 | } |
| 137 | |
| 138 | VideoRtpSender::VideoRtpSender(VideoTrackInterface* track, |
| 139 | uint32 ssrc, |
| 140 | VideoProviderInterface* provider) |
| 141 | : id_(track->id()), |
| 142 | track_(track), |
| 143 | ssrc_(ssrc), |
| 144 | provider_(provider), |
| 145 | cached_track_enabled_(track->enabled()) { |
| 146 | track_->RegisterObserver(this); |
| 147 | VideoSourceInterface* source = track_->GetSource(); |
| 148 | if (source) { |
| 149 | provider_->SetCaptureDevice(ssrc_, source->GetVideoCapturer()); |
| 150 | } |
| 151 | Reconfigure(); |
| 152 | } |
| 153 | |
| 154 | VideoRtpSender::~VideoRtpSender() { |
| 155 | track_->UnregisterObserver(this); |
| 156 | Stop(); |
| 157 | } |
| 158 | |
| 159 | void VideoRtpSender::OnChanged() { |
| 160 | if (cached_track_enabled_ != track_->enabled()) { |
| 161 | cached_track_enabled_ = track_->enabled(); |
| 162 | Reconfigure(); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | bool VideoRtpSender::SetTrack(MediaStreamTrackInterface* track) { |
| 167 | if (track->kind() != "video") { |
| 168 | LOG(LS_ERROR) << "SetTrack called on video RtpSender with " << track->kind() |
| 169 | << " track."; |
| 170 | return false; |
| 171 | } |
| 172 | VideoTrackInterface* video_track = static_cast<VideoTrackInterface*>(track); |
| 173 | |
| 174 | // Detach from old track. |
| 175 | track_->UnregisterObserver(this); |
| 176 | |
| 177 | // Attach to new track. |
| 178 | track_ = video_track; |
| 179 | cached_track_enabled_ = track_->enabled(); |
| 180 | track_->RegisterObserver(this); |
| 181 | Reconfigure(); |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | void VideoRtpSender::Stop() { |
| 186 | // TODO(deadbeef): Need to do more here to fully stop sending packets. |
| 187 | if (!provider_) { |
| 188 | return; |
| 189 | } |
| 190 | provider_->SetCaptureDevice(ssrc_, nullptr); |
| 191 | provider_->SetVideoSend(ssrc_, false, nullptr); |
| 192 | provider_ = nullptr; |
| 193 | } |
| 194 | |
| 195 | void VideoRtpSender::Reconfigure() { |
| 196 | if (!provider_) { |
| 197 | return; |
| 198 | } |
| 199 | const cricket::VideoOptions* options = nullptr; |
| 200 | VideoSourceInterface* source = track_->GetSource(); |
| 201 | if (track_->enabled() && source) { |
| 202 | options = source->options(); |
| 203 | } |
| 204 | provider_->SetVideoSend(ssrc_, track_->enabled(), options); |
| 205 | } |
| 206 | |
| 207 | } // namespace webrtc |