File: audio_mirroring_manager.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (214 lines) | stat: -rw-r--r-- 7,288 bytes parent folder | download | duplicates (2)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// 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/media/capture/audio_mirroring_manager.h"

#include <algorithm>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/lazy_instance.h"

namespace content {

namespace {

base::LazyInstance<AudioMirroringManager>::Leaky g_audio_mirroring_manager =
    LAZY_INSTANCE_INITIALIZER;

}  // namespace

// static
AudioMirroringManager* AudioMirroringManager::GetInstance() {
  return g_audio_mirroring_manager.Pointer();
}

AudioMirroringManager::AudioMirroringManager() {
  // Only *after* construction, check that AudioMirroringManager is being
  // invoked on the same single thread.
  thread_checker_.DetachFromThread();
}

AudioMirroringManager::~AudioMirroringManager() {}

void AudioMirroringManager::AddDiverter(
    int render_process_id, int render_frame_id, Diverter* diverter) {
  DCHECK(thread_checker_.CalledOnValidThread());
  DCHECK(diverter);

  // DCHECK(diverter not already in routes_)
#ifndef NDEBUG
  for (StreamRoutes::const_iterator it = routes_.begin();
       it != routes_.end(); ++it) {
    DCHECK_NE(diverter, it->diverter);
  }
#endif
  routes_.push_back(StreamRoutingState(
      SourceFrameRef(render_process_id, render_frame_id),
      diverter));

  // Query existing destinations to see whether to immediately start diverting
  // the stream.
  std::set<SourceFrameRef> candidates;
  candidates.insert(routes_.back().source_render_frame);
  InitiateQueriesToFindNewDestination(NULL, candidates);
}

void AudioMirroringManager::RemoveDiverter(Diverter* diverter) {
  DCHECK(thread_checker_.CalledOnValidThread());

  // Find and remove the entry from the routing table.  If the stream is being
  // diverted, it is stopped.
  for (StreamRoutes::iterator it = routes_.begin(); it != routes_.end(); ++it) {
    if (it->diverter == diverter) {
      ChangeRoute(&(*it), NULL);
      routes_.erase(it);
      return;
    }
  }
  NOTREACHED();
}

void AudioMirroringManager::StartMirroring(MirroringDestination* destination) {
  DCHECK(thread_checker_.CalledOnValidThread());
  DCHECK(destination);

  // Insert an entry into the set of active mirroring sessions, if this is a
  // previously-unknown destination.
  if (std::find(sessions_.begin(), sessions_.end(), destination) ==
          sessions_.end()) {
    sessions_.push_back(destination);
  }

  // Query the MirroringDestination to see which of the audio streams should be
  // diverted.
  std::set<SourceFrameRef> candidates;
  for (StreamRoutes::const_iterator it = routes_.begin(); it != routes_.end();
       ++it) {
    if (!it->destination || it->destination == destination)
      candidates.insert(it->source_render_frame);
  }
  if (!candidates.empty()) {
    destination->QueryForMatches(
        candidates,
        base::Bind(&AudioMirroringManager::UpdateRoutesToDestination,
                   base::Unretained(this),
                   destination,
                   false));
  }
}

void AudioMirroringManager::StopMirroring(MirroringDestination* destination) {
  DCHECK(thread_checker_.CalledOnValidThread());

  // Stop diverting each audio stream in the mirroring session being stopped.
  // Each stopped stream becomes a candidate to be diverted to another
  // destination.
  std::set<SourceFrameRef> redivert_candidates;
  for (StreamRoutes::iterator it = routes_.begin(); it != routes_.end(); ++it) {
    if (it->destination == destination) {
      ChangeRoute(&(*it), NULL);
      redivert_candidates.insert(it->source_render_frame);
    }
  }
  if (!redivert_candidates.empty())
    InitiateQueriesToFindNewDestination(destination, redivert_candidates);

  // Remove the entry from the set of active mirroring sessions.
  const Destinations::iterator dest_it =
      std::find(sessions_.begin(), sessions_.end(), destination);
  if (dest_it == sessions_.end()) {
    NOTREACHED();
    return;
  }
  sessions_.erase(dest_it);
}

void AudioMirroringManager::InitiateQueriesToFindNewDestination(
    MirroringDestination* old_destination,
    const std::set<SourceFrameRef>& candidates) {
  DCHECK(thread_checker_.CalledOnValidThread());

  for (Destinations::const_iterator it = sessions_.begin();
       it != sessions_.end(); ++it) {
    if (*it != old_destination) {
      (*it)->QueryForMatches(
          candidates,
          base::Bind(&AudioMirroringManager::UpdateRoutesToDestination,
                     base::Unretained(this),
                     *it,
                     true));
    }
  }
}

void AudioMirroringManager::UpdateRoutesToDestination(
    MirroringDestination* destination,
    bool add_only,
    const std::set<SourceFrameRef>& matches) {
  DCHECK(thread_checker_.CalledOnValidThread());

  if (std::find(sessions_.begin(), sessions_.end(), destination) ==
          sessions_.end()) {
    return;  // Query result callback invoked after StopMirroring().
  }

  DVLOG(1) << (add_only ? "Add " : "Replace with ") << matches.size()
           << " routes to MirroringDestination@" << destination;

  // Start/stop diverting based on |matches|.  Any stopped stream becomes a
  // candidate to be diverted to another destination.
  std::set<SourceFrameRef> redivert_candidates;
  for (StreamRoutes::iterator it = routes_.begin(); it != routes_.end(); ++it) {
    if (matches.find(it->source_render_frame) != matches.end()) {
      // Only change the route if the stream is not already being diverted.
      if (!it->destination)
        ChangeRoute(&(*it), destination);
    } else if (!add_only) {
      // Only stop diverting if the stream is currently routed to |destination|.
      if (it->destination == destination) {
        ChangeRoute(&(*it), NULL);
        redivert_candidates.insert(it->source_render_frame);
      }
    }
  }
  if (!redivert_candidates.empty())
    InitiateQueriesToFindNewDestination(destination, redivert_candidates);
}

// static
void AudioMirroringManager::ChangeRoute(
    StreamRoutingState* route, MirroringDestination* new_destination) {
  if (route->destination == new_destination)
    return;  // No change.

  if (route->destination) {
    DVLOG(1) << "Stop diverting render_process_id:render_frame_id="
             << route->source_render_frame.first << ':'
             << route->source_render_frame.second
             << " --> MirroringDestination@" << route->destination;
    route->diverter->StopDiverting();
    route->destination = NULL;
  }

  if (new_destination) {
      DVLOG(1) << "Start diverting of render_process_id:render_frame_id="
               << route->source_render_frame.first << ':'
               << route->source_render_frame.second
               << " --> MirroringDestination@" << new_destination;
    route->diverter->StartDiverting(
        new_destination->AddInput(route->diverter->GetAudioParameters()));
    route->destination = new_destination;
  }
}

AudioMirroringManager::StreamRoutingState::StreamRoutingState(
    const SourceFrameRef& source_frame, Diverter* stream_diverter)
  : source_render_frame(source_frame),
    diverter(stream_diverter),
    destination(NULL) {}

AudioMirroringManager::StreamRoutingState::~StreamRoutingState() {}

}  // namespace content