wpf application for broadcasting video with 15sec delay - c#

I have a WPF application for broadcasting video using Microsoft.expression.encoder and framework 4.0, but i got a delay of 15 sec while broadcasting.Is there any suggestion to reduce the delay while broadcasting.
below is the Code
using Microsoft.Expression.Encoder.Live;
using Microsoft.Expression.Encoder;
private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
EncoderDevice video = null;
EncoderDevice audio = null;
GetSelectedVideoAndAudioDevices(out video, out audio);
StopJob();
if (video == null)
{
return;
}
StopJob();
_job = new LiveJob();
if (video != null && audio != null)
{
//StopJob();
_deviceSource = null;
_deviceSource = _job.AddDeviceSource(video, audio);
_job.ActivateSource(_deviceSource);
// Finds and applys a smooth streaming preset
//_job.ApplyPreset(LivePresets.VC1HighSpeedBroadband4x3);
// Creates the publishing format for the job
PullBroadcastPublishFormat format = new PullBroadcastPublishFormat();
format.BroadcastPort = 9090;
format.MaximumNumberOfConnections = 50;
// Adds the publishing format to the job
_job.PublishFormats.Add(format);
// Starts encoding
_job.StartEncoding();
}
//webCamCtrl.StartCapture();
}
catch (Exception ex)
{
WriteLogFile(this.GetType().Name, "button1_Click", ex.Message.ToString());
}
}
I am using MediaElement to show the webcam both on my server and client systems.
on Client Side
try
{
theMainWindow.getServerIPAddress();
IP = theMainWindow.machineIP;
MediaElement1.Source = new Uri("http://" + IP + ":9090/");
}
catch (Exception ex)
{
}

There is unfortunately no solution (at least as of Jan 2011). According to Microsoft:
"We add a few seconds of delay during the encoding, then there's caching going on at the server level that can add another 5-20 seconds and finally Silverlight also caches for another few seconds of delay."
http://social.expression.microsoft.com/Forums/is/encoder/thread/898b2659-c0d5-4c84-8fba-225f58806f5d

You can eliminate some delay in the client by using a PreviewWindow instead of a MediaElement, bypassing the need to encode the stream before displaying it in the client. PreviewWindow is a WinForms control, so this will only work in WPF.
In XAML:
<WindowsFormsHost>
<wf:Panel x:Name="PreviewPanel" />
</WindowsFormsHost>
Code behind:
var previewWindow = new PreviewWindow(new HandleRef(this.PreviewPanel, this.PreviewPanel.Handle));
_deviceSource.PreviewWindow = previewWindow;
// ..
_job.ActivateSource(_deviceSource);

Related

RDLC issues on monitors with higher recommended scaling

