public void Play(int projectId)
        {
            var video = dispatcher.Dispatch(VideoActions.GetVideoForProject(projectId));

            Guard.This(video).AgainstDefaultValue("No video found for project");

            mp3Player = new Mp3AudioPlayer();
            mp3Player.Load(video.AudioFilePath);

            var appPreferences = dispatcher.Dispatch(AppPreferencesActions.GetAppPreferences());

            var startTime = DateTime.Now.AddMilliseconds(appPreferences.PlaybackBuffer);

            foreach (var devicePlayback in devicePlaybacks)
            {
                devicePlayback.Client.PlayVideo(devicePlayback.ProjectDeviceId,
                                                new Pi.Api.ApiRequests.PlayVideoRequest
                {
                    PlayAt           = startTime,
                    TimeReferenceUrl = string.Format("http://{0}:8001/api/time", GetTimeReferenceHost(devicePlayback.Client))
                });
            }

            while (DateTime.Now.CompareTo(startTime.AddMilliseconds(-20)) < 0)
            {
                continue;
            }

            mp3Player.Play();
        }
Ejemplo n.º 2
0
        public void PerformActions(VideoActions action)
        {
            switch (action)
            {
            case VideoActions.AddTag:
                Console.WriteLine("Which tag would you like to add?");
                break;

            case VideoActions.Play:
                Console.WriteLine("Started playing video");
                break;

            case VideoActions.Pause:
                Console.WriteLine("Video paused");
                break;

            case VideoActions.Stop:
                Console.WriteLine("Stopped video reproduction");
                break;

            default:
                Console.WriteLine("Invalid command");
                break;
            }
        }
        public void LoadAudio(int projectId)
        {
            var video = dispatcher.Dispatch(VideoActions.GetVideoForProject(projectId));

            Guard.This(video).AgainstDefaultValue("Could not find video for project");

            if (audioTrack == null || audioTrack.AudioFilePath != video.FilePath)
            {
                audioTrack = AudioTrack.FromAudioFile(video.AudioFilePath);
            }
        }
Ejemplo n.º 4
0
        public GlobalJsonResult <VideoMetadataResponse> GetProjectVideoMetadata(int projectId)
        {
            var video = dispatcher.Dispatch(VideoActions.GetVideoForProject(projectId));

            Guard.This(video).AgainstDefaultValue(string.Format("No video exists for project with id {0}", projectId));
            Guard.This(video.FilePath).WithRule(path => File.Exists(path), string.Format("Video {0} does not exist", video.FilePath));

            videoFileReader.Open(video.FilePath);
            var result = new VideoMetadataResponse();

            result.Populate(videoFileReader);

            return(GlobalJsonResult <VideoMetadataResponse> .Success(System.Net.HttpStatusCode.OK, result));
        }
Ejemplo n.º 5
0
        public GlobalJsonResult <ProjectVideo> AddMostRecentVideoUploadToProject(int projectId, int videoReferenceId)
        {
            var result = dispatcher.Dispatch(VideoActions.SetVideoForProject(projectId, videoReferenceId));

            // When a video changes, create new version of project devices
            var projectDevices = dispatcher.Dispatch(ProjectDeviceActions.GetProjectDevices(projectId));

            foreach (var projectDevice in projectDevices)
            {
                var latestProjectDeviceVersion = dispatcher.Dispatch(ProjectDeviceActions.GetLatestProjectDeviceVersion(projectDevice.Id));
                dispatcher.Dispatch(ProjectDeviceActions.SetLatestProjectDeviceVersion(latestProjectDeviceVersion));
            }

            return(GlobalJsonResult <ProjectVideo> .Success(System.Net.HttpStatusCode.Created, result));
        }
Ejemplo n.º 6
0
        public VideoReference ConsumeAndFinishUpload()
        {
            var timeoutStopwatch = new Stopwatch();

            timeoutStopwatch.Start();

            while (timeoutStopwatch.ElapsedMilliseconds < int.Parse(config.AppSettings["VideoUploadTimeoutMilliseconds"]))
            {
                if (!isUploading)
                {
                    var audio = AudioTrack.FromVideo(Path.Combine(UploadDirectory(), fileName));

                    return(dispatcher.Dispatch(VideoActions.CreateVideoReference(new VideoReference
                    {
                        FilePath = Path.Combine(UploadDirectory(), fileName),
                        AudioFilePath = audio.AudioFilePath
                    })));
                }
            }

            isUploading = false;
            throw new Exception("Video upload timed out");
        }
