summaryrefslogtreecommitdiffstats
path: root/dom/media/MediaStreamWindowCapturer.cpp
blob: 0e3eca801b63b8490c322ecf0f7aea0b94500f53 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "MediaStreamWindowCapturer.h"

#include "AudioStreamTrack.h"
#include "DOMMediaStream.h"
#include "MediaTrackGraph.h"

namespace mozilla {
using dom::AudioStreamTrack;
using dom::MediaStreamTrack;

NS_IMPL_CYCLE_COLLECTION_INHERITED(MediaStreamWindowCapturer,
                                   DOMMediaStream::TrackListener)
NS_IMPL_ADDREF_INHERITED(MediaStreamWindowCapturer,
                         DOMMediaStream::TrackListener)
NS_IMPL_RELEASE_INHERITED(MediaStreamWindowCapturer,
                          DOMMediaStream::TrackListener)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaStreamWindowCapturer)
NS_INTERFACE_MAP_END_INHERITING(DOMMediaStream::TrackListener)

MediaStreamWindowCapturer::CapturedTrack::CapturedTrack(
    MediaStreamTrack* aTrack, uint64_t aWindowID)
    : mTrack(aTrack),
      mPort(aTrack->Graph()->ConnectToCaptureTrack(aWindowID,
                                                   aTrack->GetTrack())) {}

MediaStreamWindowCapturer::CapturedTrack::~CapturedTrack() {
  if (mPort) {
    mPort->Destroy();
  }
}

MediaStreamWindowCapturer::MediaStreamWindowCapturer(DOMMediaStream* aStream,
                                                     uint64_t aWindowId)
    : mStream(aStream), mWindowId(aWindowId) {
  nsTArray<RefPtr<AudioStreamTrack>> tracks;
  mStream->GetAudioTracks(tracks);
  for (const auto& t : tracks) {
    if (t->Ended()) {
      continue;
    }
    AddTrack(t);
  }
}

MediaStreamWindowCapturer::~MediaStreamWindowCapturer() = default;

void MediaStreamWindowCapturer::NotifyTrackAdded(
    const RefPtr<MediaStreamTrack>& aTrack) {
  if (AudioStreamTrack* at = aTrack->AsAudioStreamTrack()) {
    AddTrack(at);
  }
}

void MediaStreamWindowCapturer::NotifyTrackRemoved(
    const RefPtr<MediaStreamTrack>& aTrack) {
  if (AudioStreamTrack* at = aTrack->AsAudioStreamTrack()) {
    RemoveTrack(at);
  }
}

void MediaStreamWindowCapturer::AddTrack(AudioStreamTrack* aTrack) {
  if (aTrack->Ended()) {
    return;
  }
  mTracks.AppendElement(MakeUnique<CapturedTrack>(aTrack, mWindowId));
}

void MediaStreamWindowCapturer::RemoveTrack(AudioStreamTrack* aTrack) {
  for (size_t i = mTracks.Length(); i > 0; --i) {
    if (mTracks[i - 1]->mTrack == aTrack) {
      mTracks.RemoveElementAt(i - 1);
      break;
    }
  }
}
}  // namespace mozilla