Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: content/public/renderer/media_stream_api.cc

Issue 1721273002: MediaStream audio object graph untangling and clean-ups. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: REBASE Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
10 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/guid.h"
11 #include "base/memory/scoped_ptr.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/renderer/media/media_stream_audio_source.h"
14 #include "content/renderer/media/media_stream_video_capturer_source.h" 15 #include "content/renderer/media/media_stream_video_capturer_source.h"
15 #include "content/renderer/media/media_stream_video_track.h" 16 #include "content/renderer/media/media_stream_video_track.h"
16 #include "content/renderer/render_thread_impl.h" 17 #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/WebURL.h"
20 #include "third_party/WebKit/public/web/WebMediaStreamRegistry.h" 21 #include "third_party/WebKit/public/web/WebMediaStreamRegistry.h"
21 #include "url/gurl.h" 22 #include "url/gurl.h"
22 23
23 namespace content { 24 namespace content {
24 25
25 namespace { 26 bool AddVideoTrackToMediaStream(
26 27 scoped_ptr<media::VideoCapturerSource> video_source,
27 blink::WebString MakeTrackId() { 28 bool is_remote,
28 std::string track_id; 29 bool is_readonly,
29 base::Base64Encode(base::RandBytesAsString(64), &track_id); 30 blink::WebMediaStream* web_media_stream) {
30 return base::UTF8ToUTF16(track_id); 31 DCHECK(video_source.get());
31 } 32 if (!web_media_stream || web_media_stream->isNull()) {
32 33 DLOG(ERROR) << "WebMediaStream is null";
33 } // namespace
34
35 bool AddVideoTrackToMediaStream(scoped_ptr<media::VideoCapturerSource> source,
36 bool is_remote,
37 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) {
50 if (web_stream->isNull()) {
51 DLOG(ERROR) << "Stream not found";
52 return false; 34 return false;
53 } 35 }
54 const blink::WebString track_id = MakeTrackId(); 36
55 blink::WebMediaStreamSource webkit_source; 37 blink::WebMediaStreamSource web_media_stream_source;
56 scoped_ptr<MediaStreamVideoSource> media_stream_source( 38 MediaStreamVideoSource* const media_stream_source =
57 new MediaStreamVideoCapturerSource( 39 new MediaStreamVideoCapturerSource(
58 MediaStreamSource::SourceStoppedCallback(), std::move(source))); 40 MediaStreamSource::SourceStoppedCallback(), std::move(video_source));
59 webkit_source.initialize(track_id, blink::WebMediaStreamSource::TypeVideo, 41 const blink::WebString track_id =
60 track_id, is_remote, is_readonly); 42 blink::WebString::fromUTF8(base::GenerateGUID());
61 webkit_source.setExtraData(media_stream_source.get()); 43 web_media_stream_source.initialize(track_id,
44 blink::WebMediaStreamSource::TypeVideo,
45 track_id, is_remote, is_readonly);
46 // Takes ownership of |media_stream_source|.
47 web_media_stream_source.setExtraData(media_stream_source);
62 48
63 blink::WebMediaConstraints constraints; 49 blink::WebMediaConstraints constraints;
64 constraints.initialize(); 50 constraints.initialize();
65 web_stream->addTrack(MediaStreamVideoTrack::CreateVideoTrack( 51 web_media_stream->addTrack(MediaStreamVideoTrack::CreateVideoTrack(
66 media_stream_source.release(), constraints, 52 media_stream_source, constraints,
67 MediaStreamVideoSource::ConstraintsCallback(), true)); 53 MediaStreamVideoSource::ConstraintsCallback(), true));
68 return true; 54 return true;
69 } 55 }
70 56
71 bool AddAudioTrackToMediaStream( 57 bool AddAudioTrackToMediaStream(
72 const scoped_refptr<media::AudioCapturerSource>& source, 58 scoped_refptr<media::AudioCapturerSource> audio_source,
73 const media::AudioParameters& params, 59 int sample_rate,
60 media::ChannelLayout channel_layout,
61 int frames_per_buffer,
74 bool is_remote, 62 bool is_remote,
75 bool is_readonly, 63 bool is_readonly,
76 const std::string& media_stream_url) { 64 blink::WebMediaStream* web_media_stream) {
77 DCHECK(params.IsValid()) << params.AsHumanReadableString(); 65 DCHECK(audio_source.get());
78 blink::WebMediaStream web_stream = 66 if (!web_media_stream || web_media_stream->isNull()) {
79 blink::WebMediaStreamRegistry::lookupMediaStreamDescriptor( 67 DLOG(ERROR) << "WebMediaStream is null";
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,
88 bool is_readonly,
89 blink::WebMediaStream* web_stream) {
90 if (web_stream->isNull()) {
91 DLOG(ERROR) << "Stream not found";
92 return false; 68 return false;
93 } 69 }
94 70
95 media::AudioParameters params( 71 const media::AudioParameters params(
96 media::AudioParameters::AUDIO_PCM_LINEAR, media::CHANNEL_LAYOUT_STEREO, 72 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout,
97 48000, /* sample rate */ 73 sample_rate, sizeof(int16_t) * 8, frames_per_buffer);
98 16, /* bits per sample */ 74 if (!params.IsValid()) {
99 480); /* frames per buffer */ 75 DLOG(ERROR) << "Invalid audio parameters.";
76 return false;
77 }
100 78
101 blink::WebMediaStreamSource webkit_source; 79 blink::WebMediaStreamSource web_media_stream_source;
102 const blink::WebString track_id = MakeTrackId(); 80 const blink::WebString track_id =
103 webkit_source.initialize(track_id, 81 blink::WebString::fromUTF8(base::GenerateGUID());
104 blink::WebMediaStreamSource::TypeAudio, 82 web_media_stream_source.initialize(track_id,
105 track_id, 83 blink::WebMediaStreamSource::TypeAudio,
106 is_remote, 84 track_id, is_remote, is_readonly);
107 is_readonly);
108 85
109 MediaStreamAudioSource* audio_source( 86 MediaStreamAudioSource* media_stream_source(new MediaStreamAudioSource(
110 new MediaStreamAudioSource( 87 -1, StreamDeviceInfo(), MediaStreamSource::SourceStoppedCallback(),
111 -1, 88 RenderThreadImpl::current()->GetPeerConnectionDependencyFactory()));
112 StreamDeviceInfo(),
113 MediaStreamSource::SourceStoppedCallback(),
114 RenderThreadImpl::current()->GetPeerConnectionDependencyFactory()));
115 89
116 blink::WebMediaConstraints constraints; 90 blink::WebMediaConstraints constraints;
117 constraints.initialize(); 91 constraints.initialize();
118 scoped_refptr<WebRtcAudioCapturer> capturer( 92 {
119 WebRtcAudioCapturer::CreateCapturer(-1, StreamDeviceInfo(), constraints, 93 // TODO(miu): In an upcoming change, a source purposed for passing audio
120 nullptr, audio_source)); 94 // directly (i.e., without modification) will replace this "hacky" use of
121 capturer->SetCapturerSource(source, params); 95 // WebRtcAudioCapturer. http://crbug.com/577881
122 audio_source->SetAudioCapturer(capturer); 96 scoped_ptr<WebRtcAudioCapturer> capturer(
123 webkit_source.setExtraData(audio_source); 97 WebRtcAudioCapturer::CreateCapturer(-1, StreamDeviceInfo(), constraints,
98 nullptr, media_stream_source));
99 capturer->SetCapturerSource(std::move(audio_source), params);
100 media_stream_source->SetAudioCapturer(std::move(capturer));
101 }
102 web_media_stream_source.setExtraData(
103 media_stream_source); // Takes ownership.
124 104
125 blink::WebMediaStreamTrack web_media_audio_track; 105 blink::WebMediaStreamTrack web_media_stream_track;
126 web_media_audio_track.initialize(webkit_source); 106 web_media_stream_track.initialize(web_media_stream_source);
127 RenderThreadImpl::current()->GetPeerConnectionDependencyFactory()-> 107 RenderThreadImpl::current()
128 CreateLocalAudioTrack(web_media_audio_track); 108 ->GetPeerConnectionDependencyFactory()
109 ->CreateLocalAudioTrack(web_media_stream_track);
129 110
130 web_stream->addTrack(web_media_audio_track); 111 web_media_stream->addTrack(web_media_stream_track);
131 return true; 112 return true;
132 } 113 }
133 114
134 } // namespace content 115 } // namespace content
OLDNEW
« no previous file with comments | « content/public/renderer/media_stream_api.h ('k') | content/public/renderer/media_stream_audio_sink.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698