Created
February 2, 2019 19:27
-
-
Save Lucas2k/b818d59f8ba4f53ac978217b6f1ecfee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Android.App; | |
using Android.Content; | |
using Android.OS; | |
using Android.Runtime; | |
using Android.Views; | |
using Android.Widget; | |
using Xamarin.Forms.Platform.Android; | |
using System; | |
using Android.App; | |
using Android.Hardware.Camera2; | |
using Android.OS; | |
using Android.Widget; | |
using Com.Github.Faucamp.Simplertmp; | |
using Java.IO; | |
using Java.Lang; | |
using Java.Net; | |
using Net.Ossrs.Yasea; | |
using Xamarin.Forms; | |
using Sample; | |
using Sample.Droid; | |
using ARelativeLayout = Android.Widget.RelativeLayout; | |
using System.ComponentModel; | |
[assembly: ExportRenderer(typeof(Yasea), | |
typeof(YaseaRenderer))] | |
namespace Sample.Droid | |
{ | |
public class YaseaRenderer : ViewRenderer<Yasea, ARelativeLayout>, RtmpHandler.IRtmpListener, SrsRecordHandler.ISrsRecordListener, SrsEncodeHandler.ISrsEncodeListener | |
{ | |
SrsPublisher mPublisher; | |
SrsCameraView srsCameraView; | |
string rtmpUrl = string.Empty; | |
public YaseaRenderer(Context context) : base(context) | |
{ | |
} | |
protected override void OnElementChanged(ElementChangedEventArgs<Yasea> args) | |
{ | |
base.OnElementChanged(args); | |
if (args.NewElement != null) | |
{ | |
if (Control == null) | |
{ | |
srsCameraView = new SrsCameraView(Context); | |
mPublisher = new SrsPublisher(srsCameraView); | |
mPublisher.SetEncodeHandler(new SrsEncodeHandler(this)); | |
mPublisher.SetRtmpHandler(new RtmpHandler(this)); | |
mPublisher.SetRecordHandler(new SrsRecordHandler(this)); | |
mPublisher.SetPreviewResolution(640, 360); | |
mPublisher.SetOutputResolution(360, 640); | |
mPublisher.SetVideoHDMode(); | |
mPublisher.StartCamera(); | |
// Put the VideoView in a RelativeLayout | |
ARelativeLayout relativeLayout = new ARelativeLayout(Context); | |
relativeLayout.AddView(srsCameraView); | |
// Center the VideoView in the RelativeLayout | |
ARelativeLayout.LayoutParams layoutParams = | |
new ARelativeLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent); | |
layoutParams.AddRule(LayoutRules.CenterInParent); | |
srsCameraView.LayoutParameters = layoutParams; | |
// Handle a VideoView event | |
//srsCameraView.Prepared += OnVideoViewPrepared; | |
SetNativeControl(relativeLayout); | |
} | |
args.NewElement.StartPublishRequested += OnStartPublish; | |
args.NewElement.StopPublishRequest += OnStopPublish; | |
args.NewElement.CameraSwitchRequest += OnCameraSwitch; | |
} | |
if (args.OldElement != null) | |
{ | |
args.OldElement.StartPublishRequested -= OnStartPublish; | |
args.OldElement.StopPublishRequest -= OnStopPublish; | |
args.OldElement.CameraSwitchRequest -= OnCameraSwitch; | |
} | |
} | |
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs args) | |
{ | |
base.OnElementPropertyChanged(sender, args); | |
if (args.PropertyName == Yasea.RtmpUrlProperty.PropertyName) | |
{ | |
rtmpUrl = Element.RtmpUrl; | |
} | |
} | |
protected override void Dispose(bool disposing) | |
{ | |
base.Dispose(disposing); | |
} | |
void OnStartPublish(object sender, EventArgs args) | |
{ | |
mPublisher.StartPublish(rtmpUrl); | |
mPublisher.StartCamera(); | |
} | |
void OnStopPublish(object sender, EventArgs args) | |
{ | |
mPublisher.StopPublish(); | |
} | |
void OnCameraSwitch(object sender, EventArgs args) | |
{ | |
var manager = (CameraManager)Context.GetSystemService(Context.CameraService); | |
mPublisher.SwitchCameraFace((mPublisher.CamraId + 1) % manager.GetCameraIdList().Length); | |
} | |
void ShowShortToast(string message) | |
{ | |
Toast.MakeText(Context, message, ToastLength.Short).Show(); | |
} | |
void HandleException(Java.Lang.Exception e) | |
{ | |
try | |
{ | |
ShowShortToast(e.Message); | |
mPublisher.StopPublish(); | |
mPublisher.StopRecord(); | |
} | |
catch | |
{ } | |
} | |
#region eventos del RTMP | |
public void OnEncodeIllegalArgumentException(IllegalArgumentException e) => HandleException(e); | |
public void OnNetworkResume() => ShowShortToast("Network resume"); | |
public void OnNetworkWeak() => ShowShortToast("Network weak"); | |
public void OnRecordFinished(string p0) | |
{ | |
} | |
public void OnRecordIllegalArgumentException(IllegalArgumentException p0) | |
{ | |
} | |
public void OnRecordIOException(IOException p0) | |
{ | |
} | |
public void OnRecordPause() | |
{ | |
} | |
public void OnRecordResume() | |
{ | |
} | |
public void OnRecordStarted(string p0) | |
{ | |
} | |
public void OnRtmpAudioBitrateChanged(double p0) | |
{ | |
} | |
public void OnRtmpAudioStreaming() | |
{ | |
} | |
public void OnRtmpConnected(string message) | |
{ | |
((IElementController)Element).SetValueFromRenderer(Yasea.StatusConnectedProperty, true); | |
ShowShortToast(message); | |
} | |
public void OnRtmpConnecting(string message) => ShowShortToast(message); | |
public void OnRtmpDisconnected() | |
{ | |
((IElementController)Element).SetValueFromRenderer(Yasea.StatusConnectedProperty, false); | |
ShowShortToast("Disconnected"); | |
} | |
public void OnRtmpStopped() | |
{ | |
((IElementController)Element).SetValueFromRenderer(Yasea.StatusConnectedProperty, false); | |
ShowShortToast("Stopped"); | |
} | |
public void OnRtmpIllegalArgumentException(IllegalArgumentException e) => HandleException(e); | |
public void OnRtmpIllegalStateException(IllegalStateException e) => HandleException(e); | |
public void OnRtmpIOException(IOException e) => HandleException(e); | |
public void OnRtmpSocketException(SocketException e) => HandleException(e); | |
public void OnRtmpVideoBitrateChanged(double p0) | |
{ | |
} | |
public void OnRtmpVideoFpsChanged(double p0) | |
{ | |
} | |
public void OnRtmpVideoStreaming() | |
{ | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment