1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
media / mojo / services / mojo_renderer_service.h [blame]
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef MEDIA_MOJO_SERVICES_MOJO_RENDERER_SERVICE_H_
#define MEDIA_MOJO_SERVICES_MOJO_RENDERER_SERVICE_H_
#include <stdint.h>
#include <memory>
#include <optional>
#include "base/compiler_specific.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "base/unguessable_token.h"
#include "media/base/buffering_state.h"
#include "media/base/media_resource.h"
#include "media/base/pipeline_status.h"
#include "media/base/renderer_client.h"
#include "media/mojo/mojom/renderer.mojom.h"
#include "media/mojo/services/media_mojo_export.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "mojo/public/cpp/bindings/pending_associated_remote.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
namespace media {
class CdmContextRef;
class MediaResourceShim;
class MojoCdmServiceContext;
class Renderer;
// A mojom::Renderer implementation that use a media::Renderer to render
// media streams.
class MEDIA_MOJO_EXPORT MojoRendererService final : public mojom::Renderer,
public RendererClient {
public:
// Helper function to bind MojoRendererService with a SelfOwendReceiver,
// which is safely accessible via the returned SelfOwnedReceiverRef.
static mojo::SelfOwnedReceiverRef<mojom::Renderer> Create(
MojoCdmServiceContext* mojo_cdm_service_context,
std::unique_ptr<media::Renderer> renderer,
mojo::PendingReceiver<mojom::Renderer> receiver);
// |mojo_cdm_service_context| can be used to find the CDM to support
// encrypted media. If null, encrypted media is not supported.
MojoRendererService(MojoCdmServiceContext* mojo_cdm_service_context,
std::unique_ptr<media::Renderer> renderer);
MojoRendererService(const MojoRendererService&) = delete;
MojoRendererService& operator=(const MojoRendererService&) = delete;
~MojoRendererService() final;
// mojom::Renderer implementation.
void Initialize(
mojo::PendingAssociatedRemote<mojom::RendererClient> client,
std::optional<std::vector<mojo::PendingRemote<mojom::DemuxerStream>>>
streams,
mojom::MediaUrlParamsPtr media_url_params,
InitializeCallback callback) final;
void Flush(FlushCallback callback) final;
void StartPlayingFrom(base::TimeDelta time_delta) final;
void SetPlaybackRate(double playback_rate) final;
void SetVolume(float volume) final;
void SetCdm(const std::optional<base::UnguessableToken>& cdm_id,
SetCdmCallback callback) final;
void SetLatencyHint(std::optional<base::TimeDelta> latency_hint) final;
private:
enum State {
STATE_UNINITIALIZED,
STATE_INITIALIZING,
STATE_FLUSHING,
STATE_PLAYING,
STATE_ERROR
};
// RendererClient implementation.
void OnError(PipelineStatus status) final;
void OnFallback(PipelineStatus status) final;
void OnEnded() final;
void OnStatisticsUpdate(const PipelineStatistics& stats) final;
void OnBufferingStateChange(BufferingState state,
BufferingStateChangeReason reason) final;
void OnWaiting(WaitingReason reason) final;
void OnAudioConfigChange(const AudioDecoderConfig& config) final;
void OnVideoConfigChange(const VideoDecoderConfig& config) final;
void OnVideoNaturalSizeChange(const gfx::Size& size) final;
void OnVideoOpacityChange(bool opaque) final;
void OnVideoFrameRateChange(std::optional<int> fps) final;
// Called when the MediaResourceShim is ready to go (has a config,
// pipe handle, etc) and can be handed off to a renderer for use.
void OnAllStreamsReady(base::OnceCallback<void(bool)> callback);
// Called when |audio_renderer_| initialization has completed.
void OnRendererInitializeDone(base::OnceCallback<void(bool)> callback,
PipelineStatus status);
// Periodically polls the media time from the renderer and notifies the client
// if the media time has changed since the last update.
// If |force| is true, the client is notified even if the time is unchanged.
// If |range| is true, an interpolation time range is reported.
void UpdateMediaTime(bool force);
void CancelPeriodicMediaTimeUpdates();
void SchedulePeriodicMediaTimeUpdates();
// Callback executed once Flush() completes.
void OnFlushCompleted(FlushCallback callback);
// Callback executed once SetCdm() completes.
void OnCdmAttached(base::OnceCallback<void(bool)> callback, bool success);
const raw_ptr<MojoCdmServiceContext> mojo_cdm_service_context_ = nullptr;
State state_;
double playback_rate_;
std::unique_ptr<MediaResource> media_resource_;
base::RepeatingTimer time_update_timer_;
base::TimeDelta last_media_time_;
mojo::AssociatedRemote<mojom::RendererClient> client_;
// Holds the CdmContextRef to keep the CdmContext alive for the lifetime of
// the |renderer_|.
std::unique_ptr<CdmContextRef> cdm_context_ref_;
// Note: Destroy |renderer_| first to avoid access violation into other
// members, e.g. |media_resource_| and |cdm_|.
// Must use "media::" because "Renderer" is ambiguous.
std::unique_ptr<media::Renderer> renderer_;
base::WeakPtr<MojoRendererService> weak_this_;
base::WeakPtrFactory<MojoRendererService> weak_factory_{this};
};
} // namespace media
#endif // MEDIA_MOJO_SERVICES_MOJO_RENDERER_SERVICE_H_