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"
8 #include "base/atomic_sequence_num.h"
9 #include "base/json/json_writer.h"
10 #include "base/values.h"
14 // A count of all MediaLogs created in the current process. Used to generate
16 static base::StaticAtomicSequenceNumber g_media_log_count
;
18 std::string
MediaLog::MediaLogLevelToString(MediaLogLevel level
) {
31 MediaLogEvent::Type
MediaLog::MediaLogLevelToEventType(MediaLogLevel level
) {
34 return MediaLogEvent::MEDIA_ERROR_LOG_ENTRY
;
36 return MediaLogEvent::MEDIA_INFO_LOG_ENTRY
;
38 return MediaLogEvent::MEDIA_DEBUG_LOG_ENTRY
;
41 return MediaLogEvent::MEDIA_ERROR_LOG_ENTRY
;
44 std::string
MediaLog::EventTypeToString(MediaLogEvent::Type type
) {
46 case MediaLogEvent::WEBMEDIAPLAYER_CREATED
:
47 return "WEBMEDIAPLAYER_CREATED";
48 case MediaLogEvent::WEBMEDIAPLAYER_DESTROYED
:
49 return "WEBMEDIAPLAYER_DESTROYED";
50 case MediaLogEvent::PIPELINE_CREATED
:
51 return "PIPELINE_CREATED";
52 case MediaLogEvent::PIPELINE_DESTROYED
:
53 return "PIPELINE_DESTROYED";
54 case MediaLogEvent::LOAD
:
56 case MediaLogEvent::SEEK
:
58 case MediaLogEvent::PLAY
:
60 case MediaLogEvent::PAUSE
:
62 case MediaLogEvent::PIPELINE_STATE_CHANGED
:
63 return "PIPELINE_STATE_CHANGED";
64 case MediaLogEvent::PIPELINE_ERROR
:
65 return "PIPELINE_ERROR";
66 case MediaLogEvent::VIDEO_SIZE_SET
:
67 return "VIDEO_SIZE_SET";
68 case MediaLogEvent::DURATION_SET
:
69 return "DURATION_SET";
70 case MediaLogEvent::TOTAL_BYTES_SET
:
71 return "TOTAL_BYTES_SET";
72 case MediaLogEvent::NETWORK_ACTIVITY_SET
:
73 return "NETWORK_ACTIVITY_SET";
74 case MediaLogEvent::ENDED
:
76 case MediaLogEvent::TEXT_ENDED
:
78 case MediaLogEvent::BUFFERED_EXTENTS_CHANGED
:
79 return "BUFFERED_EXTENTS_CHANGED";
80 case MediaLogEvent::MEDIA_ERROR_LOG_ENTRY
:
81 return "MEDIA_ERROR_LOG_ENTRY";
82 case MediaLogEvent::MEDIA_INFO_LOG_ENTRY
:
83 return "MEDIA_INFO_LOG_ENTRY";
84 case MediaLogEvent::MEDIA_DEBUG_LOG_ENTRY
:
85 return "MEDIA_DEBUG_LOG_ENTRY";
86 case MediaLogEvent::PROPERTY_CHANGE
:
87 return "PROPERTY_CHANGE";
93 std::string
MediaLog::PipelineStatusToString(PipelineStatus status
) {
96 return "pipeline: ok";
97 case PIPELINE_ERROR_URL_NOT_FOUND
:
98 return "pipeline: url not found";
99 case PIPELINE_ERROR_NETWORK
:
100 return "pipeline: network error";
101 case PIPELINE_ERROR_DECODE
:
102 return "pipeline: decode error";
103 case PIPELINE_ERROR_DECRYPT
:
104 return "pipeline: decrypt error";
105 case PIPELINE_ERROR_ABORT
:
106 return "pipeline: abort";
107 case PIPELINE_ERROR_INITIALIZATION_FAILED
:
108 return "pipeline: initialization failed";
109 case PIPELINE_ERROR_COULD_NOT_RENDER
:
110 return "pipeline: could not render";
111 case PIPELINE_ERROR_READ
:
112 return "pipeline: read error";
113 case PIPELINE_ERROR_OPERATION_PENDING
:
114 return "pipeline: operation pending";
115 case PIPELINE_ERROR_INVALID_STATE
:
116 return "pipeline: invalid state";
117 case DEMUXER_ERROR_COULD_NOT_OPEN
:
118 return "demuxer: could not open";
119 case DEMUXER_ERROR_COULD_NOT_PARSE
:
120 return "demuxer: could not parse";
121 case DEMUXER_ERROR_NO_SUPPORTED_STREAMS
:
122 return "demuxer: no supported streams";
123 case DECODER_ERROR_NOT_SUPPORTED
:
124 return "decoder: not supported";
130 std::string
MediaLog::MediaEventToLogString(const MediaLogEvent
& event
) {
131 // Special case for PIPELINE_ERROR, since that's by far the most useful
132 // event for figuring out media pipeline failures, and just reporting
133 // pipeline status as numeric code is not very helpful/user-friendly.
135 if (event
.type
== MediaLogEvent::PIPELINE_ERROR
&&
136 event
.params
.GetInteger("pipeline_error", &error_code
)) {
137 PipelineStatus status
= static_cast<PipelineStatus
>(error_code
);
138 return EventTypeToString(event
.type
) + " " +
139 media::MediaLog::PipelineStatusToString(status
);
141 std::string params_json
;
142 base::JSONWriter::Write(event
.params
, ¶ms_json
);
143 return EventTypeToString(event
.type
) + " " + params_json
;
146 MediaLog::MediaLog() : id_(g_media_log_count
.GetNext()) {}
148 MediaLog::~MediaLog() {}
150 void MediaLog::AddEvent(scoped_ptr
<MediaLogEvent
> event
) {}
152 scoped_ptr
<MediaLogEvent
> MediaLog::CreateEvent(MediaLogEvent::Type type
) {
153 scoped_ptr
<MediaLogEvent
> event(new MediaLogEvent
);
156 event
->time
= base::TimeTicks::Now();
160 scoped_ptr
<MediaLogEvent
> MediaLog::CreateBooleanEvent(
161 MediaLogEvent::Type type
,
162 const std::string
& property
,
164 scoped_ptr
<MediaLogEvent
> event(CreateEvent(type
));
165 event
->params
.SetBoolean(property
, value
);
169 scoped_ptr
<MediaLogEvent
> MediaLog::CreateStringEvent(
170 MediaLogEvent::Type type
,
171 const std::string
& property
,
172 const std::string
& value
) {
173 scoped_ptr
<MediaLogEvent
> event(CreateEvent(type
));
174 event
->params
.SetString(property
, value
);
178 scoped_ptr
<MediaLogEvent
> MediaLog::CreateTimeEvent(
179 MediaLogEvent::Type type
,
180 const std::string
& property
,
181 base::TimeDelta value
) {
182 scoped_ptr
<MediaLogEvent
> event(CreateEvent(type
));
184 event
->params
.SetString(property
, "unknown");
186 event
->params
.SetDouble(property
, value
.InSecondsF());
190 scoped_ptr
<MediaLogEvent
> MediaLog::CreateLoadEvent(const std::string
& url
) {
191 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::LOAD
));
192 event
->params
.SetString("url", url
);
196 scoped_ptr
<MediaLogEvent
> MediaLog::CreateSeekEvent(float seconds
) {
197 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::SEEK
));
198 event
->params
.SetDouble("seek_target", seconds
);
202 scoped_ptr
<MediaLogEvent
> MediaLog::CreatePipelineStateChangedEvent(
203 Pipeline::State state
) {
204 scoped_ptr
<MediaLogEvent
> event(
205 CreateEvent(MediaLogEvent::PIPELINE_STATE_CHANGED
));
206 event
->params
.SetString("pipeline_state", Pipeline::GetStateString(state
));
210 scoped_ptr
<MediaLogEvent
> MediaLog::CreatePipelineErrorEvent(
211 PipelineStatus error
) {
212 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::PIPELINE_ERROR
));
213 event
->params
.SetInteger("pipeline_error", error
);
217 scoped_ptr
<MediaLogEvent
> MediaLog::CreateVideoSizeSetEvent(
218 size_t width
, size_t height
) {
219 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::VIDEO_SIZE_SET
));
220 event
->params
.SetInteger("width", width
);
221 event
->params
.SetInteger("height", height
);
225 scoped_ptr
<MediaLogEvent
> MediaLog::CreateBufferedExtentsChangedEvent(
226 int64 start
, int64 current
, int64 end
) {
227 scoped_ptr
<MediaLogEvent
> event(
228 CreateEvent(MediaLogEvent::BUFFERED_EXTENTS_CHANGED
));
229 // These values are headed to JS where there is no int64 so we use a double
230 // and accept loss of precision above 2^53 bytes (8 Exabytes).
231 event
->params
.SetDouble("buffer_start", start
);
232 event
->params
.SetDouble("buffer_current", current
);
233 event
->params
.SetDouble("buffer_end", end
);
237 void MediaLog::AddLogEvent(MediaLogLevel level
, const std::string
& message
) {
238 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogLevelToEventType(level
)));
239 event
->params
.SetString(MediaLogLevelToString(level
), message
);
240 AddEvent(event
.Pass());
243 void MediaLog::SetStringProperty(
244 const std::string
& key
, const std::string
& value
) {
245 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE
));
246 event
->params
.SetString(key
, value
);
247 AddEvent(event
.Pass());
250 void MediaLog::SetIntegerProperty(
251 const std::string
& key
, int value
) {
252 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE
));
253 event
->params
.SetInteger(key
, value
);
254 AddEvent(event
.Pass());
257 void MediaLog::SetDoubleProperty(
258 const std::string
& key
, double value
) {
259 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE
));
260 event
->params
.SetDouble(key
, value
);
261 AddEvent(event
.Pass());
264 void MediaLog::SetBooleanProperty(
265 const std::string
& key
, bool value
) {
266 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE
));
267 event
->params
.SetBoolean(key
, value
);
268 AddEvent(event
.Pass());
271 void MediaLog::SetTimeProperty(
272 const std::string
& key
, base::TimeDelta value
) {
273 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE
));
275 event
->params
.SetString(key
, "unknown");
277 event
->params
.SetDouble(key
, value
.InSecondsF());
278 AddEvent(event
.Pass());
281 LogHelper::LogHelper(MediaLog::MediaLogLevel level
, const LogCB
& log_cb
)
282 : level_(level
), log_cb_(log_cb
) {
285 LogHelper::~LogHelper() {
286 if (log_cb_
.is_null())
288 log_cb_
.Run(level_
, stream_
.str());