OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/media/media_stream_center.h" | 5 #include "content/renderer/media/media_stream_center.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "content/renderer/media/media_stream_impl.h" |
| 13 #include "content/renderer/media/media_stream_extra_data.h" |
| 14 #include "content/renderer/render_view_impl.h" |
12 #include "third_party/libjingle/source/talk/app/webrtc/jsep.h" | 15 #include "third_party/libjingle/source/talk/app/webrtc/jsep.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebICECandid
ateDescriptor.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebICECandid
ateDescriptor.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amCenterClient.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amCenterClient.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amComponent.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amDescriptor.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amDescriptor.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amSource.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amSource.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amSourcesRequest.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amSourcesRequest.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSessionDe
scriptionDescriptor.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSessionDe
scriptionDescriptor.h" |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" |
| 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
20 | 25 |
21 namespace content { | 26 namespace content { |
22 | 27 |
| 28 static MediaStreamImpl* GetMediaStreamImpl(WebKit::WebFrame* web_frame) { |
| 29 RenderViewImpl* render_view = RenderViewImpl::FromWebView(web_frame->view()); |
| 30 if (!render_view) |
| 31 return NULL; |
| 32 |
| 33 // TODO(perkj): Avoid this cast? |
| 34 return static_cast<MediaStreamImpl*>(render_view->userMediaClient()); |
| 35 } |
| 36 |
| 37 static webrtc::MediaStreamInterface* GetNativeMediaStream( |
| 38 const WebKit::WebMediaStreamDescriptor& stream) { |
| 39 MediaStreamExtraData* extra_data = |
| 40 static_cast<MediaStreamExtraData*>(stream.extraData()); |
| 41 if (extra_data && extra_data->remote_stream()) |
| 42 return extra_data->remote_stream(); |
| 43 |
| 44 if (extra_data && extra_data->local_stream()) |
| 45 return extra_data->local_stream(); |
| 46 |
| 47 // TODO(perkj): This can occur if a JS create a new MediaStream based on an |
| 48 // existing MediaStream. |
| 49 NOTIMPLEMENTED(); |
| 50 return NULL; |
| 51 } |
| 52 |
| 53 template <class TrackList> |
| 54 static webrtc::MediaStreamTrackInterface* GetTrack( |
| 55 const std::string& source_id, |
| 56 TrackList* tracks) { |
| 57 for (size_t i = 0; i < tracks->count(); ++i) { |
| 58 if (tracks->at(i)->label() == source_id) |
| 59 return tracks->at(i); |
| 60 } |
| 61 return NULL; |
| 62 } |
| 63 |
| 64 static webrtc::MediaStreamTrackInterface* GetNativeMediaStreamTrack( |
| 65 const WebKit::WebMediaStreamDescriptor& stream, |
| 66 const WebKit::WebMediaStreamComponent& component) { |
| 67 std::string source_id = UTF16ToUTF8(component.source().id()); |
| 68 webrtc::MediaStreamInterface* native_stream = GetNativeMediaStream(stream); |
| 69 if (native_stream) { |
| 70 if (component.source().type() == WebKit::WebMediaStreamSource::TypeAudio) { |
| 71 return GetTrack<webrtc::AudioTracks>( |
| 72 source_id, native_stream->audio_tracks()); |
| 73 } |
| 74 if (component.source().type() == WebKit::WebMediaStreamSource::TypeVideo) { |
| 75 return GetTrack<webrtc::VideoTracks>( |
| 76 source_id, native_stream->video_tracks()); |
| 77 } |
| 78 } |
| 79 // TODO(perkj): This can occur if a JS create a new MediaStream based on an |
| 80 // existing MediaStream. |
| 81 NOTIMPLEMENTED(); |
| 82 return NULL; |
| 83 } |
| 84 |
23 MediaStreamCenter::MediaStreamCenter( | 85 MediaStreamCenter::MediaStreamCenter( |
24 WebKit::WebMediaStreamCenterClient* client) | 86 WebKit::WebMediaStreamCenterClient* client) |
25 : client_(client) { | 87 : client_(client) { |
26 } | 88 } |
27 | 89 |
28 void MediaStreamCenter::queryMediaStreamSources( | 90 void MediaStreamCenter::queryMediaStreamSources( |
29 const WebKit::WebMediaStreamSourcesRequest& request) { | 91 const WebKit::WebMediaStreamSourcesRequest& request) { |
30 WebKit::WebVector<WebKit::WebMediaStreamSource> audioSources, videoSources; | 92 WebKit::WebVector<WebKit::WebMediaStreamSource> audioSources, videoSources; |
31 request.didCompleteQuery(audioSources, videoSources); | 93 request.didCompleteQuery(audioSources, videoSources); |
32 } | 94 } |
33 | 95 |
34 void MediaStreamCenter::didEnableMediaStreamTrack( | 96 void MediaStreamCenter::didEnableMediaStreamTrack( |
35 const WebKit::WebMediaStreamDescriptor& stream, | 97 const WebKit::WebMediaStreamDescriptor& stream, |
36 const WebKit::WebMediaStreamComponent& component) { | 98 const WebKit::WebMediaStreamComponent& component) { |
| 99 webrtc::MediaStreamTrackInterface* track = |
| 100 GetNativeMediaStreamTrack(stream, component); |
| 101 if (track) |
| 102 track->set_enabled(true); |
37 } | 103 } |
38 | 104 |
39 void MediaStreamCenter::didDisableMediaStreamTrack( | 105 void MediaStreamCenter::didDisableMediaStreamTrack( |
40 const WebKit::WebMediaStreamDescriptor& stream, | 106 const WebKit::WebMediaStreamDescriptor& stream, |
41 const WebKit::WebMediaStreamComponent& component) { | 107 const WebKit::WebMediaStreamComponent& component) { |
| 108 webrtc::MediaStreamTrackInterface* track = |
| 109 GetNativeMediaStreamTrack(stream, component); |
| 110 if (track) |
| 111 track->set_enabled(false); |
42 } | 112 } |
43 | 113 |
44 void MediaStreamCenter::didStopLocalMediaStream( | 114 void MediaStreamCenter::didStopLocalMediaStream( |
45 const WebKit::WebMediaStreamDescriptor& stream) { | 115 const WebKit::WebMediaStreamDescriptor& stream) { |
| 116 DVLOG(1) << "MediaStreamCenter::didStopLocalMediaStream"; |
| 117 WebKit::WebFrame* web_frame = WebKit::WebFrame::frameForCurrentContext(); |
| 118 if (!web_frame) |
| 119 return; |
| 120 MediaStreamImpl* ms_impl = GetMediaStreamImpl(web_frame); |
| 121 if (ms_impl) { |
| 122 ms_impl->StopLocalMediaStream(stream); |
| 123 return; |
| 124 } |
| 125 |
| 126 NOTREACHED(); |
46 } | 127 } |
47 | 128 |
48 void MediaStreamCenter::didConstructMediaStream( | 129 void MediaStreamCenter::didCreateMediaStream( |
49 const WebKit::WebMediaStreamDescriptor& stream) { | 130 WebKit::WebMediaStreamDescriptor& stream) { |
| 131 WebKit::WebFrame* web_frame = WebKit::WebFrame::frameForCurrentContext(); |
| 132 if (!web_frame) |
| 133 return; |
| 134 MediaStreamImpl* ms_impl = GetMediaStreamImpl(web_frame); |
| 135 if (ms_impl) { |
| 136 ms_impl->CreateMediaStream(web_frame, &stream); |
| 137 return; |
| 138 } |
| 139 NOTREACHED(); |
50 } | 140 } |
51 | 141 |
52 WebKit::WebString MediaStreamCenter::constructSDP( | 142 WebKit::WebString MediaStreamCenter::constructSDP( |
53 const WebKit::WebICECandidateDescriptor& candidate) { | 143 const WebKit::WebICECandidateDescriptor& candidate) { |
54 scoped_ptr<webrtc::IceCandidateInterface> native_candidate( | 144 scoped_ptr<webrtc::IceCandidateInterface> native_candidate( |
55 webrtc::CreateIceCandidate(UTF16ToUTF8(candidate.label()), | 145 webrtc::CreateIceCandidate(UTF16ToUTF8(candidate.label()), |
56 UTF16ToUTF8(candidate.candidateLine()))); | 146 UTF16ToUTF8(candidate.candidateLine()))); |
57 std::string sdp; | 147 std::string sdp; |
58 if (!native_candidate->ToString(&sdp)) | 148 if (!native_candidate->ToString(&sdp)) |
59 LOG(ERROR) << "Could not create SDP string"; | 149 LOG(ERROR) << "Could not create SDP string"; |
(...skipping 15 matching lines...) Expand all Loading... |
75 native_desc->AddCandidate(native_candidate.get()); | 165 native_desc->AddCandidate(native_candidate.get()); |
76 } | 166 } |
77 | 167 |
78 std::string sdp; | 168 std::string sdp; |
79 if (!native_desc->ToString(&sdp)) | 169 if (!native_desc->ToString(&sdp)) |
80 LOG(ERROR) << "Could not create SDP string"; | 170 LOG(ERROR) << "Could not create SDP string"; |
81 return UTF8ToUTF16(sdp); | 171 return UTF8ToUTF16(sdp); |
82 } | 172 } |
83 | 173 |
84 } // namespace content | 174 } // namespace content |
OLD | NEW |