Skip to content

Commit

Permalink
Cleaned up the WebRtcAudioCapturer a bit.
Browse files Browse the repository at this point in the history
This CL cleaned up the capturer a bit by moving the initialize() method to private and do NOT allow re-initializing the capturer when a new getUserMedia is called.
Instead, it creates a new capturer for each getUserMedia.

BUG=333285
TEST=content_unittests

Review URL: https://codereview.chromium.org/133903004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@245205 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
xians@chromium.org committed Jan 16, 2014
1 parent 9f0dcd4 commit 8f53b27
Show file tree
Hide file tree
Showing 15 changed files with 352 additions and 322 deletions.
12 changes: 12 additions & 0 deletions content/browser/media/webrtc_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,18 @@ IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, GetAudioAndVideoStreamAndClone) {
ExpectTitle("OK");
}

IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, TwoGetUserMediaAndStop) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());

GURL url(embedded_test_server()->GetURL("/media/getusermedia.html"));
NavigateToURL(shell(), url);

ASSERT_TRUE(ExecuteJavascript(
"twoGetUserMediaAndStop({video: true, audio: true});"));

ExpectTitle("OK");
}

IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, GetUserMediaWithMandatorySourceID_1) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());

Expand Down
48 changes: 7 additions & 41 deletions content/renderer/media/media_stream_dependency_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,7 @@ void MediaStreamDependencyFactory::CreateNativeMediaSources(
}

scoped_refptr<WebRtcAudioCapturer> capturer(
MaybeCreateAudioCapturer(render_view_id, device_info,
audio_constraints));
CreateAudioCapturer(render_view_id, device_info, audio_constraints));
if (!capturer.get()) {
DLOG(WARNING) << "Failed to create the capturer for device "
<< device_info.device.id;
Expand Down Expand Up @@ -420,14 +419,9 @@ MediaStreamDependencyFactory::CreateNativeAudioMediaStreamTrack(
}
}

std::string track_id = base::UTF16ToUTF8(track.id());
scoped_refptr<WebRtcAudioCapturer> capturer;
if (GetWebRtcAudioDevice())
capturer = GetWebRtcAudioDevice()->GetDefaultCapturer();

