• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "content/renderer/media/render_media_log.h"
6 
7 #include "base/bind.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"
12 
13 namespace content {
14 
RenderMediaLog()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";
21 }
22 
AddEvent(scoped_ptr<media::MediaLogEvent> event)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)));
27     return;
28   }
29 
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.
32   //
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);
37   else
38     queued_media_events_.push_back(*event);
39 
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))
43     return;
44   last_ipc_send_time_ = curr_time;
45 
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();
49   }
50 
51   DVLOG(1) << "media log events array size " << queued_media_events_.size();
52 
53   RenderThread::Get()->Send(
54       new ViewHostMsg_MediaLogEvents(queued_media_events_));
55   queued_media_events_.clear();
56 }
57 
~RenderMediaLog()58 RenderMediaLog::~RenderMediaLog() {}
59 
SetTickClockForTesting(scoped_ptr<base::TickClock> tick_clock)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();
64 }
65 
66 }  // namespace content
67