Skip to content

Commit

Permalink
upload para o azure blob storage (quebrado)
Browse files Browse the repository at this point in the history
  • Loading branch information
rcarubbi committed Jun 29, 2017
1 parent b1b3847 commit 0a587dc
Show file tree
Hide file tree
Showing 290 changed files with 713,696 additions and 35,836 deletions.
Binary file modified .vs/WebRTCExample/v14/.suo
Binary file not shown.
12 changes: 0 additions & 12 deletions WebRTCExample.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,13 @@ EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{774ADF0F-CF39-4356-9E30-BE99EDED4141}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{774ADF0F-CF39-4356-9E30-BE99EDED4141}.Debug|Any CPU.Build.0 = Debug|Any CPU
{774ADF0F-CF39-4356-9E30-BE99EDED4141}.Debug|x64.ActiveCfg = Debug|Any CPU
{774ADF0F-CF39-4356-9E30-BE99EDED4141}.Debug|x64.Build.0 = Debug|Any CPU
{774ADF0F-CF39-4356-9E30-BE99EDED4141}.Debug|x86.ActiveCfg = Debug|x86
{774ADF0F-CF39-4356-9E30-BE99EDED4141}.Debug|x86.Build.0 = Debug|x86
{774ADF0F-CF39-4356-9E30-BE99EDED4141}.Release|Any CPU.ActiveCfg = Release|Any CPU
{774ADF0F-CF39-4356-9E30-BE99EDED4141}.Release|Any CPU.Build.0 = Release|Any CPU
{774ADF0F-CF39-4356-9E30-BE99EDED4141}.Release|x64.ActiveCfg = Release|Any CPU
{774ADF0F-CF39-4356-9E30-BE99EDED4141}.Release|x64.Build.0 = Release|Any CPU
{774ADF0F-CF39-4356-9E30-BE99EDED4141}.Release|x86.ActiveCfg = Release|x86
{774ADF0F-CF39-4356-9E30-BE99EDED4141}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
45 changes: 20 additions & 25 deletions WebRTCPOC/Api/RecordMemory.cs → WebRTCPOC/Api/VideoRecorder.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Accord.Video.FFMPEG;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Web;
using System;

namespace WebRTCPOC.Api
{
public static class RecordMemory
public static class VideoRecorder
{
private static object _syncRoot = new object();
private static Dictionary<string, VideoFileWriter> writers = new Dictionary<string, VideoFileWriter>();
Expand All @@ -26,45 +26,40 @@ public static void CloseWriter(string id)

public static void AppendVideoFrame(string id, Bitmap bitmap)
{
lock (writers[id])
{
writers[id].WriteVideoFrame(bitmap);

}
writers[id].WriteVideoFrame(bitmap);
}


public static void AddAudio(string id, byte[] frame)
{
lock (_syncRoot)
{

writers[id].WriteAudioFrame(frame);
}

writers[id].WriteAudioFrame(frame);

}
public static void CreateWriter(string id)
{
lock (_syncRoot)

if (!writers.ContainsKey(id))
{
if (!writers.ContainsKey(id))
{
ready.Add(id, false);
writers.Add(id, new VideoFileWriter());
writers[id].Open(Path.Combine(HttpContext.Current.Server.MapPath("~/records"), $"{id}.mp4"), 320, 240, 10, VideoCodec.MPEG4, 16000, AudioCodec.MP3, 44100 * 16, 44100, 1);
}
ready.Add(id, false);
writers.Add(id, new VideoFileWriter());
writers[id].Open(Path.Combine(HttpContext.Current.Server.MapPath("~/bin"), $"{id}.mp4"), 320, 240, 10, VideoCodec.MPEG4, 16000, AudioCodec.MP3, 44100 * 16, 44100, 1);
}
}



internal static void VideoReady(string id)
}
public static void VideoReady(string id)
{
ready[id] = true;
}

internal static bool VideoIsReady(string id)
public static bool VideoIsReady(string id)
{
return ready[id];
}

public static void UploadVideo(string id, CloudBlobContainer container)
{
CloudBlockBlob blockBlobVideo = container.GetBlockBlobReference($"{id}.mp4");
blockBlobVideo.UploadFromFile(Path.Combine(HttpContext.Current.Server.MapPath("~/bin"), $"{id}.mp4"));
}
}
}
88 changes: 61 additions & 27 deletions WebRTCPOC/Api/WebRTCAudioRecordController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using NAudio.Wave;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using NAudio.Wave;
using System;
using System.IO;
using System.Net;
Expand All @@ -18,7 +21,7 @@ public class WebRTCAudioRecordController : ApiController
private string _id;
private string _audioFilename;

NAudio.Wave.WaveFileWriter _writer;
WaveFileWriter _audioWriter;
FileStream _stream;


