mdi_bitstream.c 41.4 KB
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES, 
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE, 
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE, 
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*******************************************************************************
 * Filename:
 * ---------
 *  mdi_bitstream.c
 *
 * Project:
 * --------
 *  MAUI
 *
 * Description:
 * ------------
 *  Interface for bitstreaming playback audio and video buffer
 *
 * Author:
 * -------
 * -------
 * -------
 *
 *==============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!! 
 *------------------------------------------------------------------------------
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 * removed!
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *============================================================================
 *******************************************************************************/

#include "MMI_features.h"
#if defined(__BITSTREAM_API_SUPPORT__)

#include "mmi_frm_gprot.h"

#include "kal_public_api.h"

#include "l1audio.h"
#include "med_struct.h"
#include "med_main.h"
#include "med_global.h"
#include "bitstream_api.h"
#include "aud_main.h"
#include "gdi_datatype.h"
#include "gdi_include.h"
#include "mdi_datatype.h"   /* mdi datatype */
#include "mdi_bitstream.h"
#include "mdi_include.h"
#include "mdi_audio.h"
#include "mdi_video.h"
#include "mdi_bitstream.h"
#include "UcmSrvGprot.h"
#ifdef __MMI_TVOUT__
#include "mdi_tv.h"
#endif 
#if defined(__MMI_BT_AUDIO_VIA_SCO__)
//#include "ProfileGprots.h"
//#include "BTMMISCOPathGprots.h"
#include "BthScoPathSrvGProt.h"
#endif

#ifdef __MTK_TARGET__
#define MDI_BITSTREAM_DRIVER_AVAILABLE
#endif

/***************************************************/
/* defines                                         */
/***************************************************/


/***************************************************/
/* structures                                      */
/***************************************************/

typedef struct
{
    kal_uint8 open_num;
} mdi_bitstream_audio_context_struct;

typedef struct
{
    mdi_bitstream_audio_context_struct audio;
} mdi_bitstream_context_struct;


/***************************************************/
/* globals                                         */
/***************************************************/
mdi_bitstream_context_struct g_mdi_bitstream_cntx;
mdi_bitstream_context_struct *mdi_bitstream_p = &g_mdi_bitstream_cntx;
void (*g_mdi_bitsream_audio_callback_func)(MDI_HANDLE handle, MDI_RESULT result);
void (*g_mdi_bitsream_record_callback_func)(MDI_HANDLE handle, MDI_RESULT result);

/***************************************************/
/* functions                                       */
/***************************************************/

/*****************************************************************************
 * FUNCTION
 *  mdi_bitstream_init
 * DESCRIPTION
 *  
 * PARAMETERS
 * 
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_bitstream_init(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    mdi_bitstream_p->audio.open_num = 0;

}


/*****************************************************************************
 * FUNCTION
 *  mdi_bitstream_codec_type_mdi_to_med
 * DESCRIPTION
 *  
 * PARAMETERS
 *
 * RETURNS
 *  
 *****************************************************************************/
kal_uint32 mdi_bitstream_codec_type_mdi_to_med(kal_uint16 mdi_codectype)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/    
    kal_uint32 med_codectype;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/   
    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_CODEC_TYPE_MDI_TO_MED);
    
    switch (mdi_codectype)
    {
        case MDI_BITSTREAM_CODEC_TYPE_NONE:
            med_codectype = MED_BITSTREAM_CODEC_TYPE_NONE;
            break;

        case MDI_BITSTREAM_CODEC_TYPE_AMR:
            med_codectype = MED_BITSTREAM_CODEC_TYPE_AMR;
            break;

        case MDI_BITSTREAM_CODEC_TYPE_AMRWB:
        case MDI_BITSTREAM_CODEC_TYPE_MP4A:
        case MDI_BITSTREAM_CODEC_TYPE_MP4AG:
        default:
            /* not supported for now */
            med_codectype = MED_BITSTREAM_CODEC_TYPE_NONE;            
            break;            
    }
    
    return med_codectype;
}


