Allow preventing launching Chrome with invalid Intents
[chromium-blink-merge.git] / media / base / media_log.cc
blob745957d5c0cae3befbcb0a75ca416ea4fb18b4e4
1 // Copyright (c) 2012 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 "media/base/media_log.h"
7 #include <string>
9 #include "base/atomic_sequence_num.h"
10 #include "base/json/json_writer.h"
11 #include "base/logging.h"
12 #include "base/values.h"
14 namespace media {
16 // A count of all MediaLogs created in the current process. Used to generate
17 // unique IDs.
18 static base::StaticAtomicSequenceNumber g_media_log_count;
20 std::string MediaLog::MediaLogLevelToString(MediaLogLevel level) {
21 switch (level) {
22 case MEDIALOG_ERROR:
23 return "error";
24 case MEDIALOG_INFO:
25 return "info";
26 case MEDIALOG_DEBUG:
27 return "debug";
29 NOTREACHED();
30 return NULL;
33 MediaLogEvent::Type MediaLog::MediaLogLevelToEventType(MediaLogLevel level) {
34 switch (level) {
35 case MEDIALOG_ERROR:
36 return MediaLogEvent::MEDIA_ERROR_LOG_ENTRY;
37 case MEDIALOG_INFO:
38 return MediaLogEvent::MEDIA_INFO_LOG_ENTRY;
39 case MEDIALOG_DEBUG:
40 return MediaLogEvent::MEDIA_DEBUG_LOG_ENTRY;
42 NOTREACHED();
43 return MediaLogEvent::MEDIA_ERROR_LOG_ENTRY;
46 std::string MediaLog::EventTypeToString(MediaLogEvent::Type type) {
47 switch (type) {
48 case MediaLogEvent::WEBMEDIAPLAYER_CREATED:
49 return "WEBMEDIAPLAYER_CREATED";
50 case MediaLogEvent::WEBMEDIAPLAYER_DESTROYED:
51 return "WEBMEDIAPLAYER_DESTROYED";
52 case MediaLogEvent::PIPELINE_CREATED:
53 return "PIPELINE_CREATED";
54 case MediaLogEvent::PIPELINE_DESTROYED:
55 return "PIPELINE_DESTROYED";
56 case MediaLogEvent::LOAD:
57 return "LOAD";
58 case MediaLogEvent::SEEK:
59 return "SEEK";
60 case MediaLogEvent::PLAY:
61 return "PLAY";
62 case MediaLogEvent::PAUSE:
63 return "PAUSE";
64 case MediaLogEvent::PIPELINE_STATE_CHANGED:
65 return "PIPELINE_STATE_CHANGED";
66 case MediaLogEvent::PIPELINE_ERROR:
67 return "PIPELINE_ERROR";
68 case MediaLogEvent::VIDEO_SIZE_SET:
69 return "VIDEO_SIZE_SET";
70 case MediaLogEvent::DURATION_SET:
71 return "DURATION_SET";
72 case MediaLogEvent::TOTAL_BYTES_SET:
73 return "TOTAL_BYTES_SET";
74 case MediaLogEvent::NETWORK_ACTIVITY_SET:
75 return "NETWORK_ACTIVITY_SET";
76 case MediaLogEvent::ENDED:
77 return "ENDED";
78 case MediaLogEvent::TEXT_ENDED:
79 return "TEXT_ENDED";
80 case MediaLogEvent::BUFFERED_EXTENTS_CHANGED:
81 return "BUFFERED_EXTENTS_CHANGED";
82 case MediaLogEvent::MEDIA_ERROR_LOG_ENTRY:
83 return "MEDIA_ERROR_LOG_ENTRY";
84 case MediaLogEvent::MEDIA_INFO_LOG_ENTRY:
85 return "MEDIA_INFO_LOG_ENTRY";
86 case MediaLogEvent::MEDIA_DEBUG_LOG_ENTRY:
87 return "MEDIA_DEBUG_LOG_ENTRY";
88 case MediaLogEvent::PROPERTY_CHANGE:
89 return "PROPERTY_CHANGE";
91 NOTREACHED();
92 return NULL;
95 std::string MediaLog::PipelineStatusToString(PipelineStatus status) {
96 switch (status) {
97 case PIPELINE_OK:
98 return "pipeline: ok";
99 case PIPELINE_ERROR_URL_NOT_FOUND:
100 return "pipeline: url not found";
101 case PIPELINE_ERROR_NETWORK:
102 return "pipeline: network error";
103 case PIPELINE_ERROR_DECODE:
104 return "pipeline: decode error";
105 case PIPELINE_ERROR_DECRYPT:
106 return "pipeline: decrypt error";
107 case PIPELINE_ERROR_ABORT:
108 return "pipeline: abort";
109 case PIPELINE_ERROR_INITIALIZATION_FAILED:
110 return "pipeline: initialization failed";
111 case PIPELINE_ERROR_COULD_NOT_RENDER:
112 return "pipeline: could not render";
113 case PIPELINE_ERROR_READ:
114 return "pipeline: read error";
115 case PIPELINE_ERROR_OPERATION_PENDING:
116 return "pipeline: operation pending";
117 case PIPELINE_ERROR_INVALID_STATE:
118 return "pipeline: invalid state";
119 case DEMUXER_ERROR_COULD_NOT_OPEN:
120 return "demuxer: could not open";
121 case DEMUXER_ERROR_COULD_NOT_PARSE:
122 return "demuxer: could not parse";
123 case DEMUXER_ERROR_NO_SUPPORTED_STREAMS:
124 return "demuxer: no supported streams";
125 case DECODER_ERROR_NOT_SUPPORTED:
126 return "decoder: not supported";
128 NOTREACHED();
129 return NULL;
132 std::string MediaLog::MediaEventToLogString(const MediaLogEvent& event) {
133 // Special case for PIPELINE_ERROR, since that's by far the most useful
134 // event for figuring out media pipeline failures, and just reporting
135 // pipeline status as numeric code is not very helpful/user-friendly.
136 int error_code = 0;
137 if (event.type == MediaLogEvent::PIPELINE_ERROR &&
138 event.params.GetInteger("pipeline_error", &error_code)) {
139 PipelineStatus status = static_cast<PipelineStatus>(error_code);
140 return EventTypeToString(event.type) + " " +
141 media::MediaLog::PipelineStatusToString(status);
143 std::string params_json;
144 base::JSONWriter::Write(&event.params, &params_json);
145 return EventTypeToString(event.type) + " " + params_json;
148 MediaLog::MediaLog() : id_(g_media_log_count.GetNext()) {}
150 MediaLog::~MediaLog() {}
152 void MediaLog::AddEvent(scoped_ptr<MediaLogEvent> event) {}
154 scoped_ptr<MediaLogEvent> MediaLog::CreateEvent(MediaLogEvent::Type type) {
155 scoped_ptr<MediaLogEvent> event(new MediaLogEvent);
156 event->id = id_;
157 event->type = type;
158 event->time = base::TimeTicks::Now();
159 return event.Pass();
162 scoped_ptr<MediaLogEvent> MediaLog::CreateBooleanEvent(
163 MediaLogEvent::Type type,
164 const std::string& property,
165 bool value) {
166 scoped_ptr<MediaLogEvent> event(CreateEvent(type));
167 event->params.SetBoolean(property, value);
168 return event.Pass();
171 scoped_ptr<MediaLogEvent> MediaLog::CreateStringEvent(
172 MediaLogEvent::Type type,
173 const std::string& property,
174 const std::string& value) {
175 scoped_ptr<MediaLogEvent> event(CreateEvent(type));
176 event->params.SetString(property, value);
177 return event.Pass();
180 scoped_ptr<MediaLogEvent> MediaLog::CreateTimeEvent(
181 MediaLogEvent::Type type,
182 const std::string& property,
183 base::TimeDelta value) {
184 scoped_ptr<MediaLogEvent> event(CreateEvent(type));
185 if (value.is_max())
186 event->params.SetString(property, "unknown");
187 else
188 event->params.SetDouble(property, value.InSecondsF());
189 return event.Pass();
192 scoped_ptr<MediaLogEvent> MediaLog::CreateLoadEvent(const std::string& url) {
193 scoped_ptr<MediaLogEvent> event(CreateEvent(MediaLogEvent::LOAD));
194 event->params.SetString("url", url);
195 return event.Pass();
198 scoped_ptr<MediaLogEvent> MediaLog::CreateSeekEvent(float seconds) {
199 scoped_ptr<MediaLogEvent> event(CreateEvent(MediaLogEvent::SEEK));
200 event->params.SetDouble("seek_target", seconds);
201 return event.Pass();
204 scoped_ptr<MediaLogEvent> MediaLog::CreatePipelineStateChangedEvent(
205 Pipeline::State state) {
206 scoped_ptr<MediaLogEvent> event(
207 CreateEvent(MediaLogEvent::PIPELINE_STATE_CHANGED));
208 event->params.SetString("pipeline_state", Pipeline::GetStateString(state));
209 return event.Pass();
212 scoped_ptr<MediaLogEvent> MediaLog::CreatePipelineErrorEvent(
213 PipelineStatus error) {
214 scoped_ptr<MediaLogEvent> event(CreateEvent(MediaLogEvent::PIPELINE_ERROR));
215 event->params.SetInteger("pipeline_error", error);
216 return event.Pass();
219 scoped_ptr<MediaLogEvent> MediaLog::CreateVideoSizeSetEvent(
220 size_t width, size_t height) {
221 scoped_ptr<MediaLogEvent> event(CreateEvent(MediaLogEvent::VIDEO_SIZE_SET));
222 event->params.SetInteger("width", width);
223 event->params.SetInteger("height", height);
224 return event.Pass();
227 scoped_ptr<MediaLogEvent> MediaLog::CreateBufferedExtentsChangedEvent(
228 int64 start, int64 current, int64 end) {
229 scoped_ptr<MediaLogEvent> event(
230 CreateEvent(MediaLogEvent::BUFFERED_EXTENTS_CHANGED));
231 // These values are headed to JS where there is no int64 so we use a double
232 // and accept loss of precision above 2^53 bytes (8 Exabytes).
233 event->params.SetDouble("buffer_start", start);
234 event->params.SetDouble("buffer_current", current);
235 event->params.SetDouble("buffer_end", end);
236 return event.Pass();
239 scoped_ptr<MediaLogEvent> MediaLog::CreateLogEvent(MediaLogLevel level,
240 const std::string& message) {
241 const MediaLogEvent::Type type = MediaLogLevelToEventType(level);
242 const std::string param = MediaLogLevelToString(level);
243 scoped_ptr<MediaLogEvent> event(CreateEvent(type));
244 event->params.SetString(param, message);
245 return event.Pass();
248 void MediaLog::SetStringProperty(
249 const std::string& key, const std::string& value) {
250 scoped_ptr<MediaLogEvent> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE));
251 event->params.SetString(key, value);
252 AddEvent(event.Pass());
255 void MediaLog::SetIntegerProperty(
256 const std::string& key, int value) {
257 scoped_ptr<MediaLogEvent> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE));
258 event->params.SetInteger(key, value);
259 AddEvent(event.Pass());
262 void MediaLog::SetDoubleProperty(
263 const std::string& key, double value) {
264 scoped_ptr<MediaLogEvent> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE));
265 event->params.SetDouble(key, value);
266 AddEvent(event.Pass());
269 void MediaLog::SetBooleanProperty(
270 const std::string& key, bool value) {
271 scoped_ptr<MediaLogEvent> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE));
272 event->params.SetBoolean(key, value);
273 AddEvent(event.Pass());
276 void MediaLog::SetTimeProperty(
277 const std::string& key, base::TimeDelta value) {
278 scoped_ptr<MediaLogEvent> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE));
279 if (value.is_max())
280 event->params.SetString(key, "unknown");
281 else
282 event->params.SetDouble(key, value.InSecondsF());
283 AddEvent(event.Pass());
286 LogHelper::LogHelper(MediaLog::MediaLogLevel level, const LogCB& log_cb)
287 : level_(level), log_cb_(log_cb) {
290 LogHelper::~LogHelper() {
291 if (log_cb_.is_null())
292 return;
293 log_cb_.Run(level_, stream_.str());
296 } //namespace media