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.
5 #include "content/public/renderer/media_stream_api.h"
7 #include "base/base64.h"
8 #include "base/callback.h"
9 #include "base/rand_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "content/renderer/media/media_stream_audio_source.h"
12 #include "content/renderer/media/media_stream_video_capturer_source.h"
13 #include "content/renderer/media/media_stream_video_track.h"
14 #include "media/base/audio_capturer_source.h"
15 #include "media/base/video_capturer_source.h"
16 #include "third_party/WebKit/public/platform/WebMediaDeviceInfo.h"
17 #include "third_party/WebKit/public/platform/WebMediaStream.h"
18 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
19 #include "third_party/WebKit/public/platform/WebURL.h"
20 #include "third_party/WebKit/public/web/WebMediaStreamRegistry.h"
27 blink::WebString
MakeTrackId() {
29 base::Base64Encode(base::RandBytesAsString(64), &track_id
);
30 return base::UTF8ToUTF16(track_id
);
35 bool AddVideoTrackToMediaStream(
36 scoped_ptr
<media::VideoCapturerSource
> source
,
39 const std::string
& media_stream_url
) {
40 blink::WebMediaStream stream
=
41 blink::WebMediaStreamRegistry::lookupMediaStreamDescriptor(
42 GURL(media_stream_url
));
44 if (stream
.isNull()) {
45 LOG(ERROR
) << "Stream not found";
48 blink::WebString track_id
= MakeTrackId();
49 blink::WebMediaStreamSource webkit_source
;
50 scoped_ptr
<MediaStreamVideoSource
> media_stream_source(
51 new MediaStreamVideoCapturerSource(
52 MediaStreamSource::SourceStoppedCallback(),
54 webkit_source
.initialize(
56 blink::WebMediaStreamSource::TypeVideo
,
60 webkit_source
.setExtraData(media_stream_source
.get());
62 blink::WebMediaConstraints constraints
;
63 constraints
.initialize();
64 stream
.addTrack(MediaStreamVideoTrack::CreateVideoTrack(
65 media_stream_source
.release(),
67 MediaStreamVideoSource::ConstraintsCallback(),
72 bool AddAudioTrackToMediaStream(
73 scoped_refptr
<media::AudioCapturerSource
> source
,
74 const media::AudioParameters
& params
,
77 const std::string
& media_stream_url
) {
78 DCHECK(params
.IsValid()) << params
.AsHumanReadableString();
79 blink::WebMediaStream stream
=
80 blink::WebMediaStreamRegistry::lookupMediaStreamDescriptor(
81 GURL(media_stream_url
));
83 if (stream
.isNull()) {
84 LOG(ERROR
) << "Stream not found";
87 blink::WebMediaStreamSource webkit_source
;
88 blink::WebString track_id
= MakeTrackId();
89 webkit_source
.initialize(
91 blink::WebMediaStreamSource::TypeAudio
,
96 MediaStreamAudioSource
* audio_source(
97 new MediaStreamAudioSource(
100 MediaStreamSource::SourceStoppedCallback(),
101 RenderThreadImpl::current()->GetPeerConnectionDependencyFactory()));
103 blink::WebMediaConstraints constraints
;
104 constraints
.initialize();
105 scoped_refptr
<WebRtcAudioCapturer
> capturer(
106 WebRtcAudioCapturer::CreateCapturer(-1, StreamDeviceInfo(), constraints
,
107 nullptr, audio_source
));
108 capturer
->SetCapturerSource(source
, params
);
109 audio_source
->SetAudioCapturer(capturer
);
110 webkit_source
.setExtraData(audio_source
);
112 blink::WebMediaStreamTrack web_media_audio_track
;
113 web_media_audio_track
.initialize(webkit_source
);
114 RenderThreadImpl::current()->GetPeerConnectionDependencyFactory()->
115 CreateLocalAudioTrack(web_media_audio_track
);
117 stream
.addTrack(web_media_audio_track
);
121 } // namespace content