OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/public/renderer/media_stream_api.h" | 5 #include "content/public/renderer/media_stream_api.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/bind_helpers.h" |
10 #include "base/callback.h" | 11 #include "base/callback.h" |
11 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
13 #include "content/renderer/media/media_stream_audio_source.h" | 14 #include "content/common/media/media_stream_options.h" |
| 15 #include "content/renderer/media/external_media_stream_audio_source.h" |
14 #include "content/renderer/media/media_stream_video_capturer_source.h" | 16 #include "content/renderer/media/media_stream_video_capturer_source.h" |
15 #include "content/renderer/media/media_stream_video_track.h" | 17 #include "content/renderer/media/media_stream_video_track.h" |
16 #include "content/renderer/render_thread_impl.h" | |
17 #include "third_party/WebKit/public/platform/WebMediaStream.h" | 18 #include "third_party/WebKit/public/platform/WebMediaStream.h" |
18 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | 19 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
19 #include "third_party/WebKit/public/platform/WebURL.h" | 20 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
20 #include "third_party/WebKit/public/web/WebMediaStreamRegistry.h" | |
21 #include "url/gurl.h" | |
22 | 21 |
23 namespace content { | 22 namespace content { |
24 | 23 |
25 namespace { | 24 namespace { |
26 | 25 |
27 blink::WebString MakeTrackId() { | 26 blink::WebString MakeTrackId() { |
28 std::string track_id; | 27 std::string track_id; |
29 base::Base64Encode(base::RandBytesAsString(64), &track_id); | 28 base::Base64Encode(base::RandBytesAsString(64), &track_id); |
30 return base::UTF8ToUTF16(track_id); | 29 return base::UTF8ToUTF16(track_id); |
31 } | 30 } |
32 | 31 |
33 } // namespace | 32 } // namespace |
34 | 33 |
35 bool AddVideoTrackToMediaStream(scoped_ptr<media::VideoCapturerSource> source, | 34 bool AddVideoTrackToMediaStream(scoped_ptr<media::VideoCapturerSource> source, |
36 bool is_remote, | 35 bool is_remote, |
37 bool is_readonly, | 36 bool is_readonly, |
38 const std::string& media_stream_url) { | |
39 blink::WebMediaStream web_stream = | |
40 blink::WebMediaStreamRegistry::lookupMediaStreamDescriptor( | |
41 GURL(media_stream_url)); | |
42 return AddVideoTrackToMediaStream(std::move(source), is_remote, is_readonly, | |
43 &web_stream); | |
44 } | |
45 | |
46 bool AddVideoTrackToMediaStream(scoped_ptr<media::VideoCapturerSource> source, | |
47 bool is_remote, | |
48 bool is_readonly, | |
49 blink::WebMediaStream* web_stream) { | 37 blink::WebMediaStream* web_stream) { |
50 if (web_stream->isNull()) { | 38 DCHECK(source.get()); |
51 DLOG(ERROR) << "Stream not found"; | 39 DCHECK(web_stream); |
52 return false; | 40 DCHECK(!web_stream->isNull()); |
53 } | 41 |
54 const blink::WebString track_id = MakeTrackId(); | 42 const blink::WebString track_id = MakeTrackId(); |
55 blink::WebMediaStreamSource webkit_source; | 43 blink::WebMediaStreamSource webkit_source; |
56 scoped_ptr<MediaStreamVideoSource> media_stream_source( | 44 MediaStreamVideoSource* const media_stream_source = |
57 new MediaStreamVideoCapturerSource( | 45 new MediaStreamVideoCapturerSource( |
58 MediaStreamSource::SourceStoppedCallback(), std::move(source))); | 46 MediaStreamSource::SourceStoppedCallback(), std::move(source)); |
59 webkit_source.initialize(track_id, blink::WebMediaStreamSource::TypeVideo, | 47 webkit_source.initialize(track_id, blink::WebMediaStreamSource::TypeVideo, |
60 track_id, is_remote, is_readonly); | 48 track_id, is_remote, is_readonly); |
61 webkit_source.setExtraData(media_stream_source.get()); | 49 // Takes ownership of |media_stream_source|. |
| 50 webkit_source.setExtraData(media_stream_source); |
62 | 51 |
63 blink::WebMediaConstraints constraints; | 52 blink::WebMediaConstraints constraints; |
64 constraints.initialize(); | 53 constraints.initialize(); |
65 web_stream->addTrack(MediaStreamVideoTrack::CreateVideoTrack( | 54 web_stream->addTrack(MediaStreamVideoTrack::CreateVideoTrack( |
66 media_stream_source.release(), constraints, | 55 media_stream_source, constraints, |
67 MediaStreamVideoSource::ConstraintsCallback(), true)); | 56 MediaStreamVideoSource::ConstraintsCallback(), true)); |
68 return true; | 57 return true; |
69 } | 58 } |
70 | 59 |
71 bool AddAudioTrackToMediaStream( | 60 bool AddAudioTrackToMediaStream( |
72 const scoped_refptr<media::AudioCapturerSource>& source, | 61 const scoped_refptr<media::AudioCapturerSource>& source, |
73 const media::AudioParameters& params, | 62 int sample_rate, |
74 bool is_remote, | 63 media::ChannelLayout channel_layout, |
75 bool is_readonly, | 64 int frames_per_buffer, |
76 const std::string& media_stream_url) { | |
77 DCHECK(params.IsValid()) << params.AsHumanReadableString(); | |
78 blink::WebMediaStream web_stream = | |
79 blink::WebMediaStreamRegistry::lookupMediaStreamDescriptor( | |
80 GURL(media_stream_url)); | |
81 return AddAudioTrackToMediaStream(source, | |
82 is_remote, is_readonly, &web_stream); | |
83 } | |
84 | |
85 bool AddAudioTrackToMediaStream( | |
86 const scoped_refptr<media::AudioCapturerSource>& source, | |
87 bool is_remote, | 65 bool is_remote, |
88 bool is_readonly, | 66 bool is_readonly, |
89 blink::WebMediaStream* web_stream) { | 67 blink::WebMediaStream* web_stream) { |
90 if (web_stream->isNull()) { | 68 DCHECK(source.get()); |
91 DLOG(ERROR) << "Stream not found"; | 69 DCHECK(web_stream); |
| 70 DCHECK(!web_stream->isNull()); |
| 71 |
| 72 // Create Blink and Chromium objects to represent the MediaStreamSource. |
| 73 blink::WebMediaStreamSource web_source; |
| 74 const blink::WebString track_id = MakeTrackId(); |
| 75 web_source.initialize(track_id, blink::WebMediaStreamSource::TypeAudio, |
| 76 track_id, is_remote, is_readonly); |
| 77 MediaStreamAudioSource* const audio_source = |
| 78 new ExternalMediaStreamAudioSource( |
| 79 source, sample_rate, channel_layout, frames_per_buffer, is_remote); |
| 80 web_source.setExtraData(audio_source); // Takes ownership of |audio_source|. |
| 81 |
| 82 // Create the Blink and Chromium objects to represent the MediaStreamTrack, |
| 83 // and connect the audio source to the track. |
| 84 blink::WebMediaStreamTrack web_track; |
| 85 web_track.initialize(web_source); |
| 86 if (!audio_source->ConnectToTrack(web_track)) |
92 return false; | 87 return false; |
93 } | |
94 | 88 |
95 media::AudioParameters params( | 89 // Add the MediaStreamTrack to the MediaStream. |
96 media::AudioParameters::AUDIO_PCM_LINEAR, media::CHANNEL_LAYOUT_STEREO, | 90 web_stream->addTrack(web_track); |
97 48000, /* sample rate */ | |
98 16, /* bits per sample */ | |
99 480); /* frames per buffer */ | |
100 | |
101 blink::WebMediaStreamSource webkit_source; | |
102 const blink::WebString track_id = MakeTrackId(); | |
103 webkit_source.initialize(track_id, | |
104 blink::WebMediaStreamSource::TypeAudio, | |
105 track_id, | |
106 is_remote, | |
107 is_readonly); | |
108 | |
109 MediaStreamAudioSource* audio_source( | |
110 new MediaStreamAudioSource( | |
111 -1, | |
112 StreamDeviceInfo(), | |
113 MediaStreamSource::SourceStoppedCallback(), | |
114 RenderThreadImpl::current()->GetPeerConnectionDependencyFactory())); | |
115 | |
116 blink::WebMediaConstraints constraints; | |
117 constraints.initialize(); | |
118 scoped_refptr<WebRtcAudioCapturer> capturer( | |
119 WebRtcAudioCapturer::CreateCapturer(-1, StreamDeviceInfo(), constraints, | |
120 nullptr, audio_source)); | |
121 capturer->SetCapturerSource(source, params); | |
122 audio_source->SetAudioCapturer(capturer); | |
123 webkit_source.setExtraData(audio_source); | |
124 | |
125 blink::WebMediaStreamTrack web_media_audio_track; | |
126 web_media_audio_track.initialize(webkit_source); | |
127 RenderThreadImpl::current()->GetPeerConnectionDependencyFactory()-> | |
128 CreateLocalAudioTrack(web_media_audio_track); | |
129 | |
130 web_stream->addTrack(web_media_audio_track); | |
131 return true; | 91 return true; |
132 } | 92 } |
133 | 93 |
134 } // namespace content | 94 } // namespace content |
OLD | NEW |