Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / html_viewer / media_factory.cc
blob260df923657f7b982f960f5f3ee30929eaf0caba
1 // Copyright 2014 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 "components/html_viewer/media_factory.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/files/file_path.h"
10 #include "base/path_service.h"
11 #include "base/threading/thread.h"
12 #include "build/build_config.h"
13 #include "media/audio/audio_manager.h"
14 #include "media/audio/audio_manager_base.h"
15 #include "media/audio/audio_output_stream_sink.h"
16 #include "media/base/audio_hardware_config.h"
17 #include "media/base/media.h"
18 #include "media/base/media_log.h"
19 #include "media/blink/webencryptedmediaclient_impl.h"
20 #include "media/blink/webmediaplayer_impl.h"
21 #include "media/blink/webmediaplayer_params.h"
22 #include "media/cdm/default_cdm_factory.h"
23 #include "media/filters/default_media_permission.h"
24 #include "media/mojo/interfaces/renderer.mojom.h"
25 #include "media/mojo/services/mojo_cdm_factory.h"
26 #include "media/mojo/services/mojo_renderer_factory.h"
27 #include "media/renderers/default_renderer_factory.h"
28 #include "media/renderers/gpu_video_accelerator_factories.h"
29 #include "mojo/application/public/cpp/connect.h"
30 #include "mojo/application/public/interfaces/shell.mojom.h"
32 namespace html_viewer {
34 namespace {
36 // Enable mojo media Renderer in media pipeline instead of using the internal
37 // media::Renderer implementation.
38 // TODO(xhwang): Move this to media_switches.h.
39 const char kEnableMojoMediaRenderer[] = "enable-mojo-media-renderer";
41 bool AreSecureCodecsSupported() {
42 // Hardware-secure codecs are not currently supported by HTML Viewer on any
43 // platform.
44 return false;
47 void OnGotContentHandlerID(uint32_t content_handler_id) {}
49 } // namespace
51 MediaFactory::MediaFactory(
52 const scoped_refptr<base::SingleThreadTaskRunner>& compositor_task_runner,
53 mojo::Shell* shell)
55 #if !defined(OS_ANDROID)
56 media_thread_("Media"),
57 audio_manager_(media::AudioManager::Create(&fake_audio_log_factory_)),
58 audio_hardware_config_(
59 audio_manager_->GetInputStreamParameters(
60 media::AudioManagerBase::kDefaultDeviceId),
61 audio_manager_->GetDefaultOutputStreamParameters()),
62 #endif
63 enable_mojo_media_renderer_(base::CommandLine::ForCurrentProcess()
64 ->HasSwitch(kEnableMojoMediaRenderer)),
65 compositor_task_runner_(compositor_task_runner),
66 shell_(shell) {
67 media::InitializeMediaLibrary();
70 MediaFactory::~MediaFactory() {
73 blink::WebMediaPlayer* MediaFactory::CreateMediaPlayer(
74 blink::WebLocalFrame* frame,
75 const blink::WebURL& url,
76 blink::WebMediaPlayerClient* client,
77 blink::WebMediaPlayerEncryptedMediaClient* encrypted_client,
78 blink::WebContentDecryptionModule* initial_cdm,
79 mojo::Shell* shell) {
80 #if defined(OS_ANDROID)
81 // TODO(xhwang): Get CreateMediaPlayer working on android.
82 return nullptr;
83 #else
84 scoped_refptr<media::MediaLog> media_log(new media::MediaLog());
85 scoped_ptr<media::RendererFactory> media_renderer_factory;
87 if (enable_mojo_media_renderer_) {
88 media_renderer_factory.reset(
89 new media::MojoRendererFactory(GetMediaServiceFactory()));
90 } else {
91 media_renderer_factory.reset(
92 new media::DefaultRendererFactory(media_log,
93 nullptr, // No GPU factory.
94 GetAudioHardwareConfig()));
97 media::WebMediaPlayerParams params(
98 media::WebMediaPlayerParams::DeferLoadCB(), CreateAudioRendererSink(),
99 media_log, GetMediaThreadTaskRunner(), GetMediaThreadTaskRunner(),
100 compositor_task_runner_, media::WebMediaPlayerParams::Context3DCB(),
101 GetMediaPermission(), initial_cdm);
102 base::WeakPtr<media::WebMediaPlayerDelegate> delegate;
104 return new media::WebMediaPlayerImpl(frame, client, encrypted_client,
105 delegate, media_renderer_factory.Pass(),
106 GetCdmFactory(), params);
107 #endif // defined(OS_ANDROID)
110 blink::WebEncryptedMediaClient* MediaFactory::GetEncryptedMediaClient() {
111 if (!web_encrypted_media_client_) {
112 web_encrypted_media_client_.reset(new media::WebEncryptedMediaClientImpl(
113 base::Bind(&AreSecureCodecsSupported), GetCdmFactory(),
114 GetMediaPermission()));
116 return web_encrypted_media_client_.get();
119 media::interfaces::ServiceFactory* MediaFactory::GetMediaServiceFactory() {
120 if (!media_service_factory_) {
121 mojo::ServiceProviderPtr service_provider;
122 mojo::URLRequestPtr request(mojo::URLRequest::New());
123 request->url = mojo::String::From("mojo:media");
124 shell_->ConnectToApplication(request.Pass(), GetProxy(&service_provider),
125 nullptr, nullptr,
126 base::Bind(&OnGotContentHandlerID));
127 mojo::ConnectToService(service_provider.get(), &media_service_factory_);
130 return media_service_factory_.get();
133 media::MediaPermission* MediaFactory::GetMediaPermission() {
134 // TODO(xhwang): Replace DefaultMediaPermission with something real when
135 // permissions are supported in html_viewer.
136 NOTIMPLEMENTED();
138 if (!media_permission_)
139 media_permission_.reset(new media::DefaultMediaPermission(true));
140 return media_permission_.get();
143 media::CdmFactory* MediaFactory::GetCdmFactory() {
144 if (!cdm_factory_) {
145 if (enable_mojo_media_renderer_)
146 cdm_factory_.reset(new media::MojoCdmFactory(GetMediaServiceFactory()));
147 else
148 cdm_factory_.reset(new media::DefaultCdmFactory());
151 return cdm_factory_.get();
154 #if !defined(OS_ANDROID)
155 const media::AudioHardwareConfig& MediaFactory::GetAudioHardwareConfig() {
156 return audio_hardware_config_;
159 scoped_refptr<media::AudioRendererSink>
160 MediaFactory::CreateAudioRendererSink() {
161 // TODO(dalecurtis): Replace this with an interface to an actual mojo service;
162 // the AudioOutputStreamSink will not work in sandboxed processes.
163 return new media::AudioOutputStreamSink();
166 scoped_refptr<base::SingleThreadTaskRunner>
167 MediaFactory::GetMediaThreadTaskRunner() {
168 if (!media_thread_.IsRunning())
169 media_thread_.Start();
171 return media_thread_.task_runner();
173 #endif // !defined(OS_ANDROID)
175 } // namespace html_viewer