Skip to content

Commit

Permalink
feat: add AudioSourceStats. (#427)
Browse files Browse the repository at this point in the history
* feat: add AudioSourceStats.

* import sorter.

* update.
  • Loading branch information
cloudwebrtc authored Dec 19, 2023
1 parent 78d0ed2 commit cbf665f
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/src/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import 'participant/remote.dart';
import 'publication/local.dart';
import 'publication/remote.dart';
import 'publication/track_publication.dart';
import 'track/stats.dart';
import 'stats/stats.dart';
import 'track/track.dart';
import 'types/other.dart';
import 'types/participant_permissions.dart';
Expand Down
51 changes: 51 additions & 0 deletions lib/src/stats/audio_source_stats.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import 'package:flutter_webrtc/flutter_webrtc.dart' as rtc;

import 'stats.dart';

class AudioSourceStats {
final num? audioLevel;
final num? totalAudioEnergy;
final num? totalSamplesDuration;
final num? echoReturnLoss;
final num? echoReturnLossEnhancement;
final String? trackIdentifier;
final bool remoteSource;

AudioSourceStats({
required this.echoReturnLossEnhancement,
required this.audioLevel,
required this.totalAudioEnergy,
required this.totalSamplesDuration,
required this.echoReturnLoss,
required this.trackIdentifier,
required this.remoteSource,
});

factory AudioSourceStats.fromReport(rtc.StatsReport report) {
return AudioSourceStats(
echoReturnLossEnhancement:
getNumValFromReport(report.values, 'echoReturnLossEnhancement'),
audioLevel: getNumValFromReport(report.values, 'audioLevel'),
totalAudioEnergy: getNumValFromReport(report.values, 'totalAudioEnergy'),
totalSamplesDuration:
getNumValFromReport(report.values, 'totalSamplesDuration'),
echoReturnLoss: getNumValFromReport(report.values, 'echoReturnLoss'),
trackIdentifier: getStringValFromReport(report.values, 'trackIdentifier'),
remoteSource: getBoolValFromReport(report.values, 'remoteSource'),
);
}
}
17 changes: 11 additions & 6 deletions lib/src/track/stats.dart → lib/src/stats/stats.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import 'package:flutter/foundation.dart';

import '../proto/livekit_models.pb.dart';
import 'audio_source_stats.dart';

const monitorFrequency = 2000;

Expand Down Expand Up @@ -54,12 +54,11 @@ class SenderStats extends CodecStats {

class AudioSenderStats extends SenderStats {
AudioSenderStats(String streamId, num timestamp) : super(streamId, timestamp);
TrackType type = TrackType.AUDIO;
AudioSourceStats? audioSourceStats;
}

class VideoSenderStats extends SenderStats {
VideoSenderStats(String streamId, num timestamp) : super(streamId, timestamp);
TrackType type = TrackType.VIDEO;

num? firCount;

Expand Down Expand Up @@ -107,7 +106,6 @@ class ReceiverStats extends CodecStats {
class AudioReceiverStats extends ReceiverStats {
AudioReceiverStats(String streamId, num timestamp)
: super(streamId, timestamp);
TrackType type = TrackType.AUDIO;

num? concealedSamples;

Expand All @@ -120,14 +118,14 @@ class AudioReceiverStats extends ReceiverStats {
num? totalAudioEnergy;

num? totalSamplesDuration;

AudioSourceStats? audioSourceStats;
}

class VideoReceiverStats extends ReceiverStats {
VideoReceiverStats(String streamId, num timestamp)
: super(streamId, timestamp);

TrackType type = TrackType.VIDEO;

num? framesDecoded;

num? framesDropped;
Expand Down Expand Up @@ -212,3 +210,10 @@ String? getStringValFromReport(Map<dynamic, dynamic> values, String key) {
}
return null;
}

bool getBoolValFromReport(Map<dynamic, dynamic> values, String key) {
if (values.containsKey(key) && values[key] is bool) {
return values[key] as bool;
}
return false;
}
7 changes: 5 additions & 2 deletions lib/src/track/local/audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import 'package:meta/meta.dart';
import '../../events.dart';
import '../../logger.dart';
import '../../proto/livekit_models.pb.dart' as lk_models;
import '../../stats/audio_source_stats.dart';
import '../../stats/stats.dart';
import '../../types/other.dart';
import '../audio_management.dart';
import '../options.dart';
import '../stats.dart';
import 'local.dart';

class LocalAudioTrack extends LocalTrack
Expand Down Expand Up @@ -101,7 +102,9 @@ class LocalAudioTrack extends LocalTrack
senderStats.channels = getNumValFromReport(c.values, 'channels');
senderStats.clockRate = getNumValFromReport(c.values, 'clockRate');
}
break;
} else if (v.type == 'media-source') {
senderStats ??= AudioSenderStats(v.id, v.timestamp);
senderStats.audioSourceStats = AudioSourceStats.fromReport(v);
}
}
return senderStats;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/track/local/video.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import '../../events.dart';
import '../../logger.dart';
import '../../proto/livekit_models.pb.dart' as lk_models;
import '../../proto/livekit_rtc.pb.dart' as lk_rtc;
import '../../stats/stats.dart';
import '../../support/platform.dart';
import '../../types/other.dart';
import '../options.dart';
import '../stats.dart';
import 'audio.dart';
import 'local.dart';

Expand Down
7 changes: 5 additions & 2 deletions lib/src/track/remote/audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import '../../events.dart';
import '../../internal/events.dart';
import '../../logger.dart';
import '../../proto/livekit_models.pb.dart' as lk_models;
import '../../stats/audio_source_stats.dart';
import '../../stats/stats.dart';
import '../../types/other.dart';
import '../audio_management.dart';
import '../local/local.dart';
import '../stats.dart';
import 'remote.dart';

import '../web/_audio_api.dart' if (dart.library.html) '../web/_audio_html.dart'
Expand Down Expand Up @@ -150,7 +151,9 @@ class RemoteAudioTrack extends RemoteTrack
receiverStats.channels = getNumValFromReport(c.values, 'channels');
receiverStats.clockRate = getNumValFromReport(c.values, 'clockRate');
}
break;
} else if (v.type == 'track') {
receiverStats ??= AudioReceiverStats(v.id, v.timestamp);
receiverStats.audioSourceStats = AudioSourceStats.fromReport(v);
}
}
return receiverStats;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/track/remote/video.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import 'package:meta/meta.dart';
import '../../events.dart';
import '../../logger.dart';
import '../../proto/livekit_models.pb.dart' as lk_models;
import '../../stats/stats.dart';
import '../../types/other.dart';
import '../local/local.dart';
import '../stats.dart';
import 'remote.dart';

class RemoteVideoTrack extends RemoteTrack with VideoTrack {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/track/track.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import '../internal/events.dart';
import '../logger.dart';
import '../managers/event.dart';
import '../proto/livekit_models.pb.dart' as lk_models;
import '../stats/stats.dart';
import '../support/disposable.dart';
import '../types/other.dart';
import 'stats.dart';

/// Wrapper around a MediaStreamTrack with additional metadata.
/// Base for [AudioTrack] and [VideoTrack],
Expand Down

0 comments on commit cbf665f

Please sign in to comment.