• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "media/cast/logging/raw_event_subscriber_bundle.h"
6 
7 #include "media/cast/cast_environment.h"
8 #include "media/cast/logging/receiver_time_offset_estimator_impl.h"
9 
10 namespace media {
11 namespace cast {
12 
RawEventSubscriberBundleForStream(const scoped_refptr<CastEnvironment> & cast_environment,bool is_audio,ReceiverTimeOffsetEstimator * offset_estimator)13 RawEventSubscriberBundleForStream::RawEventSubscriberBundleForStream(
14     const scoped_refptr<CastEnvironment>& cast_environment,
15     bool is_audio,
16     ReceiverTimeOffsetEstimator* offset_estimator)
17     : cast_environment_(cast_environment),
18       event_subscriber_(
19           is_audio ? AUDIO_EVENT : VIDEO_EVENT,
20           is_audio ? kMaxAudioEventEntries : kMaxVideoEventEntries),
21       stats_subscriber_(
22           is_audio ? AUDIO_EVENT : VIDEO_EVENT,
23           cast_environment->Clock(), offset_estimator) {
24   cast_environment_->Logging()->AddRawEventSubscriber(&event_subscriber_);
25   cast_environment_->Logging()->AddRawEventSubscriber(&stats_subscriber_);
26 }
27 
~RawEventSubscriberBundleForStream()28 RawEventSubscriberBundleForStream::~RawEventSubscriberBundleForStream() {
29   cast_environment_->Logging()->RemoveRawEventSubscriber(&event_subscriber_);
30   cast_environment_->Logging()->RemoveRawEventSubscriber(&stats_subscriber_);
31 }
32 
33 EncodingEventSubscriber*
GetEncodingEventSubscriber()34 RawEventSubscriberBundleForStream::GetEncodingEventSubscriber() {
35   return &event_subscriber_;
36 }
37 
38 StatsEventSubscriber*
GetStatsEventSubscriber()39 RawEventSubscriberBundleForStream::GetStatsEventSubscriber() {
40   return &stats_subscriber_;
41 }
42 
RawEventSubscriberBundle(const scoped_refptr<CastEnvironment> & cast_environment)43 RawEventSubscriberBundle::RawEventSubscriberBundle(
44     const scoped_refptr<CastEnvironment>& cast_environment)
45     : cast_environment_(cast_environment) {}
46 
~RawEventSubscriberBundle()47 RawEventSubscriberBundle::~RawEventSubscriberBundle() {
48   if (receiver_offset_estimator_.get()) {
49     cast_environment_->Logging()->RemoveRawEventSubscriber(
50         receiver_offset_estimator_.get());
51   }
52 }
53 
AddEventSubscribers(bool is_audio)54 void RawEventSubscriberBundle::AddEventSubscribers(bool is_audio) {
55   if (!receiver_offset_estimator_.get()) {
56     receiver_offset_estimator_.reset(new ReceiverTimeOffsetEstimatorImpl);
57     cast_environment_->Logging()->AddRawEventSubscriber(
58         receiver_offset_estimator_.get());
59   }
60   SubscribersMapByStream::iterator it = subscribers_.find(is_audio);
61   if (it != subscribers_.end())
62     return;
63 
64   subscribers_.insert(std::make_pair(
65       is_audio,
66       make_linked_ptr(new RawEventSubscriberBundleForStream(
67           cast_environment_, is_audio, receiver_offset_estimator_.get()))));
68 }
69 
RemoveEventSubscribers(bool is_audio)70 void RawEventSubscriberBundle::RemoveEventSubscribers(bool is_audio) {
71   SubscribersMapByStream::iterator it = subscribers_.find(is_audio);
72   if (it == subscribers_.end())
73     return;
74 
75   subscribers_.erase(it);
76   if (subscribers_.empty()) {
77     cast_environment_->Logging()->RemoveRawEventSubscriber(
78         receiver_offset_estimator_.get());
79     receiver_offset_estimator_.reset();
80   }
81 }
82 
83 EncodingEventSubscriber*
GetEncodingEventSubscriber(bool is_audio)84 RawEventSubscriberBundle::GetEncodingEventSubscriber(bool is_audio) {
85   SubscribersMapByStream::iterator it = subscribers_.find(is_audio);
86   return it == subscribers_.end() ?
87       NULL : it->second->GetEncodingEventSubscriber();
88 }
89 
90 StatsEventSubscriber*
GetStatsEventSubscriber(bool is_audio)91 RawEventSubscriberBundle::GetStatsEventSubscriber(bool is_audio) {
92   SubscribersMapByStream::iterator it = subscribers_.find(is_audio);
93   return it == subscribers_.end() ?
94       NULL : it->second->GetStatsEventSubscriber();
95 }
96 
97 }  // namespace cast
98 }  // namespace media
99