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 "content/renderer/media/render_media_log.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "content/common/view_messages.h"
11 #include "content/public/renderer/render_thread.h"
15 RenderMediaLog::RenderMediaLog()
16 : render_loop_(base::MessageLoopProxy::current()),
17 tick_clock_(new base::DefaultTickClock()),
18 last_ipc_send_time_(tick_clock_
->NowTicks()) {
19 DCHECK(RenderThread::Get()) <<
20 "RenderMediaLog must be constructed on the render thread";
23 void RenderMediaLog::AddEvent(scoped_ptr
<media::MediaLogEvent
> event
) {
24 if (!RenderThread::Get()) {
25 render_loop_
->PostTask(FROM_HERE
, base::Bind(
26 &RenderMediaLog::AddEvent
, this, base::Passed(&event
)));
30 // Keep track of the latest buffered extents properties to avoid sending
31 // thousands of events over IPC. See http://crbug.com/352585 for details.
33 // TODO(scherkus): We should overhaul MediaLog entirely to have clearer
34 // separation of properties vs. events.
35 if (event
->type
== media::MediaLogEvent::BUFFERED_EXTENTS_CHANGED
)
36 last_buffered_extents_changed_event_
.swap(event
);
38 queued_media_events_
.push_back(*event
);
40 // Limit the send rate of high frequency events.
41 base::TimeTicks curr_time
= tick_clock_
->NowTicks();
42 if ((curr_time
- last_ipc_send_time_
) < base::TimeDelta::FromSeconds(1))
44 last_ipc_send_time_
= curr_time
;
46 if (last_buffered_extents_changed_event_
) {
47 queued_media_events_
.push_back(*last_buffered_extents_changed_event_
);
48 last_buffered_extents_changed_event_
.reset();
51 DVLOG(1) << "media log events array size " << queued_media_events_
.size();
53 RenderThread::Get()->Send(
54 new ViewHostMsg_MediaLogEvents(queued_media_events_
));
55 queued_media_events_
.clear();
58 RenderMediaLog::~RenderMediaLog() {}
60 void RenderMediaLog::SetTickClockForTesting(
61 scoped_ptr
<base::TickClock
> tick_clock
) {
62 tick_clock_
.swap(tick_clock
);
63 last_ipc_send_time_
= tick_clock_
->NowTicks();
66 } // namespace content