Skip to content

Commit

Permalink
Moved OnVideoMediaReceived to BotMediaStream and also added a callbac…
Browse files Browse the repository at this point in the history
…k for OnAudioMediaReceived. (#306)
  • Loading branch information
Identifier authored Aug 17, 2020
1 parent 270523a commit 31e4911
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ public BotMediaStream(ILocalMediaSession mediaSession, IGraphLogger logger)
}

this.audioSocket.AudioSendStatusChanged += this.OnAudioSendStatusChanged;
this.audioSocket.AudioMediaReceived += this.OnAudioMediaReceived;

this.mainVideoSocket = this.mediaSession.VideoSockets?.FirstOrDefault();
if (this.mainVideoSocket != null)
{
this.mainVideoSocket.VideoSendStatusChanged += this.OnVideoSendStatusChanged;
this.mainVideoSocket.VideoKeyFrameNeeded += this.OnVideoKeyFrameNeeded;
this.mainVideoSocket.VideoMediaReceived += this.OnVideoMediaReceived;
}

this.videoSockets = this.mediaSession.VideoSockets?.ToList();
Expand Down Expand Up @@ -185,12 +187,14 @@ public async Task ShutdownAsync()
if (this.audioSocket != null)
{
this.audioSocket.AudioSendStatusChanged -= this.OnAudioSendStatusChanged;
this.audioSocket.AudioMediaReceived -= this.OnAudioMediaReceived;
}

if (this.mainVideoSocket != null)
{
this.mainVideoSocket.VideoKeyFrameNeeded -= this.OnVideoKeyFrameNeeded;
this.mainVideoSocket.VideoSendStatusChanged -= this.OnVideoSendStatusChanged;
this.mainVideoSocket.VideoMediaReceived -= this.OnVideoMediaReceived;
}

if (this.vbssSocket != null)
Expand Down Expand Up @@ -325,6 +329,25 @@ private void OnAudioSendStatusChanged(object sender, AudioSendStatusChangedEvent
}
}

/// <summary>
/// Save screenshots when we receive audio from the subscribed participant.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The audio media received arguments.
/// </param>
private void OnAudioMediaReceived(object sender, AudioMediaReceivedEventArgs e)
{
// leave only logging in here
this.logger.Info($"[AudioMediaReceivedEventArgs(Data=<{e.Buffer.Data.ToString()}>, Length={e.Buffer.Length}, Timestamp={e.Buffer.Timestamp}, AudioFormat={e.Buffer.AudioFormat}, IsSilence={e.Buffer.IsSilence}, ActiveSpeakers=[{string.Join(", ", e.Buffer.ActiveSpeakers)}])]");

/* TODO: Do something with audio here */

e.Buffer.Dispose();
}

/// <summary>
/// Callback for informational updates from the media plaform about video status changes.
/// Once the Status becomes active, then video can be sent.
Expand All @@ -348,9 +371,9 @@ private void OnVideoSendStatusChanged(object sender, VideoSendStatusChangedEvent
// starting from beginning
if (!this.videoSendStatusActive.TrySetResult(true))
{
if (this.videoKnownSupportedFormats != null && this.videoKnownSupportedFormats.Any() &&

// here it means we got a new video fromat so we need to restart the player
if (this.videoKnownSupportedFormats != null && this.videoKnownSupportedFormats.Any() &&

// here it means we got a new video fromat so we need to restart the player
this.videoKnownSupportedFormats.Select(x => x.GetId()).Except(previousSupportedFormats.Select(y => y.GetId())).Any())
{
// we restart the player
Expand All @@ -375,6 +398,25 @@ private void OnVideoSendStatusChanged(object sender, VideoSendStatusChangedEvent
}
}

/// <summary>
/// Save screenshots when we receive video from the subscribed participant.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The video media received arguments.
/// </param>
private void OnVideoMediaReceived(object sender, VideoMediaReceivedEventArgs e)
{
// leave only logging in here
this.logger.Info($"[VideoMediaReceivedEventArgs(Data=<{e.Buffer.Data.ToString()}>, Length={e.Buffer.Length}, Timestamp={e.Buffer.Timestamp}, Width={e.Buffer.VideoFormat.Width}, Height={e.Buffer.VideoFormat.Height}, ColorFormat={e.Buffer.VideoFormat.VideoColorFormat}, FrameRate={e.Buffer.VideoFormat.FrameRate})]");

/* TODO: Do something with video here */

e.Buffer.Dispose();
}

/// <summary>
/// If the application has configured the VideoSocket to receive encoded media, this
/// event is raised each time a key frame is needed. Events are serialized, so only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ public CallHandler(ICall statefulCall)
// subscribe to dominant speaker event on the audioSocket
this.Call.GetLocalMediaSession().AudioSocket.DominantSpeakerChanged += this.OnDominantSpeakerChanged;

// subscribe to the VideoMediaReceived event on the main video socket
this.Call.GetLocalMediaSession().VideoSockets.FirstOrDefault().VideoMediaReceived += this.OnVideoMediaReceived;

// susbscribe to the participants updates, this will inform the bot if a particpant left/joined the conference
this.Call.Participants.OnUpdated += this.ParticipantsOnUpdated;

Expand Down Expand Up @@ -91,7 +88,6 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);

this.Call.GetLocalMediaSession().AudioSocket.DominantSpeakerChanged -= this.OnDominantSpeakerChanged;
this.Call.GetLocalMediaSession().VideoSockets.FirstOrDefault().VideoMediaReceived -= this.OnVideoMediaReceived;

this.Call.OnUpdated -= this.CallOnUpdated;
this.Call.Participants.OnUpdated -= this.ParticipantsOnUpdated;
Expand Down Expand Up @@ -289,23 +285,6 @@ private void OnDominantSpeakerChanged(object sender, DominantSpeakerChangedEvent
}
}

/// <summary>
/// Save screenshots when we receive video from subscribed participant.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The video media received arguments.
/// </param>
private void OnVideoMediaReceived(object sender, VideoMediaReceivedEventArgs e)
{
// leave only logging in here
this.GraphLogger.Info($"[{this.Call.Id}]: Capturing image: [VideoMediaReceivedEventArgs(Data=<{e.Buffer.Data.ToString()}>, Length={e.Buffer.Length}, Timestamp={e.Buffer.Timestamp}, Width={e.Buffer.VideoFormat.Width}, Height={e.Buffer.VideoFormat.Height}, ColorFormat={e.Buffer.VideoFormat.VideoColorFormat}, FrameRate={e.Buffer.VideoFormat.FrameRate})]");

e.Buffer.Dispose();
}

/// <summary>
/// Gets the participant with the corresponding MSI.
/// </summary>
Expand Down

0 comments on commit 31e4911

Please sign in to comment.