DecodeWorker.js 25.9 KB
Newer Older
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
/**
 * Created by wangweijie5 on 2016/12/5.
 */
(function (event) {
    const AUDIO_TYPE = 0;	// 音频
    const VIDEO_TYPE = 1;   // 视频
    const PRIVT_TYPE = 2;  // 私有帧

    const PLAYM4_AUDIO_FRAME = 100; // 音频帧
    const PLAYM4_VIDEO_FRAME = 101; // 视频帧

    const PLAYM4_OK = 1;
    const PLAYM4_DECODE_ERROR = 44 	// 解码失败
    const PLAYM4_NOT_KEYFRAME = 48; 	// 非关键帧
    const PLAYM4_NEED_MORE_DATA = 31;   // 需要更多数据才能解析
    const PLAYM4_SYS_NOT_SUPPORT = 16; 	// 不支持

    const PLAYM4_PARA_ENCODER_ERROR                     = 71;  //音频编码参数错误
    const PLAYM4_PRECONDITION_ENCODER_ERROR             = 72;  //不满足音频编码条件错误
    const PLAYM4_ENCODER_ERROR                          = 73;  //音频编码失败
    const PLAYM4_CREATE_ENCODER_ERROR                   = 74;  //创建音频编码器失败
    const PLAYM4_NOSUPPORT_ENCODER_ERROR                = 75;  //音频编码不支持
    const PLAYM4_ALLOC_MEMORY_ENCODER_ERROR	            = 76;  //音频编码相关内存申请失败
    const PLAYM4_BUF_OVER_ENCODER_ERROR                 = 77;  //音频编码相关buffer满
    const PLAYM4_NEED_MORE_DATA_ENCODER_ERROR           = 78;  //音频编码需要更多数据进行编码
    const PLAYM4_CALL_ORDER_ENCODER_ERROR               = 79;  //音频编码调用顺序错误

    const PLAYM4_ITYPE_DECODE_ERROR                     =100;   //定位后送进来的第一帧I帧解码失败
    const PLAYM4_FIRST_FRAME_NOT_ICURRENT               =101;   //定位后送进来的第一帧不是定位帧所在的I帧(Ni>Mp)


    importScripts('Decoder.js');
    Module.postRun.push(function () {
        postMessage({'function': "loaded"});
    });

    var iStreamMode = 0;  // 流模式

    var bOpenMode = false;
    var bOpenStream = false;
    
    var funGetFrameData = null;
    var funGetAudFrameData = null;
	
	var bWorkerPrintLog=false;//worker层log开关

    onmessage = function (event)
    {
        var eventData = event.data;
        var res = 0;
        switch (eventData.command)
        {
			case "printLog":
			    let downloadFlag=eventData.data;
			    if(downloadFlag===true)
                {
                    bWorkerPrintLog=true;
                    res = Module._SetPrintLogFlag(downloadFlag);
                }
			    else
                {
                    bWorkerPrintLog=false;
                    res = Module._SetPrintLogFlag(downloadFlag);
                }

				if (res !== PLAYM4_OK)
                {
					console.log("DecodeWorker.js: PlayerSDK print log failed,res"+res);
                    postMessage({'function': "printLog", 'errorCode': res});
                }
				break;
            case "SetPlayPosition":
                let nFrameNumOrTime=eventData.data;
                let enPosType=eventData.type;
                res = Module._SetPlayPosition(nFrameNumOrTime,enPosType);
                if (res !== PLAYM4_OK)
                {
                    postMessage({'function': "SetPlayPosition", 'errorCode': res});
                    return;
                }
                //有没有buffer需要清除

                break;
            case "SetStreamOpenMode":
                iStreamMode = eventData.data;
                res = Module._SetStreamOpenMode(iStreamMode);
                if (res !== PLAYM4_OK)
                {
                    postMessage({'function': "SetStreamOpenMode", 'errorCode': res});
                    return;
                }
                bOpenMode = true;
                break;

            case "OpenStream":
                // 接收到的数据
                var iHeadLen = eventData.dataSize;
                var pHead = Module._malloc(iHeadLen + 4);
                if (pHead === null)
                {
                    return;
                }
                var aHead = Module.HEAPU8.subarray(pHead, pHead + iHeadLen);
                aHead.set(eventData.data);
                res = Module._OpenStream(pHead, iHeadLen, eventData.bufPoolSize);
                postMessage({'function': "OpenStream", 'errorCode': res});
                if (res !== PLAYM4_OK)
                {
                    //释放内存
                    Module._free(pHead);
                    pHead = null;
                    return;
                }
                bOpenStream = true;

                // 加4字节长度信息
                var a32 = new Uint32Array([iHeadLen]);
                var a8 = new Uint8Array(a32.buffer);
                var tempBuf = new Uint8Array(iHeadLen + 4);
                tempBuf.set(a8, 0);
                tempBuf.set(eventData.data, 4);
                a32 = null;
                a8 = null;

                aHead = Module.HEAPU8.subarray(pHead, pHead + iHeadLen + 4);
                aHead.set(tempBuf);
                tempBuf = null;

                res = Module._InputData(pHead, iHeadLen + 4);
                if (res !== PLAYM4_OK)
                {
                    postMessage({'function': "InputData", 'errorCode': res});
                    Module._free(pHead);
                    pHead = null;
                    return;
                }

                // 释放内存
                Module._free(pHead);
                pHead = null;

                if (funGetFrameData === null) {
                    funGetFrameData = Module.cwrap('GetFrameData', 'number');
                }

                if (iStreamMode === 0) {
                    // Module._GetFrameData();
                    funGetFrameData();
                }
                break;

            case "InputData":
                // 接收到的数据
                var iLen = eventData.dataSize;
                if(bWorkerPrintLog)
                {
                    console.log("<<<Worker: DecodeWorker-InputData iLen:"+iLen);
                }
                
                if (iLen > 0)
                {
                    var pInputData = Module._malloc(iLen);
                    if (pInputData === null)
                    {
                        return;
                    }
                    var inputData = new Uint8Array(eventData.data);
                    // var aInputData = Module.HEAPU8.subarray(pInputData, pInputData + iLen);
                    // aInputData.set(inputData);
                    Module.writeArrayToMemory(inputData, pInputData);
                    inputData = null;
                    res = Module._InputData(pInputData, iLen);
                    //console.log("DecodeWorker-InputData-ret:%d", res);
					if(bWorkerPrintLog)
					{
						console.log("<<<Worker:InputData result:"+ +res);
					}
					
                    if (res !== PLAYM4_OK)
                    {
                        if (res === 98)
                        {
                            res = 1;
                        }
                        postMessage({'function': "InputData", 'errorCode': res});
                    }
                    Module._free(pInputData);
                    pInputData = null;
                }

                /////////////////////
                if (funGetFrameData === null)
                {
                    funGetFrameData = Module.cwrap('GetFrameData', 'number');
                }

                while (bOpenMode && bOpenStream)
                {
                    
                    var ret = getFrameData(funGetFrameData);
                    // 直到获取视频帧或数据不足为止
                    if (PLAYM4_VIDEO_FRAME === ret || PLAYM4_NEED_MORE_DATA === ret)
                    {
                        break;
                    }
                }
                break;

            case "SetSecretKey":
                var keyLen = eventData.nKeyLen;
                var pKeyData = Module._malloc(keyLen);
                if (pKeyData === null) {
                    return;
                }
                var nKeySize = eventData.data.length
                var bufData = stringToBytes (eventData.data);
                var aKeyData = Module.HEAPU8.subarray(pKeyData, pKeyData + keyLen);
                aKeyData.set(new Uint8Array(bufData));

                res = Module._SetSecretKey(eventData.nKeyType, pKeyData, keyLen, nKeySize);
                if (res !== PLAYM4_OK) {
                    postMessage({'function': "SetSecretKey", 'errorCode': res});
                    Module._free(pKeyData);
                    pKeyData = null;
                    return;
                }

                Module._free(pKeyData);
                pKeyData = null;
                break;

            case "GetBMP":
                var nBMPWidth = eventData.width;
                var nBMPHeight = eventData.height;
                var pYUVData = eventData.data;
                var nYUVSize = nBMPWidth * nBMPHeight * 3 / 2;
                var oBMPCropRect = eventData.rect;

                var pDataYUV = Module._malloc(nYUVSize);
                if (pDataYUV === null) {
                    return;
                }

                Module.writeArrayToMemory(new Uint8Array(pYUVData, 0, nYUVSize), pDataYUV);

                // 分配BMP空间
                var nBmpSize = nBMPWidth * nBMPHeight * 4 + 60;
                var pBmpData = Module._malloc(nBmpSize);
                var pBmpSize = Module._malloc(4);
                if (pBmpData === null || pBmpSize === null) {
                    Module._free(pDataYUV);
                    pDataYUV = null;

                    if (pBmpData != null) {
                        Module._free(pBmpData);
                        pBmpData = null;
                    }

                    if (pBmpSize != null) {
                        Module._free(pBmpSize);
                        pBmpSize = null;
                    }
                    return;
                }

                Module._memset(pBmpSize, nBmpSize, 4); // 防止bmp截图出现输入数据过大的错误码

                res = Module._GetBMP(pDataYUV, nYUVSize, pBmpData, pBmpSize,
                    oBMPCropRect.left, oBMPCropRect.top, oBMPCropRect.right, oBMPCropRect.bottom);
                if (res !== PLAYM4_OK) {
                    postMessage({'function': "GetBMP", 'errorCode': res});
                    Module._free(pDataYUV);
                    pDataYUV = null;
                    Module._free(pBmpData);
                    pBmpData = null;
                    Module._free(pBmpSize);
                    pBmpSize = null;
                    return;
                }

                // 获取BMP图片大小
                var nBmpDataSize = Module.getValue(pBmpSize, "i32");

                // 获取BMP图片数据
                var aBmpData = new Uint8Array(nBmpDataSize);
                aBmpData.set(Module.HEAPU8.subarray(pBmpData, pBmpData + nBmpDataSize));

                postMessage({'function': "GetBMP", 'data': aBmpData, 'errorCode': res}, [aBmpData.buffer]);
                aBmpData=null;
                if (pDataYUV != null) {
                    Module._free(pDataYUV);
                    pDataYUV = null;
                }
                if (pBmpData != null) {
                    Module._free(pBmpData);
                    pBmpData = null;
                }
                if (pBmpSize != null) {
                    Module._free(pBmpSize);
                    pBmpSize = null;
                }
                break;

            case "GetJPEG":
                var nJpegWidth = eventData.width;
                var nJpegHeight = eventData.height;
                var pYUVData1 = eventData.data;
                var nYUVSize1 = nJpegWidth * nJpegHeight * 3 / 2;
                var oJpegCropRect = eventData.rect;

                var pDataYUV1 = Module._malloc(nYUVSize1);
                if (pDataYUV1 === null) {
                    return;
                }

                Module.writeArrayToMemory(new Uint8Array(pYUVData1, 0, nYUVSize1), pDataYUV1);

                // 分配JPEG空间
                var pJpegData = Module._malloc(nYUVSize1);
                var pJpegSize = Module._malloc(4);
                if (pJpegData === null || pJpegSize === null) {
                    if (pJpegData != null) {
                        Module._free(pJpegData);
                        pJpegData = null;
                    }

                    if (pJpegSize != null) {
                        Module._free(pJpegSize);
                        pJpegSize = null;
                    }

                    if (pDataYUV1 != null) {
                        Module._free(pDataYUV1);
                        pDataYUV1 = null;
                    }
                    return;
                }

                Module.setValue(pJpegSize, nJpegWidth * nJpegHeight * 2, "i32");    // JPEG抓图,输入缓冲长度不小于当前帧YUV大小

                res = Module._GetJPEG(pDataYUV1, nYUVSize1, pJpegData, pJpegSize,
                    oJpegCropRect.left, oJpegCropRect.top, oJpegCropRect.right, oJpegCropRect.bottom);
                if (res !== PLAYM4_OK) {
                    postMessage({'function': "GetJPEG", 'errorCode': res});
                    if (pJpegData != null) {
                        Module._free(pJpegData);
                        pJpegData = null;
                    }

                    if (pJpegSize != null) {
                        Module._free(pJpegSize);
                        pJpegSize = null;
                    }

                    if (pDataYUV1 != null) {
                        Module._free(pDataYUV1);
                        pDataYUV1 = null;
                    }
                    return;
                }

                // 获取JPEG图片大小
                var nJpegSize = Module.getValue(pJpegSize, "i32");

                // 获取JPEG图片数据
                var aJpegData = new Uint8Array(nJpegSize);
                aJpegData.set(Module.HEAPU8.subarray(pJpegData, pJpegData + nJpegSize));

                postMessage({'function': "GetJPEG", 'data': aJpegData, 'errorCode': res}, [aJpegData.buffer]);

                nJpegSize = null;
                aJpegData = null;

                if (pDataYUV1 != null) {
                    Module._free(pDataYUV1);
                    pDataYUV1 = null;
                }
                if (pJpegData != null) {
                    Module._free(pJpegData);
                    pJpegData = null;
                }
                if (pJpegSize != null) {
                    Module._free(pJpegSize);
                    pJpegSize = null;
                }
                break;

            case "SetDecodeFrameType":
                var nFrameType = eventData.data;
                res = Module._SetDecodeFrameType(nFrameType);
                if (res !== PLAYM4_OK) {
                    postMessage({'function': "SetDecodeFrameType", 'errorCode': res});
                    return;
                }
                break;

            case "DisplayRegion":
                var nRegionNum = eventData.nRegionNum;
                var srcRect = eventData.srcRect;
                var hDestWnd = eventData.hDestWnd;
                var bEnable = eventData.bEnable;

                res = Module._SetDisplayRegion(nRegionNum, srcRect, hDestWnd, bEnable);
                if (res !== PLAYM4_OK) {
                    postMessage({'function': "DisplayRegion", 'errorCode': res});
                    return;
                }
                break;

            case "CloseStream":
                res = Module._CloseStream();
                if (res !== PLAYM4_OK) {
                    postMessage({'function': "CloseStream", 'errorCode': res});
                    return;
                }
                break;

            case "SetIFrameDecInterval":
                Module._SetIFrameDecInterval(eventData.data);
                break;
                /*******************************************worker音频编码相关接口实现**********************************************************/
            case "CreateAudEncode":
			
                res = Module._CreateAudEncode(eventData.encodertype);
                
                postMessage({'function':"CreateAudEncode",'errorCode':res});
                break;
            case "SetAudEncodeParam":
                
                res = Module._SetAudEncodeParam(eventData.samplerate, eventData.channel, eventData.bitrate, eventData.bitwidth);
			
                postMessage({'function':"SetAudEncodeParam",'errorCode':res});
                break;
            case "DestroyAudEncode":
               
                res = Module._DestroyAudEncode();
                
                postMessage({'function':"DestroyAudEncode",'errorCode':res});
                break;
            case "InputAudEncodeData":
			    if(bWorkerPrintLog)
				{
					console.log("<<<Worker: 20200113 DecodeWorker-InputAudEncodeData 1");
				}
                
                var iLen = eventData.dataSize;
                if(iLen > 0)
                {
                    
                    var pAudInputData = Module._malloc(iLen);
                    if (pAudInputData === null)
                    {
                        return;
                    }
                    var audinputData = new Uint8Array(eventData.data);
                    Module.writeArrayToMemory(audinputData, pAudInputData);
                    audinputData = null;
                    res = Module._InputAudEncodeData(pAudInputData, iLen);
					if(bWorkerPrintLog)
					{
						console.log("<<<Worker: 20200113 DecodeWorker-InputAudEncodeData 2 res:"+res);
					}
                    
                    if(res == PLAYM4_OK)  //接口返回成功,表明已编码好一帧音频数据
                    {
                        
                        if (funGetAudFrameData === null)
                        {
                            funGetAudFrameData = Module.cwrap('GetAudFrameData', 'number');
                        }
                        if(bWorkerPrintLog)
						{
							console.log("<<<Worker: 20200113 DecodeWorker-InputAudEncodeData 2-1 succ");
						}
                        //调用C++ GetAudFrameData
                        var ret = getAudFrameData(funGetAudFrameData);
                    }
                    Module._free(pAudInputData);
                    pAudInputData = null;
                }
                break;
            default:
                break;
        }
    };

    function getOSDTime(oFrameInfo) {
        var iYear = oFrameInfo.year;
        var iMonth = oFrameInfo.month;
        var iDay = oFrameInfo.day;
        var iHour = oFrameInfo.hour;
        var iMinute = oFrameInfo.minute;
        var iSecond = oFrameInfo.second;

        if (iMonth < 10) {
            iMonth = "0" + iMonth;
        }
        if (iDay < 10) {
            iDay = "0" + iDay;
        }
        if (iHour < 10) {
            iHour = "0" + iHour;
        }
        if (iMinute < 10) {
            iMinute = "0" + iMinute;
        }
        if (iSecond < 10) {
            iSecond = "0" + iSecond;
        }

        return iYear + "-" + iMonth + "-" + iDay + " " + iHour + ":" + iMinute + ":" + iSecond;
    }

    function getAudFrameData(fun)
    {
		if(bWorkerPrintLog)
		{
			console.log("<<<Worker: DecodeWorker-getAudFrameData 1");
		}
    
        var res = fun(); // 调用C++GetAudFrameData函数
        if(res === PLAYM4_OK)
        {
            var oFrameInfo = Module._GetAudFrameInfo();
            var iSize = oFrameInfo.frameSize;
            if (0 === iSize)
            {
                return null;
            }

            var pEncodeAud = Module._GetAudFrameBuffer();
            if(pEncodeAud == null)
            {
                return null;
            }
            var aEncodeAudData = new Uint8Array(iSize);
            aEncodeAudData.set(Module.HEAPU8.subarray(pEncodeAud, pEncodeAud + iSize));
			if(bWorkerPrintLog)
			{
				console.log("<<<Worker: DecodeWorker-getAudFrameData 2");
			}
            
            //获取到音频编码帧数据通过worker返回
            postMessage({'function':"GetAudEncodeData",'data':aEncodeAudData.buffer,'dataSize':iSize, 'errorCode': res});
        }
        else
        {
            postMessage({'function':"GetAudEncodeData",'data':null,'dataSize':-1, 'errorCode': res});
        }
        oFrameInfo=null;
        pEncodeAud=null;
        aEncodeAudData=null;
        return res;
    }

    // 获取帧数据
    function getFrameData(fun)
    {
    // function getFrameData() {
        // 获取帧数据
        // var res = Module._GetFrameData();
        var res = fun();
        if(bWorkerPrintLog)
        {
            console.log("<<<Worker: getFrameData Result:"+res);
        }

        if (res === PLAYM4_OK)
        {
            var oFrameInfo = Module._GetFrameInfo();
            switch (oFrameInfo.frameType)
            {
                case AUDIO_TYPE:
                    var iSize = oFrameInfo.frameSize;
                    if (0 === iSize)
                    {
                        return -1;
                    }
                    var pPCM = Module._GetFrameBuffer();
                    // var audioBuf = new ArrayBuffer(iSize);
                    var aPCMData = new Uint8Array(iSize);
                    aPCMData.set(Module.HEAPU8.subarray(pPCM, pPCM + iSize));
                    if(bWorkerPrintLog)
                    {
                        console.log("<<<Worker: audio media Info: nSise:"+ oFrameInfo.frameSize+",nSampleRate:"+oFrameInfo.samplesPerSec+',channel:'+oFrameInfo.channels+',bitsPerSample:'+oFrameInfo.bitsPerSample);
                    }
                    postMessage({
                        'function': "GetFrameData", 'type': "audioType", 'data': aPCMData.buffer,
                        'frameInfo': oFrameInfo, 'errorCode': res
                    }, [aPCMData.buffer]);

                    oFrameInfo = null;
                    pPCM = null;
                    aPCMData = null;
                    return PLAYM4_AUDIO_FRAME;

                case VIDEO_TYPE:
                    var szOSDTime = getOSDTime(oFrameInfo);

                    var iWidth = oFrameInfo.width;
                    var iHeight = oFrameInfo.height;

                    var iYUVSize = iWidth * iHeight * 3 / 2;
                    if (0 === iYUVSize)
                    {
                        return -1;
                    }

                    var pYUV = Module._GetFrameBuffer();

                    // 图像数据渲染后压回,若从主码流切到子码流,存在数组大小与图像大小不匹配现象
                    var aYUVData = new Uint8Array(iYUVSize);
                    aYUVData.set(Module.HEAPU8.subarray(pYUV, pYUV + iYUVSize));
                    if(bWorkerPrintLog)
                    {
                        console.log("<<<Worker: InputData-getFrameData Video: Width:"+ oFrameInfo.width+",Height:"+oFrameInfo.height+",timeStamp:"+oFrameInfo.timeStamp);
                    }

                    postMessage({
                      'function': "GetFrameData", 'type': "videoType", 'data': aYUVData.buffer,
                   'dataLen': aYUVData.length,'osd': szOSDTime, 'frameInfo': oFrameInfo, 'errorCode': res
                    }, [aYUVData.buffer]);

                    oFrameInfo = null;
                    pYUV = null;
                    aYUVData = null;
                    return PLAYM4_VIDEO_FRAME;

                case PRIVT_TYPE:
                    postMessage({
                        'function': "GetFrameData", 'type': "", 'data': null,
                        'dataLen': -1, 'osd': 0, 'frameInfo': null, 'errorCode': PLAYM4_SYS_NOT_SUPPORT
                    });
                    return PLAYM4_SYS_NOT_SUPPORT;

                default:
                    postMessage({
                        'function': "GetFrameData", 'type': "", 'data': null,
                        'dataLen': -1, 'osd': 0, 'frameInfo': null, 'errorCode': PLAYM4_SYS_NOT_SUPPORT
                    });
                    return PLAYM4_SYS_NOT_SUPPORT;
            }
        }
        else {
            //解码失败返回裸数据
            if(PLAYM4_DECODE_ERROR===res)
            {
                var rawInfo=Module._GetRawDataInfo();
                var pRawData = Module._GetRawDataBuffer();
                var aRawData = new Uint8Array(rawInfo.isize);
                aRawData.set(Module.HEAPU8.subarray(pRawData, pRawData + rawInfo.isize));
                postMessage({
                    'function': "GetRawData", 'type': "", 'data':aRawData.buffer,
                    'rawDataLen': rawInfo.isize, 'osd': 0, 'frameInfo': null, 'errorCode': res
                });
                rawInfo=null;
                pRawData=null;
                aRawData=null;
            }
            //定位返回的错误
            if(PLAYM4_FIRST_FRAME_NOT_ICURRENT===res|| PLAYM4_ITYPE_DECODE_ERROR===res)
            {
                postMessage({
                    'function': "GetFrameData", 'type': "", 'data': null,
                    'dataLen': -1, 'osd': 0, 'frameInfo': null, 'errorCode': res
                });
            }
            //需要更多数据
            if (PLAYM4_NEED_MORE_DATA === res || PLAYM4_SYS_NOT_SUPPORT === res || PLAYM4_NOT_KEYFRAME === res){
                postMessage({
                    'function': "GetFrameData", 'type': "", 'data': null,
                    'dataLen': -1, 'osd': 0, 'frameInfo': null, 'errorCode': res
                });
            }
            return res;
        }
    }

    // 开始计算时间
    function startTime() {
        return new Date().getTime();
    }

    // 结束计算时间
    function endTime() {
        return new Date().getTime();
    }

    // 字母字符串转byte数组
    function stringToBytes ( str ) {
        var ch, st, re = [];
        for (var i = 0; i < str.length; i++ ) {
            ch = str.charCodeAt(i);  // get char
            st = [];                 // set up "stack"
            do {
                st.push( ch & 0xFF );  // push byte to stack
                ch = ch >> 8;          // shift value down by 1 byte
            }
            while ( ch );
            // add stack contents to result
            // done because chars have "wrong" endianness
            re = re.concat( st.reverse() );
        }
        // return an array of bytes
        return re;
    }
})();