I'm on .net framework 4.8 in my WPF app and I have two usages on RDLC. 1st is a fully fetched ReportViewer that uses a DataTable from postgres, 2nd is just a LocalReport with small number of parameters rendered as EMF and printed directly with use of default printer.
They both have what would seem to be rendering issues, but just on monitors that have recommended scaling (RS) >100%. The outcome is squashing of letters vertically and adding some extra space in between (I can provide samples as soon as I get access to client machine again). If I just increase scaling on my 100% RS monitor, everything prints out just fine. If I replace the >100% RS monitor with a 1080p 100% RS one, again, everything prints out fine. Printouts on machines with monitors with >100% RS come out always messed up irrelevant of the scaling I set in Windows. Issues can be quickly reproduced with just 'Print Layout' view in ReportViewer, exporting to PDF produces same results.
Since I have ReportViewer and a direct printout of LocalReport I was able to try out several different approaches:
Making the app DPIAware / not aware / true/PM etc. (also included manifest, App.config and App.xaml changes)
Putting the ReportViewer in ViewBox
Using DpiX/Y and PrintDpiX/Y on DeviceInfo
ScaleTransform and DrawImageUnscaled on PrintPage callback with and without the DeviceInfo changes
countless printer options in Windows
Client machines run on either latest Windows 10 or close to latest and are rather empty otherwise.
Does it ring any bells? Any idea for potential fix?
I would love to use RDLC in my app, for the simplicity of development and usage, but those issues are really a no go for the technology.
Code
No preview printout
Used to print a single document directly without preview from parameters.
class CytologiaPrinter : IDisposable
{
private static readonly ILog log = LogManager.GetLogger(typeof(CytologiaPrinter));
private int m_currentPageIndex;
private IList<Stream> m_streams;
private int WizytaID;
private CytologiaAnkieta Cytologia;
public CytologiaPrinter(int wizytaID)
{
WizytaID = wizytaID;
}
public CytologiaPrinter(CytologiaAnkieta cytologia)
{
Cytologia = cytologia;
}
public void Print()
{
try
{
CytologiaAnkieta cytologia;
if (Cytologia == null)
{
cytologia = DBCommunication.fetchCytologia(WizytaID);
}
else
{
cytologia = Cytologia;
}
if (cytologia != null && cytologia.AnkietaNumer != null && cytologia.AnkietaNumer.Length > 0)
{
LocalReport report = new LocalReport();
var cytologie = new List<CytologiaAnkieta>();
cytologie.Add(cytologia);
ReportDataSource reportDataSource = new ReportDataSource("DataSet1", cytologie);
report.DataSources.Add(reportDataSource);
report.ReportEmbeddedResource = "Suplement.CytologiaAnkieta.rdlc";
var parameters = new List<ReportParameter>();
//parameters.Add(...); //setting all parameters omitted for demo
report.SetParameters(parameters);
m_currentPageIndex = 0;
Print(cytologia);
}
}
catch (Exception ex)
{
log.Error("Error (" + ex.Message + "), stack:" + ex.StackTrace);
}
}
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
{
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
}
private void Export(LocalReport report)
{
string deviceInfo =
#"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>29.7cm</PageWidth>
<PageHeight>21cm</PageHeight>
<MarginTop>1cm</MarginTop>
<MarginLeft>1cm</MarginLeft>
<MarginRight>1cm</MarginRight>
<MarginBottom>1cm</MarginBottom>
</DeviceInfo>"; //printing in landscape
Warning[] warnings;
m_streams = new List<Stream>();
report.Render("Image", deviceInfo, CreateStream,
out warnings);
if (warnings != null && warnings.Length > 0)
{
foreach (var warn in warnings)
{
log.Warn("Cytologia printing issues: " + warn.Message);
}
}
foreach (Stream stream in m_streams)
stream.Position = 0;
}
private void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = new
Metafile(m_streams[m_currentPageIndex]);
Rectangle adjustedRect = new Rectangle(
ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,
ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,
ev.PageBounds.Width,
ev.PageBounds.Height);
ev.Graphics.FillRectangle(Brushes.White, adjustedRect);
ev.Graphics.DrawImage(pageImage, adjustedRect);
m_currentPageIndex++;
ev.HasMorePages = m_currentPageIndex < m_streams.Count;
}
private void Print(CytologiaAnkieta cytologia)
{
if (m_streams == null || m_streams.Count == 0)
throw new Exception("Error: no stream to print.");
PrintDocument printDoc = new PrintDocument();
printDoc.DefaultPageSettings.Landscape = true;
if (!printDoc.PrinterSettings.IsValid)
{
throw new Exception("Error: cannot find the default printer.");
}
else
{
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
m_currentPageIndex = 0;
printDoc.Print();
}
}
public void Dispose()
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}
}
Preview WinForms
Xaml
xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"
...
<WindowsFormsHost DockPanel.Dock="Bottom" Margin="0 0 0 0" >
<rv:ReportViewer x:Name="RVDemo"/>
</WindowsFormsHost>
C# code part
private void RaportGenerate_Click(object sender, RoutedEventArgs e)
{
RVDemo.Reset();
ReportDataSource reportDataSource = new ReportDataSource("Ankiety", DBCommunication.fetchCytologiaAnkietyReport(...));
RVDemo.LocalReport.DataSources.Add(reportDataSource);
RVDemo.LocalReport.ReportEmbeddedResource = "Suplement.Cytologie.rdlc";
var parameters = new List<ReportParameter>();
//parameters.Add(...); // omitted for demo
RVDemo.LocalReport.SetParameters(parameters);
RVDemo.RefreshReport();
}
If there are no fixes available for RDLC scaling issue on WPF app.
One possible workaround would be migrating the file rendering part to Web version of RDLC, which would ignore screen DPI (as far as I know).
You'll need extra resource to develop this.
But a few generic functions would be enough, in most cases.
Then your reports should be able to rendered with consistence scaling.
(You may not need addtional project library for ReportViewer.WebForms if the library Microsoft.ReportViewer.Common version can be used by both WebForms and WinForms ReportViewer.)
Here's the possible solution:
1) Add a Library Project to your WPF solution
The solution should use .NET Framework 4+. It would look something like this.
2) Download the WebForm version of RDLC to the new library through NuGet
Look for Microsoft.ReportViewer.WebForms by Microsoft.
Correct version of Microsoft.ReportViewer.Common will be installed for you as dependence.
3) Create the code for Rendering through Web Version of RDLC
Create a static class for WDF project to use, this is a very simple sample for you to test if it works, before you continuing on.
Copy this class in the "RLDCRendering" Project:
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RDLCRendering
{
public class RDLCRender
{
public static byte[] RenderReport(String reportPath, DataTable data)
{
Microsoft.Reporting.WebForms.ReportDataSource rdc1 =
new Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", data);
Microsoft.Reporting.WebForms.ReportViewer v1 = new Microsoft.Reporting.WebForms.ReportViewer();
v1.LocalReport.DataSources.Clear();
v1.LocalReport.ReportPath = reportPath;
v1.LocalReport.DataSources.Add(rdc1);
return v1.LocalReport.Render(format: "PDF", deviceInfo: "");
}
}
}
The project would looks like this:
4) Hide the WPF version's report print button
Hide the Print /Save button with this example code so users would not use the faulted rendering method
ReportViewer.ShowPrintButton = false;
ReportViewer.ShowExportButton = false;
Add a print button on your WDF page, how you do it is up to you.
The end result is something like this:
Add a callback when the button is clicked, then provide all the needed data source, report path, output path to the library we created.
The follow is a sample code for you:
string connString = "Server=someIP;Database=catalogName;User Id=uid;Password=pwd";
SqlConnection sqlConn = new SqlConnection(connString);
SqlDataAdapter sqlDA = new SqlDataAdapter("select top 100 * from samplesData", sqlConn);
DataTable dt= new DataTable();
sqlDA.Fill(dt);
//Important
Byte[] bytes = RDLCRendering.RDLCRender.RenderReport(#"the path to the report teamplate\Report1.rdlc", dt);
using (FileStream stream = new FileStream(#"C:\test\test.pdf", FileMode.Create))
{
stream.Write(bytes, 0, bytes.Length);
}
5) Validate
You may change the output path as you need. When the button is clicked, a PDF file should be rendered and saved under the location you specified. In my sample, it is in C:\test\test.pdf.
If this solution works for you, you may continue to add parameters and etc. to the/other rendering function byte[] RenderReport.
Then handle the returned byte file by sending it to printer or save to some local folder and open with other applications.
The problem is well known one, it affects performance as well. The reason is simple:
RDLC was created to deliver simple reports such as receipt or invoice. anything more then that is going to cause you alot of issues and alot of headache.
There are simple solution offered by Microsoft technical support across the web:
Change the system DPI settings
Change the font on report
Make the report viewer adapt to higher resolution automatically.
But all of them just ignore the simple fact, RDLC was never meant for big reports or higher resolutions.
Only for documents such as invo
ice or receipt which are modest and with small amount of details to address

Live Video Streaming using Raspberry Pi and C#

I'm in a uni project of live Streaming the video (Taken from a Web Cam) and stream it to the desktop using C# (UWP, Windows 10 IoT Core). Even though I found some projects doing the server side implementation in Java (For Rasp) and Client side using UWP I couldn't find any Projects regarding Server side programming in C#.
Plus, is it really possible to do such server side programming using C# for live streaming as this Microsoft link say it isn't.
View the Microsoft Link
Any help would be deeply appreciated.
Regards,
T.S.
Even though I found some projects doing the server side implementation in Java (For Rasp) and Client side using UWP I couldn't find any Projects regarding Server side programming in C#.
There is another project I have coded and tested successfully. You could have a reference if it could help you.
In the MyVideoServer App the important is getting the camera id and previewFrame of the video. previewFrame = await MyMediaCapture.GetPreviewFrameAsync(videoFrame);Then send video stream to client through streamSocketClient.await streamSocketClient.sendBuffer(buffer);
public MainPage()
{
this.InitializeComponent();
InitializeCameraAsync();
InitSocket();
}
MediaCapture MyMediaCapture;
VideoFrame videoFrame;
VideoFrame previewFrame;
IBuffer buffer;
DispatcherTimer timer;
StreamSocketListenerServer streamSocketSrv;
StreamSocketClient streamSocketClient;
private async void InitializeCameraAsync()
{
var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
DeviceInformation cameraDevice = allVideoDevices.FirstOrDefault();
var mediaInitSettings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id };
MyMediaCapture = new MediaCapture();
try
{
await MyMediaCapture.InitializeAsync(mediaInitSettings);
}
catch (UnauthorizedAccessException)
{
}
PreviewControl.Height = 180;
PreviewControl.Width = 240;
PreviewControl.Source = MyMediaCapture;
await MyMediaCapture.StartPreviewAsync();
videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, 240, 180, 0);
buffer = new Windows.Storage.Streams.Buffer((uint)(240 * 180 * 8));
}
Then the key server code is trying to create a server and connect client by socket communication in InitSocket function. StreamSocketListenerServer should be created as an object and started. At the same time the server ip port is setup.streamSocketSrv = new StreamSocketListenerServer();
await streamSocketSrv.start("22333");Last but not least, the Timer_Tick will send video stream to client every 100ms.
private async void InitSocket()
{
streamSocketSrv = new StreamSocketListenerServer();
await streamSocketSrv.start("22333");
streamSocketClient = new StreamSocketClient();
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(100);
timer.Tick += Timer_Tick;
timer.Start();
}
Following you could deploy MyVideoServer App on Raspberry Pi 3.
Then you could deploy MyVideoClient App on PC. Then enter Raspberry Pi 3 IP Address and click Connect button. The video stream would display on the App.
This is the sample code and you could take a reference.

