Vuo  2.3.2
VuoAvWriter.mm
Go to the documentation of this file.
1 
10 #include "module.h"
11 #include "VuoAvWriter.h"
12 #include "VuoAvWriterObject.h"
13 
14 #ifdef VUO_COMPILER
16  "title" : "VuoAvWriter",
17  "dependencies" : [
18  "VuoImage",
19  "VuoAudioSamples",
20  "VuoAvWriterObject",
21  "CoreMedia.framework",
22  "AVFoundation.framework"
23  ]
24  });
25 #endif
26 
30 void VuoAvWriter_free(VuoAvWriter writer);
31 
33 {
34  VuoAvWriterObject* av = [[VuoAvWriterObject alloc] init];
36  return (void*) av;
37 }
38 
42 bool VuoAvWriter_initializeMovie(VuoAvWriter writer, int width, int height, int channels, VuoText url, bool overwrite, VuoMovieFormat format)
43 {
44  VuoAvWriterObject* av = (VuoAvWriterObject*)writer;
45 
46  if( [av isRecording] == NO )
47  {
48  NSError *error = nil;
49 
50  NSString* apple_string = [[NSString stringWithUTF8String:url] stringByExpandingTildeInPath];
51  NSString* extension = [apple_string pathExtension];
52 
53  if( [extension caseInsensitiveCompare:@"mov"] != NSOrderedSame )
54  apple_string = [apple_string stringByAppendingPathExtension:@"mov"];
55 
56  NSURL* file_url = [NSURL fileURLWithPath:apple_string];
57 
58  // check if a file already exists at this location
59  if ([[NSFileManager defaultManager] fileExistsAtPath:[file_url path]])
60  {
61  if(overwrite)
62  {
63  if (![[NSFileManager defaultManager] removeItemAtURL:file_url error:&error])
64  {
65  VUserLog("Failed deleting old video file at path: %s", [apple_string UTF8String]);
66  return NO;
67  }
68  }
69  else
70  {
71  // file exits and overwrite is false
72  return NO;
73  }
74  }
75 
76  // try to initialize the AssetWriter
77  bool success = [av setupAssetWriterWithUrl:file_url
78  imageWidth:width
79  imageHeight:height
80  channelCount:channels
81  movieFormat:format];
82 
83  // if it doesn't succeed for whatever reason, exit
84  if( !success )
85  {
86  return false;
87  }
88  }
89 
90  // we're good to start recording. now set the correct width, height, and audio channel count
91  // note that once these values are set they may not be changed until Finalize is called.
92  return true;
93 }
94 
96 {
97  VuoAvWriterObject* av = (VuoAvWriterObject*)writer;
98  return av != nil && [av isRecording];
99 }
100 
106 void VuoAvWriter_appendImage(VuoAvWriter writer, VuoImage image, VuoReal timestamp, bool blockIfNotReady)
107 {
108  VuoAvWriterObject* av = (VuoAvWriterObject*)writer;
109 
110  if(image == nil || av == nil || ![av isRecording])
111  return;
112 
113  [av appendImage:image presentationTime:timestamp blockIfNotReady:blockIfNotReady];
114 }
115 
116 void VuoAvWriter_appendAudio(VuoAvWriter writer, VuoList_VuoAudioSamples samples, VuoReal timestamp, bool blockIfNotReady)
117 {
118  VuoAvWriterObject* av = (VuoAvWriterObject*)writer;
119 
120  if(samples == nil || av == nil || ![av isRecording])
121  return;
122 
123  [av appendAudio:samples presentationTime:timestamp blockIfNotReady:blockIfNotReady];
124 }
125 
127 {
128  VuoAvWriterObject* writerObject = (VuoAvWriterObject*)writer;
129 
130  if(writerObject)
131  {
132  if([writerObject isRecording])
133  {
134  [writerObject finalizeRecording];
135  }
136  }
137 }
138 
140 {
141  VuoAvWriterObject* writerObject = (VuoAvWriterObject*)writer;
142 
143  [writerObject release];
144 }