summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/inspector/inspector_media_agent.cc
blob: 40e9ae5054b1b728324141e585b4b318dc02c8bf (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/inspector/inspector_media_agent.h"
#include "third_party/blink/renderer/core/inspector/inspector_media_context_impl.h"

#include <utility>

#include "third_party/blink/renderer/core/page/page.h"

namespace blink {

namespace {

const char* ConvertMessageLevelEnum(InspectorPlayerMessage::Level level) {
  switch (level) {
    case InspectorPlayerMessage::Level::kError:
      return protocol::Media::PlayerMessage::LevelEnum::Error;
    case InspectorPlayerMessage::Level::kWarning:
      return protocol::Media::PlayerMessage::LevelEnum::Warning;
    case InspectorPlayerMessage::Level::kInfo:
      return protocol::Media::PlayerMessage::LevelEnum::Info;
    case InspectorPlayerMessage::Level::kDebug:
      return protocol::Media::PlayerMessage::LevelEnum::Debug;
  }
}

const char* ConvertErrorTypeEnum(InspectorPlayerError::Type level) {
  switch (level) {
    case InspectorPlayerError::Type::kPipelineError:
      return protocol::Media::PlayerError::TypeEnum::Pipeline_error;
    case InspectorPlayerError::Type::kMediaStatus:
      return protocol::Media::PlayerError::TypeEnum::Media_error;
  }
}

std::unique_ptr<protocol::Media::PlayerEvent> ConvertToProtocolType(
    const InspectorPlayerEvent& event) {
  return protocol::Media::PlayerEvent::create()
      .setTimestamp(event.timestamp.since_origin().InSecondsF())
      .setValue(event.value)
      .build();
}

std::unique_ptr<protocol::Media::PlayerProperty> ConvertToProtocolType(
    const InspectorPlayerProperty& property) {
  return protocol::Media::PlayerProperty::create()
      .setName(property.name)
      .setValue(property.value)
      .build();
}

std::unique_ptr<protocol::Media::PlayerMessage> ConvertToProtocolType(
    const InspectorPlayerMessage& message) {
  return protocol::Media::PlayerMessage::create()
      .setLevel(ConvertMessageLevelEnum(message.level))
      .setMessage(message.message)
      .build();
}

std::unique_ptr<protocol::Media::PlayerError> ConvertToProtocolType(
    const InspectorPlayerError& error) {
  return protocol::Media::PlayerError::create()
      .setType(ConvertErrorTypeEnum(error.type))
      .setErrorCode(error.errorCode)
      .build();
}

template <typename To, typename From>
std::unique_ptr<protocol::Array<To>> ConvertVector(const Vector<From>& from) {
  auto result = std::make_unique<protocol::Array<To>>();
  result->reserve(from.size());
  for (const From& each : from)
    result->push_back(ConvertToProtocolType(each));
  return result;
}

}  // namespace

InspectorMediaAgent::InspectorMediaAgent(InspectedFrames* inspected_frames)
    : local_frame_(inspected_frames->Root()),
      enabled_(&agent_state_, /*default_value = */ false) {}

InspectorMediaAgent::~InspectorMediaAgent() = default;

void InspectorMediaAgent::Restore() {
  if (!enabled_.Get())
    return;
  RegisterAgent();
}

void InspectorMediaAgent::RegisterAgent() {
  instrumenting_agents_->AddInspectorMediaAgent(this);
  auto* cache = MediaInspectorContextImpl::From(*local_frame_->DomWindow());
  Vector<WebString> players = cache->AllPlayerIds();
  PlayersCreated(players);
  for (const auto& player_id : players) {
    const auto& media_player = cache->MediaPlayerFromId(player_id);
    Vector<InspectorPlayerProperty> properties;
    properties.AppendRange(media_player.properties.Values().begin(),
                           media_player.properties.Values().end());

    PlayerPropertiesChanged(player_id, properties);
    PlayerMessagesLogged(player_id, media_player.messages);
    PlayerEventsAdded(player_id, media_player.events);
    PlayerErrorsRaised(player_id, media_player.errors);
  }
}

protocol::Response InspectorMediaAgent::enable() {
  if (enabled_.Get())
    return protocol::Response::Success();
  enabled_.Set(true);
  RegisterAgent();
  return protocol::Response::Success();
}

protocol::Response InspectorMediaAgent::disable() {
  if (!enabled_.Get())
    return protocol::Response::Success();
  enabled_.Clear();
  instrumenting_agents_->RemoveInspectorMediaAgent(this);
  return protocol::Response::Success();
}

void InspectorMediaAgent::PlayerPropertiesChanged(
    const WebString& playerId,
    const Vector<InspectorPlayerProperty>& properties) {
  GetFrontend()->playerPropertiesChanged(
      playerId, ConvertVector<protocol::Media::PlayerProperty>(properties));
}

void InspectorMediaAgent::PlayerEventsAdded(
    const WebString& playerId,
    const Vector<InspectorPlayerEvent>& events) {
  GetFrontend()->playerEventsAdded(
      playerId, ConvertVector<protocol::Media::PlayerEvent>(events));
}

void InspectorMediaAgent::PlayerErrorsRaised(
    const WebString& playerId,
    const Vector<InspectorPlayerError>& errors) {
  GetFrontend()->playerErrorsRaised(
      playerId, ConvertVector<protocol::Media::PlayerError>(errors));
}

void InspectorMediaAgent::PlayerMessagesLogged(
    const WebString& playerId,
    const Vector<InspectorPlayerMessage>& messages) {
  GetFrontend()->playerMessagesLogged(
      playerId, ConvertVector<protocol::Media::PlayerMessage>(messages));
}

void InspectorMediaAgent::PlayersCreated(const Vector<WebString>& player_ids) {
  auto protocol_players =
      std::make_unique<protocol::Array<protocol::Media::PlayerId>>();
  protocol_players->reserve(player_ids.size());
  for (const auto& player_id : player_ids)
    protocol_players->push_back(player_id);
  GetFrontend()->playersCreated(std::move(protocol_players));
}

void InspectorMediaAgent::Trace(Visitor* visitor) {
  visitor->Trace(local_frame_);
  InspectorBaseAgent::Trace(visitor);
}

}  // namespace blink