Expand All @@ -40,44 +43,75 @@ public byte[] TrimEnd(byte[] array)
}
private async Task ProcessRecord(AspNetWebSocketContext context)
{
bool idReceived = false;
WebSocket socket = context.WebSocket;
while (true)
try
{
bool idReceived = false;
WebSocket socket = context.WebSocket;
while (true)
{


if (socket.State == WebSocketState.Open)
{
if (!idReceived)
if (socket.State == WebSocketState.Open)
{
await ReceiveIdAsync(socket);
idReceived = true;
if (!idReceived)
{
await ReceiveIdAsync(socket);
idReceived = true;

_stream = new FileStream(_audioFilename, FileMode.CreateNew, FileAccess.ReadWrite);
_audioWriter = new WaveFileWriter(_stream, WaveFormat.CreateIeeeFloatWaveFormat(44100, 1));
}
else
{
await ReceiveSamplesAsync(socket);
}




_stream = new FileStream(_audioFilename, FileMode.CreateNew, FileAccess.Write);
_writer = new NAudio.Wave.WaveFileWriter(_stream, WaveFormat.CreateIeeeFloatWaveFormat(44100, 1));
}
else
{
await ReceiveSamplesAsync(socket);
}

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("AzureBlobStorage.ConnectionString"));

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference("webrtcpoccontainer");
container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

}
else
{
_writer.Close();
_writer.Dispose();
_writer = null;
while(!RecordMemory.VideoIsReady(_id))
{
Thread.Sleep(1000);

CloudBlockBlob blockBlob = container.GetBlockBlobReference(Path.GetFileName(_audioFilename));

blockBlob.UploadFromStream(_stream);


while (!VideoRecorder.VideoIsReady(_id))
{
Thread.Sleep(1000);
}
VideoRecorder.CloseWriter(_id);
VideoRecorder.UploadVideo(_id, container);

_audioWriter.Close();
_audioWriter.Dispose();
_audioWriter = null;
_stream.Close();
_stream.Dispose();
_stream = null;

File.Delete(_audioFilename);
File.Delete(Path.ChangeExtension(_audioFilename, ".mp4"));
break;
}
RecordMemory.CloseWriter(_id);
break;
}
}
catch (Exception ex)
{
var x = ex;
}
}

private async Task ReceiveSamplesAsync(WebSocket socket)
Expand All @@ -90,7 +124,7 @@ private async Task ReceiveSamplesAsync(WebSocket socket)
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[4096]);
result = await socket.ReceiveAsync(buffer, CancellationToken.None);
var bufferDecoded = TrimEnd(buffer.Array);
_writer.Write(bufferDecoded, 0, bufferDecoded.Length);
_audioWriter.Write(bufferDecoded, 0, bufferDecoded.Length);
} while (!result.EndOfMessage);
}
catch (Exception ex)
Expand All @@ -104,7 +138,7 @@ private async Task ReceiveIdAsync(WebSocket socket)
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]);
WebSocketReceiveResult result = await socket.ReceiveAsync(buffer, CancellationToken.None);
_id = Encoding.UTF8.GetString(buffer.Array, 0, result.Count);
_audioFilename = Path.Combine(HttpContext.Current.Server.MapPath("~/records"), $"{_id}.wav");
_audioFilename = Path.Combine(HttpContext.Current.Server.MapPath("~/bin"), $"{_id}.wav");
buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes("Id-Received"));
await socket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
}
Expand Down
14 changes: 9 additions & 5 deletions WebRTCPOC/Api/WebRTCVideoRecordController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -41,7 +44,7 @@ private async Task ProcessRecord(AspNetWebSocketContext context)
{
await ReceiveIdAsync(socket);
idReceived = true;
RecordMemory.CreateWriter(_id);
VideoRecorder.CreateWriter(_id);
}
else
{
Expand All @@ -50,9 +53,10 @@ private async Task ProcessRecord(AspNetWebSocketContext context)
}
else
{
RecordMemory.VideoReady(_id);
VideoRecorder.VideoReady(_id);



break;

}
Expand Down Expand Up @@ -89,7 +93,7 @@ private async Task ReceiveFrameAsync(WebSocket socket)
} while (!result.EndOfMessage);

