summaryrefslogtreecommitdiffstats
path: root/media/cast/logging/raw_event_subscriber_bundle.cc
blob: 5dc5f793f5a7b311e00f8edb8daced4aa09881a2 (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
// Copyright 2014 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.

#include "media/cast/logging/raw_event_subscriber_bundle.h"

#include "media/cast/cast_environment.h"
#include "media/cast/logging/receiver_time_offset_estimator_impl.h"

namespace media {
namespace cast {

RawEventSubscriberBundleForStream::RawEventSubscriberBundleForStream(
    const scoped_refptr<CastEnvironment>& cast_environment,
    bool is_audio,
    ReceiverTimeOffsetEstimator* offset_estimator)
    : cast_environment_(cast_environment),
      event_subscriber_(
          is_audio ? AUDIO_EVENT : VIDEO_EVENT,
          is_audio ? kMaxAudioEventEntries : kMaxVideoEventEntries),
      stats_subscriber_(
          is_audio ? AUDIO_EVENT : VIDEO_EVENT,
          cast_environment->Clock(), offset_estimator) {
  cast_environment_->Logging()->AddRawEventSubscriber(&event_subscriber_);
  cast_environment_->Logging()->AddRawEventSubscriber(&stats_subscriber_);
}

RawEventSubscriberBundleForStream::~RawEventSubscriberBundleForStream() {
  cast_environment_->Logging()->RemoveRawEventSubscriber(&event_subscriber_);
  cast_environment_->Logging()->RemoveRawEventSubscriber(&stats_subscriber_);
}

EncodingEventSubscriber*
RawEventSubscriberBundleForStream::GetEncodingEventSubscriber() {
  return &event_subscriber_;
}

StatsEventSubscriber*
RawEventSubscriberBundleForStream::GetStatsEventSubscriber() {
  return &stats_subscriber_;
}

RawEventSubscriberBundle::RawEventSubscriberBundle(
    const scoped_refptr<CastEnvironment>& cast_environment)
    : cast_environment_(cast_environment) {}

RawEventSubscriberBundle::~RawEventSubscriberBundle() {
  if (receiver_offset_estimator_.get()) {
    cast_environment_->Logging()->RemoveRawEventSubscriber(
        receiver_offset_estimator_.get());
  }
}

void RawEventSubscriberBundle::AddEventSubscribers(bool is_audio) {
  if (!receiver_offset_estimator_.get()) {
    receiver_offset_estimator_.reset(new ReceiverTimeOffsetEstimatorImpl);
    cast_environment_->Logging()->AddRawEventSubscriber(
        receiver_offset_estimator_.get());
  }
  SubscribersMapByStream::iterator it = subscribers_.find(is_audio);
  if (it != subscribers_.end())
    return;

  subscribers_.insert(std::make_pair(
      is_audio,
      make_linked_ptr(new RawEventSubscriberBundleForStream(
          cast_environment_, is_audio, receiver_offset_estimator_.get()))));
}

void RawEventSubscriberBundle::RemoveEventSubscribers(bool is_audio) {
  SubscribersMapByStream::iterator it = subscribers_.find(is_audio);
  if (it == subscribers_.end())
    return;

  subscribers_.erase(it);
  if (subscribers_.empty()) {
    cast_environment_->Logging()->RemoveRawEventSubscriber(
        receiver_offset_estimator_.get());
    receiver_offset_estimator_.reset();
  }
}

EncodingEventSubscriber*
RawEventSubscriberBundle::GetEncodingEventSubscriber(bool is_audio) {
  SubscribersMapByStream::iterator it = subscribers_.find(is_audio);
  return it == subscribers_.end() ?
      NULL : it->second->GetEncodingEventSubscriber();
}

StatsEventSubscriber*
RawEventSubscriberBundle::GetStatsEventSubscriber(bool is_audio) {
  SubscribersMapByStream::iterator it = subscribers_.find(is_audio);
  return it == subscribers_.end() ?
      NULL : it->second->GetStatsEventSubscriber();
}

}  // namespace cast
}  // namespace media