scoped_refptr<webrtc::AudioTrackInterface> audio_track(
CreateLocalAudioTrack(track_id,
capturer,
CreateLocalAudioTrack(track.id().utf8(),
source_data->GetAudioCapturer(),
webaudio_source.get(),
source_data->local_audio_source()));
AddNativeTrackToBlinkTrack(audio_track.get(), track, true);
Expand Down Expand Up @@ -894,45 +888,17 @@ void MediaStreamDependencyFactory::CleanupPeerConnectionFactory() {
}

scoped_refptr<WebRtcAudioCapturer>
MediaStreamDependencyFactory::MaybeCreateAudioCapturer(
MediaStreamDependencyFactory::CreateAudioCapturer(
int render_view_id,
const StreamDeviceInfo& device_info,
const blink::WebMediaConstraints& constraints) {
// TODO(xians): Handle the cases when gUM is called without a proper render
// view, for example, by an extension.
DCHECK_GE(render_view_id, 0);

scoped_refptr<WebRtcAudioCapturer> capturer =
GetWebRtcAudioDevice()->GetDefaultCapturer();

// If the default capturer does not exist or |render_view_id| == -1, create
// a new capturer.
bool is_new_capturer = false;
if (!capturer.get()) {
capturer = WebRtcAudioCapturer::CreateCapturer();
is_new_capturer = true;
}

if (!capturer->Initialize(
render_view_id,
static_cast<media::ChannelLayout>(
device_info.device.input.channel_layout),
device_info.device.input.sample_rate,
device_info.device.input.frames_per_buffer,
device_info.session_id,
device_info.device.id,
device_info.device.matched_output.sample_rate,
device_info.device.matched_output.frames_per_buffer,
device_info.device.input.effects,
constraints)) {
return NULL;
}

// Add the capturer to the WebRtcAudioDeviceImpl if it is a new capturer.
if (is_new_capturer)
GetWebRtcAudioDevice()->AddAudioCapturer(capturer);

return capturer;
return WebRtcAudioCapturer::CreateCapturer(render_view_id, device_info,
constraints,
GetWebRtcAudioDevice());
}

void MediaStreamDependencyFactory::AddNativeTrackToBlinkTrack(
Expand Down
2 changes: 1 addition & 1 deletion content/renderer/media/media_stream_dependency_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class CONTENT_EXPORT MediaStreamDependencyFactory
// Returns a new capturer or existing capturer based on the |render_view_id|
// and |device_info|. When the |render_view_id| and |device_info| are valid,
// it reuses existing capture if any; otherwise it creates a new capturer.
virtual scoped_refptr<WebRtcAudioCapturer> MaybeCreateAudioCapturer(
virtual scoped_refptr<WebRtcAudioCapturer> CreateAudioCapturer(
int render_view_id, const StreamDeviceInfo& device_info,
const blink::WebMediaConstraints& constraints);

Expand Down
13 changes: 3 additions & 10 deletions content/renderer/media/media_stream_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,8 @@ void MediaStreamImpl::StopLocalSource(
<< "{device_id = " << extra_data->device_info().device.id << "})";

if (source.type() == blink::WebMediaStreamSource::TypeAudio) {
if (extra_data->GetAudioCapturer()) {
if (extra_data->GetAudioCapturer())
extra_data->GetAudioCapturer()->Stop();
}
}

if (notify_dispatcher)
Expand Down Expand Up @@ -801,19 +800,13 @@ bool MediaStreamImpl::GetAuthorizedDeviceInfoForAudioRenderer(
int* output_sample_rate,
int* output_frames_per_buffer) {
DCHECK(CalledOnValidThread());

WebRtcAudioDeviceImpl* audio_device =
dependency_factory_->GetWebRtcAudioDevice();
if (!audio_device)
return false;

if (!audio_device->GetDefaultCapturer())
return false;

return audio_device->GetDefaultCapturer()->GetPairedOutputParameters(
session_id,
output_sample_rate,
output_frames_per_buffer);
return audio_device->GetAuthorizedDeviceInfoForAudioRenderer(
session_id, output_sample_rate, output_frames_per_buffer);
}

MediaStreamSourceExtraData::MediaStreamSourceExtraData(
Expand Down
12 changes: 8 additions & 4 deletions content/renderer/media/mock_media_stream_dependency_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,12 @@ MockMediaStreamDependencyFactory::CreateLocalAudioTrack(
WebAudioCapturerSource* webaudio_source,
webrtc::AudioSourceInterface* source) {
DCHECK(mock_pc_factory_created_);
DCHECK(!capturer.get());
blink::WebMediaConstraints constraints;
scoped_refptr<WebRtcAudioCapturer> audio_capturer = capturer ?
capturer : WebRtcAudioCapturer::CreateCapturer(-1, StreamDeviceInfo(),
constraints, NULL);
return WebRtcLocalAudioTrack::Create(
id, WebRtcAudioCapturer::CreateCapturer(), webaudio_source, source);
id, audio_capturer, webaudio_source, source);
}

SessionDescriptionInterface*
Expand All @@ -502,10 +505,11 @@ MockMediaStreamDependencyFactory::CreateIceCandidate(
}

scoped_refptr<WebRtcAudioCapturer>
MockMediaStreamDependencyFactory::MaybeCreateAudioCapturer(
MockMediaStreamDependencyFactory::CreateAudioCapturer(
int render_view_id, const StreamDeviceInfo& device_info,
const blink::WebMediaConstraints& constraints) {
return WebRtcAudioCapturer::CreateCapturer();
return WebRtcAudioCapturer::CreateCapturer(-1, device_info,
constraints, NULL);
}

} // namespace content
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class MockMediaStreamDependencyFactory : public MediaStreamDependencyFactory {
virtual bool EnsurePeerConnectionFactory() OVERRIDE;
virtual bool PeerConnectionFactoryCreated() OVERRIDE;

virtual scoped_refptr<WebRtcAudioCapturer> MaybeCreateAudioCapturer(
virtual scoped_refptr<WebRtcAudioCapturer> CreateAudioCapturer(
int render_view_id, const StreamDeviceInfo& device_info,
const blink::WebMediaConstraints& constraints) OVERRIDE;

Expand Down
18 changes: 11 additions & 7 deletions content/renderer/media/rtc_peer_connection_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "base/strings/utf_string_conversions.h"
#include "content/public/common/content_switches.h"
#include "content/renderer/media/media_stream_dependency_factory.h"
#include "content/renderer/media/media_stream_source_extra_data.h"
#include "content/renderer/media/peer_connection_tracker.h"
#include "content/renderer/media/remote_media_stream_impl.h"
#include "content/renderer/media/rtc_data_channel_handler.h"
Expand Down Expand Up @@ -549,13 +550,16 @@ bool RTCPeerConnectionHandler::addStream(
this, stream, PeerConnectionTracker::SOURCE_LOCAL);

// A media stream is connected to a peer connection, enable the
// peer connection mode for the capturer.
WebRtcAudioDeviceImpl* audio_device =
dependency_factory_->GetWebRtcAudioDevice();
if (audio_device) {
WebRtcAudioCapturer* capturer = audio_device->GetDefaultCapturer();
if (capturer)
capturer->EnablePeerConnectionMode();
// peer connection mode for the sources.
blink::WebVector<blink::WebMediaStreamTrack> audio_tracks;
stream.audioTracks(audio_tracks);
for (size_t i = 0; i < audio_tracks.size(); ++i) {
const blink::WebMediaStreamSource& source = audio_tracks[i].source();
MediaStreamSourceExtraData* extra_data =
static_cast<MediaStreamSourceExtraData*>(source.extraData());
// |extra_data| is NULL if the track is a remote audio track.
if (extra_data && extra_data->GetAudioCapturer())
extra_data->GetAudioCapturer()->EnablePeerConnectionMode();
}

return AddStream(stream, &constraints);
Expand Down
Loading

0 comments on commit 8f53b27

Please sign in to comment.