/*****************************************************************************
 * FUNCTION
 *  mdi_bitstream_get_result
 * DESCRIPTION
 *  This function is to...
 * PARAMETERS
 *
 * RETURNS
 *  void
 *****************************************************************************/
MDI_RESULT mdi_bitstream_get_result(kal_int32 result)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MDI_RESULT mdi_result;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    switch (result)
    {
        case MED_RES_OK:
            mdi_result = MDI_RES_BITSTREAM_SUCCEED;
            break;
        case MED_RES_BITSTREAM_BUFFER_UNDERFLOW:
        case MED_RES_BITSTREAM_RECOVER:
            mdi_result = MDI_RES_BITSTREAM_BUFFER_OVERFLOW;
            break;
        case MED_RES_BITSTREAM_BUFFER_OVERFLOW:
            mdi_result = MDI_RES_BITSTREAM_BUFFER_UNDERFLOW;
            break;
        case MED_RES_BITSTREAM_BUFFER_NOT_AVAILABLE:
            mdi_result = MDI_RES_BITSTREAM_ERR_FAILED;
            break;
        case MED_RES_BITSTREAM_UNSUPPORTED_CODEC:
            mdi_result = MDI_RES_BITSTREAM_ERR_UNSUPPORTED_FORMAT;
            break;
        case MED_RES_BITSTREAM_INVALID_RESOLUTION:
            mdi_result = MDI_RES_BITSTREAM_ERR_INVALID_RESOULTION;      /*  video width , height or both are not supported*/
            break;            
        case MED_RES_BITSTREAM_MEMORY_INSUFFICIENT:
            mdi_result = MDI_RES_BITSTREAM_ERR_INSUFFICIENT_MEMORY;     /* MED memory is not enough, should check MED memory scenario */
            break;
        case MED_RES_BITSTREAM_INVALID_FORMAT:
            mdi_result = MDI_RES_BITSTREAM_ERR_INVALID_FORMAT;          /* there is some error while decoding the frame, the frame may not be a valid one */
            break;
        case MED_RES_BITSTREAM_NOT_SUPPORTED:
            mdi_result = MDI_RES_BITSTREAM_NOT_SUPPORTED;               /* Something which is not supported by Bitstream API, e.g. incorrect invoking sequence, features not available */
            break;
        case MED_RES_BITSTREAM_INVALID_PARAMETER:
            mdi_result = MDI_RES_BITSTREAM_INVALID_PARAMETER;           /* The parameter passed through the API is invalid */
            break;

        case MED_RES_BITSTREAM_FAILED:
        default:
            mdi_result = MDI_RES_BITSTREAM_ERR_FAILED;
            break;
    }

    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_GET_RESULT, mdi_result);
    return mdi_result;
}


/*****************************************************************************
 * FUNCTION
 *  mdi_bitstream_get_event
 * DESCRIPTION
 *  This function is to...
 * PARAMETERS
 *
 * RETURNS
 *  void
 *****************************************************************************/
MDI_RESULT mdi_bitstream_get_event(kal_int32 result)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MDI_RESULT mdi_event;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    switch (result)
    {
        case MEDIA_DATA_REQUEST:
            mdi_event = MDI_RES_BITSTREAM_EVENT_DATA_REQUEST;
            break;
        case MEDIA_ERROR:
            mdi_event = MDI_RES_BITSTREAM_EVENT_ERROR;
            break;
        default:
            mdi_event = MDI_RES_BITSTREAM_EVENT_NONE;
            break;
    }

    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_GET_EVENT, mdi_event);
    return mdi_event;
}


/***************************************************/
/*  Audio                                          */
/***************************************************/

/*****************************************************************************
 * FUNCTION
 *  mdi_bitstream_audio_callback_handler
 * DESCRIPTION
 *  This function is to...
 * PARAMETERS
 *
 * RETURNS
 *  void
 *****************************************************************************/