How to implement chat functionality in windows phone 7

I want to implement a simple online chat application in WP7.
I am using Matrix SDK to implement chat on my WP7
This is how I am trying to connect but I am not able to connect and send any messages.
Neither the events are getting fired..I am not getting any exception also..
what have I done wrong????
Please guide me
Thanks in advance
XmppClient xmppConn;
xmppConn = new XmppClient();
Jid jidUser = new Jid("username");
xmppConn.Username = jidUser.User;
xmppConn.Password = "password";
xmppConn.SetXmppDomain(jidUser.Server);
xmppConn.Uri = new System.Uri("http://server.com:7070/http-bind/",UriKind.RelativeOrAbsolute);
xmppConn.Status = "Testing on Windows Phone 7";
xmppConn.Show = Matrix.Xmpp.Show.Chat;
try
{
xmppConn.Open();
xmppConn.OnLogin += new EventHandler<Matrix.EventArgs>(xmppConn_OnLogin);
//xmppConn.OnPresence += new EventHandler<PresenceEventArgs>(xmppConn_OnPresence);
// xmpp.OnLogin += new EventHandler<Matrix.EventArgs>(xmpp_OnLogin);
}
catch
{
Console.WriteLine("Wrong login data!");
}
}
private void SendButton_Click(object sender, System.EventArgs e)
{
// loose focus to hide keyboard
this.Focus();
messages.Add(new ChatMessage()
{
Side = MessageSide.Me,
Text = TextInput.Text
});
var pm = new PresenceManager(xmppConn);
string sub_id = "xxxxxxxxx";
Jid jid = sub_id;
pm.Subscribe(jid);
xmppConn.Send(new Message(new Jid(jid), MessageType.chat, TextInput.Text));
xmppConn.OnMessage += new EventHandler<MessageEventArgs>(xmppConn_OnMessage);
TextInput.Text = "";
}
Take a look at SignalR for real time web based communications (including chat).
There is an official sample for Windows Phone 8 and also a 3rd party helper for WP7.
Before doing anything on xmpp you first need to set OnBind Event of xmmp class.
Reason: Most of the xmpp works asynchronously.When you call something like client.Open it returns immediately so you first need to wait for OnBind event.

