1# Interface (VideoOutput) 2<!--Kit: Camera Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @qano--> 5<!--SE: @leo_ysl--> 6<!--TSE: @xchaosioda--> 7 8> **NOTE** 9> 10> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 11 12VideoOutput implements output information used in a video session. It inherits from [CameraOutput](arkts-apis-camera-CameraOutput.md). 13 14## Modules to Import 15 16```ts 17import { camera } from '@kit.CameraKit'; 18``` 19 20## start 21 22start(callback: AsyncCallback\<void\>): void 23 24Starts video recording. This API uses an asynchronous callback to return the result. 25 26**Atomic service API**: This API can be used in atomic services since API version 19. 27 28**System capability**: SystemCapability.Multimedia.Camera.Core 29 30**Parameters** 31 32| Name | Type | Mandatory| Description | 33| -------- | -------------------- | ---- | -------------------- | 34| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. If the operation fails, an error code defined in [CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode) is returned.| 35 36**Error codes** 37 38For details about the error codes, see [Camera Error Codes](errorcode-camera.md). 39 40| ID | Error Message | 41| --------------- | --------------- | 42| 7400103 | Session not config. | 43| 7400201 | Camera service fatal error. | 44 45**Example** 46 47```ts 48import { BusinessError } from '@kit.BasicServicesKit'; 49 50function startVideoOutput(videoOutput: camera.VideoOutput): void { 51 videoOutput.start((err: BusinessError) => { 52 if (err) { 53 console.error(`Failed to start the video output, error code: ${err.code}.`); 54 return; 55 } 56 console.info('Callback invoked to indicate the video output start success.'); 57 }); 58} 59``` 60 61## start 62 63start(): Promise\<void\> 64 65Starts video recording. This API uses a promise to return the result. 66 67**Atomic service API**: This API can be used in atomic services since API version 19. 68 69**System capability**: SystemCapability.Multimedia.Camera.Core 70 71**Return value** 72 73| Type | Description | 74| -------------- | ----------------------- | 75| Promise\<void\> | Promise that returns no value.| 76 77**Error codes** 78 79For details about the error codes, see [Camera Error Codes](errorcode-camera.md). 80 81| ID | Error Message | 82| --------------- | --------------- | 83| 7400103 | Session not config. | 84| 7400201 | Camera service fatal error. | 85 86**Example** 87 88```ts 89import { BusinessError } from '@kit.BasicServicesKit'; 90 91function startVideoOutput(videoOutput: camera.VideoOutput): void { 92 videoOutput.start().then(() => { 93 console.info('Promise returned to indicate that start method execution success.'); 94 }).catch((error: BusinessError) => { 95 console.error(`Failed to video output start, error code: ${error.code}.`); 96 }); 97} 98``` 99 100## stop 101 102stop(callback: AsyncCallback\<void\>): void 103 104Stops video recording. This API uses an asynchronous callback to return the result. 105 106**Atomic service API**: This API can be used in atomic services since API version 19. 107 108**System capability**: SystemCapability.Multimedia.Camera.Core 109 110**Parameters** 111 112| Name | Type | Mandatory| Description | 113| -------- | -------------------- | ---- | ------------------------ | 114| callback | AsyncCallback\<void\> | Yes | Callback used to return the result.| 115 116**Example** 117 118```ts 119import { BusinessError } from '@kit.BasicServicesKit'; 120 121function stopVideoOutput(videoOutput: camera.VideoOutput): void { 122 videoOutput.stop((err: BusinessError) => { 123 if (err) { 124 console.error(`Failed to stop the video output, error code: ${err.code}.`); 125 return; 126 } 127 console.info('Callback invoked to indicate the video output stop success.'); 128 }); 129} 130``` 131 132## stop 133 134stop(): Promise\<void\> 135 136Stops video recording. This API uses a promise to return the result. 137 138**Atomic service API**: This API can be used in atomic services since API version 19. 139 140**System capability**: SystemCapability.Multimedia.Camera.Core 141 142**Return value** 143 144| Type | Description | 145| -------------- | ----------------------- | 146| Promise\<void\> | Promise that returns no value.| 147 148**Example** 149 150```ts 151import { BusinessError } from '@kit.BasicServicesKit'; 152 153function stopVideoOutput(videoOutput: camera.VideoOutput): void { 154 videoOutput.stop().then(() => { 155 console.info('Promise returned to indicate that stop method execution success.'); 156 }).catch((error: BusinessError) => { 157 console.error(`Failed to video output stop, error code: ${error.code}.`); 158 }); 159} 160``` 161 162## on('frameStart') 163 164on(type: 'frameStart', callback: AsyncCallback\<void\>): void 165 166Subscribes to video recording start events. This API uses an asynchronous callback to return the result. 167 168> **NOTE** 169> 170> Currently, you cannot use **off()** to unregister the callback in the callback method of **on()**. 171 172**Atomic service API**: This API can be used in atomic services since API version 19. 173 174**System capability**: SystemCapability.Multimedia.Camera.Core 175 176**Parameters** 177 178| Name | Type | Mandatory| Description | 179| -------- | -------------------- | ---- | ----------------------------------------- | 180| type | string | Yes | Event type. The value is fixed at **'frameStart'**. The event can be listened for when a videoOutput instance is created. The event is triggered and the corresponding information is returned when the bottom layer starts exposure for the first time.| 181| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. The recording starts as long as this event is returned. | 182 183**Example** 184 185```ts 186import { BusinessError } from '@kit.BasicServicesKit'; 187 188function callback(err: BusinessError): void { 189 if (err !== undefined && err.code !== 0) { 190 console.error(`Callback Error, errorCode: ${err.code}`); 191 return; 192 } 193 console.info('Video frame started'); 194} 195 196function registerVideoOutputFrameStart(videoOutput: camera.VideoOutput): void { 197 videoOutput.on('frameStart', callback); 198} 199``` 200 201## off('frameStart') 202 203off(type: 'frameStart', callback?: AsyncCallback\<void\>): void 204 205Unsubscribes from video recording start events. 206 207> **NOTE** 208> 209> Currently, you cannot use **off()** to unregister the callback in the callback method of **on()**. 210 211**Atomic service API**: This API can be used in atomic services since API version 19. 212 213**System capability**: SystemCapability.Multimedia.Camera.Core 214 215**Parameters** 216 217| Name | Type | Mandatory| Description | 218| -------- | -------------------- | ---- | ----------------------------------------- | 219| type | string | Yes | Event type. The value is fixed at **'frameStart'**. The event can be listened for when a videoOutput instance is created.| 220| callback | AsyncCallback\<void\> | No | Callback used to return the result. If this parameter is specified, the subscription to the specified event with the specified callback is canceled. (The callback object cannot be an anonymous function.) Otherwise, the subscriptions to the specified event with all the callbacks are canceled.| 221 222**Example** 223 224```ts 225function unregisterVideoOutputFrameStart(videoOutput: camera.VideoOutput): void { 226 videoOutput.off('frameStart'); 227} 228 229``` 230 231## on('frameEnd') 232 233on(type: 'frameEnd', callback: AsyncCallback\<void\>): void 234 235Subscribes to video recording stop events. This API uses an asynchronous callback to return the result. 236 237**Atomic service API**: This API can be used in atomic services since API version 19. 238 239**System capability**: SystemCapability.Multimedia.Camera.Core 240 241**Parameters** 242 243| Name | Type | Mandatory| Description | 244| -------- | -------------------- | ---- | ------------------------------------------ | 245| type | string | Yes | Event type. The value is fixed at **'frameEnd'**. The event can be listened for when a videoOutput instance is created. This event is triggered and returned when the last frame of recording is complete.| 246| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. The recording ends as long as this event is returned. | 247 248**Example** 249 250```ts 251import { BusinessError } from '@kit.BasicServicesKit'; 252 253function callback(err: BusinessError): void { 254 if (err !== undefined && err.code !== 0) { 255 console.error(`Callback Error, errorCode: ${err.code}`); 256 return; 257 } 258 console.info('Video frame ended'); 259} 260 261function registerVideoOutputFrameEnd(videoOutput: camera.VideoOutput): void { 262 videoOutput.on('frameEnd', callback); 263} 264``` 265 266## off('frameEnd') 267 268off(type: 'frameEnd', callback?: AsyncCallback\<void\>): void 269 270Unsubscribes from video recording stop events. 271 272**Atomic service API**: This API can be used in atomic services since API version 19. 273 274**System capability**: SystemCapability.Multimedia.Camera.Core 275 276**Parameters** 277 278| Name | Type | Mandatory| Description | 279| -------- | -------------------- | ---- | ------------------------------------------ | 280| type | string | Yes | Event type. The value is fixed at **'frameEnd'**. The event can be listened for when a videoOutput instance is created.| 281| callback | AsyncCallback\<void\> | No | Callback used to return the result. If this parameter is specified, the subscription to the specified event with the specified callback is canceled. (The callback object cannot be an anonymous function.) Otherwise, the subscriptions to the specified event with all the callbacks are canceled.| 282 283**Example** 284 285```ts 286function unregisterVideoOutputFrameEnd(videoOutput: camera.VideoOutput): void { 287 videoOutput.off('frameEnd'); 288} 289``` 290 291## on('error') 292 293on(type: 'error', callback: ErrorCallback): void 294 295Subscribes to VideoOutput error events. This API uses an asynchronous callback to return the result. 296 297> **NOTE** 298> 299> Currently, you cannot use **off()** to unregister the callback in the callback method of **on()**. 300 301**Atomic service API**: This API can be used in atomic services since API version 19. 302 303**System capability**: SystemCapability.Multimedia.Camera.Core 304 305**Parameters** 306 307| Name | Type | Mandatory| Description | 308| -------- | ----------- | ---- | -------------------------------------- | 309| type | string | Yes | Event type. The value is fixed at **'error'**. The event can be listened for when a videoOutput instance is created. This event is triggered and the corresponding error message is returned when an error occurs during the use of a recording-related API such as [start](#start-1) or [CameraOutput.release](arkts-apis-camera-CameraOutput.md#release-1).| 310| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | Yes | Callback used to return an error code defined in [CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode). | 311 312**Example** 313 314```ts 315import { BusinessError } from '@kit.BasicServicesKit'; 316 317function callback(err: BusinessError): void { 318 console.error(`Video output error code: ${err.code}`); 319} 320 321function registerVideoOutputError(videoOutput: camera.VideoOutput): void { 322 videoOutput.on('error', callback); 323} 324``` 325 326## off('error') 327 328off(type: 'error', callback?: ErrorCallback): void 329 330Unsubscribes from VideoOutput error events. 331 332**Atomic service API**: This API can be used in atomic services since API version 19. 333 334**System capability**: SystemCapability.Multimedia.Camera.Core 335 336**Parameters** 337 338| Name | Type | Mandatory| Description | 339| -------- | ------------- | ---- | ----------------------------------- | 340| type | string | Yes | Event type. The value is fixed at **'error'**. The event can be listened for when a photoOutput instance is created.| 341| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | No | Callback used to return the result. If this parameter is specified, the subscription to the specified event with the specified callback is canceled. (The callback object cannot be an anonymous function.) Otherwise, the subscriptions to the specified event with all the callbacks are canceled.| 342 343**Example** 344 345```ts 346function unregisterVideoOutputError(videoOutput: camera.VideoOutput): void { 347 videoOutput.off('error'); 348} 349``` 350 351## getSupportedFrameRates<sup>12+</sup> 352 353getSupportedFrameRates(): Array\<FrameRateRange\> 354 355Obtains the supported frame rates. 356 357**Atomic service API**: This API can be used in atomic services since API version 19. 358 359**System capability**: SystemCapability.Multimedia.Camera.Core 360 361**Return value** 362 363| Type | Description | 364| ------------- | ------------ | 365| Array<[FrameRateRange](arkts-apis-camera-i.md#frameraterange)> | Array of supported frame rates.| 366 367**Example** 368 369```ts 370function getSupportedFrameRates(videoOutput: camera.VideoOutput): Array<camera.FrameRateRange> { 371 let supportedFrameRatesArray: Array<camera.FrameRateRange> = videoOutput.getSupportedFrameRates(); 372 return supportedFrameRatesArray; 373} 374``` 375 376## setFrameRate<sup>12+</sup> 377 378setFrameRate(minFps: number, maxFps: number): void 379 380Sets a frame rate range for video streams. The range must be within the supported frame rate range, which can be obtained by calling [getSupportedFrameRates](#getsupportedframerates12). 381 382> **NOTE** 383> 384> This API is valid only in [PhotoSession](arkts-apis-camera-PhotoSession.md) or [VideoSession](arkts-apis-camera-VideoSession.md) mode. 385 386**Atomic service API**: This API can be used in atomic services since API version 19. 387 388**System capability**: SystemCapability.Multimedia.Camera.Core 389 390**Parameters** 391 392| Name | Type | Mandatory| Description | 393| -------- | --------------| ---- | ------------------------ | 394| minFps | number | Yes | Minimum frame rate.| 395| maxFps | number | Yes | Maximum frame rate. When the minimum value is greater than the maximum value, the API does not take effect.| 396 397**Error codes** 398 399For details about the error codes, see [Camera Error Codes](errorcode-camera.md). 400 401| ID | Error Message | 402| --------------- | --------------- | 403| 7400101 | Parameter missing or parameter type incorrect. | 404| 7400110 | Unresolved conflicts with current configurations. | 405 406**Example** 407 408```ts 409function setFrameRateRange(videoOutput: camera.VideoOutput, frameRateRange: Array<number>): void { 410 videoOutput.setFrameRate(frameRateRange[0], frameRateRange[1]); 411} 412``` 413 414## getActiveFrameRate<sup>12+</sup> 415 416getActiveFrameRate(): FrameRateRange 417 418Obtains the configured frame rate range. 419 420This API is valid only after [setFrameRate](#setframerate12) is called to set a frame rate range for video streams. 421 422**Atomic service API**: This API can be used in atomic services since API version 19. 423 424**System capability**: SystemCapability.Multimedia.Camera.Core 425 426**Return value** 427 428| Type | Description | 429| ------------- | ------------ | 430| [FrameRateRange](arkts-apis-camera-i.md#frameraterange) | Frame rate range.| 431 432**Example** 433 434```ts 435function getActiveFrameRate(videoOutput: camera.VideoOutput): camera.FrameRateRange { 436 let activeFrameRate: camera.FrameRateRange = videoOutput.getActiveFrameRate(); 437 return activeFrameRate; 438} 439``` 440 441## getActiveProfile<sup>12+</sup> 442 443getActiveProfile(): VideoProfile 444 445Obtains the profile that takes effect currently. 446 447**Atomic service API**: This API can be used in atomic services since API version 19. 448 449**System capability**: SystemCapability.Multimedia.Camera.Core 450 451**Return value** 452 453| Type | Description | 454| ------------- |-----------| 455| [VideoProfile](arkts-apis-camera-i.md#videoprofile) | Profile obtained.| 456 457**Error codes** 458 459For details about the error codes, see [Camera Error Codes](errorcode-camera.md). 460 461| ID | Error Message | 462|---------|------------------------------| 463| 7400201 | Camera service fatal error. | 464 465**Example** 466 467```ts 468import { BusinessError } from '@kit.BasicServicesKit'; 469 470function testGetActiveProfile(videoOutput: camera.VideoOutput): camera.Profile | undefined { 471 let activeProfile: camera.VideoProfile | undefined = undefined; 472 try { 473 activeProfile = videoOutput.getActiveProfile(); 474 } catch (error) { 475 // If the operation fails, error.code is returned and processed. 476 let err = error as BusinessError; 477 console.error(`The videoOutput.getActiveProfile call failed. error code: ${err.code}`); 478 } 479 return activeProfile; 480} 481``` 482 483## isMirrorSupported<sup>15+</sup> 484 485isMirrorSupported(): boolean 486 487Checks whether mirror recording is supported. 488 489**Atomic service API**: This API can be used in atomic services since API version 19. 490 491**System capability**: SystemCapability.Multimedia.Camera.Core 492 493**Return value** 494 495| Type | Description | 496| -------------- |---------------------------------| 497| boolean | Check result for the support of mirror recording. **true** if supported, **false** otherwise.| 498 499**Example** 500 501```ts 502function testIsMirrorSupported(videoOutput: camera.VideoOutput): boolean { 503 let isSupported: boolean = videoOutput.isMirrorSupported(); 504 return isSupported; 505} 506``` 507 508## enableMirror<sup>15+</sup> 509 510enableMirror(enabled: boolean): void 511 512Enables or disables mirror recording. 513 514- Before calling this API, check whether mirror recording is supported by using [isMirrorSupported](#ismirrorsupported15). 515 516- After enabling or disabling mirror recording, call [getVideoRotation](#getvideorotation12) and [updateRotation](../apis-media-kit/arkts-apis-media-AVRecorder.md#updaterotation12) to update the rotation angle. 517 518**Atomic service API**: This API can be used in atomic services since API version 19. 519 520**System capability**: SystemCapability.Multimedia.Camera.Core 521 522**Parameters** 523 524| Name | Type | Mandatory| Description | 525|----------| ---------------------- | ---- |---------------------------| 526| enabled | boolean | Yes | Whether to enable mirror recording. **true** to enable, **false** otherwise.| 527 528**Error codes** 529 530For details about the error codes, see [Camera Error Codes](errorcode-camera.md). 531 532| ID | Error Message | 533| -------- |------------------------------------------------| 534| 7400101 | Parameter missing or parameter type incorrect. | 535| 7400103 | Session not config. | 536 537 538**Example** 539 540```ts 541import { camera } from '@kit.CameraKit'; 542import { media } from '@kit.MediaKit'; 543import { BusinessError } from '@kit.BasicServicesKit'; 544 545function enableMirror(videoOutput: camera.VideoOutput, mirrorMode: boolean, aVRecorder: media.AVRecorder, deviceDegree : number): void { 546 try { 547 videoOutput.enableMirror(mirrorMode); 548 aVRecorder.updateRotation(videoOutput.getVideoRotation(deviceDegree)); 549 } catch (error) { 550 let err = error as BusinessError; 551 } 552} 553``` 554 555## getVideoRotation<sup>12+</sup> 556 557getVideoRotation(deviceDegree: number): ImageRotation 558 559Obtains the video rotation degree. 560 561- Device' natural orientation: The default orientation of the device (phone) is in portrait mode, with the charging port facing downward. 562- Camera lens angle: equivalent to the angle at which the camera is rotated clockwise to match the device's natural direction. The rear camera sensor of a phone is installed in landscape mode. Therefore, it needs to be rotated by 90 degrees clockwise to match the device's natural direction. 563- Screen orientation: The upper left corner of the image displayed on the screen is the first pixel, which is the coordinate origin. In the case of lock screen, the direction is the same as the device's natural orientation. 564 565**Atomic service API**: This API can be used in atomic services since API version 19. 566 567**System capability**: SystemCapability.Multimedia.Camera.Core 568 569**Parameters** 570 571| Name | Type | Mandatory| Description | 572| -------- | --------------| ---- | ------------------------ | 573| deviceDegree | number | Yes | Rotation angle, in degrees.| 574 575**Return value** 576 577| Type | Description | 578| ------------- |-----------| 579| [ImageRotation](arkts-apis-camera-e.md#imagerotation) | Video rotation degree.| 580 581**Error codes** 582 583For details about the error codes, see [Camera Error Codes](errorcode-camera.md). 584 585| ID | Error Message | 586|---------|------------------------------| 587| 7400101 | Parameter missing or parameter type incorrect. | 588| 7400201 | Camera service fatal error. | 589 590**Example** 591 592```ts 593import { camera } from '@kit.CameraKit'; 594import { Decimal } from '@kit.ArkTS'; 595import { sensor } from '@kit.SensorServiceKit'; 596import { BusinessError } from '@kit.BasicServicesKit'; 597 598function getVideoRotation(videoOutput: camera.VideoOutput): camera.ImageRotation { 599 let videoRotation: camera.ImageRotation = camera.ImageRotation.ROTATION_0; 600 try { 601 videoRotation = videoOutput.getVideoRotation(getDeviceDegree()); 602 } catch (error) { 603 let err = error as BusinessError; 604 } 605 return videoRotation; 606} 607 608// Obtain deviceDegree. 609function getDeviceDegree(): number { 610 let deviceDegree: number = -1; 611 try { 612 sensor.once(sensor.SensorId.GRAVITY, (data: sensor.GravityResponse) => { 613 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 614 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 615 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 616 let x = data.x; 617 let y = data.y; 618 let z = data.z; 619 if ((x * x + y * y) * 3 < z * z) { 620 deviceDegree = -1; 621 } else { 622 let sd: Decimal = Decimal.atan2(y, -x); 623 let sc: Decimal = Decimal.round(Number(sd) / 3.141592653589 * 180) 624 deviceDegree = 90 - Number(sc); 625 deviceDegree = deviceDegree >= 0 ? deviceDegree% 360 : deviceDegree% 360 + 360; 626 } 627 }); 628 } catch (error) { 629 let err: BusinessError = error as BusinessError; 630 } 631 return deviceDegree; 632} 633``` 634