static void mdi_bitstream_audio_callback_handler(void *inMsg)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_bitstream_audio_callback_ind_struct *msg_p = (media_bitstream_audio_callback_ind_struct*) inMsg;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_AUD_CALLBACK_HANDLER);

    if(g_mdi_bitsream_audio_callback_func != NULL)
    {
        g_mdi_bitsream_audio_callback_func(msg_p->handle, mdi_bitstream_get_event(msg_p->event));
    }
}


/*****************************************************************************
* FUNCTION
*  mdi_bitstream_audio_open_handle
* DESCRIPTION
*  
* PARAMETERS
*  
* RETURNS           
*  
*****************************************************************************/
MDI_RESULT mdi_bitstream_audio_open_handle(
    MDI_HANDLE* p_handle,
    mdi_bitstream_audio_cfg_struct* p_cfg,
    void (*result_callback)(MDI_HANDLE handle, MDI_RESULT result))
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32 result;
    kal_int32 bsa_handle;
//    kal_uint8 bsa_type;
    bitstream_audio_open_param_struct open_param;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_AUD_OPEN_HANDLE, p_cfg->codec_type);

    if(srv_ucm_query_call_count(SRV_UCM_CALL_STATE_ALL, SRV_UCM_CALL_TYPE_NO_CSD, NULL) != 0)
    {
        return MDI_RES_BITSTREAM_ERR_FAILED;
    }

    switch (p_cfg->codec_type)
    {
        case MDI_BITSTREAM_CODEC_TYPE_SILENT:
            return MDI_RES_BITSTREAM_ERR_UNSUPPORTED_FORMAT;
//            break;
        case MDI_BITSTREAM_CODEC_TYPE_AMR:
            open_param.codec_type = MED_TYPE_AMR;
            break;
        case MDI_BITSTREAM_CODEC_TYPE_AMRWB:
            open_param.codec_type = MED_TYPE_AMR_WB;
            break;
        case MDI_BITSTREAM_CODEC_TYPE_AAC:
            open_param.codec_type = MED_TYPE_AAC;
            break;
        case MDI_BITSTREAM_CODEC_TYPE_DAF:
            open_param.codec_type = MED_TYPE_DAF;
            break;
    #ifdef WAV_CODEC
        case MDI_BITSTREAM_CODEC_TYPE_PCM:
            open_param.codec_type = MED_TYPE_PCM_16K; /* always use 16K, but the real bitrate is decided by the input param */
            open_param.isStereo = p_cfg->isStereo;
            open_param.bitPerSample = p_cfg->bitPerSample;

            switch(p_cfg->sampleFreq)
            {
                case MDI_BITSTREAM_SAMPLE_FREQ_8000:
                    open_param.sampleFreq = 8000;
                    break;
                case MDI_BITSTREAM_SAMPLE_FREQ_11025:
                    open_param.sampleFreq = 11025;
                    break;
                case MDI_BITSTREAM_SAMPLE_FREQ_16000:
                    open_param.sampleFreq = 16000;
                    break;
                case MDI_BITSTREAM_SAMPLE_FREQ_22050:
                    open_param.sampleFreq = 22050;
                    break;
                case MDI_BITSTREAM_SAMPLE_FREQ_24000:
                    open_param.sampleFreq = 24000;
                    break;
                case MDI_BITSTREAM_SAMPLE_FREQ_32000:
                    open_param.sampleFreq = 32000;
                    break;
                case MDI_BITSTREAM_SAMPLE_FREQ_44100:
                    open_param.sampleFreq = 44100;
                    break;
                case MDI_BITSTREAM_SAMPLE_FREQ_48000:
                    open_param.sampleFreq = 48000;
                    break;
                default :
                    return MDI_RES_BITSTREAM_ERR_UNSUPPORTED_FORMAT;
            }

            break;
    #endif /* WAV_CODEC */

        default:
            return MDI_RES_BITSTREAM_ERR_UNSUPPORTED_FORMAT;
//            break;
    }

