Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / cast / rtcp / receiver_rtcp_event_subscriber.h
1 // Copyright 2014 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 #ifndef MEDIA_CAST_RTCP_RECEIVER_RTCP_EVENT_SUBSCRIBER_H_
6 #define MEDIA_CAST_RTCP_RECEIVER_RTCP_EVENT_SUBSCRIBER_H_
7
8 #include <map>
9
10 #include "base/threading/thread_checker.h"
11 #include "media/cast/logging/logging_defines.h"
12 #include "media/cast/logging/raw_event_subscriber.h"
13 #include "media/cast/rtcp/rtcp_defines.h"
14
15 namespace base {
16 class SingleThreadTaskRunner;
17 }
18
19 namespace media {
20 namespace cast {
21
22 // A RawEventSubscriber implementation with the following properties:
23 // - Only processes raw event types that are relevant for sending from cast
24 //   receiver to cast sender via RTCP.
25 // - Captures information to be sent over to RTCP from raw event logs into the
26 //   more compact RtcpEvent struct.
27 // - Orders events by RTP timestamp with a multimap.
28 // - Internally, the map is capped at a maximum size configurable by the caller.
29 //   The subscriber only keeps the most recent events (determined by RTP
30 //   timestamp) up to the size limit.
31 class ReceiverRtcpEventSubscriber : public RawEventSubscriber {
32  public:
33   // Identifies whether the subscriber will process audio or video related
34   // frame events.
35   enum Type {
36     kAudioEventSubscriber,  // Only processes audio events
37     kVideoEventSubscriber   // Only processes video events
38   };
39
40   // |max_size_to_retain|: The object will keep up to |max_size_to_retain|
41   // events
42   // in the map. Once threshold has been reached, an event with the smallest
43   // RTP timestamp will be removed.
44   // |type|: Determines whether the subscriber will process only audio or video
45   // events.
46   ReceiverRtcpEventSubscriber(const size_t max_size_to_retain, Type type);
47
48   virtual ~ReceiverRtcpEventSubscriber();
49
50   // RawEventSubscriber implementation.
51   virtual void OnReceiveFrameEvent(const FrameEvent& frame_event) OVERRIDE;
52   virtual void OnReceivePacketEvent(const PacketEvent& packet_event) OVERRIDE;
53   virtual void OnReceiveGenericEvent(const GenericEvent& generic_event)
54       OVERRIDE;
55
56   // Converts all collected events since last invocation into
57   // a RtcpReceiverFrameLogMessage, assigns it to |receiver_log|, and clears
58   // |rtcp_events_|.
59   void GetReceiverLogMessageAndReset(RtcpReceiverLogMessage* receiver_log);
60
61  private:
62   // If |rtcp_events_.size()| exceeds |max_size_to_retain_|, remove an oldest
63   // entry (determined by RTP timestamp) so its size no greater than
64   // |max_size_to_retain_|.
65   void TruncateMapIfNeeded();
66
67   // Returns |true| if events of |event_type| should be processed.
68   bool ShouldProcessEvent(CastLoggingEvent event_type);
69
70   const size_t max_size_to_retain_;
71   Type type_;
72
73   // The key should really be something more than just a RTP timestamp in order
74   // to differentiate between video and audio frames, but since the
75   // implementation doesn't mix audio and video frame events, RTP timestamp
76   // only as key is fine.
77   std::multimap<RtpTimestamp, RtcpEvent> rtcp_events_;
78
79   // Ensures methods are only called on the main thread.
80   base::ThreadChecker thread_checker_;
81
82   DISALLOW_COPY_AND_ASSIGN(ReceiverRtcpEventSubscriber);
83 };
84
85 }  // namespace cast
86 }  // namespace media
87
88 #endif  // MEDIA_CAST_RTCP_RECEIVER_RTCP_EVENT_SUBSCRIBER_H_