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"
9 #include "base/atomic_sequence_num.h"
10 #include "base/logging.h"
11 #include "base/values.h"
15 // A count of all MediaLogs created in the current process. Used to generate
17 static base::StaticAtomicSequenceNumber g_media_log_count
;
19 std::string
MediaLog::EventTypeToString(MediaLogEvent::Type type
) {
21 case MediaLogEvent::WEBMEDIAPLAYER_CREATED
:
22 return "WEBMEDIAPLAYER_CREATED";
23 case MediaLogEvent::WEBMEDIAPLAYER_DESTROYED
:
24 return "WEBMEDIAPLAYER_DESTROYED";
25 case MediaLogEvent::PIPELINE_CREATED
:
26 return "PIPELINE_CREATED";
27 case MediaLogEvent::PIPELINE_DESTROYED
:
28 return "PIPELINE_DESTROYED";
29 case MediaLogEvent::LOAD
:
31 case MediaLogEvent::SEEK
:
33 case MediaLogEvent::PLAY
:
35 case MediaLogEvent::PAUSE
:
37 case MediaLogEvent::PIPELINE_STATE_CHANGED
:
38 return "PIPELINE_STATE_CHANGED";
39 case MediaLogEvent::PIPELINE_ERROR
:
40 return "PIPELINE_ERROR";
41 case MediaLogEvent::VIDEO_SIZE_SET
:
42 return "VIDEO_SIZE_SET";
43 case MediaLogEvent::DURATION_SET
:
44 return "DURATION_SET";
45 case MediaLogEvent::TOTAL_BYTES_SET
:
46 return "TOTAL_BYTES_SET";
47 case MediaLogEvent::NETWORK_ACTIVITY_SET
:
48 return "NETWORK_ACTIVITY_SET";
49 case MediaLogEvent::ENDED
:
51 case MediaLogEvent::TEXT_ENDED
:
53 case MediaLogEvent::BUFFERED_EXTENTS_CHANGED
:
54 return "BUFFERED_EXTENTS_CHANGED";
55 case MediaLogEvent::MEDIA_SOURCE_ERROR
:
56 return "MEDIA_SOURCE_ERROR";
57 case MediaLogEvent::PROPERTY_CHANGE
:
58 return "PROPERTY_CHANGE";
64 std::string
MediaLog::PipelineStatusToString(PipelineStatus status
) {
67 return "pipeline: ok";
68 case PIPELINE_ERROR_URL_NOT_FOUND
:
69 return "pipeline: url not found";
70 case PIPELINE_ERROR_NETWORK
:
71 return "pipeline: network error";
72 case PIPELINE_ERROR_DECODE
:
73 return "pipeline: decode error";
74 case PIPELINE_ERROR_DECRYPT
:
75 return "pipeline: decrypt error";
76 case PIPELINE_ERROR_ABORT
:
77 return "pipeline: abort";
78 case PIPELINE_ERROR_INITIALIZATION_FAILED
:
79 return "pipeline: initialization failed";
80 case PIPELINE_ERROR_COULD_NOT_RENDER
:
81 return "pipeline: could not render";
82 case PIPELINE_ERROR_READ
:
83 return "pipeline: read error";
84 case PIPELINE_ERROR_OPERATION_PENDING
:
85 return "pipeline: operation pending";
86 case PIPELINE_ERROR_INVALID_STATE
:
87 return "pipeline: invalid state";
88 case DEMUXER_ERROR_COULD_NOT_OPEN
:
89 return "demuxer: could not open";
90 case DEMUXER_ERROR_COULD_NOT_PARSE
:
91 return "demuxer: could not parse";
92 case DEMUXER_ERROR_NO_SUPPORTED_STREAMS
:
93 return "demuxer: no supported streams";
94 case DECODER_ERROR_NOT_SUPPORTED
:
95 return "decoder: not supported";
101 LogHelper::LogHelper(const LogCB
& log_cb
) : log_cb_(log_cb
) {}
103 LogHelper::~LogHelper() {
104 if (log_cb_
.is_null())
106 log_cb_
.Run(stream_
.str());
109 MediaLog::MediaLog() : id_(g_media_log_count
.GetNext()) {}
111 MediaLog::~MediaLog() {}
113 void MediaLog::AddEvent(scoped_ptr
<MediaLogEvent
> event
) {}
115 scoped_ptr
<MediaLogEvent
> MediaLog::CreateEvent(MediaLogEvent::Type type
) {
116 scoped_ptr
<MediaLogEvent
> event(new MediaLogEvent
);
119 event
->time
= base::TimeTicks::Now();
123 scoped_ptr
<MediaLogEvent
> MediaLog::CreateBooleanEvent(
124 MediaLogEvent::Type type
,
125 const std::string
& property
,
127 scoped_ptr
<MediaLogEvent
> event(CreateEvent(type
));
128 event
->params
.SetBoolean(property
, value
);
132 scoped_ptr
<MediaLogEvent
> MediaLog::CreateStringEvent(
133 MediaLogEvent::Type type
,
134 const std::string
& property
,
135 const std::string
& value
) {
136 scoped_ptr
<MediaLogEvent
> event(CreateEvent(type
));
137 event
->params
.SetString(property
, value
);
141 scoped_ptr
<MediaLogEvent
> MediaLog::CreateTimeEvent(
142 MediaLogEvent::Type type
,
143 const std::string
& property
,
144 base::TimeDelta value
) {
145 scoped_ptr
<MediaLogEvent
> event(CreateEvent(type
));
147 event
->params
.SetString(property
, "unknown");
149 event
->params
.SetDouble(property
, value
.InSecondsF());
153 scoped_ptr
<MediaLogEvent
> MediaLog::CreateLoadEvent(const std::string
& url
) {
154 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::LOAD
));
155 event
->params
.SetString("url", url
);
159 scoped_ptr
<MediaLogEvent
> MediaLog::CreateSeekEvent(float seconds
) {
160 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::SEEK
));
161 event
->params
.SetDouble("seek_target", seconds
);
165 scoped_ptr
<MediaLogEvent
> MediaLog::CreatePipelineStateChangedEvent(
166 Pipeline::State state
) {
167 scoped_ptr
<MediaLogEvent
> event(
168 CreateEvent(MediaLogEvent::PIPELINE_STATE_CHANGED
));
169 event
->params
.SetString("pipeline_state", Pipeline::GetStateString(state
));
173 scoped_ptr
<MediaLogEvent
> MediaLog::CreatePipelineErrorEvent(
174 PipelineStatus error
) {
175 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::PIPELINE_ERROR
));
176 event
->params
.SetInteger("pipeline_error", error
);
180 scoped_ptr
<MediaLogEvent
> MediaLog::CreateVideoSizeSetEvent(
181 size_t width
, size_t height
) {
182 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::VIDEO_SIZE_SET
));
183 event
->params
.SetInteger("width", width
);
184 event
->params
.SetInteger("height", height
);
188 scoped_ptr
<MediaLogEvent
> MediaLog::CreateBufferedExtentsChangedEvent(
189 int64 start
, int64 current
, int64 end
) {
190 scoped_ptr
<MediaLogEvent
> event(
191 CreateEvent(MediaLogEvent::BUFFERED_EXTENTS_CHANGED
));
192 // These values are headed to JS where there is no int64 so we use a double
193 // and accept loss of precision above 2^53 bytes (8 Exabytes).
194 event
->params
.SetDouble("buffer_start", start
);
195 event
->params
.SetDouble("buffer_current", current
);
196 event
->params
.SetDouble("buffer_end", end
);
200 scoped_ptr
<MediaLogEvent
> MediaLog::CreateMediaSourceErrorEvent(
201 const std::string
& error
) {
202 scoped_ptr
<MediaLogEvent
> event(
203 CreateEvent(MediaLogEvent::MEDIA_SOURCE_ERROR
));
204 event
->params
.SetString("error", error
);
208 void MediaLog::SetStringProperty(
209 const std::string
& key
, const std::string
& value
) {
210 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE
));
211 event
->params
.SetString(key
, value
);
212 AddEvent(event
.Pass());
215 void MediaLog::SetIntegerProperty(
216 const std::string
& key
, int value
) {
217 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE
));
218 event
->params
.SetInteger(key
, value
);
219 AddEvent(event
.Pass());
222 void MediaLog::SetDoubleProperty(
223 const std::string
& key
, double value
) {
224 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE
));
225 event
->params
.SetDouble(key
, value
);
226 AddEvent(event
.Pass());
229 void MediaLog::SetBooleanProperty(
230 const std::string
& key
, bool value
) {
231 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE
));
232 event
->params
.SetBoolean(key
, value
);
233 AddEvent(event
.Pass());
236 void MediaLog::SetTimeProperty(
237 const std::string
& key
, base::TimeDelta value
) {
238 scoped_ptr
<MediaLogEvent
> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE
));
240 event
->params
.SetString(key
, "unknown");
242 event
->params
.SetDouble(key
, value
.InSecondsF());
243 AddEvent(event
.Pass());