#ifdef __MMI_BT_AUDIO_VIA_SCO__
	/* Connect audio via SCO when stream opened, and remain SCO open */
    srv_btsco_connect_audio_via_sco();
    srv_btsco_set_always_on(MMI_FALSE);
#endif /*__MMI_BT_AUDIO_VIA_SCO__*/

    result = media_bitstream_audio_open_handle(kal_get_active_module_id(), &bsa_handle, &open_param);

    if((result == MED_RES_OK) && (bsa_handle != 0))
    {
        SetProtocolEventHandler(mdi_bitstream_audio_callback_handler, MSG_ID_MEDIA_BITSTREAM_AUDIO_CALLBACK_IND);
        *p_handle = bsa_handle;
        g_mdi_bitsream_audio_callback_func = result_callback;

        mdi_bitstream_p->audio.open_num++;
    }

    return mdi_bitstream_get_result(result);
}


/*****************************************************************************
* FUNCTION
*  mdi_bitstream_audio_close
* DESCRIPTION
*  
* PARAMETERS
*  
* RETURNS           
*  
*****************************************************************************/
MDI_RESULT mdi_bitstream_audio_close(MDI_HANDLE handle)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_AUD_CLOSE, mdi_bitstream_p->audio.open_num);

    media_bitstream_audio_close(kal_get_active_module_id(), handle);

    ClearProtocolEventHandler(MSG_ID_MEDIA_BITSTREAM_AUDIO_CALLBACK_IND);
    g_mdi_bitsream_audio_callback_func = NULL;

    mdi_bitstream_p->audio.open_num--;

#ifdef __MMI_BT_AUDIO_VIA_SCO__
    if(mdi_bitstream_p->audio.open_num == 0)
    {
        srv_btsco_set_always_on(MMI_FALSE);
    }
#endif /*__MMI_BT_AUDIO_VIA_SCO__*/

    return MDI_RES_BITSTREAM_SUCCEED;
}


/*****************************************************************************
* FUNCTION
*  mdi_bitstream_audio_get_buffer_status
* DESCRIPTION
*  
* PARAMETERS
*  
* RETURNS           
*  
*****************************************************************************/
MDI_RESULT mdi_bitstream_audio_get_buffer_status(MDI_HANDLE handle, mdi_bitstream_audio_buffer_status* p_status)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32 result;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    result = media_bitstream_audio_get_buffer_status(kal_get_active_module_id(), handle, &p_status->total_buf_size, &p_status->free_buf_size);

    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_AUD_GET_BUF_STATUS, p_status->total_buf_size, p_status->free_buf_size);

    return mdi_bitstream_get_result(result);
}


/*****************************************************************************
* FUNCTION
*  mdi_bitstream_audio_put_data
* DESCRIPTION
*  
* PARAMETERS
*  
* RETURNS           
*  
*****************************************************************************/
MDI_RESULT mdi_bitstream_audio_put_data(MDI_HANDLE handle, U8* p_buffer, U32 buffer_size, U32* p_used_size)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32 result;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    result = media_bitstream_audio_put_data(kal_get_active_module_id(), handle, p_buffer, buffer_size, p_used_size);

    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_AUD_PUT_DATA, buffer_size, *p_used_size);

    return mdi_bitstream_get_result(result);
}


/*****************************************************************************
* FUNCTION
*  mdi_bitstream_audio_put_frame
* DESCRIPTION
*  This function is to put audio frame align to video frame with timestamp
* PARAMETERS
*  
* RETURNS           
*  
*****************************************************************************/
MDI_RESULT mdi_bitstream_audio_put_frame(MDI_HANDLE handle, U8* p_buffer, U32 buffer_size, U32 timestamp)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32 result;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_AUD_PUT_FRAME, buffer_size, timestamp);

    result = media_bitstream_audio_put_frame(kal_get_active_module_id(), handle, p_buffer, buffer_size, timestamp);

    return mdi_bitstream_get_result(result);
}


