summaryrefslogtreecommitdiffstats
path: root/chromium/content/renderer/media/batching_media_log.h
blob: b68535aea420e212918f291f199593a0deec0366 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_RENDERER_MEDIA_BATCHING_MEDIA_LOG_H_
#define CONTENT_RENDERER_MEDIA_BATCHING_MEDIA_LOG_H_

#include <string>
#include <utility>
#include <vector>

#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/single_thread_task_runner.h"
#include "base/time/time.h"
#include "content/common/content_export.h"
#include "media/base/media_log.h"
#include "url/gurl.h"

namespace base {
class TickClock;
}

namespace content {

// BatchingMediaLog is an implementation of MediaLog that sends messages
// grouped together in order to reduce IPC pressure.
// In order to subclass it, a subclass of the BatchingMediaLog::EventHandler
// should implement behavior when recieving a group of messages.
//
// It must be constructed on the render thread.
class CONTENT_EXPORT BatchingMediaLog : public media::MediaLog {
 public:
  class EventHandler {
   public:
    virtual ~EventHandler() = default;
    virtual void SendQueuedMediaEvents(std::vector<media::MediaLogRecord>) = 0;
    virtual void OnWebMediaPlayerDestroyed() = 0;
  };

  BatchingMediaLog(const GURL& security_origin,
                   scoped_refptr<base::SingleThreadTaskRunner> task_runner,
                   std::vector<std::unique_ptr<EventHandler>> impl);
  ~BatchingMediaLog() override;

  // Will reset |last_ipc_send_time_| with the value of NowTicks().
  void SetTickClockForTesting(const base::TickClock* tick_clock);

 protected:
  // MediaLog implementation.
  void AddLogRecordLocked(
      std::unique_ptr<media::MediaLogRecord> event) override;
  void OnWebMediaPlayerDestroyedLocked() override;
  std::string GetErrorMessageLocked() override;

 private:
  // Posted as a delayed task on |task_runner_| to throttle ipc message
  // frequency.
  void SendQueuedMediaEvents();

  std::string MediaEventToMessageString(const media::MediaLogRecord& event);

  // Security origin of the current frame.
  const GURL security_origin_;

  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;

  // |lock_| protects access to all of the following member variables.  It
  // allows any render process thread to AddEvent(), while preserving their
  // sequence for throttled send on |task_runner_| and coherent retrieval by
  // GetErrorMessage().  This is needed in addition to the synchronization
  // guarantees provided by MediaLog, since SendQueuedMediaEvents must also
  // be synchronized with respect to AddEvent.
  mutable base::Lock lock_;
  const base::TickClock* tick_clock_ GUARDED_BY(LOCK);
  base::TimeTicks last_ipc_send_time_ GUARDED_BY(LOCK);
  std::vector<media::MediaLogRecord> queued_media_events_ GUARDED_BY(LOCK);

  // impl for sending queued events.
  std::vector<std::unique_ptr<EventHandler>> event_handlers_ GUARDED_BY(LOCK);

  // For enforcing max 1 pending send.
  bool ipc_send_pending_ GUARDED_BY(LOCK);

  // Limits the number of events we send over IPC to one.
  std::unique_ptr<media::MediaLogRecord> last_duration_changed_event_ GUARDED_BY(LOCK);

  // Holds the earliest MEDIA_ERROR_LOG_ENTRY event added to this log. This is
  // most likely to contain the most specific information available describing
  // any eventual fatal error.
  // TODO(wolenetz): Introduce a reset method to clear this in cases like
  // non-fatal error recovery like decoder fallback.
  std::unique_ptr<media::MediaLogRecord> cached_media_error_for_message_;

  // Holds a copy of the most recent PIPELINE_ERROR, if any.
  std::unique_ptr<media::MediaLogRecord> last_pipeline_error_;

  base::WeakPtr<BatchingMediaLog> weak_this_;
  base::WeakPtrFactory<BatchingMediaLog> weak_factory_{this};

  DISALLOW_COPY_AND_ASSIGN(BatchingMediaLog);
};

}  // namespace content

#endif  // CONTENT_RENDERER_MEDIA_BATCHING_MEDIA_LOG_H_