1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/media/webrtc/webrtc_media_stream_adapter.h"
7 #include "base/logging.h"
8 #include "content/renderer/media/media_stream_audio_source.h"
9 #include "content/renderer/media/media_stream_track.h"
10 #include "content/renderer/media/webrtc/media_stream_video_webrtc_sink.h"
11 #include "content/renderer/media/webrtc/peer_connection_dependency_factory.h"
12 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
13 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
14 #include "third_party/WebKit/public/platform/WebString.h"
18 WebRtcMediaStreamAdapter::WebRtcMediaStreamAdapter(
19 const blink::WebMediaStream
& web_stream
,
20 PeerConnectionDependencyFactory
* factory
)
21 : web_stream_(web_stream
),
23 webrtc_media_stream_
=
24 factory_
->CreateLocalMediaStream(web_stream
.id().utf8());
26 blink::WebVector
<blink::WebMediaStreamTrack
> audio_tracks
;
27 web_stream_
.audioTracks(audio_tracks
);
28 for (blink::WebMediaStreamTrack
& audio_track
: audio_tracks
)
29 CreateAudioTrack(audio_track
);
31 blink::WebVector
<blink::WebMediaStreamTrack
> video_tracks
;
32 web_stream_
.videoTracks(video_tracks
);
33 for (blink::WebMediaStreamTrack
& video_track
: video_tracks
)
34 CreateVideoTrack(video_track
);
36 MediaStream
* const native_stream
= MediaStream::GetMediaStream(web_stream_
);
37 native_stream
->AddObserver(this);
40 WebRtcMediaStreamAdapter::~WebRtcMediaStreamAdapter() {
41 MediaStream
* const native_stream
= MediaStream::GetMediaStream(web_stream_
);
42 native_stream
->RemoveObserver(this);
45 void WebRtcMediaStreamAdapter::TrackAdded(
46 const blink::WebMediaStreamTrack
& track
) {
47 if (track
.source().type() == blink::WebMediaStreamSource::TypeAudio
)
48 CreateAudioTrack(track
);
50 CreateVideoTrack(track
);
53 void WebRtcMediaStreamAdapter::TrackRemoved(
54 const blink::WebMediaStreamTrack
& track
) {
55 const std::string track_id
= track
.id().utf8();
56 if (track
.source().type() == blink::WebMediaStreamSource::TypeAudio
) {
57 webrtc_media_stream_
->RemoveTrack(
58 webrtc_media_stream_
->FindAudioTrack(track_id
));
60 DCHECK_EQ(track
.source().type(), blink::WebMediaStreamSource::TypeVideo
);
61 scoped_refptr
<webrtc::VideoTrackInterface
> webrtc_track
=
62 webrtc_media_stream_
->FindVideoTrack(track_id
).get();
63 webrtc_media_stream_
->RemoveTrack(webrtc_track
.get());
65 for (ScopedVector
<MediaStreamVideoWebRtcSink
>::iterator it
=
66 video_adapters_
.begin(); it
!= video_adapters_
.end(); ++it
) {
67 if ((*it
)->webrtc_video_track() == webrtc_track
.get()) {
68 video_adapters_
.erase(it
);
75 void WebRtcMediaStreamAdapter::CreateAudioTrack(
76 const blink::WebMediaStreamTrack
& track
) {
77 DCHECK_EQ(track
.source().type(), blink::WebMediaStreamSource::TypeAudio
);
78 // A media stream is connected to a peer connection, enable the
79 // peer connection mode for the sources.
80 MediaStreamTrack
* native_track
= MediaStreamTrack::GetTrack(track
);
81 if (!native_track
|| !native_track
->is_local_track()) {
82 // We don't support connecting remote audio tracks to PeerConnection yet.
83 // See issue http://crbug/344303.
84 // TODO(xians): Remove this after we support connecting remote audio track
86 NOTIMPLEMENTED() << "webrtc audio track can not be created from a remote "
91 // This is a local audio track.
92 const blink::WebMediaStreamSource
& source
= track
.source();
93 MediaStreamAudioSource
* audio_source
=
94 static_cast<MediaStreamAudioSource
*>(source
.extraData());
95 if (audio_source
&& audio_source
->GetAudioCapturer().get())
96 audio_source
->GetAudioCapturer()->EnablePeerConnectionMode();
98 webrtc_media_stream_
->AddTrack(native_track
->GetAudioAdapter());
101 void WebRtcMediaStreamAdapter::CreateVideoTrack(
102 const blink::WebMediaStreamTrack
& track
) {
103 DCHECK_EQ(track
.source().type(), blink::WebMediaStreamSource::TypeVideo
);
104 MediaStreamVideoWebRtcSink
* adapter
=
105 new MediaStreamVideoWebRtcSink(track
, factory_
);
106 video_adapters_
.push_back(adapter
);
107 webrtc_media_stream_
->AddTrack(adapter
->webrtc_video_track());
110 } // namespace content