/*****************************************************************************
* FUNCTION
*  mdi_bitstream_audio_start
* DESCRIPTION
*  
* PARAMETERS
*  
* RETURNS           
*  
*****************************************************************************/
MDI_RESULT mdi_bitstream_audio_start(MDI_HANDLE handle, mdi_bitstream_audio_start_param* p_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32 result;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_AUD_START);

#ifdef __BT_SPK_VOL_CONTROL__
   mdi_audio_set_volume_to_bt(p_para->volume);
#endif

    result = media_bitstream_audio_start(kal_get_active_module_id(), handle, p_para->start_time, p_para->audio_path, p_para->volume);

    return mdi_bitstream_get_result(result);
}


/*****************************************************************************
* FUNCTION
*  mdi_bitstream_audio_stop
* DESCRIPTION
*  
* PARAMETERS
*  
* RETURNS           
*  
*****************************************************************************/
MDI_RESULT mdi_bitstream_audio_stop(MDI_HANDLE handle)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32 result;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_AUD_STOP);

    result = media_bitstream_audio_stop(kal_get_active_module_id(), handle);

    return mdi_bitstream_get_result(result);
}

/*****************************************************************************
* FUNCTION
*  mdi_bitstream_audio_finished
* DESCRIPTION
*  
* PARAMETERS
*  
* RETURNS           
*  
*****************************************************************************/
MDI_RESULT mdi_bitstream_audio_finished(MDI_HANDLE handle)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32 result;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_AUD_FINISHED);

    result = media_bitstream_audio_finished(kal_get_active_module_id(), handle);

    return mdi_bitstream_get_result(result);
}


/*****************************************************************************
* FUNCTION
*  mdi_bitstream_audio_get_play_time
* DESCRIPTION
*  
* PARAMETERS
*  
* RETURNS           
*  
*****************************************************************************/
MDI_RESULT mdi_bitstream_audio_get_play_time(MDI_HANDLE handle, U32* p_ms_current_time)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32 result;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    result = media_bitstream_audio_get_play_time(kal_get_active_module_id(), handle, p_ms_current_time);

    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_AUD_GET_PLAY_TIME, *p_ms_current_time);

	return mdi_bitstream_get_result(result);
}


/*****************************************************************************
* FUNCTION
*  mdi_bitstream_audio_get_total_handle
* DESCRIPTION
*  
* PARAMETERS
*  
* RETURNS           
*  
*****************************************************************************/
U16 mdi_bitstream_audio_get_open_handle_count(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return mdi_bitstream_p->audio.open_num;
}


/*****************************************************************************
 * FUNCTION
 *  mdi_bitstream_record_callback_handler
 * DESCRIPTION
 *  This function is to...
 * PARAMETERS
 *
 * RETURNS
 *  void
 *****************************************************************************/
static void mdi_bitstream_record_callback_handler(void *data_p)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_bitstream_record_callback_ind_struct *msg_p = (media_bitstream_record_callback_ind_struct*) data_p;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_RECORD_CALLBACK_HANDLER);

    if (g_mdi_bitsream_record_callback_func != NULL)
    {
        g_mdi_bitsream_record_callback_func(msg_p->handle, mdi_bitstream_get_event(msg_p->event));
    }
}


/*****************************************************************************
 * FUNCTION
 *  mdi_bitstream_record_open
 * DESCRIPTION
 *  This function is to open a record handle for bit-stream record.
 * PARAMETERS
 *  handle_p        [OUT]       Recorder handle.
 *  cfg_p           [IN]        Recorder configuration.
 *  callback        [IN]        Callback function to handle record event.
 * RETURNS
 *  MDI_RES_BITSTREAM_SUCCEED   Successful.
 *  Others                      Failed.
 *****************************************************************************/
