OLD | NEW |
(Empty) | |
| 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 |
| 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 if (provider_) { |
| 79 Stop(); |
| 80 } |
| 81 } |
| 82 |
| 83 void AudioRtpSender::OnChanged() { |
| 84 if (cached_track_enabled_ != track_->enabled()) { |
| 85 cached_track_enabled_ = track_->enabled(); |
| 86 Reconfigure(); |
| 87 } |
| 88 } |
| 89 |
| 90 bool AudioRtpSender::SetTrack(MediaStreamTrackInterface* track) { |
| 91 if (track->kind() != "audio") { |
| 92 LOG(LS_ERROR) << "SetTrack called on audio RtpSender with " << track->kind() |
| 93 << " track."; |
| 94 return false; |
| 95 } |
| 96 AudioTrackInterface* audio_track = static_cast<AudioTrackInterface*>(track); |
| 97 |
| 98 // Detach from old track. |
| 99 track_->RemoveSink(sink_adapter_.get()); |
| 100 track_->UnregisterObserver(this); |
| 101 |
| 102 // Attach to new track. |
| 103 track_ = audio_track; |
| 104 cached_track_enabled_ = track_->enabled(); |
| 105 track_->RegisterObserver(this); |
| 106 track_->AddSink(sink_adapter_.get()); |
| 107 Reconfigure(); |
| 108 return true; |
| 109 } |
| 110 |
| 111 void AudioRtpSender::Stop() { |
| 112 // TODO(deadbeef): Need to do more here to fully stop sending packets. |
| 113 cricket::AudioOptions options; |
| 114 provider_->SetAudioSend(ssrc_, false, options, nullptr); |
| 115 provider_ = nullptr; |
| 116 } |
| 117 |
| 118 void AudioRtpSender::Reconfigure() { |
| 119 if (!provider_) { |
| 120 return; |
| 121 } |
| 122 cricket::AudioOptions options; |
| 123 if (track_->enabled() && track_->GetSource()) { |
| 124 // TODO(xians): Remove this static_cast since we should be able to connect |
| 125 // a remote audio track to peer connection. |
| 126 options = static_cast<LocalAudioSource*>(track_->GetSource())->options(); |
| 127 } |
| 128 |
| 129 // Use the renderer if the audio track has one, otherwise use the sink |
| 130 // adapter owned by this class. |
| 131 cricket::AudioRenderer* renderer = |
| 132 track_->GetRenderer() ? track_->GetRenderer() : sink_adapter_.get(); |
| 133 ASSERT(renderer != nullptr); |
| 134 provider_->SetAudioSend(ssrc_, track_->enabled(), options, renderer); |
| 135 } |
| 136 |
| 137 VideoRtpSender::VideoRtpSender(VideoTrackInterface* track, |
| 138 uint32 ssrc, |
| 139 VideoProviderInterface* provider) |
| 140 : id_(track->id()), |
| 141 track_(track), |
| 142 ssrc_(ssrc), |
| 143 provider_(provider), |
| 144 cached_track_enabled_(track->enabled()) { |
| 145 track_->RegisterObserver(this); |
| 146 VideoSourceInterface* source = track_->GetSource(); |
| 147 if (source) { |
| 148 provider_->SetCaptureDevice(ssrc_, source->GetVideoCapturer()); |
| 149 } |
| 150 Reconfigure(); |
| 151 } |
| 152 |
| 153 VideoRtpSender::~VideoRtpSender() { |
| 154 track_->UnregisterObserver(this); |
| 155 if (provider_) { |
| 156 Stop(); |
| 157 } |
| 158 } |
| 159 |
| 160 void VideoRtpSender::OnChanged() { |
| 161 if (cached_track_enabled_ != track_->enabled()) { |
| 162 cached_track_enabled_ = track_->enabled(); |
| 163 Reconfigure(); |
| 164 } |
| 165 } |
| 166 |
| 167 bool VideoRtpSender::SetTrack(MediaStreamTrackInterface* track) { |
| 168 if (track->kind() != "video") { |
| 169 LOG(LS_ERROR) << "SetTrack called on video RtpSender with " << track->kind() |
| 170 << " track."; |
| 171 return false; |
| 172 } |
| 173 VideoTrackInterface* video_track = static_cast<VideoTrackInterface*>(track); |
| 174 |
| 175 // Detach from old track. |
| 176 track_->UnregisterObserver(this); |
| 177 |
| 178 // Attach to new track. |
| 179 track_ = video_track; |
| 180 cached_track_enabled_ = track_->enabled(); |
| 181 track_->RegisterObserver(this); |
| 182 Reconfigure(); |
| 183 return true; |
| 184 } |
| 185 |
| 186 void VideoRtpSender::Stop() { |
| 187 // TODO(deadbeef): Need to do more here to fully stop sending packets. |
| 188 provider_->SetCaptureDevice(ssrc_, nullptr); |
| 189 provider_->SetVideoSend(ssrc_, false, nullptr); |
| 190 provider_ = nullptr; |
| 191 } |
| 192 |
| 193 void VideoRtpSender::Reconfigure() { |
| 194 if (!provider_) { |
| 195 return; |
| 196 } |
| 197 const cricket::VideoOptions* options = nullptr; |
| 198 VideoSourceInterface* source = track_->GetSource(); |
| 199 if (track_->enabled() && source) { |
| 200 options = source->options(); |
| 201 } |
| 202 provider_->SetVideoSend(ssrc_, track_->enabled(), options); |
| 203 } |
| 204 |
| 205 } // namespace webrtc |
OLD | NEW |