summaryrefslogtreecommitdiffstats
path: root/content/browser/renderer_host/media/audio_mirroring_manager.cc
blob: 8a2bc4b0b5c69c80a656e23c99334f40d5759fbd (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) 2013 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 "content/browser/renderer_host/media/audio_mirroring_manager.h"

#include "content/public/browser/browser_thread.h"

namespace content {

namespace {

// Debug utility to make sure methods of AudioMirroringManager are not invoked
// more than once in a single call stack.  In release builds, this compiles to
// nothing and gets completely optimized out.
class ReentrancyGuard {
 public:
#ifdef NDEBUG
  ReentrancyGuard() {}
  ~ReentrancyGuard() {}
#else
  ReentrancyGuard() {
    DCHECK(!inside_a_method_);
    inside_a_method_ = true;
  }
  ~ReentrancyGuard() {
    inside_a_method_ = false;
  }

  static bool inside_a_method_;  // Safe to be static, since AMM is a singleton.
#endif
};

#ifndef NDEBUG
bool ReentrancyGuard::inside_a_method_ = false;
#endif

}  // namespace

AudioMirroringManager::AudioMirroringManager() {}

AudioMirroringManager::~AudioMirroringManager() {
  DCHECK(diverters_.empty());
  DCHECK(sessions_.empty());
}

void AudioMirroringManager::AddDiverter(
    int render_process_id, int render_view_id, Diverter* diverter) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  ReentrancyGuard guard;
  DCHECK(diverter);

  // DCHECK(diverter not already in diverters_ under any key)
#ifndef NDEBUG
  for (DiverterMap::const_iterator it = diverters_.begin();
       it != diverters_.end(); ++it) {
    DCHECK_NE(diverter, it->second);
  }
#endif

  // Add the diverter to the set of active diverters.
  const Target target(render_process_id, render_view_id);
  diverters_.insert(std::make_pair(target, diverter));

  // If a mirroring session is active, start diverting the audio stream
  // immediately.
  SessionMap::iterator session_it = sessions_.find(target);
  if (session_it != sessions_.end()) {
    diverter->StartDiverting(
        session_it->second->AddInput(diverter->GetAudioParameters()));
  }
}

void AudioMirroringManager::RemoveDiverter(
    int render_process_id, int render_view_id, Diverter* diverter) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  ReentrancyGuard guard;

  // Stop diverting the audio stream if a mirroring session is active.
  const Target target(render_process_id, render_view_id);
  SessionMap::iterator session_it = sessions_.find(target);
  if (session_it != sessions_.end())
    diverter->StopDiverting();

  // Remove the diverter from the set of active diverters.
  for (DiverterMap::iterator it = diverters_.lower_bound(target);
       it != diverters_.end() && it->first == target; ++it) {
    if (it->second == diverter) {
      diverters_.erase(it);
      break;
    }
  }
}

void AudioMirroringManager::StartMirroring(
    int render_process_id, int render_view_id,
    MirroringDestination* destination) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  ReentrancyGuard guard;
  DCHECK(destination);

  // Insert an entry into the set of active mirroring sessions.  If a mirroring
  // session is already active for |render_process_id| + |render_view_id|,
  // replace the entry.
  const Target target(render_process_id, render_view_id);
  SessionMap::iterator session_it = sessions_.find(target);
  MirroringDestination* old_destination;
  if (session_it == sessions_.end()) {
    old_destination = NULL;
    sessions_.insert(std::make_pair(target, destination));

    DVLOG(1) << "Start mirroring render_process_id:render_view_id="
             << render_process_id << ':' << render_view_id
             << " --> MirroringDestination@" << destination;
  } else {
    old_destination = session_it->second;
    session_it->second = destination;

    DVLOG(1) << "Switch mirroring of render_process_id:render_view_id="
             << render_process_id << ':' << render_view_id
             << "  MirroringDestination@" << old_destination
             << " --> MirroringDestination@" << destination;
  }

  // Divert audio streams coming from |target| to |destination|.  If streams
  // were already diverted to the |old_destination|, remove them.
  for (DiverterMap::iterator it = diverters_.lower_bound(target);
       it != diverters_.end() && it->first == target; ++it) {
    Diverter* const diverter = it->second;
    if (old_destination)
      diverter->StopDiverting();
    diverter->StartDiverting(
        destination->AddInput(diverter->GetAudioParameters()));
  }
}

void AudioMirroringManager::StopMirroring(
    int render_process_id, int render_view_id,
    MirroringDestination* destination) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  ReentrancyGuard guard;

  // Stop mirroring if there is an active session *and* the destination
  // matches.
  const Target target(render_process_id, render_view_id);
  SessionMap::iterator session_it = sessions_.find(target);
  if (session_it == sessions_.end() || destination != session_it->second)
    return;

  DVLOG(1) << "Stop mirroring render_process_id:render_view_id="
           << render_process_id << ':' << render_view_id
           << " --> MirroringDestination@" << destination;

  // Stop diverting each audio stream in the mirroring session being stopped.
  for (DiverterMap::iterator it = diverters_.lower_bound(target);
       it != diverters_.end() && it->first == target; ++it) {
    it->second->StopDiverting();
  }

  // Remove the entry from the set of active mirroring sessions.
  sessions_.erase(session_it);
}

}  // namespace content