Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / app / webrtc / mediastreamhandler.cc
index b09af78..ca8e105 100644 (file)
@@ -56,14 +56,38 @@ void TrackHandler::OnChanged() {
   }
 }
 
+LocalAudioSinkAdapter::LocalAudioSinkAdapter() : sink_(NULL) {}
+
+LocalAudioSinkAdapter::~LocalAudioSinkAdapter() {}
+
+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) {
+      provider_(provider),
+      sink_adapter_(new LocalAudioSinkAdapter()) {
   OnEnabledChanged();
+  track->AddSink(sink_adapter_.get());
 }
 
 LocalAudioTrackHandler::~LocalAudioTrackHandler() {
@@ -74,6 +98,7 @@ void LocalAudioTrackHandler::OnStateChanged() {
 }
 
 void LocalAudioTrackHandler::Stop() {
+  audio_track_->RemoveSink(sink_adapter_.get());
   cricket::AudioOptions options;
   provider_->SetAudioSend(ssrc(), false, options, NULL);
 }
@@ -81,11 +106,18 @@ void LocalAudioTrackHandler::Stop() {
 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();
   }
-  provider_->SetAudioSend(ssrc(), audio_track_->enabled(), options,
-                          audio_track_->GetRenderer());
+
+  // 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(
@@ -95,10 +127,12 @@ RemoteAudioTrackHandler::RemoteAudioTrackHandler(
     : TrackHandler(track, ssrc),
       audio_track_(track),
       provider_(provider) {
+  track->GetSource()->RegisterAudioObserver(this);
   OnEnabledChanged();
 }
 
 RemoteAudioTrackHandler::~RemoteAudioTrackHandler() {
+  audio_track_->GetSource()->UnregisterAudioObserver(this);
 }
 
 void RemoteAudioTrackHandler::Stop() {
@@ -113,6 +147,14 @@ void RemoteAudioTrackHandler::OnEnabledChanged() {
                              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,