summaryrefslogtreecommitdiffstats
path: root/media/cast/logging/log_event_dispatcher.h
blob: d84e79b7216082dfba9cc1aeeee5e97a591b2ec6 (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
// Copyright 2015 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 MEDIA_CAST_LOGGING_LOG_EVENT_DISPATCHER_H_
#define MEDIA_CAST_LOGGING_LOG_EVENT_DISPATCHER_H_

#include <vector>

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "media/cast/logging/logging_defines.h"
#include "media/cast/logging/raw_event_subscriber.h"

namespace media {
namespace cast {

class CastEnvironment;

// A thread-safe receiver of logging events that manages an active list of
// EventSubscribers and dispatches the logging events to them on the MAIN
// thread.  All methods, constructor, and destructor can be invoked on any
// thread.
class LogEventDispatcher {
 public:
  // |env| outlives this instance (and generally owns this instance).
  explicit LogEventDispatcher(CastEnvironment* env);

  ~LogEventDispatcher();

  // Called on any thread to schedule the sending of event(s) to all
  // EventSubscribers on the MAIN thread.
  void DispatchFrameEvent(scoped_ptr<FrameEvent> event) const;
  void DispatchPacketEvent(scoped_ptr<PacketEvent> event) const;
  void DispatchBatchOfEvents(
      scoped_ptr<std::vector<FrameEvent>> frame_events,
      scoped_ptr<std::vector<PacketEvent>> packet_events) const;

  // Adds |subscriber| to the active list to begin receiving events on MAIN
  // thread.  Unsubscribe() must be called before |subscriber| is destroyed.
  void Subscribe(RawEventSubscriber* subscriber);

  // Removes |subscriber| from the active list.  Once this method returns, the
  // |subscriber| is guaranteed not to receive any more events.
  void Unsubscribe(RawEventSubscriber* subscriber);

 private:
  // The part of the implementation that runs exclusively on the MAIN thread.
  class Impl : public base::RefCountedThreadSafe<Impl> {
   public:
    Impl();

    void DispatchFrameEvent(scoped_ptr<FrameEvent> event) const;
    void DispatchPacketEvent(scoped_ptr<PacketEvent> event) const;
    void DispatchBatchOfEvents(
        scoped_ptr<std::vector<FrameEvent>> frame_events,
        scoped_ptr<std::vector<PacketEvent>> packet_events) const;
    void Subscribe(RawEventSubscriber* subscriber);
    void Unsubscribe(RawEventSubscriber* subscriber);

   private:
    friend class base::RefCountedThreadSafe<Impl>;

    ~Impl();

    std::vector<RawEventSubscriber*> subscribers_;

    DISALLOW_COPY_AND_ASSIGN(Impl);
  };

  CastEnvironment* const env_;  // Owner of this instance.
  const scoped_refptr<Impl> impl_;

  DISALLOW_COPY_AND_ASSIGN(LogEventDispatcher);
};

}  // namespace cast
}  // namespace media

#endif  // MEDIA_CAST_LOGGING_LOG_EVENT_DISPATCHER_H_