Home | History | Annotate | Download | only in acm2
      1 /*
      2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include "webrtc/modules/audio_coding/main/acm2/acm_gsmfr.h"
     12 
     13 #ifdef WEBRTC_CODEC_GSMFR
     14 // NOTE! GSM-FR is not included in the open-source package. Modify this file
     15 // or your codec API to match the function calls and names of used GSM-FR API
     16 // file.
     17 #include "webrtc/modules/audio_coding/main/codecs/gsmfr/interface/gsmfr_interface.h"
     18 #include "webrtc/modules/audio_coding/main/acm2/acm_common_defs.h"
     19 #include "webrtc/system_wrappers/interface/trace.h"
     20 #endif
     21 
     22 namespace webrtc {
     23 
     24 namespace acm2 {
     25 
     26 #ifndef WEBRTC_CODEC_GSMFR
     27 
     28 ACMGSMFR::ACMGSMFR(int16_t /* codec_id */) : encoder_inst_ptr_(NULL) {}
     29 
     30 ACMGSMFR::~ACMGSMFR() { return; }
     31 
     32 int16_t ACMGSMFR::InternalEncode(uint8_t* /* bitstream */,
     33                                  int16_t* /* bitstream_len_byte */) {
     34   return -1;
     35 }
     36 
     37 int16_t ACMGSMFR::EnableDTX() { return -1; }
     38 
     39 int16_t ACMGSMFR::DisableDTX() { return -1; }
     40 
     41 int16_t ACMGSMFR::InternalInitEncoder(
     42     WebRtcACMCodecParams* /* codec_params */) {
     43   return -1;
     44 }
     45 
     46 ACMGenericCodec* ACMGSMFR::CreateInstance(void) { return NULL; }
     47 
     48 int16_t ACMGSMFR::InternalCreateEncoder() { return -1; }
     49 
     50 void ACMGSMFR::DestructEncoderSafe() { return; }
     51 
     52 void ACMGSMFR::InternalDestructEncoderInst(void* /* ptr_inst */) {
     53   return;
     54 }
     55 
     56 #else  //===================== Actual Implementation =======================
     57 
     58 ACMGSMFR::ACMGSMFR(int16_t codec_id)
     59     : codec_id_(codec_id),
     60       has_internal_dtx_(true),
     61       encoder_inst_ptr_(NULL) {}
     62 
     63 ACMGSMFR::~ACMGSMFR() {
     64   if (encoder_inst_ptr_ != NULL) {
     65     WebRtcGSMFR_FreeEnc(encoder_inst_ptr_);
     66     encoder_inst_ptr_ = NULL;
     67   }
     68   return;
     69 }
     70 
     71 int16_t ACMGSMFR::InternalEncode(uint8_t* bitstream,
     72                                  int16_t* bitstream_len_byte) {
     73   *bitstream_len_byte = WebRtcGSMFR_Encode(
     74       encoder_inst_ptr_, &in_audio_[in_audio_ix_read_], frame_len_smpl_,
     75       reinterpret_cast<int16_t*>(bitstream));
     76 
     77   // increment the read index this tell the caller that how far
     78   // we have gone forward in reading the audio buffer
     79   in_audio_ix_read_ += frame_len_smpl_;
     80   return *bitstream_len_byte;
     81 }
     82 
     83 int16_t ACMGSMFR::EnableDTX() {
     84   if (dtx_enabled_) {
     85     return 0;
     86   } else if (encoder_exist_) {
     87     if (WebRtcGSMFR_EncoderInit(encoder_inst_ptr_, 1) < 0) {
     88       WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
     89                    "EnableDTX: cannot init encoder for GSMFR");
     90       return -1;
     91     }
     92     dtx_enabled_ = true;
     93     return 0;
     94   } else {
     95     return -1;
     96   }
     97 }
     98 
     99 int16_t ACMGSMFR::DisableDTX() {
    100   if (!dtx_enabled_) {
    101     return 0;
    102   } else if (encoder_exist_) {
    103     if (WebRtcGSMFR_EncoderInit(encoder_inst_ptr_, 0) < 0) {
    104       WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
    105                    "DisableDTX: cannot init encoder for GSMFR");
    106       return -1;
    107     }
    108     dtx_enabled_ = false;
    109     return 0;
    110   } else {
    111     // encoder doesn't exists, therefore disabling is harmless
    112     return 0;
    113   }
    114 }
    115 
    116 int16_t ACMGSMFR::InternalInitEncoder(WebRtcACMCodecParams* codec_params) {
    117   if (WebRtcGSMFR_EncoderInit(encoder_inst_ptr_,
    118                               ((codec_params->enable_dtx) ? 1 : 0)) < 0) {
    119     WEBRTC_TRACE(webrtc::kTraceError,
    120                  webrtc::kTraceAudioCoding,
    121                  unique_id_,
    122                  "InternalInitEncoder: cannot init encoder for GSMFR");
    123   }
    124   return 0;
    125 }
    126 
    127 ACMGenericCodec* ACMGSMFR::CreateInstance(void) { return NULL; }
    128 
    129 int16_t ACMGSMFR::InternalCreateEncoder() {
    130   if (WebRtcGSMFR_CreateEnc(&encoder_inst_ptr_) < 0) {
    131     WEBRTC_TRACE(webrtc::kTraceError,
    132                  webrtc::kTraceAudioCoding,
    133                  unique_id_,
    134                  "InternalCreateEncoder: cannot create instance for GSMFR "
    135                  "encoder");
    136     return -1;
    137   }
    138   return 0;
    139 }
    140 
    141 void ACMGSMFR::DestructEncoderSafe() {
    142   if (encoder_inst_ptr_ != NULL) {
    143     WebRtcGSMFR_FreeEnc(encoder_inst_ptr_);
    144     encoder_inst_ptr_ = NULL;
    145   }
    146   encoder_exist_ = false;
    147   encoder_initialized_ = false;
    148 }
    149 
    150 void ACMGSMFR::InternalDestructEncoderInst(void* ptr_inst) {
    151   if (ptr_inst != NULL) {
    152     WebRtcGSMFR_FreeEnc(static_cast<GSMFR_encinst_t_*>(ptr_inst));
    153   }
    154   return;
    155 }
    156 
    157 #endif
    158 
    159 }  // namespace acm2
    160 
    161 }  // namespace webrtc
    162