1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
|
/*
* libjingle
* Copyright 2012, Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "talk/app/webrtc/mediastreamhandler.h"
#include "talk/app/webrtc/localaudiosource.h"
#include "talk/app/webrtc/videosource.h"
#include "talk/app/webrtc/videosourceinterface.h"
namespace webrtc {
TrackHandler::TrackHandler(MediaStreamTrackInterface* track, uint32 ssrc)
: track_(track),
ssrc_(ssrc),
state_(track->state()),
enabled_(track->enabled()) {
track_->RegisterObserver(this);
}
TrackHandler::~TrackHandler() {
track_->UnregisterObserver(this);
}
void TrackHandler::OnChanged() {
if (state_ != track_->state()) {
state_ = track_->state();
OnStateChanged();
}
if (enabled_ != track_->enabled()) {
enabled_ = track_->enabled();
OnEnabledChanged();
}
}
LocalAudioSinkAdapter::LocalAudioSinkAdapter() : sink_(NULL) {}
LocalAudioSinkAdapter::~LocalAudioSinkAdapter() {
talk_base::CritScope lock(&lock_);
if (sink_)
sink_->OnClose();
}
void LocalAudioSinkAdapter::OnData(const void* audio_data,
int bits_per_sample,
int sample_rate,
int number_of_channels,
int number_of_frames) {
talk_base::CritScope lock(&lock_);
if (sink_) {
sink_->OnData(audio_data, bits_per_sample, sample_rate,
number_of_channels, number_of_frames);
}
}
void LocalAudioSinkAdapter::SetSink(cricket::AudioRenderer::Sink* sink) {
talk_base::CritScope lock(&lock_);
ASSERT(!sink || !sink_);
sink_ = sink;
}
LocalAudioTrackHandler::LocalAudioTrackHandler(
AudioTrackInterface* track,
uint32 ssrc,
AudioProviderInterface* provider)
: TrackHandler(track, ssrc),
audio_track_(track),
provider_(provider),
sink_adapter_(new LocalAudioSinkAdapter()) {
OnEnabledChanged();
track->AddSink(sink_adapter_.get());
}
LocalAudioTrackHandler::~LocalAudioTrackHandler() {
}
void LocalAudioTrackHandler::OnStateChanged() {
// TODO(perkj): What should happen when the state change?
}
void LocalAudioTrackHandler::Stop() {
audio_track_->RemoveSink(sink_adapter_.get());
cricket::AudioOptions options;
provider_->SetAudioSend(ssrc(), false, options, NULL);
}
void LocalAudioTrackHandler::OnEnabledChanged() {
cricket::AudioOptions options;
if (audio_track_->enabled() && audio_track_->GetSource()) {
// TODO(xians): Remove this static_cast since we should be able to connect
// a remote audio track to peer connection.
options = static_cast<LocalAudioSource*>(
audio_track_->GetSource())->options();
}
// Use the renderer if the audio track has one, otherwise use the sink
// adapter owned by this class.
cricket::AudioRenderer* renderer = audio_track_->GetRenderer() ?
audio_track_->GetRenderer() : sink_adapter_.get();
ASSERT(renderer != NULL);
provider_->SetAudioSend(ssrc(), audio_track_->enabled(), options, renderer);
}
RemoteAudioTrackHandler::RemoteAudioTrackHandler(
AudioTrackInterface* track,
uint32 ssrc,
AudioProviderInterface* provider)
: TrackHandler(track, ssrc),
audio_track_(track),
provider_(provider) {
track->GetSource()->RegisterAudioObserver(this);
OnEnabledChanged();
}
RemoteAudioTrackHandler::~RemoteAudioTrackHandler() {
audio_track_->GetSource()->UnregisterAudioObserver(this);
}
void RemoteAudioTrackHandler::Stop() {
provider_->SetAudioPlayout(ssrc(), false, NULL);
}
void RemoteAudioTrackHandler::OnStateChanged() {
}
void RemoteAudioTrackHandler::OnEnabledChanged() {
provider_->SetAudioPlayout(ssrc(), audio_track_->enabled(),
audio_track_->GetRenderer());
}
void RemoteAudioTrackHandler::OnSetVolume(double volume) {
// When the track is disabled, the volume of the source, which is the
// corresponding WebRtc Voice Engine channel will be 0. So we do not allow
// setting the volume to the source when the track is disabled.
if (audio_track_->enabled())
provider_->SetAudioPlayoutVolume(ssrc(), volume);
}
LocalVideoTrackHandler::LocalVideoTrackHandler(
VideoTrackInterface* track,
uint32 ssrc,
VideoProviderInterface* provider)
: TrackHandler(track, ssrc),
local_video_track_(track),
provider_(provider) {
VideoSourceInterface* source = local_video_track_->GetSource();
if (source)
provider_->SetCaptureDevice(ssrc, source->GetVideoCapturer());
OnEnabledChanged();
}
LocalVideoTrackHandler::~LocalVideoTrackHandler() {
}
void LocalVideoTrackHandler::OnStateChanged() {
}
void LocalVideoTrackHandler::Stop() {
provider_->SetCaptureDevice(ssrc(), NULL);
provider_->SetVideoSend(ssrc(), false, NULL);
}
void LocalVideoTrackHandler::OnEnabledChanged() {
const cricket::VideoOptions* options = NULL;
VideoSourceInterface* source = local_video_track_->GetSource();
if (local_video_track_->enabled() && source) {
options = source->options();
}
provider_->SetVideoSend(ssrc(), local_video_track_->enabled(), options);
}
RemoteVideoTrackHandler::RemoteVideoTrackHandler(
VideoTrackInterface* track,
uint32 ssrc,
VideoProviderInterface* provider)
: TrackHandler(track, ssrc),
remote_video_track_(track),
provider_(provider) {
OnEnabledChanged();
provider_->SetVideoPlayout(ssrc, true,
remote_video_track_->GetSource()->FrameInput());
}
RemoteVideoTrackHandler::~RemoteVideoTrackHandler() {
}
void RemoteVideoTrackHandler::Stop() {
// Since cricket::VideoRenderer is not reference counted
// we need to remove the renderer before we are deleted.
provider_->SetVideoPlayout(ssrc(), false, NULL);
}
void RemoteVideoTrackHandler::OnStateChanged() {
}
void RemoteVideoTrackHandler::OnEnabledChanged() {
}
MediaStreamHandler::MediaStreamHandler(MediaStreamInterface* stream,
AudioProviderInterface* audio_provider,
VideoProviderInterface* video_provider)
: stream_(stream),
audio_provider_(audio_provider),
video_provider_(video_provider) {
}
MediaStreamHandler::~MediaStreamHandler() {
for (TrackHandlers::iterator it = track_handlers_.begin();
it != track_handlers_.end(); ++it) {
delete *it;
}
}
void MediaStreamHandler::RemoveTrack(MediaStreamTrackInterface* track) {
for (TrackHandlers::iterator it = track_handlers_.begin();
it != track_handlers_.end(); ++it) {
if ((*it)->track() == track) {
TrackHandler* track = *it;
track->Stop();
delete track;
track_handlers_.erase(it);
break;
}
}
}
TrackHandler* MediaStreamHandler::FindTrackHandler(
MediaStreamTrackInterface* track) {
TrackHandlers::iterator it = track_handlers_.begin();
for (; it != track_handlers_.end(); ++it) {
if ((*it)->track() == track) {
return *it;
break;
}
}
return NULL;
}
MediaStreamInterface* MediaStreamHandler::stream() {
return stream_.get();
}
void MediaStreamHandler::OnChanged() {
}
void MediaStreamHandler::Stop() {
for (TrackHandlers::const_iterator it = track_handlers_.begin();
it != track_handlers_.end(); ++it) {
(*it)->Stop();
}
}
LocalMediaStreamHandler::LocalMediaStreamHandler(
MediaStreamInterface* stream,
AudioProviderInterface* audio_provider,
VideoProviderInterface* video_provider)
: MediaStreamHandler(stream, audio_provider, video_provider) {
}
LocalMediaStreamHandler::~LocalMediaStreamHandler() {
}
void LocalMediaStreamHandler::AddAudioTrack(AudioTrackInterface* audio_track,
uint32 ssrc) {
ASSERT(!FindTrackHandler(audio_track));
TrackHandler* handler(new LocalAudioTrackHandler(audio_track, ssrc,
audio_provider_));
track_handlers_.push_back(handler);
}
void LocalMediaStreamHandler::AddVideoTrack(VideoTrackInterface* video_track,
uint32 ssrc) {
ASSERT(!FindTrackHandler(video_track));
TrackHandler* handler(new LocalVideoTrackHandler(video_track, ssrc,
video_provider_));
track_handlers_.push_back(handler);
}
RemoteMediaStreamHandler::RemoteMediaStreamHandler(
MediaStreamInterface* stream,
AudioProviderInterface* audio_provider,
VideoProviderInterface* video_provider)
: MediaStreamHandler(stream, audio_provider, video_provider) {
}
RemoteMediaStreamHandler::~RemoteMediaStreamHandler() {
}
void RemoteMediaStreamHandler::AddAudioTrack(AudioTrackInterface* audio_track,
uint32 ssrc) {
ASSERT(!FindTrackHandler(audio_track));
TrackHandler* handler(
new RemoteAudioTrackHandler(audio_track, ssrc, audio_provider_));
track_handlers_.push_back(handler);
}
void RemoteMediaStreamHandler::AddVideoTrack(VideoTrackInterface* video_track,
uint32 ssrc) {
ASSERT(!FindTrackHandler(video_track));
TrackHandler* handler(
new RemoteVideoTrackHandler(video_track, ssrc, video_provider_));
track_handlers_.push_back(handler);
}
MediaStreamHandlerContainer::MediaStreamHandlerContainer(
AudioProviderInterface* audio_provider,
VideoProviderInterface* video_provider)
: audio_provider_(audio_provider),
video_provider_(video_provider) {
}
MediaStreamHandlerContainer::~MediaStreamHandlerContainer() {
ASSERT(remote_streams_handlers_.empty());
ASSERT(local_streams_handlers_.empty());
}
void MediaStreamHandlerContainer::TearDown() {
for (StreamHandlerList::iterator it = remote_streams_handlers_.begin();
it != remote_streams_handlers_.end(); ++it) {
(*it)->Stop();
delete *it;
}
remote_streams_handlers_.clear();
for (StreamHandlerList::iterator it = local_streams_handlers_.begin();
it != local_streams_handlers_.end(); ++it) {
(*it)->Stop();
delete *it;
}
local_streams_handlers_.clear();
}
void MediaStreamHandlerContainer::RemoveRemoteStream(
MediaStreamInterface* stream) {
DeleteStreamHandler(&remote_streams_handlers_, stream);
}
void MediaStreamHandlerContainer::AddRemoteAudioTrack(
MediaStreamInterface* stream,
AudioTrackInterface* audio_track,
uint32 ssrc) {
MediaStreamHandler* handler = FindStreamHandler(remote_streams_handlers_,
stream);
if (handler == NULL) {
handler = CreateRemoteStreamHandler(stream);
}
handler->AddAudioTrack(audio_track, ssrc);
}
void MediaStreamHandlerContainer::AddRemoteVideoTrack(
MediaStreamInterface* stream,
VideoTrackInterface* video_track,
uint32 ssrc) {
MediaStreamHandler* handler = FindStreamHandler(remote_streams_handlers_,
stream);
if (handler == NULL) {
handler = CreateRemoteStreamHandler(stream);
}
handler->AddVideoTrack(video_track, ssrc);
}
void MediaStreamHandlerContainer::RemoveRemoteTrack(
MediaStreamInterface* stream,
MediaStreamTrackInterface* track) {
MediaStreamHandler* handler = FindStreamHandler(remote_streams_handlers_,
stream);
if (!VERIFY(handler != NULL)) {
LOG(LS_WARNING) << "Local MediaStreamHandler for stream with id "
<< stream->label() << "doesnt't exist.";
return;
}
handler->RemoveTrack(track);
}
void MediaStreamHandlerContainer::RemoveLocalStream(
MediaStreamInterface* stream) {
DeleteStreamHandler(&local_streams_handlers_, stream);
}
void MediaStreamHandlerContainer::AddLocalAudioTrack(
MediaStreamInterface* stream,
AudioTrackInterface* audio_track,
uint32 ssrc) {
MediaStreamHandler* handler = FindStreamHandler(local_streams_handlers_,
stream);
if (handler == NULL) {
handler = CreateLocalStreamHandler(stream);
}
handler->AddAudioTrack(audio_track, ssrc);
}
void MediaStreamHandlerContainer::AddLocalVideoTrack(
MediaStreamInterface* stream,
VideoTrackInterface* video_track,
uint32 ssrc) {
MediaStreamHandler* handler = FindStreamHandler(local_streams_handlers_,
stream);
if (handler == NULL) {
handler = CreateLocalStreamHandler(stream);
}
handler->AddVideoTrack(video_track, ssrc);
}
void MediaStreamHandlerContainer::RemoveLocalTrack(
MediaStreamInterface* stream,
MediaStreamTrackInterface* track) {
MediaStreamHandler* handler = FindStreamHandler(local_streams_handlers_,
stream);
if (!VERIFY(handler != NULL)) {
LOG(LS_WARNING) << "Remote MediaStreamHandler for stream with id "
<< stream->label() << "doesnt't exist.";
return;
}
handler->RemoveTrack(track);
}
MediaStreamHandler* MediaStreamHandlerContainer::CreateRemoteStreamHandler(
MediaStreamInterface* stream) {
ASSERT(!FindStreamHandler(remote_streams_handlers_, stream));
RemoteMediaStreamHandler* handler =
new RemoteMediaStreamHandler(stream, audio_provider_, video_provider_);
remote_streams_handlers_.push_back(handler);
return handler;
}
MediaStreamHandler* MediaStreamHandlerContainer::CreateLocalStreamHandler(
MediaStreamInterface* stream) {
ASSERT(!FindStreamHandler(local_streams_handlers_, stream));
LocalMediaStreamHandler* handler =
new LocalMediaStreamHandler(stream, audio_provider_, video_provider_);
local_streams_handlers_.push_back(handler);
return handler;
}
MediaStreamHandler* MediaStreamHandlerContainer::FindStreamHandler(
const StreamHandlerList& handlers,
MediaStreamInterface* stream) {
StreamHandlerList::const_iterator it = handlers.begin();
for (; it != handlers.end(); ++it) {
if ((*it)->stream() == stream) {
return *it;
}
}
return NULL;
}
void MediaStreamHandlerContainer::DeleteStreamHandler(
StreamHandlerList* streamhandlers, MediaStreamInterface* stream) {
StreamHandlerList::iterator it = streamhandlers->begin();
for (; it != streamhandlers->end(); ++it) {
if ((*it)->stream() == stream) {
(*it)->Stop();
delete *it;
streamhandlers->erase(it);
break;
}
}
}
} // namespace webrtc
|