MDI_RESULT mdi_bitstream_record_open(MDI_HANDLE* handle_p,
                                     mdi_bitstream_record_cfg_struct* cfg_p,
                                     void (*callback)(MDI_HANDLE handle, MDI_RESULT result))
{

    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32  result;
    kal_int32  handle;
    kal_uint8  med_type;
    kal_uint8  med_quality;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_RECORD_OPEN, cfg_p->codec_type, cfg_p->quality);

    switch (cfg_p->codec_type)
    {
        case MDI_BITSTREAM_CODEC_TYPE_AMR:
            med_type = MED_TYPE_AMR;
            break;

    #ifdef AMRWB_ENCODE
        case MDI_BITSTREAM_CODEC_TYPE_AMRWB:
            med_type = MED_TYPE_AMR_WB;
            break;
    #endif /*AMRWB_ENCODE*/

    #ifdef WAV_CODEC
        case MDI_BITSTREAM_CODEC_TYPE_WAV:
            med_type = MED_TYPE_WAV;
            break;
        case MDI_BITSTREAM_CODEC_TYPE_ADPCM:
            med_type = MED_TYPE_WAV_DVI_ADPCM;
            break;
        case MDI_BITSTREAM_CODEC_TYPE_PCM:
            med_type = MED_TYPE_PCM_8K;
            break;
    #endif /*WAV_CODEC*/

        default:
            return MDI_RES_BITSTREAM_ERR_UNSUPPORTED_FORMAT;
    }

    switch (cfg_p->quality)
    {
        case MDI_BITSTREAM_RECORD_QUALITY_LOW:   /* Low quality */
            med_quality = AUD_REC_QUALITY_LOW;
            break;
        case MDI_BITSTREAM_RECORD_QUALITY_HIGH:  /* High quality */
            med_quality = AUD_REC_QUALITY_HIGH;
            break;
        case MDI_BITSTREAM_RECORD_AMR_4_75:      /* AMR 4.75 kbit/s */
            med_quality = AUD_REC_QUALITY_AMR_4_75;
            break;
        case MDI_BITSTREAM_RECORD_AMR_5_15:      /* AMR 5.15 kbit/s */
            med_quality = AUD_REC_QUALITY_AMR_5_15;
            break;
        case MDI_BITSTREAM_RECORD_AMR_5_9:       /* AMR 5.9  kbit/s */
            med_quality = AUD_REC_QUALITY_AMR_5_9;
            break;
        case MDI_BITSTREAM_RECORD_AMR_6_7:       /* AMR 6.7  kbit/s */
            med_quality = AUD_REC_QUALITY_AMR_6_7;
            break;
        case MDI_BITSTREAM_RECORD_AMR_7_4:       /* AMR 7.4  kbit/s */
            med_quality = AUD_REC_QUALITY_AMR_7_4;
            break;
        case MDI_BITSTREAM_RECORD_AMR_7_95:      /* AMR 7.95 kbit/s */
            med_quality = AUD_REC_QUALITY_AMR_7_95;
            break;
        case MDI_BITSTREAM_RECORD_AMR_10_2:      /* AMR 10.2 kbit/s */
            med_quality = AUD_REC_QUALITY_AMR_10_2;
            break;
        case MDI_BITSTREAM_RECORD_AMR_12_2:      /* AMR 12.2 kbit/s */
            med_quality = AUD_REC_QUALITY_AMR_12_2;
            break;
        case MDI_BITSTREAM_RECORD_AWB_6_6:       /* AWB 6.6   kbit/s */
            med_quality = AUD_REC_QUALITY_AWB_6_6;
            break;
        case MDI_BITSTREAM_RECORD_AWB_8_85:      /* AWB 8.85  kbit/s */
            med_quality = AUD_REC_QUALITY_AWB_8_85;
            break;
        case MDI_BITSTREAM_RECORD_AWB_12_65:     /* AWB 12.65 kbit/s */
            med_quality = AUD_REC_QUALITY_AWB_12_65;
            break;
        case MDI_BITSTREAM_RECORD_AWB_14_25:     /* AWB 14.25 kbit/s */
            med_quality = AUD_REC_QUALITY_AWB_14_25;
            break;
        case MDI_BITSTREAM_RECORD_AWB_15_85:     /* AWB 15.85 kbit/s */
            med_quality = AUD_REC_QUALITY_AWB_15_85;
            break;
        case MDI_BITSTREAM_RECORD_AWB_18_25:     /* AWB 18.25 kbit/s */
            med_quality = AUD_REC_QUALITY_AWB_18_25;
            break;
        case MDI_BITSTREAM_RECORD_AWB_19_85:     /* AWB 19.85 kbit/s */
            med_quality = AUD_REC_QUALITY_AWB_19_85;
            break;
        case MDI_BITSTREAM_RECORD_AWB_23_05:     /* AWB 23.05 kbit/s */
            med_quality = AUD_REC_QUALITY_AWB_23_05;
            break;
        case MDI_BITSTREAM_RECORD_AWB_23_85:     /* AWB 23.85 kbit/s */
            med_quality = AUD_REC_QUALITY_AWB_23_85;
            break;
        case MDI_BITSTREAM_RECORD_QUALITY_MED:   /* Reserved */
        case MDI_BITSTREAM_RECORD_QUALITY_BEST:  /* Reserved */
        default:
            return MDI_RES_BITSTREAM_ERR_UNSUPPORTED_FORMAT;
    }

    result = media_bitstream_record_open(med_type, med_quality, &handle);

    if (result == MED_RES_OK)
    {
        SetProtocolEventHandler(mdi_bitstream_record_callback_handler, MSG_ID_MEDIA_BITSTREAM_RECORD_CALLBACK_IND);
        g_mdi_bitsream_record_callback_func = callback;
        
        *handle_p = (MDI_HANDLE)handle;
    }

    return mdi_bitstream_get_result(result);
}


