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