OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/public/renderer/media_stream_api.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/callback.h" | |
10 #include "base/guid.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/rand_util.h" | |
13 #include "base/strings/utf_string_conversions.h" | |
14 #include "content/renderer/media/media_stream_audio_source.h" | |
15 #include "content/renderer/media/media_stream_video_capturer_source.h" | |
16 #include "content/renderer/media/media_stream_video_source.h" | |
17 #include "content/renderer/media/media_stream_video_track.h" | |
18 #include "content/renderer/render_thread_impl.h" | |
19 #include "third_party/WebKit/public/platform/WebMediaStream.h" | |
20 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | |
21 #include "third_party/WebKit/public/platform/WebURL.h" | |
22 #include "third_party/WebKit/public/web/WebMediaStreamRegistry.h" | |
23 #include "url/gurl.h" | |
24 | |
25 namespace content { | |
26 | |
27 bool AddVideoTrackToMediaStream( | |
28 scoped_ptr<media::VideoCapturerSource> video_source, | |
29 bool is_remote, | |
30 bool is_readonly, | |
31 blink::WebMediaStream* web_media_stream) { | |
32 DCHECK(video_source.get()); | |
33 if (!web_media_stream || web_media_stream->isNull()) { | |
34 DLOG(ERROR) << "WebMediaStream is null"; | |
35 return false; | |
36 } | |
37 | |
38 blink::WebMediaStreamSource web_media_stream_source; | |
39 MediaStreamVideoSource* const media_stream_source = | |
40 new MediaStreamVideoCapturerSource( | |
41 MediaStreamSource::SourceStoppedCallback(), std::move(video_source)); | |
42 const blink::WebString track_id = | |
43 blink::WebString::fromUTF8(base::GenerateGUID()); | |
44 web_media_stream_source.initialize(track_id, | |
45 blink::WebMediaStreamSource::TypeVideo, | |
46 track_id, is_remote, is_readonly); | |
47 // Takes ownership of |media_stream_source|. | |
48 web_media_stream_source.setExtraData(media_stream_source); | |
49 | |
50 blink::WebMediaConstraints constraints; | |
51 constraints.initialize(); | |
52 web_media_stream->addTrack(MediaStreamVideoTrack::CreateVideoTrack( | |
53 media_stream_source, constraints, | |
54 MediaStreamVideoSource::ConstraintsCallback(), true)); | |
55 return true; | |
56 } | |
57 | |
58 bool AddAudioTrackToMediaStream( | |
59 scoped_refptr<media::AudioCapturerSource> audio_source, | |
60 int sample_rate, | |
61 media::ChannelLayout channel_layout, | |
62 int frames_per_buffer, | |
63 bool is_remote, | |
64 bool is_readonly, | |
65 blink::WebMediaStream* web_media_stream) { | |
66 DCHECK(audio_source.get()); | |
67 if (!web_media_stream || web_media_stream->isNull()) { | |
68 DLOG(ERROR) << "WebMediaStream is null"; | |
69 return false; | |
70 } | |
71 | |
72 const media::AudioParameters params( | |
73 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, | |
74 sample_rate, sizeof(int16_t) * 8, frames_per_buffer); | |
75 if (!params.IsValid()) { | |
76 DLOG(ERROR) << "Invalid audio parameters."; | |
77 return false; | |
78 } | |
79 | |
80 blink::WebMediaStreamSource web_media_stream_source; | |
81 const blink::WebString track_id = | |
82 blink::WebString::fromUTF8(base::GenerateGUID()); | |
83 web_media_stream_source.initialize(track_id, | |
84 blink::WebMediaStreamSource::TypeAudio, | |
85 track_id, is_remote, is_readonly); | |
86 | |
87 MediaStreamAudioSource* media_stream_source(new MediaStreamAudioSource( | |
88 -1, StreamDeviceInfo(), MediaStreamSource::SourceStoppedCallback(), | |
89 RenderThreadImpl::current()->GetPeerConnectionDependencyFactory())); | |
90 | |
91 blink::WebMediaConstraints constraints; | |
92 constraints.initialize(); | |
93 { | |
94 // TODO(miu): In an upcoming change, a source purposed for passing audio | |
95 // directly (i.e., without modification) will replace this "hacky" use of | |
96 // WebRtcAudioCapturer. http://crbug.com/577881 | |
97 scoped_ptr<WebRtcAudioCapturer> capturer( | |
98 WebRtcAudioCapturer::CreateCapturer(-1, StreamDeviceInfo(), constraints, | |
99 nullptr, media_stream_source)); | |
100 capturer->SetCapturerSource(std::move(audio_source), params); | |
101 media_stream_source->SetAudioCapturer(std::move(capturer)); | |
102 } | |
103 web_media_stream_source.setExtraData( | |
104 media_stream_source); // Takes ownership. | |
105 | |
106 blink::WebMediaStreamTrack web_media_stream_track; | |
107 web_media_stream_track.initialize(web_media_stream_source); | |
108 RenderThreadImpl::current() | |
109 ->GetPeerConnectionDependencyFactory() | |
110 ->CreateLocalAudioTrack(web_media_stream_track); | |
111 | |
112 web_media_stream->addTrack(web_media_stream_track); | |
113 return true; | |
114 } | |
115 | |
116 const media::VideoCaptureFormat* GetCurrentVideoTrackFormat( | |
117 const blink::WebMediaStreamTrack& video_track) { | |
118 if (video_track.isNull()) | |
119 return nullptr; | |
120 | |
121 content::MediaStreamVideoSource* source = | |
122 content::MediaStreamVideoSource::GetVideoSource(video_track.source()); | |
123 if (!source) | |
124 return nullptr; | |
125 | |
126 return source->GetCurrentFormat(); | |
127 } | |
128 | |
129 } // namespace content | |
OLD | NEW |