/*****************************************************************************
 * FUNCTION
 *  mdi_bitstream_record_close
 * DESCRIPTION
 *  This function is to close a bit-stream record handle.
 * PARAMETERS
 *  handle_p        [IN]        Recorder handle.
 * RETURNS
 *  MDI_RES_BITSTREAM_SUCCEED   Successful.
 *  Others                      Failed.
 *****************************************************************************/
MDI_RESULT mdi_bitstream_record_close(MDI_HANDLE handle)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_RECORD_CLOSE, handle);

    media_bitstream_record_close(handle);
    ClearProtocolEventHandler(MSG_ID_MEDIA_BITSTREAM_RECORD_CALLBACK_IND);
    g_mdi_bitsream_record_callback_func = NULL;

    return MDI_RES_BITSTREAM_SUCCEED;
}


/*****************************************************************************
 * FUNCTION
 *  mdi_bitstream_record_start
 * DESCRIPTION
 *  This function is to start bit-stream record process.
 * PARAMETERS
 *  handle_p        [IN]        Recorder handle.
 * RETURNS
 *  MDI_RES_BITSTREAM_SUCCEED   Successful.
 *  Others                      Failed.
 *****************************************************************************/
MDI_RESULT mdi_bitstream_record_start(MDI_HANDLE handle)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32  result;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_RECORD_START, handle);

    result = media_bitstream_record_start(handle);
    
    return mdi_bitstream_get_result(result);
}


/*****************************************************************************
 * FUNCTION
 *  mdi_bitstream_record_stop
 * DESCRIPTION
 *  This function is to stop bit-stream record process.
 * PARAMETERS
 *  handle_p        [IN]        Recorder handle.
 * RETURNS
 *  MDI_RES_BITSTREAM_SUCCEED   Successful.
 *  Others                      Failed.
 *****************************************************************************/
