File: webrtc_audio_private_api.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (148 lines) | stat: -rw-r--r-- 5,377 bytes parent folder | download | duplicates (4)
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
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_EXTENSIONS_API_WEBRTC_AUDIO_PRIVATE_WEBRTC_AUDIO_PRIVATE_API_H_
#define CHROME_BROWSER_EXTENSIONS_API_WEBRTC_AUDIO_PRIVATE_WEBRTC_AUDIO_PRIVATE_API_H_

#include <stddef.h>

#include <string>

#include "base/memory/raw_ptr.h"
#include "base/system/system_monitor.h"
#include "chrome/common/extensions/api/webrtc_audio_private.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/resource_context.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/browser/extension_function.h"
#include "media/audio/audio_device_description.h"

namespace media {
class AudioSystem;
}

namespace extensions {

// Listens for device changes and forwards as an extension event.
class WebrtcAudioPrivateEventService
    : public BrowserContextKeyedAPI,
      public base::SystemMonitor::DevicesChangedObserver {
 public:
  explicit WebrtcAudioPrivateEventService(content::BrowserContext* context);
  ~WebrtcAudioPrivateEventService() override;

  // BrowserContextKeyedAPI implementation.
  void Shutdown() override;
  static BrowserContextKeyedAPIFactory<WebrtcAudioPrivateEventService>*
      GetFactoryInstance();
  static const char* service_name();

  // base::SystemMonitor::DevicesChangedObserver implementation.
  void OnDevicesChanged(base::SystemMonitor::DeviceType device_type) override;

 private:
  friend class BrowserContextKeyedAPIFactory<WebrtcAudioPrivateEventService>;

  void SignalEvent();

  raw_ptr<content::BrowserContext> browser_context_;
};

// Common base for WebrtcAudioPrivate functions, that provides a
// couple of optionally-used common implementations.
class WebrtcAudioPrivateFunction : public ExtensionFunction {
 public:
  WebrtcAudioPrivateFunction(const WebrtcAudioPrivateFunction&) = delete;
  WebrtcAudioPrivateFunction& operator=(const WebrtcAudioPrivateFunction&) =
      delete;

 protected:
  WebrtcAudioPrivateFunction();
  ~WebrtcAudioPrivateFunction() override;

  using SaltAndDeviceDescriptionsCallback =
      base::OnceCallback<void(const std::string&,
                              media::AudioDeviceDescriptions)>;
  // Calculates the HMAC for `raw_id` using extension ID as the security origin.
  // `extension_salt` must be the salt for the extension ID.
  std::string CalculateHMAC(const std::string& extension_salt,
                            const std::string& raw_id);

  // Returns the extension ID as an origin.
  url::Origin GetExtensionOrigin() const;

  void GetSalt(const url::Origin&,
               base::OnceCallback<void(const std::string&)> salt_callback);

  // Returns the device ID salt and device descriptions.
  void GetSaltAndDeviceDescriptions(const url::Origin& security_origin,
                                    bool is_input_devices,
                                    SaltAndDeviceDescriptionsCallback callback);

  media::AudioSystem* GetAudioSystem();

 private:
  void GotSaltForDeviceDescriptions(bool is_input_devices,
                                    SaltAndDeviceDescriptionsCallback callback,
                                    const std::string& device_id_salt);
  std::unique_ptr<media::AudioSystem> audio_system_;
};

class WebrtcAudioPrivateGetSinksFunction : public WebrtcAudioPrivateFunction {
 protected:
  ~WebrtcAudioPrivateGetSinksFunction() override = default;

 private:
  using SinkInfoVector = std::vector<api::webrtc_audio_private::SinkInfo>;

  DECLARE_EXTENSION_FUNCTION("webrtcAudioPrivate.getSinks",
                             WEBRTC_AUDIO_PRIVATE_GET_SINKS)

  ResponseAction Run() override;

  // Receives output device descriptions, calculates HMACs for them and sends
  // the response.
  void ReceiveOutputDeviceDescriptions(
      const std::string& extension_salt,
      media::AudioDeviceDescriptions sink_devices);
};

class WebrtcAudioPrivateGetAssociatedSinkFunction
    : public WebrtcAudioPrivateFunction {
 public:
  WebrtcAudioPrivateGetAssociatedSinkFunction();

 protected:
  ~WebrtcAudioPrivateGetAssociatedSinkFunction() override;

 private:
  DECLARE_EXTENSION_FUNCTION("webrtcAudioPrivate.getAssociatedSink",
                             WEBRTC_AUDIO_PRIVATE_GET_ASSOCIATED_SINK)

  // UI thread: Entry point, posts GetInputDeviceDescriptions() to IO thread.
  ResponseAction Run() override;

  // Receives the input device descriptions, looks up the raw source device ID
  // basing on `params`, and requests the associated raw sink ID for it.
  void ReceiveInputDeviceDescriptions(
      const url::Origin& origin,
      const std::string& salt,
      media::AudioDeviceDescriptions source_devices);

  void GotExtensionSalt(const std::string& raw_source_id,
                        const std::string& extension_salt);

  // Calculates HMAC and calls Reply().
  void CalculateHMACAndReply(const std::string& extension_salt,
                             const std::optional<std::string>& raw_sink_id);

  // Receives the associated sink ID as HMAC and sends the response.
  void Reply(const std::string& hmac);

  std::optional<api::webrtc_audio_private::GetAssociatedSink::Params> params_;
};

}  // namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_API_WEBRTC_AUDIO_PRIVATE_WEBRTC_AUDIO_PRIVATE_API_H_