Video is still downloaded after releasing an AVPlayer instance

I have problems with my video player that uses the AV Foundation API and plays a clip via HTTP progressive download. Even when the AVPlayer is released, I'm still downloading the video clip (observed via an HTTP Traffic sniffer).
My player is initialized like that:
m_player = new AVPlayer();
m_playerLayer = new AVPlayerLayer();
m_playerLayer.Player = m_player;
Then, when I have the URL of the video:
m_url = new NSUrl (...);
m_asset = new AVAsset(m_url);
m_asset.AddObserver(this, new NSString ("playable"), NSKeyValueObservingOptions.Initial | NSKeyValueObservingOptions.New, AVPlayerAssetObservationContext);
When I'am notified that the asset is playable, I'm creating an AVPlayerItem:
m_playerItem = new AVPlayerItem(m_asset);
if (m_player.CurrentItem != m_playerItem)
{
m_player.ReplaceCurrentItemWithPlayerItem (m_playerItem);
}
My video is playing without any problems. Then, when I press a back button, I have a mechanism that call a Destroy() method. Here I tried a lot of things to be sure that my player is well released:
if(m_player != null)
{
m_player.Pause();
m_player.Dispose();
m_player = null;
}
if(m_playerLayer != null)
{
m_playerLayer.Dispose();
m_playerLayer = null;
}
if(m_playerItem != null)
{
m_playerItem.Dispose();
m_playerItem = null;
}
if(m_asset != null)
{
m_asset.CancelLoading();
m_asset.RemoveObserver(this, new NSString("playable"));
m_asset.Dispose();
m_asset = null;
}
if(m_url != null)
{
m_url.Dispose();
m_url = null;
}
I tested my app with a debugger and for sure, I'am falling into this code. My objects seems to be well released, but for sure the application is still downloading the video url. Am I doing something wrong in the init / release code?
Thank in advance for your help!
The workaround I found is to add this line in the Destroy() code
m_player.ReplaceCurrentItemWithPlayerItem(new AVPlayerItem());
The video download is then interrupted.

