Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / public / renderer / media_stream_api.cc
blobda39c4be45df966ffb8cd4ec75c3e965ea1f0dce
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"
21 #include "url/gurl.h"
23 namespace content {
25 namespace {
27 blink::WebString MakeTrackId() {
28 std::string track_id;
29 base::Base64Encode(base::RandBytesAsString(64), &track_id);
30 return base::UTF8ToUTF16(track_id);
33 } // namespace
35 bool AddVideoTrackToMediaStream(
36 scoped_ptr<media::VideoCapturerSource> source,
37 bool is_remote,
38 bool is_readonly,
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";
46 return false;
48 blink::WebString track_id = MakeTrackId();
49 blink::WebMediaStreamSource webkit_source;
50 scoped_ptr<MediaStreamVideoSource> media_stream_source(
51 new MediaStreamVideoCapturerSource(
52 MediaStreamSource::SourceStoppedCallback(),
53 source.Pass()));
54 webkit_source.initialize(
55 track_id,
56 blink::WebMediaStreamSource::TypeVideo,
57 track_id,
58 is_remote,
59 is_readonly);
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(),
66 constraints,
67 MediaStreamVideoSource::ConstraintsCallback(),
68 true));
69 return true;
72 bool AddAudioTrackToMediaStream(
73 scoped_refptr<media::AudioCapturerSource> source,
74 const media::AudioParameters& params,
75 bool is_remote,
76 bool is_readonly,
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";
85 return false;
87 blink::WebMediaStreamSource webkit_source;
88 blink::WebString track_id = MakeTrackId();
89 webkit_source.initialize(
90 track_id,
91 blink::WebMediaStreamSource::TypeAudio,
92 track_id,
93 is_remote,
94 is_readonly);
96 MediaStreamAudioSource* audio_source(
97 new MediaStreamAudioSource(
98 -1,
99 StreamDeviceInfo(),
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);
118 return true;
121 } // namespace content