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_ABORT
:
104 return "pipeline: abort";
105 case PIPELINE_ERROR_INITIALIZATION_FAILED
:
106 return "pipeline: initialization failed";
107 case PIPELINE_ERROR_COULD_NOT_RENDER
:
108 return "pipeline: could not render";
109 case PIPELINE_ERROR_READ
:
110 return "pipeline: read error";
111 case PIPELINE_ERROR_OPERATION_PENDING
:
112 return "pipeline: operation pending";
113 case PIPELINE_ERROR_INVALID_STATE
:
114 return "pipeline: invalid state";
115 case DEMUXER_ERROR_COULD_NOT_OPEN
:
116 return "demuxer: could not open";
117 case DEMUXER_ERROR_COULD_NOT_PARSE
:
118 return "demuxer: could not parse";
119 case DEMUXER_ERROR_NO_SUPPORTED_STREAMS
:
120 return "demuxer: no supported streams";
121 case DECODER_ERROR_NOT_SUPPORTED
:
122 return "decoder: not supported";
128 std::string
MediaLog::MediaEventToLogString(const MediaLogEvent
& event
) {
129 // Special case for PIPELINE_ERROR, since that's by far the most useful
130 // event for figuring out media pipeline failures, and just reporting
131 // pipeline status as numeric code is not very helpful/user-friendly.
133 if (event
.type
== MediaLogEvent::PIPELINE_ERROR
&&
134 event
.params
.GetInteger("pipeline_error", &error_code
)) {
135 PipelineStatus status
= static_cast<PipelineStatus
>(error_code
);
136 return EventTypeToString(event
.type
) + " " +
137 media::MediaLog::PipelineStatusToString(status
);
139 std::string params_json
;
140 base::JSONWriter::Write(event
.params
, ¶ms_json
);
141 return EventTypeToString(event
.type
) + " " + params_json
;
144 MediaLog::MediaLog() : id_(g_media_log_count
.GetNext()) {}
146 MediaLog::~MediaLog() {}
148 void MediaLog::AddEvent(scoped_ptr
<MediaLogEvent
> event
) {}
150 scoped_ptr
<MediaLogEvent
> MediaLog::CreateEvent(MediaLogEvent::Type type
) {
151 scoped_ptr
<MediaLogEvent
> event(new MediaLogEvent
);
154 event
->time
= base::TimeTicks::Now();
158 scoped_ptr
<MediaLogEvent
> MediaLog::CreateBooleanEvent(
159 MediaLogEvent::Type type
,
160 const std::string
& property
,
162 scoped_ptr
<MediaLogEvent
> event(CreateEvent(type
));
163 event
->params
.SetBoolean(property
, value
);
167 scoped_ptr
<MediaLogEvent
> MediaLog::CreateStringEvent(
168 MediaLogEvent::Type type
,
169 const std::string
& property
,
170 const std::string
& value
) {
171 scoped_ptr
<MediaLogEvent
> event(CreateEvent(type
));
172 event
->params
.SetString(property
, value
);
176 scoped_ptr
<MediaLogEvent
> MediaLog::CreateTimeEvent(
177 MediaLogEvent::Type type
,
178 const std::string
& property
,
179 base::TimeDelta value
) {
180 scoped_ptr
<MediaLogEvent
> event(CreateEvent(type
));
182 event
->params
.SetString(property
, "unknown");
184 event
->params
.SetDouble(property
, value
.InSecondsF());
188 scoped_ptr
<MediaLogEvent
> MediaLog::CreateLoadEvent(const std::string
& url
) {
189 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::LOAD
));
190 event
->params
.SetString("url", url
);
194 scoped_ptr
<MediaLogEvent
> MediaLog::CreateSeekEvent(float seconds
) {
195 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::SEEK
));
196 event
->params
.SetDouble("seek_target", seconds
);
200 scoped_ptr
<MediaLogEvent
> MediaLog::CreatePipelineStateChangedEvent(
201 Pipeline::State state
) {
202 scoped_ptr
<MediaLogEvent
> event(
203 CreateEvent(MediaLogEvent::PIPELINE_STATE_CHANGED
));
204 event
->params
.SetString("pipeline_state", Pipeline::GetStateString(state
));
208 scoped_ptr
<MediaLogEvent
> MediaLog::CreatePipelineErrorEvent(
209 PipelineStatus error
) {
210 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::PIPELINE_ERROR
));
211 event
->params
.SetInteger("pipeline_error", error
);
215 scoped_ptr
<MediaLogEvent
> MediaLog::CreateVideoSizeSetEvent(
216 size_t width
, size_t height
) {
217 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::VIDEO_SIZE_SET
));
218 event
->params
.SetInteger("width", width
);
219 event
->params
.SetInteger("height", height
);
223 scoped_ptr
<MediaLogEvent
> MediaLog::CreateBufferedExtentsChangedEvent(
224 int64 start
, int64 current
, int64 end
) {
225 scoped_ptr
<MediaLogEvent
> event(
226 CreateEvent(MediaLogEvent::BUFFERED_EXTENTS_CHANGED
));
227 // These values are headed to JS where there is no int64 so we use a double
228 // and accept loss of precision above 2^53 bytes (8 Exabytes).
229 event
->params
.SetDouble("buffer_start", start
);
230 event
->params
.SetDouble("buffer_current", current
);
231 event
->params
.SetDouble("buffer_end", end
);
235 void MediaLog::AddLogEvent(MediaLogLevel level
, const std::string
& message
) {
236 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogLevelToEventType(level
)));
237 event
->params
.SetString(MediaLogLevelToString(level
), message
);
238 AddEvent(event
.Pass());
241 void MediaLog::SetStringProperty(
242 const std::string
& key
, const std::string
& value
) {
243 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE
));
244 event
->params
.SetString(key
, value
);
245 AddEvent(event
.Pass());
248 void MediaLog::SetIntegerProperty(
249 const std::string
& key
, int value
) {
250 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE
));
251 event
->params
.SetInteger(key
, value
);
252 AddEvent(event
.Pass());
255 void MediaLog::SetDoubleProperty(
256 const std::string
& key
, double value
) {
257 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE
));
258 event
->params
.SetDouble(key
, value
);
259 AddEvent(event
.Pass());
262 void MediaLog::SetBooleanProperty(
263 const std::string
& key
, bool value
) {
264 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE
));
265 event
->params
.SetBoolean(key
, value
);
266 AddEvent(event
.Pass());
269 void MediaLog::SetTimeProperty(
270 const std::string
& key
, base::TimeDelta value
) {
271 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE
));
273 event
->params
.SetString(key
, "unknown");
275 event
->params
.SetDouble(key
, value
.InSecondsF());
276 AddEvent(event
.Pass());
279 LogHelper::LogHelper(MediaLog::MediaLogLevel level
, const LogCB
& log_cb
)
280 : level_(level
), log_cb_(log_cb
) {
283 LogHelper::~LogHelper() {
284 if (log_cb_
.is_null())
286 log_cb_
.Run(level_
, stream_
.str());