Ejemplo n.º 7
0
        public VideoMetadata StartReadingVideo(int projectId, int projectDeviceId)
        {
            var project = dispatcher.Dispatch(ProjectActions.GetProject(projectId));

            Guard.This(project).AgainstDefaultValue(string.Format("Could not find project with project id '{0}'", projectId));

            var video = dispatcher.Dispatch(VideoActions.GetVideoForProject(projectId));

            Guard.This(video).AgainstDefaultValue(string.Format("Could not find video for project '{0}' (id '{1}')", project.Name, project.Id));

            var projectDeviceVersion = dispatcher.Dispatch(ProjectDeviceActions.GetLatestProjectDeviceVersion(projectDeviceId));

            Guard.This(projectDeviceVersion).AgainstDefaultValue(string.Format("Could not get latest project device version for project '{0}' (id '{1}')", project.Name, project.Id));

            reader = videoFileReader();
            reader.Open(video.FilePath);

            framesRead                = 0;
            framesToRead              = reader.FrameCount;
            this.videoReference       = video;
            this.projectDeviceVersion = projectDeviceVersion;

            return(new VideoMetadata(reader.FrameCount, reader.FrameRate));
        }
Ejemplo n.º 8
0
        public void ProcessProjectDeviceVersion(BackgroundJob job)
        {
            var projectDeviceVersion = dispatcher.Dispatch(ProjectDeviceActions.GetProjectDeviceVersion(job.ProjectDeviceVersionId));

            Guard.This(projectDeviceVersion).AgainstDefaultValue(string.Format("Could not find project device version '{0}'", job.ProjectDeviceVersionId));

            var device = dispatcher.Dispatch(DeviceActions.GetProjectDevice(projectDeviceVersion.ProjectDeviceId));

            Guard.This(device).AgainstDefaultValue(string.Format("Could not find project device with project device Id '{0}'", projectDeviceVersion.ProjectDeviceId));
            Guard.This(device).WithRule(d => deviceStatusService.IsOnline(device), string.Format("Device {0} (with IP Address {1}) is not online", device.Name, device.IpAddress));

            var project = dispatcher.Dispatch(ProjectActions.GetProjectFromProjectDevice(projectDeviceVersion.ProjectDeviceId));

            Guard.This(project).AgainstDefaultValue(string.Format("Could not find project from project device with id '{0}'", projectDeviceVersion.ProjectDeviceId));

            var video = dispatcher.Dispatch(VideoActions.GetVideoForProject(project.Id));

            Guard.This(video).AgainstDefaultValue(string.Format("Could not find video for project '{0}'", project.Name));

            using (var videoProcessor = videoProcessorInstantiator())
            {
                var videoMetadata = videoProcessor.StartReadingVideo(project.Id, projectDeviceVersion.ProjectDeviceId);
                var client        = piClientFactory.ForDevice(device);

                // Update video metadata (use project device id instead of video id with client (could have the same video in multiple configurations)
                var existingVideosOnPi = client.GetAllVideoMetadata();
                var existingVideoOnPi  = existingVideosOnPi.SingleOrDefault(v => v.Id == projectDeviceVersion.ProjectDeviceId);

                if (existingVideoOnPi == null)
                {
                    client.CreateVideoMetadata(new VideoMetadataCreateRequest
                    {
                        Id        = projectDeviceVersion.ProjectDeviceId,
                        FileName  = video.FilePath,
                        FrameRate = videoMetadata.FrameRate
                    });
                }
                else
                {
                    client.UpdateVideoMetadata(new VideoMetadataPutRequest
                    {
                        Id        = projectDeviceVersion.ProjectDeviceId,
                        FileName  = video.FilePath,
                        FrameRate = videoMetadata.FrameRate,
                    });
                }

                // Clear, then send frames to Pi
                client.ClearFrames(projectDeviceVersion.ProjectDeviceId);

                int framePosition = 1;
                while (true)
                {
                    var read = videoProcessor.ReadNext1000Frames();
                    client.SendFrames(projectDeviceVersion.ProjectDeviceId, new AppendFramesRequest
                    {
                        AppendFrameRequests = read.Frames
                                              .Select(f => new AppendFrameRequest {
                            BinaryData = f, Position = framePosition
                        })
                                              .ToArray()
                    });

                    dispatcher.Dispatch(BackgroundJobActions.MarkPercentageCompletion(job.Id, read.PercentageComplete));

                    if (!read.MoreFrames)
                    {
                        break;
                    }

                    framePosition++;
                }
            }
        }
Ejemplo n.º 9
0
        public GlobalJsonResult <VideoReference> GetProjectVideo(int projectId)
        {
            var result = dispatcher.Dispatch(VideoActions.GetVideoForProject(projectId));

            return(GlobalJsonResult <VideoReference> .Success(System.Net.HttpStatusCode.OK, result));
        }
Ejemplo n.º 10
0
        public GlobalJsonResult <IEnumerable <VideoReference> > GetVideoReferences()
        {
            var result = dispatcher.Dispatch(VideoActions.GetVideoReferences());

            return(GlobalJsonResult <IEnumerable <VideoReference> > .Success(System.Net.HttpStatusCode.OK, result));
        }