1 | /* $Id: VBoxIChatTheaterWrapper.m 69498 2017-10-28 15:07:25Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - iChat Theater cocoa wrapper.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifdef VBOX_WITH_ICHAT_THEATER
|
---|
19 |
|
---|
20 | /* System includes */
|
---|
21 | #import <Carbon/Carbon.h>
|
---|
22 | #import <Cocoa/Cocoa.h>
|
---|
23 | #import <InstantMessage/IMService.h>
|
---|
24 | #import <InstantMessage/IMAVManager.h>
|
---|
25 |
|
---|
26 | @interface AVHandler: NSObject
|
---|
27 | {
|
---|
28 | CGImageRef mImage;
|
---|
29 | }
|
---|
30 |
|
---|
31 | - (void) setImage: (CGImageRef)aImage;
|
---|
32 | - (void) getPixelBufferPixelFormat: (OSType *)pixelFormatOut;
|
---|
33 | - (bool) renderIntoPixelBuffer: (CVPixelBufferRef)buffer forTime: (CVTimeStamp *)timeStamp;
|
---|
34 |
|
---|
35 | - (void) AVManagerNotification: (NSNotification *)notification;
|
---|
36 | @end
|
---|
37 |
|
---|
38 | @implementation AVHandler
|
---|
39 | - (id) init
|
---|
40 | {
|
---|
41 | if (!(self = [super init])) return nil;
|
---|
42 | mImage = nil;
|
---|
43 |
|
---|
44 | /* Register for notifications of the av manager */
|
---|
45 | NSNotificationCenter *nCenter = [IMService notificationCenter];
|
---|
46 | [nCenter addObserver: self
|
---|
47 | selector: @selector (AVManagerNotification:)
|
---|
48 | name: IMAVManagerStateChangedNotification
|
---|
49 | object: nil];
|
---|
50 |
|
---|
51 | return self;
|
---|
52 | }
|
---|
53 |
|
---|
54 | - (void)dealloc
|
---|
55 | {
|
---|
56 | NSNotificationCenter *nCenter = [IMService notificationCenter];
|
---|
57 | [nCenter removeObserver:self];
|
---|
58 | IMAVManager *avManager = [IMAVManager sharedAVManager];
|
---|
59 | [avManager stop];
|
---|
60 | [avManager setVideoDataSource:nil];
|
---|
61 | [super dealloc];
|
---|
62 | }
|
---|
63 |
|
---|
64 | - (void) setImage: (CGImageRef)aImage
|
---|
65 | {
|
---|
66 | mImage = aImage;
|
---|
67 | }
|
---|
68 |
|
---|
69 | - (void) getPixelBufferPixelFormat: (OSType *)pixelFormatOut
|
---|
70 | {
|
---|
71 | /* Return 32 bit pixel format */
|
---|
72 | *pixelFormatOut = kCVPixelFormatType_32ARGB;
|
---|
73 | }
|
---|
74 |
|
---|
75 | - (bool) renderIntoPixelBuffer: (CVPixelBufferRef)buffer forTime: (CVTimeStamp *)timeStamp
|
---|
76 | {
|
---|
77 | if (mImage == nil)
|
---|
78 | return NO;
|
---|
79 |
|
---|
80 | // Lock the pixel buffer's base address so that we can draw into it.
|
---|
81 | CVReturn err;
|
---|
82 | if ((err = CVPixelBufferLockBaseAddress (buffer, 0)) != kCVReturnSuccess)
|
---|
83 | {
|
---|
84 | // Rarely is a lock refused. Return NO if this happens.
|
---|
85 | printf ("Warning: could not lock pixel buffer base address in %s - error %ld\n", __func__, (long)err);
|
---|
86 | return NO;
|
---|
87 | }
|
---|
88 | size_t width = CVPixelBufferGetWidth (buffer);
|
---|
89 | size_t height = CVPixelBufferGetHeight (buffer);
|
---|
90 | CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
---|
91 | CGContextRef context = CGBitmapContextCreate (CVPixelBufferGetBaseAddress (buffer),
|
---|
92 | width, height,
|
---|
93 | 8,
|
---|
94 | CVPixelBufferGetBytesPerRow (buffer),
|
---|
95 | colorSpace,
|
---|
96 | kCGImageAlphaPremultipliedFirst);
|
---|
97 | CGColorSpaceRelease (colorSpace);
|
---|
98 | /* Clear background */
|
---|
99 | CGContextClearRect (context, CGRectMake (0, 0, width, height));
|
---|
100 | /* Center image */
|
---|
101 | int scaledWidth;
|
---|
102 | int scaledHeight;
|
---|
103 | float aspect = (float)(CGImageGetWidth (mImage)) / CGImageGetHeight (mImage);
|
---|
104 | if (aspect > 1.0)
|
---|
105 | {
|
---|
106 | scaledWidth = width;
|
---|
107 | scaledHeight = height / aspect;
|
---|
108 | }
|
---|
109 | else
|
---|
110 | {
|
---|
111 | scaledWidth = width * aspect;
|
---|
112 | scaledHeight = height;
|
---|
113 | }
|
---|
114 | CGRect iconRect = CGRectMake ((width - scaledWidth) / 2.0,
|
---|
115 | (height - scaledHeight) / 2.0,
|
---|
116 | scaledWidth, scaledHeight);
|
---|
117 | /* Here we have to paint the context*/
|
---|
118 | CGContextDrawImage (context, iconRect, mImage);
|
---|
119 | /* Finished painting */
|
---|
120 | CGContextRelease (context);
|
---|
121 | CVPixelBufferUnlockBaseAddress (buffer, 0);
|
---|
122 |
|
---|
123 | return YES;
|
---|
124 | }
|
---|
125 |
|
---|
126 | #pragma mark -
|
---|
127 | #pragma mark Notifications
|
---|
128 | - (void) AVManagerNotification: (NSNotification *)notification
|
---|
129 | {
|
---|
130 | IMAVManager *avManager = [IMAVManager sharedAVManager];
|
---|
131 | printf ("state changed to: %d\n", [avManager state]);
|
---|
132 |
|
---|
133 | /* Get the new state */
|
---|
134 | IMAVManagerState state = [avManager state];
|
---|
135 |
|
---|
136 | if (state == IMAVRequested)
|
---|
137 | {
|
---|
138 | /* Set some properties on the av manager */
|
---|
139 | [avManager setVideoDataSource:self];
|
---|
140 | /* Possible values: IMVideoOptimizationDefault, IMVideoOptimizationStills, IMVideoOptimizationReplacement */
|
---|
141 | [avManager setVideoOptimizationOptions:IMVideoOptimizationDefault];
|
---|
142 | /** @todo Audio support */
|
---|
143 | [avManager setNumberOfAudioChannels:0];
|
---|
144 | /* Start the streaming of the video */
|
---|
145 | [avManager start];
|
---|
146 | }
|
---|
147 | else if (state == IMAVInactive)
|
---|
148 | {
|
---|
149 | /* Stop the content propagation */
|
---|
150 | [avManager stop];
|
---|
151 | [avManager setVideoDataSource:nil];
|
---|
152 | }
|
---|
153 | }
|
---|
154 | #pragma mark -
|
---|
155 | @end
|
---|
156 |
|
---|
157 | static AVHandler *sharedAVHandler = NULL;
|
---|
158 |
|
---|
159 | void initSharedAVManager ()
|
---|
160 | {
|
---|
161 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
---|
162 |
|
---|
163 | /* Some other debug tests */
|
---|
164 | /*
|
---|
165 | NSProcessInfo *pi = [NSProcessInfo processInfo];
|
---|
166 | [pi setProcessName:@"My new processName"];
|
---|
167 | OSErr err;
|
---|
168 | ProcessSerialNumber PSN;
|
---|
169 | err = GetCurrentProcess (&PSN);
|
---|
170 | err = CPSSetProcessName (&PSN,"Evil");
|
---|
171 | err = CPSEnableForegroundOperation (&PSN,0x03,0x3C,0x2C,0x1103);
|
---|
172 | err = CPSSetFrontProcess (&PSN);
|
---|
173 | */
|
---|
174 |
|
---|
175 | if (!sharedAVHandler)
|
---|
176 | sharedAVHandler = [[AVHandler alloc] init];
|
---|
177 |
|
---|
178 | [pool release];
|
---|
179 | }
|
---|
180 |
|
---|
181 | void setImageRef (CGImageRef aImage)
|
---|
182 | {
|
---|
183 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
---|
184 |
|
---|
185 | initSharedAVManager();
|
---|
186 | [sharedAVHandler setImage: aImage];
|
---|
187 |
|
---|
188 | [pool release];
|
---|
189 | }
|
---|
190 |
|
---|
191 | #endif /* VBOX_WITH_ICHAT_THEATER */
|
---|
192 |
|
---|