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/web_media_player_factory.h"
8 #include "base/files/file_path.h"
9 #include "base/path_service.h"
10 #include "base/threading/thread.h"
11 #include "media/audio/audio_manager.h"
12 #include "media/audio/audio_manager_base.h"
13 #include "media/audio/audio_output_stream_sink.h"
14 #include "media/base/audio_hardware_config.h"
15 #include "media/base/media.h"
16 #include "media/base/media_log.h"
17 #include "media/blink/webmediaplayer_impl.h"
18 #include "media/blink/webmediaplayer_params.h"
19 #include "media/cdm/default_cdm_factory.h"
20 #include "media/mojo/interfaces/media_renderer.mojom.h"
21 #include "media/mojo/services/mojo_renderer_factory.h"
22 #include "media/renderers/default_renderer_factory.h"
23 #include "media/renderers/gpu_video_accelerator_factories.h"
24 #include "mojo/application/public/cpp/connect.h"
25 #include "mojo/application/public/interfaces/shell.mojom.h"
27 using mojo::ServiceProviderPtr
;
29 namespace html_viewer
{
31 #if !defined(OS_ANDROID)
34 class RendererServiceProvider
35 : public media::MojoRendererFactory::ServiceProvider
{
37 explicit RendererServiceProvider(ServiceProviderPtr service_provider_ptr
)
38 : service_provider_ptr_(service_provider_ptr
.Pass()) {}
39 ~RendererServiceProvider() final
{}
41 void ConnectToService(
42 mojo::InterfacePtr
<mojo::MediaRenderer
>* media_renderer_ptr
) final
{
43 mojo::ConnectToService(service_provider_ptr_
.get(), media_renderer_ptr
);
47 ServiceProviderPtr service_provider_ptr_
;
49 DISALLOW_COPY_AND_ASSIGN(RendererServiceProvider
);
55 WebMediaPlayerFactory::WebMediaPlayerFactory(
56 const scoped_refptr
<base::SingleThreadTaskRunner
>& compositor_task_runner
,
57 bool enable_mojo_media_renderer
)
58 : compositor_task_runner_(compositor_task_runner
),
59 enable_mojo_media_renderer_(enable_mojo_media_renderer
),
60 media_thread_("Media"),
61 audio_manager_(media::AudioManager::Create(&fake_audio_log_factory_
)),
62 audio_hardware_config_(
63 audio_manager_
->GetInputStreamParameters(
64 media::AudioManagerBase::kDefaultDeviceId
),
65 audio_manager_
->GetDefaultOutputStreamParameters()) {
67 if (!media::IsMediaLibraryInitialized()) {
68 base::FilePath module_dir
;
69 CHECK(PathService::Get(base::DIR_EXE
, &module_dir
));
70 CHECK(media::InitializeMediaLibrary(module_dir
));
74 WebMediaPlayerFactory::~WebMediaPlayerFactory() {
77 blink::WebMediaPlayer
* WebMediaPlayerFactory::CreateMediaPlayer(
78 blink::WebLocalFrame
* frame
,
79 const blink::WebURL
& url
,
80 blink::WebMediaPlayerClient
* client
,
81 media::MediaPermission
* media_permission
,
82 media::CdmFactory
* cdm_factory
,
83 blink::WebContentDecryptionModule
* initial_cdm
,
85 #if defined(OS_ANDROID)
88 scoped_refptr
<media::MediaLog
> media_log(new media::MediaLog());
89 scoped_ptr
<media::RendererFactory
> media_renderer_factory
;
91 if (enable_mojo_media_renderer_
) {
92 ServiceProviderPtr media_renderer_service_provider
;
93 shell
->ConnectToApplication(
94 "mojo:media", GetProxy(&media_renderer_service_provider
), nullptr);
95 media_renderer_factory
.reset(new media::MojoRendererFactory(make_scoped_ptr(
96 new RendererServiceProvider(media_renderer_service_provider
.Pass()))));
98 media_renderer_factory
.reset(
99 new media::DefaultRendererFactory(media_log
,
100 nullptr, // No GPU factory.
101 GetAudioHardwareConfig()));
104 media::WebMediaPlayerParams
params(
105 media::WebMediaPlayerParams::DeferLoadCB(), CreateAudioRendererSink(),
106 media_log
, GetMediaThreadTaskRunner(), compositor_task_runner_
,
107 media::WebMediaPlayerParams::Context3DCB(), media_permission
,
109 base::WeakPtr
<media::WebMediaPlayerDelegate
> delegate
;
111 return new media::WebMediaPlayerImpl(frame
, client
, delegate
,
112 media_renderer_factory
.Pass(),
113 cdm_factory
, params
);
117 const media::AudioHardwareConfig
&
118 WebMediaPlayerFactory::GetAudioHardwareConfig() {
119 return audio_hardware_config_
;
122 scoped_refptr
<media::AudioRendererSink
>
123 WebMediaPlayerFactory::CreateAudioRendererSink() {
124 // TODO(dalecurtis): Replace this with an interface to an actual mojo service;
125 // the AudioOutputStreamSink will not work in sandboxed processes.
126 return new media::AudioOutputStreamSink();
129 scoped_refptr
<base::SingleThreadTaskRunner
>
130 WebMediaPlayerFactory::GetMediaThreadTaskRunner() {
131 if (!media_thread_
.IsRunning())
132 media_thread_
.Start();
134 return media_thread_
.message_loop_proxy();
137 } // namespace html_viewer