1 // Copyright (c) 2012 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/peer_connection_handler_base.h"
7 #include "base/logging.h"
8 #include "base/utf_string_conversions.h"
9 #include "content/renderer/media/media_stream_dependency_factory.h"
10 #include "content/renderer/media/media_stream_extra_data.h"
11 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStream.h"
12 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStreamSource.h"
13 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStreamTrack.h"
14 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
18 // TODO(hta): Unify implementations of these functions from MediaStreamCenter
19 static webrtc::MediaStreamInterface
* GetNativeMediaStream(
20 const WebKit::WebMediaStream
& stream
) {
21 MediaStreamExtraData
* extra_data
=
22 static_cast<MediaStreamExtraData
*>(stream
.extraData());
24 return extra_data
->stream();
28 PeerConnectionHandlerBase::PeerConnectionHandlerBase(
29 MediaStreamDependencyFactory
* dependency_factory
)
30 : dependency_factory_(dependency_factory
),
31 message_loop_proxy_(base::MessageLoopProxy::current()) {
34 PeerConnectionHandlerBase::~PeerConnectionHandlerBase() {
37 bool PeerConnectionHandlerBase::AddStream(
38 const WebKit::WebMediaStream
& stream
,
39 const webrtc::MediaConstraintsInterface
* constraints
) {
40 webrtc::MediaStreamInterface
* native_stream
= GetNativeMediaStream(stream
);
43 return native_peer_connection_
->AddStream(native_stream
, constraints
);
46 void PeerConnectionHandlerBase::RemoveStream(
47 const WebKit::WebMediaStream
& stream
) {
48 webrtc::MediaStreamInterface
* native_stream
= GetNativeMediaStream(stream
);
50 native_peer_connection_
->RemoveStream(native_stream
);
51 DCHECK(native_stream
);
54 WebKit::WebMediaStream
55 PeerConnectionHandlerBase::CreateRemoteWebKitMediaStream(
56 webrtc::MediaStreamInterface
* stream
) {
57 webrtc::AudioTrackVector audio_tracks
= stream
->GetAudioTracks();
58 webrtc::VideoTrackVector video_tracks
= stream
->GetVideoTracks();
59 WebKit::WebVector
<WebKit::WebMediaStreamSource
> audio_source_vector(
61 WebKit::WebVector
<WebKit::WebMediaStreamSource
> video_source_vector(
66 for (; i
< audio_tracks
.size(); ++i
) {
67 webrtc::AudioTrackInterface
* audio_track
= audio_tracks
[i
];
69 audio_source_vector
[i
].initialize(
70 UTF8ToUTF16(audio_track
->id()),
71 WebKit::WebMediaStreamSource::TypeAudio
,
72 UTF8ToUTF16(audio_track
->id()));
76 for (i
= 0; i
< video_tracks
.size(); ++i
) {
77 webrtc::VideoTrackInterface
* video_track
= video_tracks
[i
];
79 video_source_vector
[i
].initialize(
80 UTF8ToUTF16(video_track
->id()),
81 WebKit::WebMediaStreamSource::TypeVideo
,
82 UTF8ToUTF16(video_track
->id()));
84 WebKit::WebMediaStream descriptor
;
85 descriptor
.initialize(UTF8ToUTF16(stream
->label()),
86 audio_source_vector
, video_source_vector
);
87 descriptor
.setExtraData(new MediaStreamExtraData(stream
, false));
91 webrtc::MediaStreamTrackInterface
*
92 PeerConnectionHandlerBase::GetNativeMediaStreamTrack(
93 const WebKit::WebMediaStream
& stream
,
94 const WebKit::WebMediaStreamTrack
& track
) {
95 std::string track_id
= UTF16ToUTF8(track
.id());
96 webrtc::MediaStreamInterface
* native_stream
= GetNativeMediaStream(stream
);
100 if (track
.source().type() == WebKit::WebMediaStreamSource::TypeAudio
) {
101 return native_stream
->FindAudioTrack(track_id
);
103 if (track
.source().type() == WebKit::WebMediaStreamSource::TypeVideo
) {
104 return native_stream
->FindVideoTrack(track_id
);
106 NOTIMPLEMENTED(); // We have an unknown type of media stream track.
110 } // namespace content