////////// // // File: MyQuickDrawView.m // // Contains: Implementation file for the MyQuickDrawView class // // Written by: Apple Developer Technical Support // // Copyright: © 2002 by Apple Computer, Inc., all rights reserved. // // Change History (most recent first): // // <1> 5/20/02 srk first file // ////////// /* IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in consideration of your agreement to the following terms, and your use, installation, modification or redistribution of this Apple software constitutes acceptance of these terms. If you do not agree with these terms, please do not use, install, modify or redistribute this Apple software. In consideration of your agreement to abide by the following terms, and subject to these terms, Apple grants you a personal, non-exclusive license, under AppleŐs copyrights in this original Apple software (the "Apple Software"), to use, reproduce, modify and redistribute the Apple Software, with or without modifications, in source and/or binary forms; provided that if you redistribute the Apple Software in its entirety and without modifications, you must retain this notice and the following text and disclaimers in all such redistributions of the Apple Software. Neither the name, trademarks, service marks or logos of Apple Computer, Inc. may be used to endorse or promote products derived from the Apple Software without specific prior written permission from Apple. Except as expressly stated in this notice, no other rights or licenses, express or implied, are granted by Apple herein, including but not limited to any patent rights that may be infringed by your derivative works or by other works in which the Apple Software may be incorporated. The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #import "MyQuickDrawView.h" #import #import #include "AppController.h" // for Notification names static MyQuickDrawView *myQDViewObject; // callback @interface MyQuickDrawView (PrivateAPI) - (ComponentResult)setupDecomp; - (ComponentResult)decompToWindow; - (void)doDecomp:(NSRect)rect; - (void)sgIdleTimer:(id)sender; - (void)_startIdleTimer; - (void)_stopIdleTimer; @end #define BailErr(x) {err = x; if(err != noErr) goto bail;} #define BailIfNull(x) {self->seqGrabber = x; if(self->seqGrabber == nil) goto bail;} pascal OSErr mySGDataProc(SGChannel c, Ptr p, long len, long *offset, long chRefCon, TimeValue time, short writeType, long refCon); void saveQDViewObjectForCallback(void *theObject); static pascal Boolean SeqGrabberModalFilterProc(DialogPtr theDialog, const EventRecord *theEvent, short *itemHit, long refCon); @implementation MyQuickDrawView - (id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; if(self) { saveQDViewObjectForCallback(self); } return self; } - (void)dealloc { [self endGrab]; [super dealloc]; } /* Accessors */ - (GWorldPtr)gworld { return self->gWorld; } - (ImageSequence)decomSeq { return self->decompSeq; } - (ImageSequence)drawSeq { return self->drawSeq; } - (SGChannel)videoChannel { return self->videoChannel; } - (Rect)boundsRect { return self->boundsRect; } ////////// // // setupDecomp // // Code to setup our decompresion sequences. We make // two, one to decompress to a gworld, and the other // to decompress to the window // ////////// -(ComponentResult)setupDecomp { ComponentResult err = noErr; Rect sourceRect = { 0, 0 }, bounds; MatrixRecord scaleMatrix; ImageDescriptionHandle imageDesc = (ImageDescriptionHandle)NewHandle(0); PixMapHandle hPixMap; /* Set up getting grabbed data into the GWorld */ // retrieve a channelŐs current sample description, the channel returns a sample description that is // appropriate to the type of data being captured err = SGGetChannelSampleDescription(self->videoChannel,(Handle)imageDesc); BailErr(err); /***** IMPORTANT NOTE ***** Previous versions of this sample code made an incorrect decompression request. Intending to draw the DV frame at quarter-size into a quarter-size offscreen GWorld, it made the call err = DecompressSequenceBegin(..., &rect, nil, ...); passing a quarter-size rectangle as the source rectangle. The correct interpretation of this request is to draw the top-left corner of the DV frame cropped at normal size. Unfortunately, a DV-specific bug in QuickTime 5 caused it to misinterpret this request and scale the frame to fit. This bug will be fixed in QuickTime 6. If your code behaves as intended because of the bug, you should fix your code to pass a matrix scaling the frame to fit the offscreen gworld: RectMatrix( & scaleMatrix, &dvFrameRect, &gworldBounds ); err = DecompressSequenceBegin(..., nil, &scaleMatrix, ...); This approach will work in all versions of QuickTime. **************************/ // make a scaling matrix for the sequence sourceRect.right = (**imageDesc).width; sourceRect.bottom = (**imageDesc).height; RectMatrix(&scaleMatrix, &sourceRect, &self->boundsRect); #if 0 NSLog(@"%s src:{%d, %d} - bounds:{%d, %d}", __PRETTY_FUNCTION__, sourceRect.right, sourceRect.bottom, self->boundsRect.right, self->boundsRect.bottom); #endif // begin the process of decompressing a sequence of frames // this is a set-up call and is only called once for the sequence - the ICM will interrogate different codecs // and construct a suitable decompression chain, as this is a time consuming process we don't want to do this // once per frame (eg. by using DecompressImage) // for more information see Ice Floe #8 http://developer.apple.com/quicktime/icefloe/dispatch008.html // the destination is specified as the GWorld err = DecompressSequenceBegin(&self->decompSeq, // pointer to field to receive unique ID for sequence imageDesc, // handle to image description structure self->gWorld, // port for the DESTINATION image NULL, // graphics device handle, if port is set, set to NULL NULL, // source rectangle defining the portion of the image to decompress &scaleMatrix, // transformation matrix srcCopy, // transfer mode specifier NULL, // clipping region in dest. coordinate system to use as a mask 0, // flags codecNormalQuality, // accuracy in decompression bestSpeedCodec); // compressor identifier or special identifiers ie. bestSpeedCodec BailErr(err); DisposeHandle((Handle)imageDesc); imageDesc = NULL; /* Set up getting grabbed data into the Window */ hPixMap = GetGWorldPixMap(self->gWorld); GetPixBounds(hPixMap,&bounds); self->drawSeq = 0; #if 0 NSLog(@"%s pixBounds:{%d, %d}", __PRETTY_FUNCTION__, bounds.right, bounds.bottom); #endif // returns an image description for the GWorlds PixMap // on entry the imageDesc is NULL, on return it is correctly filled out // you are responsible for disposing it err = MakeImageDescriptionForPixMap(hPixMap, &imageDesc); BailErr(err); self->imageSize = (GetPixRowBytes(hPixMap) * (*imageDesc)->height); // ((**hPixMap).rowBytes & 0x3fff) * (*desc)->height; // begin the process of decompressing a sequence of frames - see above notes on this call. // destination is specified as the QuickDraw port for our NSView err = DecompressSequenceBegin(&self->drawSeq, imageDesc, [self qdPort], // Use the QuickDraw port for our NSView as destination! NULL, &bounds, NULL, ditherCopy, NULL, 0, codecNormalQuality, anyCodec); BailErr(err); bail: if (imageDesc) DisposeHandle((Handle)imageDesc); return (err); } ////////// // // decompToWindow // // Decompress an image to our window (the QuickDraw port for // our NSView) // ////////// -(ComponentResult)decompToWindow { ComponentResult err = noErr; CodecFlags ignore; err = DecompressSequenceFrameS(self->drawSeq, // sequence ID returned by DecompressSequenceBegin GetPixBaseAddr(GetGWorldPixMap(self->gWorld)), // pointer to compressed image data self->imageSize, // size of the buffer 0, // in flags &ignore, // out flags NULL); // async completion proc return err; } ////////// // // doDecomp // // Setup and run our decompression sequence, plus display // frames-per-second data to our window // ////////// -(void)doDecomp:(NSRect)rect { if(self->gWorld) { if(self->decompSeq == 0) { [self setupDecomp]; } else { [self decompToWindow]; } } } ////////// // // sgIdleTimer // // A timer whose purpose is to call the SGIdle function // to provide processing time for our sequence grabber // component. // ////////// - (void)sgIdleTimer:(id)sender { OSErr err; err = SGIdle(self->seqGrabber); if (err != noErr) { // you don't always know where these errors originate from, some may come // from the VDig... // ...to fix this we simply call SGStop and SGStartRecord again // calling stop allows the SG to release and re-prepare for grabbing // hopefully fixing any problems, this is obviously a very relaxed // approach SGStop(self->seqGrabber); SGStartRecord(self->seqGrabber); // test if error pertains err = SGIdle(self->seqGrabber); if(err != noErr) { NSNotificationCenter *nc; /* error does pertain, post it */ nc = [NSNotificationCenter defaultCenter]; [nc postNotificationName:MSCameraDidFailNotification object:[NSNumber numberWithInt:err]]; } } } - (void)_startIdleTimer { /* setup a timer to idle the sequence grabber */ self->idleTimer = [[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(sgIdleTimer:) userInfo:nil repeats:YES] retain]; } - (void)_stopIdleTimer { [self->idleTimer invalidate]; [self->idleTimer release]; self->idleTimer = nil; } ////////// // // doSeqGrab // // Initialize the Sequence Grabber, create a new // sequence grabber channel, create an offscreen // GWorld for use with our decompression sequence, // then begin recording. We also setup a timer to // idle the sequence grabber // ////////// - (OSErr)doSeqGrab:(NSRect)grabRect { Rect channelBounds; OSErr err = noErr; /* ZNeK: need to find a better way to set the possible maximum */ channelBounds.top = 0; channelBounds.left = 0; channelBounds.right = 640; channelBounds.bottom = 480; /* initialize the movie toolbox */ err = EnterMovies(); BailErr(err); // open the sequence grabber component and initialize it self->seqGrabber = OpenDefaultComponent(SeqGrabComponentType, 0); BailIfNull(self->seqGrabber); err = SGInitialize(self->seqGrabber); BailErr(err); // specify the destination data reference for a record operation // tell it we're not making a movie // if the flag seqGrabDontMakeMovie is used, the sequence grabber still calls // your data function, but does not write any data to the movie file // writeType will always be set to seqGrabWriteAppend err = SGSetDataRef(self->seqGrabber, 0, 0, seqGrabDontMakeMovie); BailErr(err); // create a new sequence grabber video channel err = SGNewChannel(self->seqGrabber, VideoMediaType, &self->videoChannel); BailErr(err); self->boundsRect.top = grabRect.origin.y; self->boundsRect.left = grabRect.origin.x; self->boundsRect.bottom = grabRect.size.height; self->boundsRect.right = grabRect.size.width; // create the GWorld err = QTNewGWorld(&self->gWorld, // returned GWorld k32ARGBPixelFormat, // pixel format &channelBounds, // bounding rectangle 0, // color table NULL, // graphic device handle 0); // flags BailErr(err); // lock the pixmap and make sure it's locked because // we can't decompress into an unlocked PixMap if(!LockPixels(GetPortPixMap(self->gWorld))) { BailErr(-1); } err = SGSetGWorld(self->seqGrabber, self->gWorld, GetMainDevice()); BailErr(err); // set the bounds for the channel #if 0 err = SGSetChannelBounds(self->videoChannel, &self->boundsRect); BailErr(err); #else err = SGSetChannelBounds(self->videoChannel, &channelBounds); BailErr(err); #endif // set the usage for our new video channel to avoid playthrough // note: we do not set seqGrabPlayDuringRecord because if you set this flag // the data from the channel may be played during the record operation, // if the destination buffer is onscreen. However, playing the // data may affect the quality of the recorded sequence by causing frames // to be dropped...something we definitely want to avoid err = SGSetChannelUsage(self->videoChannel, seqGrabRecord); BailErr(err); // specify a data function for use by the sequence grabber // whenever any channel assigned to the sequence grabber writes data, // this data function is called and may then write the data to another destination err = SGSetDataProc(self->seqGrabber, NewSGDataUPP(&mySGDataProc), NULL); BailErr(err); /* lights...camera... */ err = SGPrepare(self->seqGrabber, false, true); BailErr(err); // start recording!! err = SGStartRecord(self->seqGrabber); BailErr(err); [self _startIdleTimer]; bail: return err; } ////////// // // endGrab // // Perform clean-up when we are finished recording // ////////// - (void)endGrab { ComponentResult result; OSErr err; // kill our sequence grabber idle timer first [self _stopIdleTimer]; // stop recording SGStop(self->seqGrabber); // end our decompression sequences err = CDSequenceEnd(self->decompSeq); err = CDSequenceEnd(self->drawSeq); // finally, close our sequence grabber component result = CloseComponent(self->seqGrabber); // get rid of our gworld DisposeGWorld(self->gWorld); self->seqGrabber = NULL; } /* Drawing */ -(void)drawRect:(NSRect)rect { [self doDecomp:rect]; } - (void)setFrameSize:(NSSize)_size { ComponentResult err; BOOL didGrab; WindowRef wRef; didGrab = (self->seqGrabber != NULL); if(didGrab) SGPause(self->seqGrabber, true); // adjust frameSize [super setFrameSize:_size]; // magic happens here self->boundsRect.bottom = _size.height; self->boundsRect.right = _size.width; if(didGrab) { err = SGSetChannelBounds(self->videoChannel, &self->boundsRect); [self setupDecomp]; SGPause(self->seqGrabber, false); } } /* Actions */ - (IBAction)adjustVideoSettings:(id)_sender { Rect newActiveVideoRect; Rect adjustedActiveVideoRect; Rect curBounds, curVideoRect, newVideoRect, newBounds; short width, height; ComponentResult err; GrafPtr savedPort; RgnHandle deadRgn; SGModalFilterUPP seqGragModalFilterUPP; Rect portRect; WindowRef monitor; // no interference, please [self _stopIdleTimer]; // Get our current state err = SGGetChannelBounds(self->videoChannel, &curBounds); err = SGGetVideoRect(self->videoChannel, &curVideoRect); // Pause err = SGPause(self->seqGrabber, true); // Do the dialog thang seqGragModalFilterUPP = (SGModalFilterUPP)NewSGModalFilterUPP(SeqGrabberModalFilterProc); monitor = (WindowRef)[[self window] windowRef]; err = SGSettingsDialog(self->seqGrabber, self->videoChannel, 0, NULL, 0L, seqGragModalFilterUPP, (long)monitor); DisposeSGModalFilterUPP(seqGragModalFilterUPP); // What happened? err = SGGetVideoRect(self->videoChannel, &newVideoRect); err = SGGetSrcVideoBounds(self->videoChannel, &newActiveVideoRect); // Set up our port GetPort(&savedPort); MacSetPort((GrafPtr)GetWindowPort(monitor)); #if 0 // Has our active rect changed? // If so, it's because our video standard changed (e.g., NTSC to PAL), // and we need to adjust our monitor window if(!MacEqualRect(&self->boundsRect, &newVideoRect)) { width = newVideoRect.right - newVideoRect.left; height = newVideoRect.bottom - newVideoRect.top; NSLog(@"width:%d height:%d", width, height); self->boundsRect = newVideoRect; SizeWindow(monitor, width, height, false); GetPortBounds(GetWindowPort(monitor), &portRect); err = SGSetChannelBounds(self->videoChannel, &portRect); } #endif // Has our crop changed? // This code shows how to be crop video panel friendly // Two important things - // 1) Be aware that you might have been cropped and adjust your // video window appropriately // 2) Be aware that you might have been adjusted and attempt to // account for this. Adjusting refers to using the digitizer // rect to "adjust" the active source rect within the maximum // source rect. This is useful if you're getting those nasty // black bands on the sides of your video display - you can use // the control-arrow key sequence to shift the active source // rect around when you're in the crop video panel #if 0 adjustedActiveVideoRect = gActiveVideoRect; if(!MacEqualRect(&curVideoRect, &newVideoRect)) { GetPortBounds(GetWindowPort(gMonitor), &portRect); if((newVideoRect.left < gActiveVideoRect.left) || (newVideoRect.right > gActiveVideoRect.right) || (newVideoRect.top < gActiveVideoRect.top) || (newVideoRect.bottom > gActiveVideoRect.bottom)) { if(newVideoRect.left < gActiveVideoRect.left) { adjustedActiveVideoRect.left = newVideoRect.left; adjustedActiveVideoRect.right -= (gActiveVideoRect.left - newVideoRect.left); } if(newVideoRect.right > gActiveVideoRect.right) { adjustedActiveVideoRect.right = newVideoRect.right; adjustedActiveVideoRect.left += (newVideoRect.right - gActiveVideoRect.right); } if(newVideoRect.top < gActiveVideoRect.top) { adjustedActiveVideoRect.top = newVideoRect.top; adjustedActiveVideoRect.bottom -= (gActiveVideoRect.top - newVideoRect.top); } if(newVideoRect.bottom > gActiveVideoRect.bottom) { adjustedActiveVideoRect.bottom = newVideoRect.bottom; adjustedActiveVideoRect.top += (newVideoRect.bottom - gActiveVideoRect.bottom); } newBounds = newVideoRect; MapRect(&newBounds, &adjustedActiveVideoRect, &portRect); } else // Can't tell if we've been adjusted (digitizer rect is smaller on all sides // than the active source rect) { newBounds = newVideoRect; MapRect (&newBounds, &gActiveVideoRect, &portRect); } width = newBounds.right - newBounds.left; height = newBounds.bottom - newBounds.top; err = SGSetChannelBounds (self->videoChannel, &newBounds); } #endif #if 0 // Clean out the part of the port that isn't being drawn in deadRgn = NewRgn(); if(deadRgn != NULL) { Rect boundsRect; GetPortBounds(GetWindowPort(monitor), &portRect); err = SGGetChannelBounds(self->videoChannel, &boundsRect); err = XorRectToRgn(&boundsRect, &portRect, &deadRgn); EraseRgn(deadRgn); DisposeRgn(deadRgn); } #endif MacSetPort(savedPort); #if !TARGET_OS_MAC // This is necessary, for now, to get the grab to start again afer the // dialog goes away. For some reason the video destRect never gets reset to point // back to the monitor window. SGSetChannelBounds(self->videoChannel, &(monitor->portRect)); #endif // restart grabbing timer [self _startIdleTimer]; // The pause that refreshes err = SGPause(self->seqGrabber, false); } @end /* ---------------------------------------------------------------------- */ /* sequence grabber data procedure - this is where the work is done /* ---------------------------------------------------------------------- */ /* mySGDataProc - the sequence grabber calls the data function whenever any of the grabberŐs channels write digitized data to the destination movie file. NOTE: We really mean any, if you have an audio and video channel then the DataProc will be called for either channel whenever data has been captured. Be sure to check which channel is being passed in. In this example we never create an audio channel so we know we're always dealing with video. This data function decompresses captured video data into an offscreen GWorld, then transfers the frame to an onscreen window. For more information refer to Inside Macintosh: QuickTime Components, page 5-120 c - the channel component that is writing the digitized data. p - a pointer to the digitized data. len - the number of bytes of digitized data. offset - a pointer to a field that may specify where you are to write the digitized data, and that is to receive a value indicating where you wrote the data. chRefCon - per channel reference constant specified using SGSetChannelRefCon. time - the starting time of the data, in the channelŐs time scale. writeType - the type of write operation being performed. seqGrabWriteAppend - Append new data. seqGrabWriteReserve - Do not write data. Instead, reserve space for the amount of data specified in the len parameter. seqGrabWriteFill - Write data into the location specified by offset. Used to fill the space previously reserved with seqGrabWriteReserve. The Sequence Grabber may call the DataProc several times to fill a single reserved location. refCon - the reference constant you specified when you assigned your data function to the sequence grabber. */ pascal OSErr mySGDataProc(SGChannel c, Ptr p, long len, long *offset, long chRefCon, TimeValue time, short writeType, long refCon) { #pragma unused(offset,chRefCon,time,writeType) CodecFlags ignore; ComponentResult err = noErr; CGrafPtr theSavedPort; GDHandle theSavedDevice; char status[64]; Str255 theString; Rect bounds; if([myQDViewObject gworld]) { // decompress a frame into the GWorld - can queue a frame for async decompression when passed in a completion proc // once the image is in the GWorld it can be manipulated at will err = DecompressSequenceFrameS([myQDViewObject decomSeq], // sequence ID returned by DecompressSequenceBegin p, // pointer to compressed image data len, // size of the buffer 0, // in flags &ignore, // out flags NULL); // async completion proc BailErr(err); // ****** IMAGE IS NOW IN THE GWORLD ****** // } /* calling the display method will invoke this NSView's lockFocus, drawRect and unlockFocus methods as necessary. Our drawRect method (above) is used to decompress one of a sequence of frames. This method draws the image back to the window from the GWorld and could be used as a "preview" */ [myQDViewObject display]; bail: return err; } ////////// // // saveQDViewObjectForCallback // // This routine stores a reference to our MyQuickDrawView object. We'll // need this so we can call into methods in this class from outside the // implementation of the class methods (specifically, from our SGDataProc // C routine above). // ////////// void saveQDViewObjectForCallback(void *theObject) { myQDViewObject = (MyQuickDrawView *)theObject; } static pascal Boolean SeqGrabberModalFilterProc(DialogPtr theDialog, const EventRecord *theEvent, short *itemHit, long refCon) { #pragma unused(theDialog, itemHit) // Ordinarily, if we had multiple windows we cared about, we'd handle // updating them in here, but since we don't, we'll just clear out // any update events meant for us Boolean handled = false; if ((theEvent->what == updateEvt) && ((WindowPtr) theEvent->message == (WindowPtr) refCon)) { BeginUpdate ((WindowPtr) refCon); EndUpdate ((WindowPtr) refCon); handled = true; } return (handled); }