blob: 89af4db7144334cb55aae19438751900d8979148 [file] [log] [blame]
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2014 The WebRTC project authors. All Rights Reserved.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00009 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef PC_REMOTE_AUDIO_SOURCE_H_
12#define PC_REMOTE_AUDIO_SOURCE_H_
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000013
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000014#include <stdint.h>
15
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000016#include <list>
Tommif888bb52015-12-12 01:37:01 +010017#include <string>
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000018
Saurav Das749f6602019-12-04 09:31:36 -080019#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/call/audio_sink.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000021#include "api/media_stream_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/notifier.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000023#include "media/base/media_channel.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/message_handler.h"
Markus Handell6fcd0f82020-07-07 19:08:53 +020025#include "rtc_base/synchronization/mutex.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000026#include "rtc_base/thread.h"
27#include "rtc_base/thread_message.h"
Tommif888bb52015-12-12 01:37:01 +010028
29namespace rtc {
30struct Message;
31class Thread;
32} // namespace rtc
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000033
34namespace webrtc {
35
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000036// This class implements the audio source used by the remote audio track.
Steve Antond3679212018-01-17 17:41:02 -080037// This class works by configuring itself as a sink with the underlying media
38// engine, then when receiving data will fan out to all added sinks.
Steve Anton3b80aac2017-10-19 10:17:12 -070039class RemoteAudioSource : public Notifier<AudioSourceInterface>,
40 rtc::MessageHandler {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000041 public:
Henrik Boströmc335b0e2021-04-08 07:25:38 +020042 // In Unified Plan, receivers map to m= sections and their tracks and sources
43 // survive SSRCs being reconfigured. The life cycle of the remote audio source
44 // is associated with the life cycle of the m= section, and thus even if an
45 // audio channel is destroyed the RemoteAudioSource should kSurvive.
46 //
47 // In Plan B however, remote audio sources map 1:1 with an SSRCs and if an
48 // audio channel is destroyed, the RemoteAudioSource should kEnd.
49 enum class OnAudioChannelGoneAction {
50 kSurvive,
51 kEnd,
52 };
53
54 explicit RemoteAudioSource(
55 rtc::Thread* worker_thread,
56 OnAudioChannelGoneAction on_audio_channel_gone_action);
Steve Antond3679212018-01-17 17:41:02 -080057
58 // Register and unregister remote audio source with the underlying media
59 // engine.
Saurav Das749f6602019-12-04 09:31:36 -080060 void Start(cricket::VoiceMediaChannel* media_channel,
61 absl::optional<uint32_t> ssrc);
62 void Stop(cricket::VoiceMediaChannel* media_channel,
63 absl::optional<uint32_t> ssrc);
Henrik Boströmc335b0e2021-04-08 07:25:38 +020064 void SetState(SourceState new_state);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000065
66 // MediaSourceInterface implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000067 MediaSourceInterface::SourceState state() const override;
tommi6eca7e32015-12-15 04:27:11 -080068 bool remote() const override;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000069
70 // AudioSourceInterface implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000071 void SetVolume(double volume) override;
72 void RegisterAudioObserver(AudioObserver* observer) override;
73 void UnregisterAudioObserver(AudioObserver* observer) override;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000074
Steve Antond3679212018-01-17 17:41:02 -080075 void AddSink(AudioTrackSinkInterface* sink) override;
76 void RemoveSink(AudioTrackSinkInterface* sink) override;
77
78 protected:
79 ~RemoteAudioSource() override;
80
81 private:
82 // These are callbacks from the media engine.
83 class AudioDataProxy;
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000084
Tommif888bb52015-12-12 01:37:01 +010085 void OnData(const AudioSinkInterface::Data& audio);
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070086 void OnAudioChannelGone();
Tommif888bb52015-12-12 01:37:01 +010087
Steve Anton3b80aac2017-10-19 10:17:12 -070088 void OnMessage(rtc::Message* msg) override;
Tommif888bb52015-12-12 01:37:01 +010089
Steve Antond3679212018-01-17 17:41:02 -080090 rtc::Thread* const main_thread_;
91 rtc::Thread* const worker_thread_;
Henrik Boströmc335b0e2021-04-08 07:25:38 +020092 const OnAudioChannelGoneAction on_audio_channel_gone_action_;
Steve Antond3679212018-01-17 17:41:02 -080093 std::list<AudioObserver*> audio_observers_;
Markus Handell6fcd0f82020-07-07 19:08:53 +020094 Mutex sink_lock_;
Tommif888bb52015-12-12 01:37:01 +010095 std::list<AudioTrackSinkInterface*> sinks_;
Tommif888bb52015-12-12 01:37:01 +010096 SourceState state_;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000097};
98
99} // namespace webrtc
100
Steve Anton10542f22019-01-11 09:11:00 -0800101#endif // PC_REMOTE_AUDIO_SOURCE_H_