コード例 #1
0
        public IHttpActionResult Get(string email)
        {
            homesecurityserviceContext db_context = new homesecurityserviceContext();
            var result = db_context.Videos.Where(e => e.Email.Equals(email))
                .Select(e => new
                {
                    Vid = e.Vid,
                    RoomName = e.RoomName,
                    CreatedAt = e.CreatedAt
                }).OrderByDescending(e => e.CreatedAt).ToList();

            if (result.Count > 0)
                return Ok(result);

            return NotFound();
        }
コード例 #2
0
        private void uploadVideoToAMSAsync(string path,string fileName,string email,string roomName)
        {
            //get reference to DB context
            homesecurityserviceContext db_context = new homesecurityserviceContext();

            //upload the video as an asset to AMS using the path where the video has been stored 
            var context = new CloudMediaContext("", "");
            var uploadAsset = context.Assets.Create(Path.GetFileName(fileName), AssetCreationOptions.None);
            var assetFile = uploadAsset.AssetFiles.Create(Path.GetFileName(fileName));
            assetFile.Upload(path);

            File.Delete(path); //delete the file that was saved locally

            var encodeAssetId = uploadAsset.Id; 
            var encodingPreset = "H264 Multiple Bitrate 720p"; //multi bitrate allows the video to be viewed on phones
            var assetToEncode = context.Assets.Where(a => a.Id == encodeAssetId).FirstOrDefault();
            if (assetToEncode == null)
            {
                throw new ArgumentException("Could not find assetId: " + encodeAssetId);
            }

            //Encoding process
            IJob job = context.Jobs.Create("Encoding " + assetToEncode.Name + " to " + encodingPreset);

            IMediaProcessor latestWameMediaProcessor = (from p in context.MediaProcessors where p.Name == "Media Encoder Standard" select p).ToList().OrderBy(wame => new Version(wame.Version)).LastOrDefault();
            ITask encodeTask = job.Tasks.AddNew("Encoding", latestWameMediaProcessor, encodingPreset, TaskOptions.None);
            encodeTask.InputAssets.Add(assetToEncode);
            encodeTask.OutputAssets.AddNew(assetToEncode.Name + " as " + encodingPreset, AssetCreationOptions.None);

            job.StateChanged += new EventHandler<JobStateChangedEventArgs>((sender, jsc) => Console.WriteLine(string.Format("{0}\n  State: {1}\n  Time: {2}\n\n", ((IJob)sender).Name, jsc.CurrentState, DateTime.UtcNow.ToString(@"yyyy_M_d_hhmmss"))));
            job.Submit();
            job.GetExecutionProgressTask(CancellationToken.None).Wait();

            var preparedAsset = job.OutputMediaAssets.FirstOrDefault();

            var streamingAssetId = preparedAsset.Id; // "YOUR ASSET ID";
            var daysForWhichStreamingUrlIsActive = 365;
            var streamingAsset = context.Assets.Where(a => a.Id == streamingAssetId).FirstOrDefault();
            var accessPolicy = context.AccessPolicies.Create(streamingAsset.Name, TimeSpan.FromDays(daysForWhichStreamingUrlIsActive),
                                                     AccessPermissions.Read);

            //Returning a streamingUrl for the video
            string streamingUrl = string.Empty;
            var assetFiles = streamingAsset.AssetFiles.ToList();
            var streamingAssetFile = assetFiles.Where(f => f.Name.ToLower().EndsWith("m3u8-aapl.ism")).FirstOrDefault();
            if (streamingAssetFile != null)
            {
                var locator = context.Locators.CreateLocator(LocatorType.OnDemandOrigin, streamingAsset, accessPolicy);
                Uri hlsUri = new Uri(locator.Path + streamingAssetFile.Name + "/manifest(format=m3u8-aapl)");
                streamingUrl = hlsUri.ToString();
            }
            streamingAssetFile = assetFiles.Where(f => f.Name.ToLower().EndsWith(".ism")).FirstOrDefault();
            if (string.IsNullOrEmpty(streamingUrl) && streamingAssetFile != null)
            {
                var locator = context.Locators.CreateLocator(LocatorType.OnDemandOrigin, streamingAsset, accessPolicy);
                Uri smoothUri = new Uri(locator.Path + streamingAssetFile.Name + "/manifest");
                streamingUrl = smoothUri.ToString();
            }
            streamingAssetFile = assetFiles.Where(f => f.Name.ToLower().EndsWith(".mp4")).FirstOrDefault();
            if (string.IsNullOrEmpty(streamingUrl) && streamingAssetFile != null)
            {
                var locator = context.Locators.CreateLocator(LocatorType.Sas, streamingAsset, accessPolicy);
                var mp4Uri = new UriBuilder(locator.Path);
                mp4Uri.Path += "/" + streamingAssetFile.Name;
                streamingUrl = mp4Uri.ToString();
            }

            string dateString = DateTime.Now.ToString(@"MM\/dd\/yyyy HH:mm"); //format the date will be saved as (for the android app)

            //save the url of the video to the database so that a user can have a list of their videos in the android app
            Video v = new Video
            {
                Id = Guid.NewGuid().ToString(),
                Email = email,
                Vid = streamingUrl,
                RoomName = roomName,
                CreatedAt = dateString
            };
            db_context.Videos.Add(v);
            db_context.SaveChanges();

            //encode the streaming url so that its not visible in the url
            var encodedUrl = System.Text.Encoding.UTF8.GetBytes(streamingUrl);
            SendEmail(System.Convert.ToBase64String(encodedUrl), email);
        }