[libbluray-devel] [Git][videolan/libbluray][master] 2 commits: Protect disc info filling with mutex

Petri Hintukainen (@hpi) gitlab at videolan.org
Mon Oct 3 16:48:04 UTC 2022



Petri Hintukainen pushed to branch master at VideoLAN / libbluray


Commits:
1f268d04 by Petri Hintukainen at 2022-10-03T19:45:55+03:00
Protect disc info filling with mutex

Avoid issues if bd_open_???() and bd_get_disc_info() are called at the same time

- - - - -
6e63d778 by Petri Hintukainen at 2022-10-03T19:47:16+03:00
Protect title_list access with mutex

It doesn't make much sense to call hese functions in paraller,
but doing so might cause some harm ...

- - - - -


1 changed file:

- src/libbluray/bluray.c


Changes:

=====================================
src/libbluray/bluray.c
=====================================
@@ -1171,9 +1171,11 @@ static void _fill_disc_info(BLURAY *bd, BD_ENC_INFO *enc_info)
 
 const BLURAY_DISC_INFO *bd_get_disc_info(BLURAY *bd)
 {
+    bd_mutex_lock(&bd->mutex);
     if (!bd->disc) {
         _fill_disc_info(bd, NULL);
     }
+    bd_mutex_unlock(&bd->mutex);
     return &bd->disc_info;
 }
 
@@ -1497,7 +1499,11 @@ static int _bd_open(BLURAY *bd,
     if (!bd) {
         return 0;
     }
+
+    bd_mutex_lock(&bd->mutex);
+
     if (bd->disc) {
+        bd_mutex_unlock(&bd->mutex);
         BD_DEBUG(DBG_BLURAY | DBG_CRIT, "Disc already open\n");
         return 0;
     }
@@ -1507,11 +1513,14 @@ static int _bd_open(BLURAY *bd,
                          (void*)bd->regs, (void*)bd_psr_read, (void*)bd_psr_write);
 
     if (!bd->disc) {
+        bd_mutex_unlock(&bd->mutex);
         return 0;
     }
 
     _fill_disc_info(bd, &enc_info);
 
+    bd_mutex_unlock(&bd->mutex);
+
     return bd->disc_info.bluray_detected;
 }
 
@@ -2534,10 +2543,9 @@ int bd_play_playlist_at(BLURAY *bd, int playlist, int playitem, int playmark, in
 // Select a title for playback
 // The title index is an index into the list
 // established by bd_get_titles()
-int bd_select_title(BLURAY *bd, uint32_t title_idx)
+static int _select_title(BLURAY *bd, uint32_t title_idx)
 {
     const char *f_name;
-    int result;
 
     // Open the playlist
     if (bd->title_list == NULL) {
@@ -2549,13 +2557,18 @@ int bd_select_title(BLURAY *bd, uint32_t title_idx)
         return 0;
     }
 
-    bd_mutex_lock(&bd->mutex);
-
     bd->title_idx = title_idx;
     f_name = bd->title_list->title_info[title_idx].name;
 
-    result = _open_playlist(bd, f_name, 0);
+    return _open_playlist(bd, f_name, 0);
+}
+
+int bd_select_title(BLURAY *bd, uint32_t title_idx)
+{
+    int result;
 
+    bd_mutex_lock(&bd->mutex);
+    result = _select_title(bd, title_idx);
     bd_mutex_unlock(&bd->mutex);
 
     return result;
@@ -2637,38 +2650,55 @@ void bd_seamless_angle_change(BLURAY *bd, unsigned angle)
 
 uint32_t bd_get_titles(BLURAY *bd, uint8_t flags, uint32_t min_title_length)
 {
+    NAV_TITLE_LIST *title_list;
+    uint32_t count;
+
     if (!bd) {
         return 0;
     }
 
-    nav_free_title_list(&bd->title_list);
-    bd->title_list = nav_get_title_list(bd->disc, flags, min_title_length);
-
-    if (!bd->title_list) {
+    title_list = nav_get_title_list(bd->disc, flags, min_title_length);
+    if (!title_list) {
         BD_DEBUG(DBG_BLURAY | DBG_CRIT, "nav_get_title_list(%s) failed\n", disc_root(bd->disc));
         return 0;
     }
 
+    bd_mutex_lock(&bd->mutex);
+
+    nav_free_title_list(&bd->title_list);
+    bd->title_list = title_list;
+
     disc_event(bd->disc, DISC_EVENT_START, bd->disc_info.num_titles);
+    count = bd->title_list->count;
+
+    bd_mutex_unlock(&bd->mutex);
 
-    return bd->title_list->count;
+    return count;
 }
 
 int bd_get_main_title(BLURAY *bd)
 {
+    int main_title_idx = -1;
+
     if (!bd) {
         return -1;
     }
+
+    bd_mutex_lock(&bd->mutex);
+
     if (bd->title_type != title_undef) {
         BD_DEBUG(DBG_CRIT | DBG_BLURAY, "bd_get_main_title() can't be used with BluRay menus\n");
     }
 
     if (bd->title_list == NULL) {
         BD_DEBUG(DBG_BLURAY | DBG_CRIT, "Title list not yet read!\n");
-        return -1;
+    } else {
+        main_title_idx = bd->title_list->main_title_idx;
     }
 
-    return bd->title_list->main_title_idx;
+    bd_mutex_unlock(&bd->mutex);
+
+    return main_title_idx;
 }
 
 static int _copy_streams(const NAV_CLIP *clip, BLURAY_STREAM_INFO **pstreams,
@@ -2819,19 +2849,26 @@ static BLURAY_TITLE_INFO *_get_title_info(BLURAY *bd, uint32_t title_idx, uint32
 
 BLURAY_TITLE_INFO* bd_get_title_info(BLURAY *bd, uint32_t title_idx, unsigned angle)
 {
+    char mpls_name[11] = "";
+    int  mpls_id = -1;
+
+    bd_mutex_lock(&bd->mutex);
+
     if (bd->title_list == NULL) {
         BD_DEBUG(DBG_BLURAY | DBG_CRIT, "Title list not yet read!\n");
-        return NULL;
-    }
-    if (bd->title_list->count <= title_idx) {
+    } else if (bd->title_list->count <= title_idx) {
         BD_DEBUG(DBG_BLURAY | DBG_CRIT, "Invalid title index %d!\n", title_idx);
-        return NULL;
+    } else {
+        mpls_id = bd->title_list->title_info[title_idx].mpls_id;
+        memcpy(mpls_name, bd->title_list->title_info[title_idx].name, 11);
     }
 
-    return _get_title_info(bd,
-                           title_idx, bd->title_list->title_info[title_idx].mpls_id,
-                           bd->title_list->title_info[title_idx].name,
-                           angle);
+    bd_mutex_unlock(&bd->mutex);
+
+    if (mpls_id < 0)
+        return NULL;
+
+    return _get_title_info(bd, title_idx, mpls_id, mpls_name, angle);
 }
 
 BLURAY_TITLE_INFO* bd_get_playlist_info(BLURAY *bd, uint32_t playlist, unsigned angle)



View it on GitLab: https://code.videolan.org/videolan/libbluray/-/compare/36b925bca0ab6648c2fbf688fcb28e85efcb4dc3...6e63d7785b1ff8c9a9866b0acf892a4d1dd8090a

-- 
View it on GitLab: https://code.videolan.org/videolan/libbluray/-/compare/36b925bca0ab6648c2fbf688fcb28e85efcb4dc3...6e63d7785b1ff8c9a9866b0acf892a4d1dd8090a
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the libbluray-devel mailing list