Changeset 4793


Ignore:
Timestamp:
Mar 14, 2014 4:09:50 AM (6 years ago)
Author:
bennylp
Message:

Closed #1748: enhancements to WAV player API

Location:
pjproject/trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjmedia/include/pjmedia/wav_port.h

    r3553 r4793  
    5050    PJMEDIA_FILE_NO_LOOP = 1 
    5151}; 
     52 
     53 
     54/** 
     55 * Additional information about the WAV player. 
     56 */ 
     57typedef struct pjmedia_wav_player_info 
     58{ 
     59    /** 
     60     * Format ID of the payload. 
     61     */ 
     62    pjmedia_format_id   fmt_id; 
     63 
     64    /** 
     65     * The number of bits per sample of the file payload. For example, 
     66     * the value is 16 for PCM WAV and 8 for Alaw/Ulas WAV files. 
     67     */ 
     68    unsigned            payload_bits_per_sample; 
     69 
     70    /** 
     71     * The WAV payload size in bytes. 
     72     */ 
     73    pj_uint32_t         size_bytes; 
     74 
     75    /** 
     76     * The WAV payload size in samples. 
     77     */ 
     78    pj_uint32_t         size_samples; 
     79 
     80} pjmedia_wav_player_info; 
    5281 
    5382 
     
    77106                                                     pjmedia_port **p_port ); 
    78107 
     108/** 
     109 * Get additional info about the file player. 
     110 * 
     111 * @param port          The file port. 
     112 * @param i             The info. 
     113 * 
     114 * @return              PJ_SUCCESS on success or the appropriate error code. 
     115 */ 
     116PJ_DECL(pj_status_t) pjmedia_wav_player_get_info(pjmedia_port *port, 
     117                                                 pjmedia_wav_player_info *i); 
    79118 
    80119/** 
     
    83122 * @param port          The file player port. 
    84123 * 
    85  * @return              The length of the data, in bytes. Upon error it will 
    86  *                      return negative value. 
     124 * @return              The length of the data, in bytes. On error, the 
     125 *                      error code is given as negative value. 
    87126 */ 
    88127PJ_DECL(pj_ssize_t) pjmedia_wav_player_get_len(pjmedia_port *port); 
     
    103142 
    104143/** 
    105  * Get the file play position of WAV player. 
    106  * 
    107  * @param port          The file player port. 
    108  * 
    109  * @return              PJ_SUCCESS on success. 
     144 * Get the file play position of WAV player, in bytes. 
     145 * 
     146 * @param port          The file player port. 
     147 * 
     148 * @return              The current play position, in bytes. On error, the 
     149 *                      error code is given as negative value. 
    110150 */ 
    111151PJ_DECL(pj_ssize_t) pjmedia_wav_player_port_get_pos( pjmedia_port *port ); 
  • pjproject/trunk/pjmedia/src/pjmedia/wav_player.c

    r4537 r4793  
    426426 
    427427/* 
     428 * Get additional info about the file player. 
     429 */ 
     430PJ_DEF(pj_status_t) pjmedia_wav_player_get_info( 
     431                                        pjmedia_port *port, 
     432                                        pjmedia_wav_player_info *info) 
     433{ 
     434    struct file_reader_port *fport; 
     435    PJ_ASSERT_RETURN(port && info, PJ_EINVAL); 
     436 
     437    pj_bzero(info, sizeof(*info)); 
     438 
     439    /* Check that this is really a player port */ 
     440    PJ_ASSERT_RETURN(port->info.signature == SIGNATURE, PJ_EINVALIDOP); 
     441 
     442    fport = (struct file_reader_port*) port; 
     443 
     444    if (fport->fmt_tag == PJMEDIA_WAVE_FMT_TAG_PCM) { 
     445        info->fmt_id = PJMEDIA_FORMAT_PCM; 
     446        info->payload_bits_per_sample = 16; 
     447    } else if (fport->fmt_tag == PJMEDIA_WAVE_FMT_TAG_ULAW) { 
     448        info->fmt_id = PJMEDIA_FORMAT_ULAW; 
     449        info->payload_bits_per_sample = 8; 
     450    } else if (fport->fmt_tag == PJMEDIA_WAVE_FMT_TAG_ALAW) { 
     451        info->fmt_id = PJMEDIA_FORMAT_ALAW; 
     452        info->payload_bits_per_sample = 8; 
     453    } else { 
     454        pj_assert(!"Unsupported format"); 
     455        return PJ_ENOTSUP; 
     456    } 
     457 
     458    info->size_bytes = pjmedia_wav_player_get_len(port); 
     459    info->size_samples = info->size_bytes / 
     460                         (info->payload_bits_per_sample / 8); 
     461 
     462    return PJ_SUCCESS; 
     463} 
     464 
     465/* 
    428466 * Get the data length, in bytes. 
    429467 */ 
     
    485523 
    486524/* 
    487  * Get the file play position of WAV player. 
     525 * Get the file play position of WAV player (in bytes). 
    488526 */ 
    489527PJ_DEF(pj_ssize_t) pjmedia_wav_player_port_get_pos( pjmedia_port *port ) 
  • pjproject/trunk/pjsip-apps/src/3rdparty_media_sample/alt_pjsua_aud.c

    r4174 r4793  
    497497} 
    498498 
     499/* Get number of bits per sample of the WAV payload */ 
     500PJ_DEF(pj_status_t) pjsua_player_get_info(pjsua_player_id id, 
     501                                          pjmedia_wav_player_info *info) 
     502{ 
     503    UNIMPLEMENTED(pjsua_player_get_info) 
     504    return PJ_ENOTSUP; 
     505} 
     506 
     507/* Get position in samples */ 
     508PJ_DEF(pj_ssize_t) pjsua_player_get_pos(pjsua_player_id id) 
     509{ 
     510    UNIMPLEMENTED(pjsua_player_get_pos) 
     511    return -PJ_ENOTSUP; 
     512} 
     513 
    499514/* Set playback position. */ 
    500515PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id, 
  • pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h

    r4739 r4793  
    60726072 
    60736073/** 
     6074 * Get additional info about the file player. This operation is not valid 
     6075 * for playlist. 
     6076 * 
     6077 * @param port          The file player ID. 
     6078 * @param info          The info. 
     6079 * 
     6080 * @return              PJ_SUCCESS on success or the appropriate error code. 
     6081 */ 
     6082PJ_DECL(pj_status_t) pjsua_player_get_info(pjsua_player_id id, 
     6083                                           pjmedia_wav_player_info *info); 
     6084 
     6085 
     6086/** 
     6087 * Get playback position. This operation is not valid for playlist. 
     6088 * 
     6089 * @param id            The file player ID. 
     6090 * 
     6091 * @return              The current playback position, in samples. On error, 
     6092 *                      return the error code as negative value. 
     6093 */ 
     6094PJ_DECL(pj_ssize_t) pjsua_player_get_pos(pjsua_player_id id); 
     6095 
     6096/** 
    60746097 * Set playback position. This operation is not valid for playlist. 
    60756098 * 
     
    60826105PJ_DECL(pj_status_t) pjsua_player_set_pos(pjsua_player_id id, 
    60836106                                          pj_uint32_t samples); 
    6084  
    60856107 
    60866108/** 
  • pjproject/trunk/pjsip/include/pjsua2/media.hpp

    r4771 r4793  
    315315 
    316316/** 
     317 * This structure contains additional info about AudioMediaPlayer. 
     318 */ 
     319struct AudioMediaPlayerInfo 
     320{ 
     321    /** 
     322     * Format ID of the payload. 
     323     */ 
     324    pjmedia_format_id   formatId; 
     325 
     326    /** 
     327     * The number of bits per sample of the file payload. For example, 
     328     * the value is 16 for PCM WAV and 8 for Alaw/Ulas WAV files. 
     329     */ 
     330    unsigned            payloadBitsPerSample; 
     331 
     332    /** 
     333     * The WAV payload size in bytes. 
     334     */ 
     335    pj_uint32_t         sizeBytes; 
     336 
     337    /** 
     338     * The WAV payload size in samples. 
     339     */ 
     340    pj_uint32_t         sizeSamples; 
     341}; 
     342 
     343/** 
    317344 * Audio Media Player. 
    318345 */ 
     
    355382 
    356383    /** 
    357      * Set playback position. This operation is not valid for playlist. 
     384     * Get additional info about the player. This operation is only valid 
     385     * for player. For playlist, Error will be thrown. 
     386     * 
     387     * @return          the info. 
     388     */ 
     389    AudioMediaPlayerInfo getInfo() const throw(Error); 
     390 
     391    /** 
     392     * Get current playback position in samples. This operation is not valid 
     393     * for playlist. 
     394     * 
     395     * @return             Current playback position, in samples. 
     396     */ 
     397    pj_uint32_t getPos() const throw(Error); 
     398 
     399    /** 
     400     * Set playback position in samples. This operation is not valid for 
     401     * playlist. 
    358402     * 
    359403     * @param samples      The desired playback position, in samples. 
     
    372416 
    373417    /** 
    374      * Virtual destructor. 
     418     * Destructor. 
    375419     */ 
    376420    virtual ~AudioMediaPlayer(); 
    377421 
     422public: 
     423    /* 
     424     * Callbacks 
     425     */ 
     426 
     427    /** 
     428     * Register a callback to be called when the file player reading has 
     429     * reached the end of file, or when the file reading has reached the 
     430     * end of file of the last file for a playlist. If the file or playlist 
     431     * is set to play repeatedly, then the callback will be called multiple 
     432     * times. 
     433     * 
     434     * @return                  If the callback returns false, the playback 
     435     *                          will stop. Note that if application destroys 
     436     *                          the player in the callback, it must return 
     437     *                          false here. 
     438     */ 
     439    virtual bool onEof() 
     440    { return true; } 
     441 
     442 
    378443private: 
    379444    /** 
     
    382447    int playerId; 
    383448 
     449    /** 
     450     *  Low level PJMEDIA callback 
     451     */ 
     452    static pj_status_t eof_cb(pjmedia_port *port, 
     453                              void *usr_data); 
    384454}; 
    385455 
     
    431501 
    432502    /** 
    433      * Virtual destructor. 
     503     * Destructor. 
    434504     */ 
    435505    virtual ~AudioMediaRecorder(); 
  • pjproject/trunk/pjsip/src/pjsua-lib/pjsua_aud.c

    r4704 r4793  
    11931193PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id) 
    11941194{ 
    1195     PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL); 
     1195    PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player),PJ_EINVAL); 
    11961196    PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); 
    11971197 
     
    12051205                                           pjmedia_port **p_port) 
    12061206{ 
    1207     PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL); 
     1207    PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player),PJ_EINVAL); 
    12081208    PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); 
    12091209    PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL); 
     
    12151215 
    12161216/* 
     1217 * Get player info. 
     1218 */ 
     1219PJ_DEF(pj_status_t) pjsua_player_get_info(pjsua_player_id id, 
     1220                                          pjmedia_wav_player_info *info) 
     1221{ 
     1222    PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), 
     1223                     -PJ_EINVAL); 
     1224    PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); 
     1225    PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL); 
     1226 
     1227    return pjmedia_wav_player_get_info(pjsua_var.player[id].port, info); 
     1228} 
     1229 
     1230/* 
     1231 * Get playback position. 
     1232 */ 
     1233PJ_DEF(pj_ssize_t) pjsua_player_get_pos( pjsua_player_id id ) 
     1234{ 
     1235    pj_ssize_t pos_bytes; 
     1236    pjmedia_wav_player_info info; 
     1237    pj_status_t status; 
     1238 
     1239    PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), 
     1240                     -PJ_EINVAL); 
     1241    PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, -PJ_EINVAL); 
     1242    PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, -PJ_EINVAL); 
     1243 
     1244    pos_bytes = pjmedia_wav_player_port_get_pos(pjsua_var.player[id].port); 
     1245    if (pos_bytes < 0) 
     1246        return pos_bytes; 
     1247 
     1248    status = pjmedia_wav_player_get_info(pjsua_var.player[id].port, &info); 
     1249    if (status != PJ_SUCCESS) 
     1250        return -status; 
     1251 
     1252    return pos_bytes / (info.payload_bits_per_sample / 8); 
     1253} 
     1254 
     1255/* 
    12171256 * Set playback position. 
    12181257 */ 
     
    12201259                                          pj_uint32_t samples) 
    12211260{ 
    1222     PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL); 
     1261    pjmedia_wav_player_info info; 
     1262    pj_uint32_t pos_bytes; 
     1263    pj_status_t status; 
     1264 
     1265    PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player),PJ_EINVAL); 
    12231266    PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); 
    12241267    PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL); 
    12251268 
    1226     return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, samples); 
     1269    status = pjmedia_wav_player_get_info(pjsua_var.player[id].port, &info); 
     1270    if (status != PJ_SUCCESS) 
     1271        return status; 
     1272 
     1273    pos_bytes = samples * (info.payload_bits_per_sample / 8); 
     1274    return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, 
     1275                                           pos_bytes); 
    12271276} 
    12281277 
     
    12341283PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id) 
    12351284{ 
    1236     PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL); 
     1285    PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player),PJ_EINVAL); 
    12371286    PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); 
    12381287 
  • pjproject/trunk/pjsip/src/pjsua2/media.cpp

    r4776 r4793  
    263263                                           &playerId) ); 
    264264 
     265    /* Register EOF callback */ 
     266    pjmedia_port *port; 
     267    pj_status_t status; 
     268 
     269    status = pjsua_player_get_port(playerId, &port); 
     270    if (status != PJ_SUCCESS) { 
     271        pjsua_player_destroy(playerId); 
     272        PJSUA2_RAISE_ERROR2(status, "AudioMediaPlayer::createPlayer()"); 
     273    } 
     274    status = pjmedia_wav_player_set_eof_cb(port, this, &eof_cb); 
     275    if (status != PJ_SUCCESS) { 
     276        pjsua_player_destroy(playerId); 
     277        PJSUA2_RAISE_ERROR2(status, "AudioMediaPlayer::createPlayer()"); 
     278    } 
     279 
    265280    /* Get media port id. */ 
    266281    id = pjsua_player_get_conf_port(playerId); 
     
    281296    unsigned i, count = 0; 
    282297    pj_str_t pj_lbl = str2Pj(label); 
     298    pj_status_t status; 
    283299 
    284300    count = PJ_ARRAY_SIZE(pj_files); 
     
    297313                                             &playerId) ); 
    298314 
     315    /* Register EOF callback */ 
     316    pjmedia_port *port; 
     317    status = pjsua_player_get_port(playerId, &port); 
     318    if (status != PJ_SUCCESS) { 
     319        pjsua_player_destroy(playerId); 
     320        PJSUA2_RAISE_ERROR2(status, "AudioMediaPlayer::createPlaylist()"); 
     321    } 
     322    status = pjmedia_wav_playlist_set_eof_cb(port, this, &eof_cb); 
     323    if (status != PJ_SUCCESS) { 
     324        pjsua_player_destroy(playerId); 
     325        PJSUA2_RAISE_ERROR2(status, "AudioMediaPlayer::createPlaylist()"); 
     326    } 
     327 
    299328    /* Get media port id. */ 
    300329    id = pjsua_player_get_conf_port(playerId); 
     
    303332} 
    304333 
     334AudioMediaPlayerInfo AudioMediaPlayer::getInfo() const throw(Error) 
     335{ 
     336    AudioMediaPlayerInfo info; 
     337    pjmedia_wav_player_info pj_info; 
     338 
     339    PJSUA2_CHECK_EXPR( pjsua_player_get_info(playerId, &pj_info) ); 
     340 
     341    pj_bzero(&info, sizeof(info)); 
     342    info.formatId               = pj_info.fmt_id; 
     343    info.payloadBitsPerSample   = pj_info.payload_bits_per_sample; 
     344    info.sizeBytes              = pj_info.size_bytes; 
     345    info.sizeSamples            = pj_info.size_samples; 
     346 
     347    return info; 
     348} 
     349 
     350pj_uint32_t AudioMediaPlayer::getPos() const throw(Error) 
     351{ 
     352    pj_ssize_t pos = pjsua_player_get_pos(playerId); 
     353    if (pos < 0) { 
     354        PJSUA2_RAISE_ERROR2(-pos, "AudioMediaPlayer::getPos()"); 
     355    } 
     356    return (pj_uint32_t)pos; 
     357} 
     358 
    305359void AudioMediaPlayer::setPos(pj_uint32_t samples) throw(Error) 
    306360{ 
     
    312366{ 
    313367    return static_cast<AudioMediaPlayer*>(media); 
     368} 
     369 
     370pj_status_t AudioMediaPlayer::eof_cb(pjmedia_port *port, 
     371                                     void *usr_data) 
     372{ 
     373    AudioMediaPlayer *player = (AudioMediaPlayer*)usr_data; 
     374    return player->onEof() ? PJ_SUCCESS : PJ_EEOF; 
    314375} 
    315376 
Note: See TracChangeset for help on using the changeset viewer.