How can I use google text to speech api in windows form?

I want to use google text to speech in my windows form application, it will read a label. I added System.Speech reference. How can it read a label with a button click event?
http://translate.google.com/translate_tts?q=testing+google+speech This is the google text to speech api, or how can I use microsoft's native text to speech?
UPDATE Google's TTS API is no longer publically available. The notes at the bottom about Microsoft's TTS are still relevant and provide equivalent functionality.
You can use Google's TTS API from your WinForm application by playing the response using a variation of this question's answer (it took me a while but I have a real solution):
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.FormClosing += (sender, e) =>
{
if (waiting)
stop.Set();
};
}
private void ButtonClick(object sender, EventArgs e)
{
var clicked = sender as Button;
var relatedLabel = this.Controls.Find(clicked.Tag.ToString(), true).FirstOrDefault() as Label;
if (relatedLabel == null)
return;
var playThread = new Thread(() => PlayMp3FromUrl("http://translate.google.com/translate_tts?q=" + HttpUtility.UrlEncode(relatedLabel.Text)));
playThread.IsBackground = true;
playThread.Start();
}
bool waiting = false;
AutoResetEvent stop = new AutoResetEvent(false);
public void PlayMp3FromUrl(string url)
{
using (Stream ms = new MemoryStream())
{
using (Stream stream = WebRequest.Create(url)
.GetResponse().GetResponseStream())
{
byte[] buffer = new byte[32768];
int read;
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
}
ms.Position = 0;
using (WaveStream blockAlignedStream =
new BlockAlignReductionStream(
WaveFormatConversionStream.CreatePcmStream(
new Mp3FileReader(ms))))
{
using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
{
waveOut.Init(blockAlignedStream);
waveOut.PlaybackStopped += (sender, e) =>
{
waveOut.Stop();
};
waveOut.Play();
waiting = true;
stop.WaitOne(10000);
waiting = false;
}
}
}
}
}
NOTE: The above code requires NAudio to work (free/open source) and using statements for System.Web, System.Threading, and NAudio.Wave.
My Form1 has 2 controls on it:
A Label named label1
A Button named button1 with a Tag of label1 (used to bind the button to its label)
The above code can be simplified slightly if a you have different events for each button/label combination using something like (untested):
private void ButtonClick(object sender, EventArgs e)
{
var clicked = sender as Button;
var playThread = new Thread(() => PlayMp3FromUrl("http://translate.google.com/translate_tts?q=" + HttpUtility.UrlEncode(label1.Text)));
playThread.IsBackground = true;
playThread.Start();
}
There are problems with this solution though (this list is probably not complete; I'm sure comments and real world usage will find others):
Notice the stop.WaitOne(10000); in the first code snippet. The 10000 represents a maximum of 10 seconds of audio to be played so it will need to be tweaked if your label takes longer than that to read. This is necessary because the current version of NAudio (v1.5.4.0) seems to have a problem determining when the stream is done playing. It may be fixed in a later version or perhaps there is a workaround that I didn't take the time to find. One temporary workaround is to use a ParameterizedThreadStart that would take the timeout as a parameter to the thread. This would allow variable timeouts but would not technically fix the problem.
More importantly, the Google TTS API is unofficial (meaning not to be consumed by non-Google applications) it is subject to change without notification at any time. If you need something that will work in a commercial environment I'd suggest either the MS TTS solution (as your question suggests) or one of the many commercial alternatives. None of which tend to be even this simple though.
To answer the other side of your question:
The System.Speech.Synthesis.SpeechSynthesizer class is much easier to use and you can count on it being available reliably (where with the Google API, it could be gone tomorrow).
It is really as easy as including a reference to the System.Speech reference and:
public void SaySomething(string somethingToSay)
{
var synth = new System.Speech.Synthesis.SpeechSynthesizer();
synth.SpeakAsync(somethingToSay);
}
This just works.
Trying to use the Google TTS API was a fun experiment but I'd be hard pressed to suggest it for production use, and if you don't want to pay for a commercial alternative, Microsoft's solution is about as good as it gets.
I know this question is a bit out of date but recently Google published Google Cloud Text To Speech API.
.NET Client version of Google.Cloud.TextToSpeech can be found here:
https://github.com/jhabjan/Google.Cloud.TextToSpeech.V1
Here is short example how to use the client:
GoogleCredential credentials =
GoogleCredential.FromFile(Path.Combine(Program.AppPath, "jhabjan-test-47a56894d458.json"));
TextToSpeechClient client = TextToSpeechClient.Create(credentials);
SynthesizeSpeechResponse response = client.SynthesizeSpeech(
new SynthesisInput()
{
Text = "Google Cloud Text-to-Speech enables developers to synthesize natural-sounding speech with 32 voices"
},
new VoiceSelectionParams()
{
LanguageCode = "en-US",
Name = "en-US-Wavenet-C"
},
new AudioConfig()
{
AudioEncoding = AudioEncoding.Mp3
}
);
string speechFile = Path.Combine(Directory.GetCurrentDirectory(), "sample.mp3");
File.WriteAllBytes(speechFile, response.AudioContent);

Categories