Better audio management (#10)
* add audio_session package * `AudioManager` initial design * try to integrate audio manager * configure audio session * `DisposeAware` disposing if already published causes exception. * organize * guard flutter_webrtc calls * websocket async fix * use `ConnectionState` instead of `_isClosed` and `isReconnecting` * change create audio track defaults * update protos * protocol 3 speaker updates * fix exception * emit `RoomDisconnectedEvent` only once * fix exception * keep track of local / remote audio tracks * explicit types * manage track state * re-structure audio management * change defaults * unpublish all * use experimental build * change defaults * configuring is optional * call native `RTCAudioSession.setConfiguration` * defaults adjustment * iOS only for now * use lib 92.4515.07 * clean up * revert audio options for now * remove pod source * rename apple related audio * `createListener` method * organize native audio * `SpeakingChangedEvent` only on `Participant` * refactoring * fix ios compile * fix configure audio only for iOS logic * minor fix & clean up * change dispose logic * format protos * fix unpublishTrack * `createListener` into a mixin * simplify * use flutter_webrtc master * unpublish for example * update ios icon * android icon * web icon * favicon
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_webrtc/flutter_webrtc.dart' as rtc;
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
import '../errors.dart';
|
||||
import '../events.dart';
|
||||
import '../exceptions.dart';
|
||||
import '../extensions.dart';
|
||||
import '../logger.dart';
|
||||
import '../managers/event.dart';
|
||||
@@ -19,16 +20,17 @@ import 'participant.dart';
|
||||
|
||||
/// Represents the current participant in the room.
|
||||
class LocalParticipant extends Participant {
|
||||
final RTCEngine _engine;
|
||||
@internal
|
||||
final RTCEngine engine;
|
||||
@internal
|
||||
final TrackPublishOptions? defaultPublishOptions;
|
||||
|
||||
LocalParticipant({
|
||||
required RTCEngine engine,
|
||||
required this.engine,
|
||||
required lk_models.ParticipantInfo info,
|
||||
this.defaultPublishOptions,
|
||||
required EventsEmitter<RoomEvent> roomEvents,
|
||||
}) : _engine = engine,
|
||||
super(
|
||||
}) : super(
|
||||
info.sid,
|
||||
info.identity,
|
||||
roomEvents: roomEvents,
|
||||
@@ -36,39 +38,43 @@ class LocalParticipant extends Participant {
|
||||
updateFromInfo(info);
|
||||
}
|
||||
|
||||
/// for internal use
|
||||
/// {@nodoc}
|
||||
RTCEngine get engine => _engine;
|
||||
|
||||
/// publish an audio track to the room
|
||||
Future<TrackPublication> publishAudioTrack(LocalAudioTrack track) async {
|
||||
if (audioTracks.any((e) => e.track?.mediaStreamTrack.id == track.mediaStreamTrack.id)) {
|
||||
throw TrackPublishException('track already exists');
|
||||
}
|
||||
|
||||
// try {
|
||||
final trackInfo = await _engine.addTrack(
|
||||
cid: track.getCid(),
|
||||
name: track.name,
|
||||
kind: track.kind,
|
||||
);
|
||||
// await AudioManager().incrementPublishCounter();
|
||||
|
||||
final transceiverInit = rtc.RTCRtpTransceiverInit(
|
||||
direction: rtc.TransceiverDirection.SendOnly,
|
||||
);
|
||||
// addTransceiver cannot pass in a kind parameter due to a bug in flutter-webrtc (web)
|
||||
track.transceiver = await _engine.publisher?.pc.addTransceiver(
|
||||
track: track.mediaStreamTrack,
|
||||
kind: rtc.RTCRtpMediaType.RTCRtpMediaTypeAudio,
|
||||
init: transceiverInit,
|
||||
);
|
||||
await _engine.negotiate();
|
||||
try {
|
||||
final trackInfo = await engine.addTrack(
|
||||
cid: track.getCid(),
|
||||
name: track.name,
|
||||
kind: track.kind,
|
||||
);
|
||||
|
||||
final pub = LocalTrackPublication(trackInfo, track, this);
|
||||
addTrackPublication(pub);
|
||||
notifyListeners();
|
||||
await track.start();
|
||||
|
||||
return pub;
|
||||
final transceiverInit = rtc.RTCRtpTransceiverInit(
|
||||
direction: rtc.TransceiverDirection.SendOnly,
|
||||
);
|
||||
// addTransceiver cannot pass in a kind parameter due to a bug in flutter-webrtc (web)
|
||||
track.transceiver = await engine.publisher?.pc.addTransceiver(
|
||||
track: track.mediaStreamTrack,
|
||||
kind: rtc.RTCRtpMediaType.RTCRtpMediaTypeAudio,
|
||||
init: transceiverInit,
|
||||
);
|
||||
await engine.negotiate();
|
||||
|
||||
final pub = LocalTrackPublication(trackInfo, track, this);
|
||||
addTrackPublication(pub);
|
||||
notifyListeners();
|
||||
return pub;
|
||||
} catch (e) {
|
||||
// In any case there was an exception, revert the count.
|
||||
// await AudioManager().decrementPublishCounter();
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Publish a video track to the room
|
||||
@@ -83,13 +89,16 @@ class LocalParticipant extends Participant {
|
||||
// Use default options from `ConnectOptions` if options is null
|
||||
options = options ?? defaultPublishOptions;
|
||||
|
||||
final trackInfo = await _engine.addTrack(
|
||||
final trackInfo = await engine.addTrack(
|
||||
cid: track.getCid(),
|
||||
name: track.name,
|
||||
kind: track.kind,
|
||||
);
|
||||
|
||||
logger.fine('publishVideoTrack addTrack response: ${trackInfo}');
|
||||
|
||||
await track.start();
|
||||
|
||||
// Video encodings and simulcasts
|
||||
|
||||
// use constraints passed to getUserMedia by default
|
||||
@@ -124,14 +133,14 @@ class LocalParticipant extends Participant {
|
||||
streams: [track.mediaStream],
|
||||
);
|
||||
|
||||
logger.fine('publishVideoTrack publisher: ${_engine.publisher}');
|
||||
logger.fine('publishVideoTrack publisher: ${engine.publisher}');
|
||||
|
||||
track.transceiver = await _engine.publisher?.pc.addTransceiver(
|
||||
track.transceiver = await engine.publisher?.pc.addTransceiver(
|
||||
track: track.mediaStreamTrack,
|
||||
kind: rtc.RTCRtpMediaType.RTCRtpMediaTypeVideo,
|
||||
init: transceiverInit,
|
||||
);
|
||||
await _engine.negotiate();
|
||||
await engine.negotiate();
|
||||
|
||||
final pub = LocalTrackPublication(trackInfo, track, this);
|
||||
addTrackPublication(pub);
|
||||
@@ -156,8 +165,17 @@ class LocalParticipant extends Participant {
|
||||
|
||||
final sender = track.transceiver?.sender;
|
||||
if (sender != null) {
|
||||
await engine.publisher?.pc.removeTrack(sender);
|
||||
await engine.negotiate();
|
||||
try {
|
||||
await engine.publisher?.pc.removeTrack(sender);
|
||||
} catch (_) {
|
||||
logger.warning('[$objectId] rtc.removeTrack() did throw ${_}');
|
||||
}
|
||||
|
||||
// doesn't make sense to negotiate if already disposed
|
||||
if (!isDisposed) {
|
||||
// manual negotiation since track changed
|
||||
await engine.negotiate();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user