/
QTDataRef.c
1385 lines (1092 loc) · 35.8 KB
/
QTDataRef.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
File: QTDataRef.c
Description: Document-specific code for thread-safety test application.
Author: QTEngineering, dts
Copyright: © Copyright 2003-2004 Apple Computer, Inc. All rights reserved.
Disclaimer: 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.
Change History (most recent first): <1> dts 11/16/03 initial release
*/
//////////
//
// header files
//
//////////
#include "QTDataRef.h"
//////////
//
// global variables
//
//////////
extern short gAppResFile; // file reference number for this application's resource file
extern ModalFilterUPP gModalFilterUPP; // UPP to our custom dialog event filter
Ptr gDataBuffer = NULL; // buffer that holds data being transferred
ComponentInstance gDataReader = NULL; // the data handler that reads data from the URL
ComponentInstance gDataWriter = NULL; // the data handler that writes data to a file
DataHCompletionUPP gReadDataHCompletionUPP = NULL;
DataHCompletionUPP gWriteDataHCompletionUPP = NULL;
long gBytesToTransfer = 0L; // the number of bytes to transfer
long gBytesTransferred = 0L; // the number of bytes already transferred
Boolean gDoneTransferring = false; // are we done transferring data?
#if TARGET_OS_WIN32
UINT gTimerID; // ID of the timer that tasks the data handlers
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Data reference creation utilities.
//
// Use these functions to create data references.
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//
// QTDR_MakeFileDataRef
// Return a file data reference for the specified file.
//
// The caller is responsible for disposing of the handle returned by this function (by calling DisposeHandle).
//
//////////
Handle QTDR_MakeFileDataRef (FSSpecPtr theFile)
{
Handle myDataRef = NULL;
QTNewAlias(theFile, (AliasHandle *)&myDataRef, true);
return(myDataRef);
}
//////////
//
// QTDR_MakeResourceDataRef
// Return a resource data reference for the specified file.
//
// The caller is responsible for disposing of the handle returned by this function (by calling DisposeHandle).
//
//////////
Handle QTDR_MakeResourceDataRef (FSSpecPtr theFile, OSType theResType, SInt16 theResID)
{
Handle myDataRef = NULL;
OSType myResType;
SInt16 myResID;
OSErr myErr = noErr;
myDataRef = QTDR_MakeFileDataRef(theFile);
if (myDataRef == NULL)
goto bail;
// append the resource type and ID to the data reference
myResType = EndianU32_NtoB(theResType);
myResID = EndianS16_NtoB(theResID);
myErr = PtrAndHand(&myResType, myDataRef, sizeof(myResType));
if (myErr == noErr)
myErr = PtrAndHand(&myResID, myDataRef, sizeof(myResID));
bail:
if (myErr != noErr) {
if (myDataRef != NULL)
DisposeHandle(myDataRef);
myDataRef = NULL;
}
return(myDataRef);
}
//////////
//
// QTDR_MakeHandleDataRef
// Return a handle data reference for the specified handle.
//
// The caller is responsible for disposing of the handle returned by this function (by calling DisposeHandle).
//
//////////
Handle QTDR_MakeHandleDataRef (Handle theHandle)
{
Handle myDataRef = NULL;
myDataRef = NewHandleClear(sizeof(Handle));
if (myDataRef != NULL)
BlockMove(&theHandle, *myDataRef, sizeof(Handle));
// the following single line can replace the preceding three lines
// PtrToHand(&theHandle, &myDataRef, sizeof(Handle));
return(myDataRef);
}
//////////
//
// QTDR_MakeURLDataRef
// Return a URL data reference for the specified URL.
//
// The caller is responsible for disposing of the handle returned by this function (by calling DisposeHandle).
//
//////////
Handle QTDR_MakeURLDataRef (char *theURL)
{
Handle myDataRef = NULL;
Size mySize = 0;
// get the size of the URL, plus the terminating null byte
mySize = (Size)strlen(theURL) + 1;
if (mySize == 1)
goto bail;
// allocate a new handle and copy the URL into the handle
myDataRef = NewHandleClear(mySize);
if (myDataRef != NULL)
BlockMove(theURL, *myDataRef, mySize);
bail:
return(myDataRef);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Movie-retrieval utilities.
//
// Use these functions to get movies specified using data references.
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//
// QTDR_GetMovieFromFile
// Get the movie stored in the specified file.
//
//////////
Movie QTDR_GetMovieFromFile (FSSpecPtr theFile)
{
Movie myMovie = NULL;
Handle myDataRef = NULL;
myDataRef = QTDR_MakeFileDataRef(theFile);
if (myDataRef != NULL) {
NewMovieFromDataRef(&myMovie, newMovieActive, NULL, myDataRef, rAliasType);
DisposeHandle(myDataRef);
}
return(myMovie);
}
//////////
//
// QTDR_GetMovieFromHandle
// Get the movie stored in the specified block of memory.
//
//////////
Movie QTDR_GetMovieFromHandle (Handle theHandle)
{
Movie myMovie = NULL;
Handle myDataRef = NULL;
myDataRef = QTDR_MakeHandleDataRef(theHandle);
if (myDataRef != NULL) {
NewMovieFromDataRef(&myMovie, newMovieActive, NULL, myDataRef, HandleDataHandlerSubType);
DisposeHandle(myDataRef);
}
return(myMovie);
}
//////////
//
// QTDR_GetMovieFromResource
// Get the movie stored in the specified resource.
//
//////////
Movie QTDR_GetMovieFromResource (FSSpecPtr theFile, OSType theResType, SInt16 theResID)
{
Movie myMovie = NULL;
Handle myDataRef = NULL;
myDataRef = QTDR_MakeResourceDataRef(theFile, theResType, theResID);
if (myDataRef != NULL) {
NewMovieFromDataRef(&myMovie, newMovieActive, NULL, myDataRef, ResourceDataHandlerSubType);
DisposeHandle(myDataRef);
}
return(myMovie);
}
//////////
//
// QTDR_GetMovieFromURL
// Get the movie in the file referenced by the specified uniform resource locator (URL).
//
//////////
Movie QTDR_GetMovieFromURL (char *theURL)
{
Movie myMovie = NULL;
Handle myDataRef = NULL;
myDataRef = QTDR_MakeURLDataRef(theURL);
if (myDataRef != NULL) {
NewMovieFromDataRef(&myMovie, newMovieActive, NULL, myDataRef, URLDataHandlerSubType);
DisposeHandle(myDataRef);
}
return(myMovie);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// URL utilities.
//
// Use these functions to elicit URLs from the user and get the basename of a URL.
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//
// QTDR_GetURLFromUser
// Display a dialog box to elicit a URL from the user; return a C string that contains the text in the
// dialog box when the user clicks the OK button; otherwise, return NULL.
//
// The caller is responsible for disposing of the pointer returned by this function (by calling free).
//
// Note that the URLs handled here are limited to 255 characters; supporting longer URLs is left as an
// exercise for the reader.
//
//////////
char *QTDR_GetURLFromUser (short thePromptStringIndex)
{
short myItem;
short mySavedResFile;
GrafPtr mySavedPort;
DialogPtr myDialog = NULL;
short myItemKind;
Handle myItemHandle;
Rect myItemRect;
Str255 myString;
char *myURL = NULL;
OSErr myErr = noErr;
//////////
//
// save the current resource file and graphics port
//
//////////
mySavedResFile = CurResFile();
GetPort(&mySavedPort);
// set the application's resource file
UseResFile(gAppResFile);
//////////
//
// create the dialog box in which the user will enter a URL
//
//////////
myDialog = GetNewDialog(kGetURL_DLOGID, NULL, (WindowPtr)-1L);
if (myDialog == NULL)
goto bail;
QTFrame_ActivateController(QTFrame_GetFrontMovieWindow(), false);
MacSetPort(GetDialogPort(myDialog));
SetDialogDefaultItem(myDialog, kGetURL_OKButton);
SetDialogCancelItem(myDialog, kGetURL_CancelButton);
// set the prompt string
GetIndString(myString, kTextKindsResourceID, thePromptStringIndex);
GetDialogItem(myDialog, kGetURL_URLLabelItem, &myItemKind, &myItemHandle, &myItemRect);
SetDialogItemText(myItemHandle, myString);
MacShowWindow(GetDialogWindow(myDialog));
//////////
//
// display and handle events in the dialog box until the user clicks OK or Cancel
//
//////////
do {
ModalDialog(gModalFilterUPP, &myItem);
} while ((myItem != kGetURL_OKButton) && (myItem != kGetURL_CancelButton));
//////////
//
// handle the selected button
//
//////////
if (myItem != kGetURL_OKButton) {
myErr = userCanceledErr;
goto bail;
}
// retrieve the edited text
GetDialogItem(myDialog, kGetURL_URLTextItem, &myItemKind, &myItemHandle, &myItemRect);
GetDialogItemText(myItemHandle, myString);
myURL = QTUtils_ConvertPascalToCString(myString);
bail:
// restore the previous resource file and graphics port
MacSetPort(mySavedPort);
UseResFile(mySavedResFile);
if (myDialog != NULL)
DisposeDialog(myDialog);
return(myURL);
}
//////////
//
// QTDR_GetURLBasename
// Return the basename of the specified URL.
//
// The basename of a URL is the portion of the URL following the rightmost URL separator. This function
// is useful for setting window titles of movies opened using the URL data handler to the basename of a
// URL (just like QuickTime Player does).
//
// The caller is responsible for disposing of the pointer returned by this function (by calling free).
//
//////////
char *QTDR_GetURLBasename (char *theURL)
{
char *myBasename = NULL;
short myLength = 0;
short myIndex;
// make sure we got a URL passed in
if (theURL == NULL)
goto bail;
// get the length of the URL
myLength = strlen(theURL);
// find the position of the rightmost URL separator in theURL
if (strchr(theURL, kURLSeparator) != NULL) {
myIndex = myLength - 1;
while (theURL[myIndex] != kURLSeparator)
myIndex--;
// calculate the length of the basename
myLength = myLength - myIndex - 1;
} else {
// there is no rightmost URL separator in theURL;
// set myIndex so that myIndex + 1 == 0, for the call to BlockMove below
myIndex = -1;
}
// allocate space to hold the string that we return to the caller
myBasename = malloc(myLength + 1);
if (myBasename == NULL)
goto bail;
// copy into myBasename the substring of theURL from myIndex + 1 to the end
BlockMove(&theURL[myIndex + 1], myBasename, myLength);
myBasename[myLength] = '\0';
bail:
return(myBasename);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Data reference extension functions.
//
// Use these functions to add extensions to data references.
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//
// QTDR_AddFilenamingExtension
// Add a filenaming extension to a data reference. If theStringPtr is NULL, add a 0-length filename.
//
// A filenaming extension is a Pascal string.
//
//////////
OSErr QTDR_AddFilenamingExtension (Handle theDataRef, StringPtr theFileName)
{
unsigned char myChar = 0;
OSErr myErr = noErr;
if (theFileName == NULL)
myErr = PtrAndHand(&myChar, theDataRef, sizeof(myChar));
else
myErr = PtrAndHand(theFileName, theDataRef, theFileName[0] + 1);
return(myErr);
}
//////////
//
// QTDR_AddMacOSFileTypeDataRefExtension
// Add a Macintosh file type as a data reference extension.
//
// A Macintosh file type data extension is an atom whose data is a 4-byte OSType.
//
//////////
OSErr QTDR_AddMacOSFileTypeDataRefExtension (Handle theDataRef, OSType theType)
{
unsigned long myAtomHeader[2];
OSType myType;
OSErr myErr = noErr;
myAtomHeader[0] = EndianU32_NtoB(sizeof(myAtomHeader) + sizeof(theType));
myAtomHeader[1] = EndianU32_NtoB(kDataRefExtensionMacOSFileType);
myType = EndianU32_NtoB(theType);
myErr = PtrAndHand(myAtomHeader, theDataRef, sizeof(myAtomHeader));
if (myErr == noErr)
myErr = PtrAndHand(&myType, theDataRef, sizeof(myType));
return(myErr);
}
//////////
//
// QTDR_AddMIMETypeDataRefExtension
// Add a MIME type as a data reference extension.
//
// A MIME type data extension is an atom whose data is a Pascal string.
//
//////////
OSErr QTDR_AddMIMETypeDataRefExtension (Handle theDataRef, StringPtr theMIMEType)
{
unsigned long myAtomHeader[2];
OSErr myErr = noErr;
if (theMIMEType == NULL)
return(paramErr);
myAtomHeader[0] = EndianU32_NtoB(sizeof(myAtomHeader) + theMIMEType[0] + 1);
myAtomHeader[1] = EndianU32_NtoB(kDataRefExtensionMIMEType);
myErr = PtrAndHand(myAtomHeader, theDataRef, sizeof(myAtomHeader));
if (myErr == noErr)
myErr = PtrAndHand(theMIMEType, theDataRef, theMIMEType[0] + 1);
return(myErr);
}
//////////
//
// QTDR_AddInitDataDataRefExtension
// Add some initialization data as a data reference extension.
//
// An initialization data data extension is an atom whose data is any block of data.
//
//////////
OSErr QTDR_AddInitDataDataRefExtension (Handle theDataRef, Ptr theInitDataPtr)
{
unsigned long myAtomHeader[2];
OSErr myErr = noErr;
if (theInitDataPtr == NULL)
return(paramErr);
myAtomHeader[0] = EndianU32_NtoB(sizeof(myAtomHeader) + GetPtrSize(theInitDataPtr));
myAtomHeader[1] = EndianU32_NtoB(kDataRefExtensionInitializationData);
myErr = PtrAndHand(myAtomHeader, theDataRef, sizeof(myAtomHeader));
if (myErr == noErr)
myErr = PtrAndHand(theInitDataPtr, theDataRef, GetPtrSize(theInitDataPtr));
return(myErr);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// File-transfer functions.
//
// These functions implement our file-transfer capability.
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//
// QTDR_CopyRemoteFileToLocalFile
// Copy a remote file (located at the specified URL) into a local file.
//
//////////
OSErr QTDR_CopyRemoteFileToLocalFile (char *theURL, FSSpecPtr theFile)
{
Handle myReaderRef = NULL; // data reference for the remote file
Handle myWriterRef = NULL; // data reference for the local file
ComponentResult myErr = badComponentType;
//////////
//
// create the local file with the desired type and creator
//
//////////
// delete the target local file, if it already exists;
// if it doesn't exist yet, we'll get an error (fnfErr), which we just ignore
FSpDelete(theFile);
myErr = FSpCreate(theFile, kTransFileCreator, kTransFileType, smSystemScript);
if (myErr != noErr)
goto bail;
//////////
//
// create data references for the remote file and the local file
//
//////////
myReaderRef = QTDR_MakeURLDataRef(theURL);
if (myReaderRef == NULL)
goto bail;
myWriterRef = QTDR_MakeFileDataRef(theFile);
if (myWriterRef == NULL)
goto bail;
//////////
//
// find and open the URL and file data handlers; connect the data references to them
//
//////////
gDataReader = OpenComponent(GetDataHandler(myReaderRef, URLDataHandlerSubType, kDataHCanRead));
if (gDataReader == NULL)
goto bail;
gDataWriter = OpenComponent(GetDataHandler(myWriterRef, rAliasType, kDataHCanWrite));
if (gDataWriter == NULL)
goto bail;
// set the data reference for the URL data handler
myErr = DataHSetDataRef(gDataReader, myReaderRef);
if (myErr != noErr)
goto bail;
// set the data reference for the file data handler
myErr = DataHSetDataRef(gDataWriter, myWriterRef);
if (myErr != noErr)
goto bail;
//////////
//
// allocate a data buffer; the URL data handler copies data into this buffer,
// and the file data handler copies data out of it
//
//////////
gDataBuffer = NewPtrClear(kDataBufferSize);
myErr = MemError();
if (myErr != noErr)
goto bail;
//////////
//
// connect to the remote and local files
//
//////////
// open a read-only path to the remote data reference
myErr = DataHOpenForRead(gDataReader);
if (myErr != noErr)
goto bail;
// get the size of the remote file
myErr = DataHGetFileSize(gDataReader, &gBytesToTransfer);
if (myErr != noErr)
goto bail;
// open a write-only path to the local data reference
myErr = DataHOpenForWrite(gDataWriter);
if (myErr != noErr)
goto bail;
//////////
//
// start reading and writing data
//
//////////
gDoneTransferring = false;
gBytesTransferred = 0L;
gReadDataHCompletionUPP = NewDataHCompletionUPP(QTDR_ReadDataCompletionProc);
gWriteDataHCompletionUPP = NewDataHCompletionUPP(QTDR_WriteDataCompletionProc);
// start retrieving the data; we do this by calling our own write completion routine,
// pretending that we've just successfully finished writing 0 bytes of data
QTDR_WriteDataCompletionProc(gDataBuffer, 0L, noErr);
bail:
// if we encountered any error, close the data handler components
if (myErr != noErr)
QTDR_CloseDownHandlers();
return((OSErr)myErr);
}
//////////
//
// QTDR_ReadDataCompletionProc
// This procedure is called when the data handler has completed a read operation.
//
// The theRefCon parameter contains the number of bytes just read.
//
//////////
PASCAL_RTN void QTDR_ReadDataCompletionProc (Ptr theRequest, long theRefCon, OSErr theErr)
{
#pragma unused(theErr)
// we just finished reading some data, so schedule a write operation
DataHWrite( gDataWriter,
theRequest, // the data buffer
gBytesTransferred, // write from the current offset
theRefCon, // the number of bytes to write
gWriteDataHCompletionUPP,
theRefCon);
}
//////////
//
// QTDR_WriteDataCompletionProc
// This procedure is called when the data handler has completed a write operation.
//
// The theRefCon parameter contains the number of bytes just written.
//
//////////
PASCAL_RTN void QTDR_WriteDataCompletionProc (Ptr theRequest, long theRefCon, OSErr theErr)
{
#pragma unused(theErr)
long myNumBytesToRead;
wide myWide;
// increment our tally of the number of bytes written so far
gBytesTransferred += theRefCon;
if (gBytesTransferred < gBytesToTransfer) {
// there is still data to read and write, so schedule a read operation
// determine how big a chunk to read
if (gBytesToTransfer - gBytesTransferred > kDataBufferSize)
myNumBytesToRead = kDataBufferSize;
else
myNumBytesToRead = gBytesToTransfer - gBytesTransferred;
myWide.lo = gBytesTransferred; // read from the current offset
myWide.hi = 0;
// schedule a read operation
DataHReadAsync(gDataReader,
theRequest, // the data buffer
myNumBytesToRead,
&myWide,
gReadDataHCompletionUPP,
myNumBytesToRead);
} else {
// we've transferred all the data, so set a flag to tell us to close down the data handlers
gDoneTransferring = true;
}
}
//////////
//
// QTDR_CloseDownHandlers
// Close our read/write access to our data references and then close down the read/write data handlers.
//
//////////
void QTDR_CloseDownHandlers (void)
{
if (gDataReader != NULL) {
DataHCloseForRead(gDataReader);
CloseComponent(gDataReader);
gDataReader = NULL;
}
if (gDataWriter != NULL) {
DataHCloseForWrite(gDataWriter);
CloseComponent(gDataWriter);
gDataWriter = NULL;
}
// dispose of the data buffer
if (gDataBuffer != NULL)
DisposePtr(gDataBuffer);
// dispose of the routine descriptors
if (gReadDataHCompletionUPP != NULL)
DisposeDataHCompletionUPP(gReadDataHCompletionUPP);
if (gWriteDataHCompletionUPP != NULL)
DisposeDataHCompletionUPP(gWriteDataHCompletionUPP);
gDoneTransferring = false;
#if TARGET_OS_WIN32
// kill the timer that tasks the data handlers
KillTimer(NULL, gTimerID);
#endif
}
//////////
//
// QTDR_TimerProc
// Handle timer messages to task the data handlers.
//
//////////
#if TARGET_OS_WIN32
void CALLBACK QTDR_TimerProc (HWND theWnd, UINT theMessage, UINT theID, DWORD theTime)
{
#pragma unused(theWnd, theMessage, theID, theTime)
QTApp_HandleEvent(NULL);
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Other functions.
//
// These functions are called by ComApplication.c.
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//
// QTDR_CreateReferenceCopy
// Create a copy of the given file that is a reference movie (that is, a movie file and a separate
// media file).
//
//////////
OSErr QTDR_CreateReferenceCopy (Movie theSrcMovie, FSSpecPtr theDstMovieFile, FSSpecPtr theDstMediaFile)
{
Track mySrcTrack = NULL;
Media mySrcMedia = NULL;
Movie myDstMovie = NULL;
Track myDstTrack = NULL;
Media myDstMedia = NULL;
Handle myMediaRef = NULL; // data reference for the media file
#if !USE_ADDEMPTYTRACKTOMOVIE
Fixed myWidth, myHeight;
OSType myType;
#endif
long myFlags = createMovieFileDeleteCurFile | createMovieFileDontCreateResFile;
short myResRefNum = 0;
short myResID = movieInDataForkResID;
OSErr myErr = paramErr;
// get the first video track and media in the source movie
mySrcTrack = GetMovieIndTrackType(theSrcMovie, 1, VideoMediaType, movieTrackMediaType);
if (mySrcTrack == NULL)
goto bail;
mySrcMedia = GetTrackMedia(mySrcTrack);
if (mySrcMedia == NULL)
goto bail;
// create a file data reference for the new media file
myMediaRef = QTDR_MakeFileDataRef(theDstMediaFile);
if (myMediaRef == NULL)
goto bail;
// create a file for the destination movie data
myErr = FSpCreate(theDstMediaFile, sigMoviePlayer, MovieFileType, 0);
if (myErr != noErr)
goto bail;
// create a file for the destination movie atom and create an empty movie
myErr = CreateMovieFile(theDstMovieFile, sigMoviePlayer, smCurrentScript, myFlags, &myResRefNum, &myDstMovie);
if (myErr != noErr)
goto bail;
// assign the default progress proc to the destination movie
SetMovieProgressProc(myDstMovie, (MovieProgressUPP)-1, 0);
#if USE_ADDEMPTYTRACKTOMOVIE
myErr = AddEmptyTrackToMovie(mySrcTrack, myDstMovie, myMediaRef, rAliasType, &myDstTrack);
if (myErr != noErr)
goto bail;
myDstMedia = GetTrackMedia(myDstTrack);
myErr = GetMoviesError();
if (myErr != noErr)
goto bail;
#else
// get some information about the source track and media
GetTrackDimensions(mySrcTrack, &myWidth, &myHeight);
GetMediaHandlerDescription(mySrcMedia, &myType, 0, 0);
// create the destination movie track and media
myDstTrack = NewMovieTrack(myDstMovie, myWidth, myHeight, kNoVolume);
myErr = GetMoviesError();
if (myErr != noErr)
goto bail;
myDstMedia = NewTrackMedia(myDstTrack, myType, GetMediaTimeScale(mySrcMedia), myMediaRef, rAliasType);
myErr = GetMoviesError();
if (myErr != noErr)
goto bail;
CopyTrackSettings(mySrcTrack, myDstTrack);
#endif
// copy the entire source track into the destination track; this copies the track's media
// samples into the destination media file
myErr = BeginMediaEdits(myDstMedia);
if (myErr != noErr)
goto bail;
myErr = InsertTrackSegment(mySrcTrack, myDstTrack, 0, GetTrackDuration(mySrcTrack), 0);
if (myErr != noErr)
goto bail;
myErr = EndMediaEdits(myDstMedia);
if (myErr != noErr)
goto bail;
// add the movie atom to the data fork of the movie file
myErr = AddMovieResource(myDstMovie, myResRefNum, &myResID, NULL);
bail:
return(myErr);
}
//////////
//
// QTDR_PlayMovieFromRAM
// Play a movie.
//
//////////
OSErr QTDR_PlayMovieFromRAM (Movie theMovie)
{
WindowPtr myWindow = NULL;
Rect myBounds = {50, 50, 100, 100};
Rect myRect;
StringPtr myTitle = QTUtils_ConvertCToPascalString(kWindowTitle);
OSErr myErr = memFullErr;
myWindow = NewCWindow(NULL, &myBounds, myTitle, false, 0, (WindowPtr)-1, false, 0);
if (myWindow == NULL)
goto bail;
myErr = noErr;
MacSetPort((GrafPtr)GetWindowPort(myWindow));
GetMovieBox(theMovie, &myRect);
MacOffsetRect(&myRect, -myRect.left, -myRect.top);
SetMovieBox(theMovie, &myRect);
if (!EmptyRect(&myRect))
SizeWindow(myWindow, myRect.right, myRect.bottom, false);
else
SizeWindow(myWindow, 200, 0, false);
MacShowWindow(myWindow);
SetMovieGWorld(theMovie, GetWindowPort(myWindow), NULL);
GoToBeginningOfMovie(theMovie);
MoviesTask(theMovie, 0);
StartMovie(theMovie);
myErr = GetMoviesError();
if (myErr != noErr)
goto bail;
while (!IsMovieDone(theMovie))
MoviesTask(theMovie, 0);
bail:
free(myTitle);
if (theMovie != NULL)
DisposeMovie(theMovie);
if (myWindow != NULL)
DisposeWindow(myWindow);
return(myErr);
}