byte[] frame = frameParts.SelectMany(x => x).ToArray();
RecordMemory.AppendVideoFrame(_id, new System.Drawing.Bitmap(new MemoryStream(TrimEnd(frame))));
VideoRecorder.AppendVideoFrame(_id, new System.Drawing.Bitmap(new MemoryStream(TrimEnd(frame))));
}
catch (Exception ex)
{
Expand Down
19 changes: 19 additions & 0 deletions WebRTCPOC/Extensions/MemoryStreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace WebRTCPOC.Extensions
{
public static class MemoryStreamExtensions
{
public static void Clear(this MemoryStream source)
{
byte[] buffer = source.GetBuffer();
Array.Clear(buffer, 0, buffer.Length);
source.Position = 0;
source.SetLength(0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ by editing this MSBuild file. In order to learn more about this please visit htt
<SiteUrlToLaunchAfterPublish>https://webrtcpoc.azurewebsites.net</SiteUrlToLaunchAfterPublish>
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<ExcludeApp_Data>False</ExcludeApp_Data>
<publishUrl>ftp:https://waws-prod-cq1-005.ftp.azurewebsites.windows.net</publishUrl>
<publishUrl>ftp:https://waws-prod-cq1-009.ftp.azurewebsites.windows.net</publishUrl>
<DeleteExistingFiles>False</DeleteExistingFiles>
<FtpPassiveMode>True</FtpPassiveMode>
<FtpSitePath>site/wwwroot</FtpSitePath>
Expand Down
21 changes: 21 additions & 0 deletions WebRTCPOC/Properties/PublishProfiles/WebRTCPoc - FTP (3).pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="https://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>FTP</WebPublishMethod>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish>https://webrtcpoc.azurewebsites.net</SiteUrlToLaunchAfterPublish>
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<ExcludeApp_Data>False</ExcludeApp_Data>
<publishUrl>ftp:https://waws-prod-sn1-127.ftp.azurewebsites.windows.net</publishUrl>
<DeleteExistingFiles>False</DeleteExistingFiles>
<FtpPassiveMode>True</FtpPassiveMode>
<FtpSitePath>site/wwwroot</FtpSitePath>
<UserName>WebRTCPoc\$WebRTCPoc</UserName>
<_SavePWD>True</_SavePWD>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ by editing this MSBuild file. In order to learn more about this please visit htt
<RemoteSitePhysicalPath />
<SkipExtraFilesOnServer>True</SkipExtraFilesOnServer>
<MSDeployPublishMethod>WMSVC</MSDeployPublishMethod>
<EnableMSDeployBackup>True</EnableMSDeployBackup>
<EnableMSDeployBackup>False</EnableMSDeployBackup>
<UserName>$WebRTCPoc</UserName>
<_SavePWD>True</_SavePWD>
<_DestinationType>AzureWebSite</_DestinationType>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="https://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>MSDeploy</WebPublishMethod>
<ADUsesOwinOrOpenIdConnect>False</ADUsesOwinOrOpenIdConnect>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish>https://webrtcpoc.azurewebsites.net</SiteUrlToLaunchAfterPublish>
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<ExcludeApp_Data>False</ExcludeApp_Data>
<MSDeployServiceURL>webrtcpoc.scm.azurewebsites.net:443</MSDeployServiceURL>
<DeployIisAppPath>WebRTCPoc</DeployIisAppPath>
<RemoteSitePhysicalPath />
<SkipExtraFilesOnServer>True</SkipExtraFilesOnServer>
<MSDeployPublishMethod>WMSVC</MSDeployPublishMethod>
<EnableMSDeployBackup>True</EnableMSDeployBackup>
<UserName>$WebRTCPoc</UserName>
<_SavePWD>True</_SavePWD>
<_DestinationType>AzureWebSite</_DestinationType>
</PropertyGroup>
</Project>
4 changes: 2 additions & 2 deletions WebRTCPOC/Scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ function step3(call) {
var videoRecordTask, audioRecordTask;
function beginRecord(id)
{
var videoWebsocketUri = "wss:https://" + window.location.hostname + ":" + (window.location.port || "443") + "/api/WebRTCVideoRecord";
var videoWebsocketUri = "ws:https://" + window.location.hostname + ":" + (window.location.port || "80") + "/api/WebRTCVideoRecord";
localVideoRec = new WSVideoRecorder(window.localStream, videoWebsocketUri, id, videoReady);

var audioWebsocketUri = "wss:https://" + window.location.hostname + ":" + (window.location.port || "443") + "/api/WebRTCAudioRecord";
var audioWebsocketUri = "ws:https://" + window.location.hostname + ":" + (window.location.port || "80") + "/api/WebRTCAudioRecord";
var input = audioContext.createMediaStreamSource(window.localStream);
localAudioRec = new WSAudioRecorder(input, audioWebsocketUri, id, audioReady);
}
Expand Down
1 change: 1 addition & 0 deletions WebRTCPOC/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="owin:AutomaticAppStartup" value="false" />
<add key="AzureBlobStorage.ConnectionString" value="DefaultEndpointsProtocol=https;AccountName=webrtcpocstorage;AccountKey=hYoVw6ImtTMDeKV/MTk0j+iLLvz1Wip3ZXz1OPekvumaA48bBNsY2Vz+3M/oN8vDzYsOiuVqLj0FNMN6IrPuNQ==;EndpointSuffix=core.windows.net" />
</appSettings>
<system.web>
<customErrors mode="Off"></customErrors>
Expand Down
Loading

0 comments on commit 0a587dc

Please sign in to comment.