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"
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/media_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/interfaces/shell.mojom.h"
31 namespace html_viewer
{
35 // Enable MediaRenderer in media pipeline instead of using the internal
36 // media::Renderer implementation.
37 // TODO(xhwang): Move this to media_switches.h.
38 const char kEnableMojoMediaRenderer
[] = "enable-mojo-media-renderer";
40 bool AreSecureCodecsSupported() {
41 // Hardware-secure codecs are not currently supported by HTML Viewer on any
48 MediaFactory::MediaFactory(
49 const scoped_refptr
<base::SingleThreadTaskRunner
>& compositor_task_runner
,
52 #if !defined(OS_ANDROID)
53 media_thread_("Media"),
54 audio_manager_(media::AudioManager::Create(&fake_audio_log_factory_
)),
55 audio_hardware_config_(
56 audio_manager_
->GetInputStreamParameters(
57 media::AudioManagerBase::kDefaultDeviceId
),
58 audio_manager_
->GetDefaultOutputStreamParameters()),
60 enable_mojo_media_renderer_(base::CommandLine::ForCurrentProcess()
61 ->HasSwitch(kEnableMojoMediaRenderer
)),
62 compositor_task_runner_(compositor_task_runner
),
64 media::InitializeMediaLibrary();
67 MediaFactory::~MediaFactory() {
70 blink::WebMediaPlayer
* MediaFactory::CreateMediaPlayer(
71 blink::WebLocalFrame
* frame
,
72 const blink::WebURL
& url
,
73 blink::WebMediaPlayerClient
* client
,
74 blink::WebContentDecryptionModule
* initial_cdm
,
76 #if defined(OS_ANDROID)
77 // TODO(xhwang): Get CreateMediaPlayer working on android.
80 scoped_refptr
<media::MediaLog
> media_log(new media::MediaLog());
81 scoped_ptr
<media::RendererFactory
> media_renderer_factory
;
83 if (enable_mojo_media_renderer_
) {
84 media_renderer_factory
.reset(
85 new media::MojoRendererFactory(GetMediaServiceProvider()));
87 media_renderer_factory
.reset(
88 new media::DefaultRendererFactory(media_log
,
89 nullptr, // No GPU factory.
90 GetAudioHardwareConfig()));
93 media::WebMediaPlayerParams
params(
94 media::WebMediaPlayerParams::DeferLoadCB(), CreateAudioRendererSink(),
95 media_log
, GetMediaThreadTaskRunner(), compositor_task_runner_
,
96 media::WebMediaPlayerParams::Context3DCB(), GetMediaPermission(),
98 base::WeakPtr
<media::WebMediaPlayerDelegate
> delegate
;
100 return new media::WebMediaPlayerImpl(frame
, client
, delegate
,
101 media_renderer_factory
.Pass(),
102 GetCdmFactory(), params
);
103 #endif // defined(OS_ANDROID)
106 blink::WebEncryptedMediaClient
* MediaFactory::GetEncryptedMediaClient() {
107 if (!web_encrypted_media_client_
) {
108 web_encrypted_media_client_
.reset(new media::WebEncryptedMediaClientImpl(
109 base::Bind(&AreSecureCodecsSupported
), GetCdmFactory(),
110 GetMediaPermission()));
112 return web_encrypted_media_client_
.get();
115 mojo::ServiceProvider
* MediaFactory::GetMediaServiceProvider() {
116 if (!media_service_provider_
) {
117 mojo::URLRequestPtr
request(mojo::URLRequest::New());
118 request
->url
= mojo::String::From("mojo:media");
119 shell_
->ConnectToApplication(request
.Pass(),
120 GetProxy(&media_service_provider_
), nullptr);
123 return media_service_provider_
.get();
126 media::MediaPermission
* MediaFactory::GetMediaPermission() {
127 // TODO(xhwang): Replace DefaultMediaPermission with something real when
128 // permissions are supported in html_viewer.
131 if (!media_permission_
)
132 media_permission_
.reset(new media::DefaultMediaPermission(true));
133 return media_permission_
.get();
136 media::CdmFactory
* MediaFactory::GetCdmFactory() {
138 if (enable_mojo_media_renderer_
)
139 cdm_factory_
.reset(new media::MojoCdmFactory(GetMediaServiceProvider()));
141 cdm_factory_
.reset(new media::DefaultCdmFactory());
144 return cdm_factory_
.get();
147 #if !defined(OS_ANDROID)
148 const media::AudioHardwareConfig
& MediaFactory::GetAudioHardwareConfig() {
149 return audio_hardware_config_
;
152 scoped_refptr
<media::AudioRendererSink
>
153 MediaFactory::CreateAudioRendererSink() {
154 // TODO(dalecurtis): Replace this with an interface to an actual mojo service;
155 // the AudioOutputStreamSink will not work in sandboxed processes.
156 return new media::AudioOutputStreamSink();
159 scoped_refptr
<base::SingleThreadTaskRunner
>
160 MediaFactory::GetMediaThreadTaskRunner() {
161 if (!media_thread_
.IsRunning())
162 media_thread_
.Start();
164 return media_thread_
.task_runner();
166 #endif // !defined(OS_ANDROID)
168 } // namespace html_viewer