MDI_RESULT mdi_bitstream_record_stop(MDI_HANDLE handle)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32  result;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_RECORD_STOP, handle);

    result = media_bitstream_record_stop(handle);
    
    return mdi_bitstream_get_result(result);
}


/*****************************************************************************
 * FUNCTION
 *  mdi_bitstream_record_get
 * DESCRIPTION
 *  This function is a general get function to get information in the 
 *  bit-stream recorder.
 * PARAMETERS
 *  handle          [IN]        Recorder handle.
 *  get_type        [IN]        The information to be got.
 *  data_p          [OUT]       Data buffer. The information will be stored to
 *                              the data buffer.
 *  data_len_p      [IN/OUT]    When input, it indicates the length of data buffer. 
 *                              When output, it indicates the length of data that
 *                              is retrieved.
 * RETURNS
 *  MDI_RES_BITSTREAM_SUCCEED   Successful.
 *  Others                      Failed.
 *****************************************************************************/
MDI_RESULT mdi_bitstream_record_get(MDI_HANDLE handle,
                                    mdi_bitstream_record_get_type_enum get_type,
                                    void* data_p,
                                    U16* data_len_p)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32  result;
    kal_uint8  med_get_type = BITSTREAM_RECORD_GET_LAST_ENTRY;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_RECORD_GET, handle, get_type);

    switch (get_type)
    {
        case MDI_BITSTREAM_RECORD_GET_LENGTH:
            med_get_type = BITSTREAM_RECORD_GET_LENGTH;
            break;
        case MDI_BITSTREAM_RECORD_GET_DATA:
            med_get_type = BITSTREAM_RECORD_GET_DATA;
            break;
        case MDI_BITSTREAM_RECORD_GET_STRENGTH:
            med_get_type = BITSTREAM_RECORD_GET_STRENGTH;
            break;

        default:
            break;
    }

    result = media_bitstream_record_get(handle, med_get_type, data_p, data_len_p);
    
    return mdi_bitstream_get_result(result);
}


/*****************************************************************************
 * FUNCTION
 *  mdi_bitstream_record_set
 * DESCRIPTION
 *  This function is a general set function to parameter of the bit-stream recorder.
 * PARAMETERS
 *  handle          [IN]        Recorder handle.
 *  set_type        [IN]        The type of the parameter to be set.
 *  data_p          [IN]        Data buffer. The data in the data buffer will be
 *                              set to bit-stream recorder.
 *  data_len        [IN]        The length of data. Note that the length of data
 *                              should be equal to the length of the parameter to
 *                              be set.
 * RETURNS
 *  MDI_RES_BITSTREAM_SUCCEED   Successful.
 *  Others                      Failed.
 *****************************************************************************/
MDI_RESULT mdi_bitstream_record_set(MDI_HANDLE handle,
                                    mdi_bitstream_record_set_type_enum set_type,
                                    void* data_p,
                                    U16 data_len)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32  result;
    kal_uint8  med_set_type = BITSTREAM_RECORD_SET_LAST_ENTRY;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI, MDI_TRC_BITSTREAM_RECORD_SET, handle, set_type);

    switch (set_type)
    {
        case MDI_BITSTREAM_RECORD_SET_PAUSE:
            med_set_type = BITSTREAM_RECORD_SET_PAUSE;
            break;
        case MDI_BITSTREAM_RECORD_SET_RESUME:
            med_set_type = BITSTREAM_RECORD_SET_RESUME;
            break;
        case MDI_BITSTREAM_RECORD_SET_CB_THRESHOLD:
            med_set_type = BITSTREAM_RECORD_SET_CB_THRESHOLD;
            break;
        default:
            break;
    }
    
    result = media_bitstream_record_set(handle, med_set_type, data_p, data_len);
    
    return mdi_bitstream_get_result(result);
}


#endif /*defined(__BITSTREAM_API_SUPPORT__)*/